You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
5.0 KiB
Plaintext
164 lines
5.0 KiB
Plaintext
TITLE:: FluidNormalize
|
|
summary:: Normalize a FluidDataSet
|
|
categories:: FluidManipulation
|
|
related:: Classes/FluidStandardize, Classes/FluidDataSet
|
|
|
|
DESCRIPTION::
|
|
Normalize the entries of a link::Classes/FluidDataSet::, or normalize a data point according to the learned bounds of a data set. On the server.
|
|
|
|
See http://www.faqs.org/faqs/ai-faq/neural-nets/part2/section-16.html
|
|
|
|
CLASSMETHODS::
|
|
|
|
private:: kr
|
|
|
|
METHOD:: new
|
|
Create a new instance
|
|
ARGUMENT:: server
|
|
The link::Classes/Server:: on which to run
|
|
ARGUMENT:: min
|
|
Minimum output value, default 0
|
|
ARGUMENT:: max
|
|
Maximum output value, default 1
|
|
|
|
|
|
INSTANCEMETHODS::
|
|
|
|
METHOD:: fit
|
|
Compute the normalization factors from a link::Classes/FluidDataSet:: for later.
|
|
ARGUMENT:: dataSet
|
|
The link::Classes/FluidDataSet:: to normalize
|
|
ARGUMENT:: action
|
|
A function to run when processing is complete
|
|
|
|
METHOD:: transform
|
|
Normalize a link::Classes/FluidDataSet:: into another link::Classes/FluidDataSet::, using the learned extrema from a previous call to link::Classes/FluidNormalize#fit::
|
|
ARGUMENT:: sourceDataSet
|
|
The link::Classes/FluidDataSet:: to normalize
|
|
ARGUMENT:: destDataSet
|
|
The link::Classes/FluidDataSet:: to populate with normalized data
|
|
ARGUMENT:: action
|
|
A function to run when processing is complete
|
|
|
|
|
|
METHOD:: fitTransform
|
|
Normalize a link::Classes/FluidDataSet::
|
|
ARGUMENT:: sourceDataSet
|
|
The link::Classes/FluidDataSet:: to normalize
|
|
ARGUMENT:: destDataSet
|
|
The link::Classes/FluidDataSet:: to populate with normalized data
|
|
ARGUMENT:: action
|
|
A function to run when processing is complete
|
|
|
|
|
|
METHOD:: transformPoint
|
|
Normalize a new data point, using the learned extrema from a previous call to link::Classes/FluidNormalize#fit::
|
|
ARGUMENT:: sourceBuffer
|
|
A link::Classes/Buffer:: with the new data point
|
|
ARGUMENT:: destBuffer
|
|
A link::Classes/Buffer:: to contain the normalized value
|
|
ARGUMENT:: action
|
|
A function to run when processing is complete
|
|
|
|
EXAMPLES::
|
|
code::
|
|
s.boot;
|
|
//Preliminaries: we want some audio, a couple of FluidDataSets, some Buffers and a FluidNormalize
|
|
// FluidNormalize.dumpAllMethods
|
|
(
|
|
~audiofile = File.realpath(FluidBufPitch.class.filenameSymbol).dirname +/+ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav";
|
|
~raw = FluidDataSet(s,\norm_help_raw);
|
|
~norm = FluidDataSet(s,\norm_help_normd);
|
|
~pitch_feature = Buffer.new(s);
|
|
~stats = Buffer.alloc(s, 7, 2);
|
|
~normalizer = FluidNormalize(s);
|
|
)
|
|
|
|
// Load audio and run a pitch analysis, which gives us pitch and pitch confidence (so a 2D datum)
|
|
(
|
|
~audio = Buffer.read(s,~audiofile);
|
|
FluidBufPitch.process(s,~audio, features: ~pitch_feature);
|
|
)
|
|
|
|
// Divide the time series in to 10, and take the mean of each segment and add this as a point to
|
|
// the 'raw' FluidDataSet
|
|
(
|
|
{
|
|
var trig = LocalIn.kr(1, 1);
|
|
var buf = LocalBuf(2, 1);
|
|
var count = PulseCount.kr(trig) - 1;
|
|
var chunkLen = (~pitch_feature.numFrames / 10).asInteger;
|
|
var stats = FluidBufStats.kr(
|
|
source: ~pitch_feature, startFrame: count * chunkLen,
|
|
numFrames: chunkLen, stats: ~stats, trig: trig
|
|
);
|
|
var rd = BufRd.kr(2, ~stats, DC.kr(0), 0, 1);// pick only mean pitch and confidence
|
|
var wr1 = BufWr.kr(rd[0], buf, DC.kr(0));
|
|
var wr2 = BufWr.kr(rd[1], buf, DC.kr(1));
|
|
var dsWr = FluidDataSetWr.kr(\norm_help_raw, buf: buf, trig: Done.kr(stats));
|
|
LocalOut.kr( Done.kr(dsWr));
|
|
FreeSelf.kr(count - 9);
|
|
}.play;
|
|
)
|
|
|
|
|
|
// Normalize and load to language-side array
|
|
(
|
|
~rawarray = Array.new(10);
|
|
~normedarray= Array.new(10);
|
|
~normalizer.fitTransform(~raw,~norm, {
|
|
~raw.dump{|x| 10.do{|i|
|
|
~rawarray.add(x["data"][i.asString])
|
|
}};
|
|
~norm.dump{|x| 10.do{|i|
|
|
~normedarray.add(x["data"][i.asString])
|
|
}};
|
|
});
|
|
)
|
|
|
|
//Plot side by side. Before normalization the two dimensions have radically different scales
|
|
//which can be unhelpful in many cases
|
|
|
|
(
|
|
~rawarray.flatten(1).unlace.plot("Unnormalized",Rect(0,0,400,400),minval:0,maxval:[5000,1]).plotMode=\bars;
|
|
~plot2 = ~normedarray.flatten(1).unlace.plot("Normalized",Rect(410,0,400,400)).plotMode=\bars;
|
|
)
|
|
|
|
//Server side queries
|
|
//Setup
|
|
(
|
|
~tempPoint = Buffer.alloc(s,2);
|
|
~predictPoint = Buffer.alloc(s,2);
|
|
~avgBuf = Buffer.alloc(s,10,2);
|
|
~pitchingBus = Bus.control;
|
|
~catchingBus = Bus.control;
|
|
)
|
|
(
|
|
~normalizer.inBus_(~pitchingBus).outBus_(~catchingBus).inBuffer_(~tempPoint).outBuffer_(~predictPoint);
|
|
//Pitching (no pun intended): read frames out of buffer and pass to standardize
|
|
{
|
|
var audio = BufRd.ar(1,~audio,LFSaw.ar(BufDur.ir(~audio).reciprocal).range(0, BufFrames.ir(~audio)));
|
|
var counter = Stepper.ar(Impulse.ar(ControlRate.ir),max:9);
|
|
var trig = HPZ1.ar(counter) < 0;
|
|
//average 10 frames: one could use the MovingAverage extension here
|
|
var avg;
|
|
BufWr.kr(FluidPitch.kr(audio),~avgBuf,phase:counter);
|
|
avg = Mix.new(BufRd.kr(2, ~avgBuf, phase:10.collect{|x|x})) * 0.1;
|
|
//assemble data point
|
|
BufWr.kr(avg[0],~tempPoint,0);
|
|
BufWr.kr(avg[1],~tempPoint,1);
|
|
Poll.kr(T2K.kr(trig),BufRd.kr(1,~tempPoint,[0,1]),["pitch (raw)", "confidence (raw)"]);
|
|
Out.kr(~pitchingBus.index,[T2K.kr(trig)]);
|
|
}.play(~normalizer.synth,addAction:\addBefore);
|
|
|
|
{
|
|
Poll.kr(In.kr(~catchingBus.index),BufRd.kr(1,~predictPoint,[0,1]),["pitch (normalized)", "confidence (normalized)"])
|
|
}.play(~normalizer.synth,addAction:\addAfter);
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
::
|