class:: MovingSum
summary:: The sum of samples over a variable number of frames.
related:: Classes/RunningSum, Classes/MovingAverage
categories:: UGens>Analysis, UGens>Maths

description::
A running sum over a variable number of samples. Useful for time averaging
operations. Also see link::Classes/MovingAverage::.

classmethods::
method:: ar, kr
Output the sum of audio or control rate samples.
argument::in
An input signal.
argument::numsamp
Size of the summing window, in samples. Modulatable between 1 and
strong::maxsamp::. Default: 40.
argument::maxsamp
Maximum size of the summing window. Pre-allocates memory up to this size and
your strong::numsamp:: will be clipped at this value. strong::maxsamp:: cannot
be modulated.

examples::

code::
/*
Compare the values of RunningSum vs. MovingSum.
*/

/* load synthdefs */
(
s.waitForBoot({

	var maxWin = 1.0; // maximum summing/averaging window, in seconds.

	// noise source: Pink Noise
	SynthDef(\noisrc, { |out|
		Out.ar(out, PinkNoise.ar * 4410.reciprocal); // scale down to see on scope
	}).add;

	// RunningSum
	SynthDef(\rs_avg, { |out, in, samps=4410|
		Out.kr( out,
			A2K.kr(                // convert to control rate for scoping
				RunningSum.ar(
					In.ar(in),
					samps          // RunningSum samps is non-modulatable
				)
			)
		);
	}).add;

	// MovingSum
	SynthDef(\msum, { |out, in, samps=4410|
		Out.kr( out,
			A2K.kr(                // convert to control rate for scoping
				MovingSum.ar(
					In.ar(in),
					samps,
					SampleRate.ir * maxWin  // max window size, in samples, non-modulatable
				)
			)
		);
	}).add;

})
)

/* play the synths */
(
// A bus for the noise source
~noiBus = Bus.audio(s, 1);
~sumBus = Bus.control(s, 2);

~noiSrc = Synth(\noisrc,  [\out, ~noiBus.index]);

~rs = Synth(\rs_avg,   [\out, ~sumBus.index,   \in, ~noiBus.index, \samps, 4410], addAction: 'addToTail');
~msum = Synth(\msum, [\out, ~sumBus.index+1, \in, ~noiBus.index, \samps, 4410], addAction: 'addToTail');
)

/* scope the results */
(
var sv;
~scp = s.scope(2, ~sumBus.index, rate:'control');
~scp.style = 1; // overlay the busses to compare their values

sv = ~scp.scopeView;

sv.yZoom = 3; // zoom in to see values a bit better
sv.fill = false;
// discern different values by color: red is RunningSum2
sv.waveColors = [Color.yellow, Color.red];
)

/* change window size */
~msum.set(\samps, 4410 * 2);   // 2x RunningSum window size
~msum.set(\samps, 4410);       // identical to RunningSum fixed window size
~msum.set(\samps, 4410 * 0.1); // 0.1x RunningSum window size

/* cleanup */
[~noiBus, ~sumBus, ~noiSrc, ~rs, ~msum].do(_.free);
~scp.quit;
::
