From 54f36f77365772e8515b590222b0992e7cab9161 Mon Sep 17 00:00:00 2001 From: Gerard Date: Fri, 12 Jun 2020 19:01:04 +0100 Subject: [PATCH] MDS example update --- .../HelpSource/Classes/FluidMDS.schelp | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/release-packaging/HelpSource/Classes/FluidMDS.schelp b/release-packaging/HelpSource/Classes/FluidMDS.schelp index 9ac12f3..a4a8008 100644 --- a/release-packaging/HelpSource/Classes/FluidMDS.schelp +++ b/release-packaging/HelpSource/Classes/FluidMDS.schelp @@ -59,63 +59,62 @@ Run when done EXAMPLES:: code:: + //Preliminaries: we want some audio, a couple of FluidDataSets, some Buffers, a FluidStandardize and a FluidMDS ( ~audiofile = File.realpath(FluidBufPitch.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav"; ~raw = FluidDataSet(s,\mds_help_12D); +~standardized = FluidDataSet(s,\mds_help_12Ds); ~reduced = FluidDataSet(s,\mds_help_2D); ~audio = Buffer.read(s,~audiofile); ~mfcc_feature = Buffer.new(s); -~stats = Buffer.new(s); -~datapoint = Buffer.alloc(s,12); +~stats = Buffer.alloc(s, 7, 12); ~standardizer = FluidStandardize(s); ~mds = FluidMDS(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 ( -~raw.clear; -~norm.clear; -FluidBufMFCC.process(s,~audio,features:~mfcc_feature,action:{ - "MFCC analysis.complete. Doing stats".postln; - fork{ - var chunkLen = (~mfcc_feature.numFrames / 100).asInteger; - 100.do{ |i| - s.sync; FluidBufStats.process(s,~mfcc_feature,startFrame:i*chunkLen,numFrames:chunkLen,startChan:1, stats:~stats, action:{ - ~stats.loadToFloatArray(action:{ |statsdata| - [statsdata[0],statsdata[1]].postln; - ~datapoint.setn(0,[statsdata[0],statsdata[1]]); - s.sync; - ("Adding point" ++ i).postln; - ~raw.addPoint(i,~datapoint); - }) - }); - if(i == 99) {"Analysis done, dataset ready".postln} - } - } -}); +{ + var trig = LocalIn.kr(1, 1); + var buf = LocalBuf(12, 1); + var count = PulseCount.kr(trig) - 1; + var chunkLen = (~mfcc_feature.numFrames / 100).asInteger; + var stats = FluidBufStats.kr( + source: ~mfcc_feature, startFrame: count * chunkLen, + startChan:1, numFrames: chunkLen, stats: ~stats, trig: trig + ); + var rd = BufRd.kr(12, ~stats, DC.kr(0), 0, 1); + var bufWr, dsWr; + 12.do{|i| + bufWr = BufWr.kr(rd[i], buf, DC.kr(i)); + }; + dsWr = FluidDataSetWr.kr(\mds_help_12D, buf: buf, trig: Done.kr(stats)); + LocalOut.kr( Done.kr(dsWr)); + FreeSelf.kr(count - 99); +}.play; ) + + //First standardize our dataset, so that the MFCC dimensions are on comensurate scales //Then apply the MDS in-place on the standardized data to get 2 dimensions, using a Euclidean distance metric //Download the dataset contents into an array for plotting ( -~standardizer.fit(~raw); -~standardizer.transform(~raw, ~reduced); -~mds.fitTransform(~raw,~reduced,2, FluidMDS.euclidean); -~reducedarray= Array.new(100); -fork{ - 100.do{|i| - ~reduced.getPoint(i,~datapoint,{ - - ~datapoint.loadToFloatArray(action:{|a| ~reducedarray.add(Array.newFrom(a))}) - }); - s.sync; - if(i==99){"Data downloaded".postln}; - } -} +~reducedarray = Array.new(100); +~standardizer.fitTransform(~raw, ~standardized); +~mds.fitTransform(~standardized, ~reduced, 2, FluidMDS.euclidean, action:{ + ~reduced.dump{|x| 100.do{|i| + ~reducedarray.add(x["data"][i.asString]) + }}}); ) //Visualise the 2D projection of our original 12D data