PCA example update

nix
Gerard 6 years ago
parent 64e3fc49b7
commit a6c3757e4a

@ -61,72 +61,70 @@ Run when done
EXAMPLES:: EXAMPLES::
code:: code::
s.boot; s.boot;
//Preliminaries: we want some audio, a couple of FluidDataSets, some Buffers, a FluidStandardize and a FluidPCA //Preliminaries: we want some audio, a couple of FluidDataSets, some Buffers, a FluidStandardize and a FluidPCA
( (
~audiofile = File.realpath(FluidBufPitch.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav"; ~audiofile = File.realpath(FluidBufPitch.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav";
~raw = FluidDataSet(s,\pca_help_12D); ~raw = FluidDataSet(s,\pca_help_12D);
~standardized = FluidDataSet(s,\pca_help_12Ds);
~reduced = FluidDataSet(s,\pca_help_2D); ~reduced = FluidDataSet(s,\pca_help_2D);
~audio = Buffer.read(s,~audiofile); ~audio = Buffer.read(s,~audiofile);
~mfcc_feature = Buffer.new(s); ~mfcc_feature = Buffer.new(s);
~stats = Buffer.new(s); ~stats = Buffer.alloc(s, 7, 12);
~datapoint = Buffer.alloc(s,12); ~datapoint = Buffer.alloc(s, 12);
~standardizer = FluidStandardize(s); ~standardizer = FluidStandardize(s);
~pca = FluidPCA(s); ~pca = FluidPCA(s);
) )
// Do a mfcc analysis on the audio, which gives us 13 points, and we'll throw the 0th away
// Divide the time series in to 100, and take the mean of each segment and add this as a point to // Load audio and run an mfcc analysis, which gives us 13 points (we'll throw the 0th away)
(
~audio = Buffer.read(s,~audiofile);
FluidBufMFCC.process(s,~audio, features: ~mfcc_feature);
)
// Divide the time series in 100, and take the mean of each segment and add this as a point to
// the 'raw' FluidDataSet // the 'raw' FluidDataSet
( (
~raw.clear; {
~norm.clear; var trig = LocalIn.kr(1, 1);
FluidBufMFCC.process(s,~audio,features:~mfcc_feature,action:{ var buf = LocalBuf(12, 1);
"MFCC analysis.complete. Doing stats".postln; var count = PulseCount.kr(trig) - 1;
fork{ var chunkLen = (~mfcc_feature.numFrames / 100).asInteger;
var chunkLen = (~mfcc_feature.numFrames / 100).asInteger; var stats = FluidBufStats.kr(
100.do{ |i| source: ~mfcc_feature, startFrame: count * chunkLen,
s.sync; FluidBufStats.process(s,~mfcc_feature,startFrame:i*chunkLen,numFrames:chunkLen,startChan:1, stats:~stats, action:{ startChan:1, numFrames: chunkLen, stats: ~stats, trig: trig
~stats.loadToFloatArray(action:{ |statsdata| );
[statsdata[0],statsdata[1]].postln; var rd = BufRd.kr(12, ~stats, DC.kr(0), 0, 1);
~datapoint.setn(0,[statsdata[0],statsdata[1]]); var bufWr, dsWr;
s.sync; 12.do{|i|
("Adding point" ++ i).postln; bufWr = BufWr.kr(rd[i], buf, DC.kr(i));
~raw.addPoint(i,~datapoint); };
}) dsWr = FluidDataSetWr.kr(\pca_help_12D, buf: buf, trig: Done.kr(stats));
}); LocalOut.kr( Done.kr(dsWr));
if(i == 99) {"Analysis done, dataset ready".postln} FreeSelf.kr(count - 98);
} }.play;
}
});
) )
//First standardize our dataset, so that the MFCC dimensions are on comensurate scales //First standardize our dataset, so that the MFCC dimensions are on comensurate scales
//Then apply the PCA in-place on the standardized data //Then apply the PCA in-place on the standardized data
//Download the dataset contents into an array for plotting //Download the dataset contents into an array for plotting
( (
~standardizer.fit(~raw); ~reducedarray = Array.new(100);
~standardizer.transform(~raw, ~reduced); ~standardizer.fitTransform(~raw, ~standardized);
~pca.fitTransform(~raw,~reduced,2); ~pca.fitTransform(~standardized, ~reduced, 2, action:{
~reducedarray= Array.new(100); ~reduced.dump{|x| 100.do{|i|
fork{ ~reducedarray.add(x["data"][i.asString])
100.do{|i| }};
~reduced.getPoint(i,~datapoint,{ });
~datapoint.loadToFloatArray(action:{|a| ~reducedarray.add(Array.newFrom(a))})
});
s.sync;
if(i==99){"Data downloaded".postln};
}
}
) )
//Visualise the 2D projection of our original 12D data //Visualise the 2D projection of our original 12D data
( (
d = ~reducedarray.flatten(1).unlace.deepCollect(1, { |x| x.normalize}); d = ~reducedarray.flatten(1).unlace.deepCollect(1, { |x| x.normalize});
// d = [20.collect{1.0.rand}, 20.collect{1.0.rand}];
w = Window("scatter", Rect(128, 64, 200, 200)); w = Window("scatter", Rect(128, 64, 200, 200));
w.drawFunc = { w.drawFunc = {
Pen.use { Pen.use {
@ -142,5 +140,4 @@ w.drawFunc = {
w.refresh; w.refresh;
w.front; w.front;
) )
:: ::

Loading…
Cancel
Save