TITLE:: FluidBufStats
SUMMARY:: Computing Statistics on Buffers as Series.
CATEGORIES:: Libraries>FluidDecomposition, UGens>Buffer
RELATED:: Guides/FluCoMa, Guides/FluidDecomposition, Guides/FluidBufMultiThreading


DESCRIPTION::
This class implements non-real-time statistical analysis on buffer channels. Typically, a buffer would hold various time series (i.e. descriptors over time), and link::Classes/FluidBufStats:: allows this series to be described statistically. It is part of the LINK:: Guides/FluidDecomposition:: of LINK:: Guides/FluCoMa::. For more explanations, learning material, and discussions on its musicianly uses, visit http://www.flucoma.org/

The process returns a buffer where each channel of the STRONG::source:: buffer has been reduced to 7 statistics: mean, standard deviation, skewness, kurtosis, followed by 3 percentiles, by default the minimum value, the median, and the maximum value. Moreover, it is possible to request the same 7 stats to be applied to derivative of the input. These are useful to describe statistically the rate of change of the time series. The STRONG::stats:: buffer will grow accordingly, yielding the seven same statistical description of the n requested derivatives. Therefore, the STRONG::stats:: buffer will have as many channel as the input buffer, and as many frames as 7 times the requested STRONG::numDerivs::.

STRONG::Threading::

By default, this UGen spawns a new thread to avoid blocking the server command queue, so it is free to go about with its business. For a more detailed discussion of the available threading and monitoring options, including the two undocumented Class Methods below (.processBlocking and .kr) please read the guide LINK::Guides/FluidBufMultiThreading::.

CLASSMETHODS::

METHOD:: process
This is the method that calls for the slicing to be calculated on a given source buffer.

ARGUMENT:: server
	The server on which the buffers to be processed are allocated.

ARGUMENT:: source
	The index of the buffer to use as the source material to be processed. The different channels of multichannel buffers will be considered independently as time series.

ARGUMENT:: startFrame
	The starting point (in samples) from which to copy in the source buffer.

ARGUMENT:: numFrames
	The duration (in samples) to copy from the source buffer. The default (-1) copies the full lenght of the buffer.

ARGUMENT:: startChan
	The first channel from which to copy in the source buffer.

ARGUMENT:: numChans
	The number of channels from which to copy in the source buffer. This parameter will wrap around the number of channels in the source buffer. The default (-1) copies all of the buffer's channel.

ARGUMENT:: stats
	The index of the buffer to write the statistics to. Each channel is the fruit of the statistical computations on the same channel number of the source buffer.

ARGUMENT:: numDerivs
	The number of derivatives of the original time series for the statistic to be computed on. By default, none are computed. This will influence the number of frames the stats buffer will have.

ARGUMENT:: low
	The rank requested for the first percentile value. By default, it is percentile 0.0, which is the minimum of the given channel of the source buffer.

ARGUMENT:: middle
	The rank requested for the second percentile value. By default, it is percentile 50.0, which is the median of the given channel of the source buffer.

ARGUMENT:: high
	The rank requested for the third percentile value. By default, it is percentile 100.0, which is the maximum of the given channel of the source buffer.

ARGUMENT:: action
		A Function to be evaluated once the offline process has finished and indices instance variables have been updated on the client side. The function will be passed stats as an argument.

RETURNS::
	Nothing, as the destination buffer is declared in the function call.

EXAMPLES::

STRONG::A didactic example::

CODE::

// make a buffer of known lenght
b = Buffer.alloc(s,101);

// add known values - here, a ramp up
b.setn(0, Array.fill(101,{|i|i / 100}));

// create a new buffer as destinations
c = Buffer.new(s);

//run the process on them
(
Routine{
    t = Main.elapsedTime;
    FluidBufStats.process(s, b, stats:c, numDerivs:1);
    (Main.elapsedTime - t).postln;
}.play
)

// list the statistics. The first seven are for the source buffer values themselves, the last seven for the first derivative of the source buffer.
c.getn(0,c.numFrames,{|item|item.postln;})

