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.

127 lines
3.8 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
(
~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;
)
::