// replace the source values by a ramp down
b.setn(0, Array.fill(101,{|i| 1 - (i / 100)}));

// run the process and read the values
FluidBufStats.process(s, b, stats:c, numDerivs:1, action:{c.getn(0,c.numFrames,{|item|item.postln;})});

// replace the source values by halfsine
b.setn(0, Array.fill(101,{|i| (i * pi/ 100).sin}));
b.plot

// run the process and read the values
FluidBufStats.process(s, b, stats:c, numDerivs:1, action:{c.getn(0,c.numFrames,{|item|item.postln;})});

// replace the source values by partial halfsine
b.setn(0, Array.fill(101,{|i| (i * pi/ 50).sin.max(0)}));
b.plot

// run the process and read the values
FluidBufStats.process(s, b, stats:c, numDerivs:1, action:{c.getn(0,c.numFrames,{|item|item.postln;})});

// replace the source values by positive white noise
b.setn(0, Array.fill(101,{1.0.rand}));
b.plot

// run the process and read the values
FluidBufStats.process(s, b, stats:c, numDerivs:1, action:{c.getn(0,c.numFrames,{|item|item.postln;})});
::

STRONG::A musical example::

CODE::
// create some buffers
(
b = Buffer.read(s,File.realpath(FluidBufStats.class.filenameSymbol).dirname.withTrailingSlash ++ "../AudioFiles/Tremblay-ASWINE-ScratchySynth-M.wav");
c = Buffer.new(s);
d = Buffer.new(s);
)

//split in various chunks, collecting the indices in an array
FluidBufOnsetSlice.process(s,b, minSliceLength: 10, metric: 9, threshold: 0.4, filterSize: 7, indices: c, action:{c.loadToFloatArray(action: {|array| e = array.add(b.numFrames).addFirst(0);e.postln;})});

//describe the whole input too, here using pitch, and collecting the values in an array, dismissing the (interleaved) confidence.
FluidBufPitch.process(s,b,features:c, action:{c.loadToFloatArray(action: {|array| f = array.unlace(2)[0]; f.postln;})});

// iterate through each slice, taking the median of the first derivative of the pitch of each
(
g= Array.new;
Routine({
	e.doAdjacentPairs({
		arg start,end;
		FluidBufStats.processBlocking(s,c,(start/512).asInteger,((end-start)/512).max(2).asInteger,0,1,d,1, action: {d.loadToFloatArray(action: {
			arg array;
			g = g.add(array[12]);
			"% % %\n".postf((start/512).asInteger,((end-start)/512).max(2).asInteger, array[12]);
		})});
});
	"Done".postln;
}).play;
)

//obtain the order of indices that would sort the stats
h = g.order;

//play in loop the slice in order of pitch direction (the median of the slice's pitch variation)
(
var which = h[5];
{BufRd.ar(1, b, Phasor.ar(0,1,e[which],e[which+1],e[which]))}.play;
)
::


STRONG::Stereo Input Behaviour::

CODE::
// make a buffer of known lenght
b = Buffer.alloc(s,101,2);

// add known values - here, a ramp up on the left and negative random values on the right
b.setn(0, Array.fill(101,{|i|[i / 100,-1.0.rand]}).flat);

// plot to confirm
b.plot

// create a new buffer as destinations
c = Buffer.new(s);

// run the stats and send back the values
FluidBufStats.process(s, b, stats:c, numDerivs:1, action:{c.getn(0,c.numFrames * c.numChannels,{|item|d = item; d.postln})});

//looking at the result is not easy to grasp, since it is interleaved: first number is mean of L, second is mean of R, third is stddev of L, fourth is stddev or R
//this will make it tidier - the first value of each line is Left, the second is Right
d.reshape(14,2).do({|x,i|["mean\t\t","stddev\t\t","skew\t\t\t", "kurtosis\t", "min\t\t\t", "median\t\t", "max\t\t\t","d-mean\t","d-stddev\t","d-skew\t\t", "d-kurtosis", "d-min\t\t", "d-median\t", "d-max\t\t"].at(i).post;x.round(0.01).postln});"".postln;
::
