Forums > MaxMSP

Keep a running average of the last N samples

December 16, 2009 | 6:34 pm


I want to keep a running average of the last N samples (or X milliseconds) of an audio stream. The patch below demonstrates exactly what I’d like to do in Max terms, but I can’t seem to make a satisfactory solution with MSP objects. There is a proposed solution using [+=~] but as indicated in the patch it is not without creating a potential problem….

Any ideas or externals that might help? I want to remain entirely in the signal domain if possible….


— Pasted Max Patch, click to expand. —
December 16, 2009 | 6:59 pm

Maybe you’ve already tried this and it isn’t quite what you mean, but couldn’t you just adjust the interval time of the average~ object? It’s default mode with an argument of 44100 seems to do the same thing as your example patch.

December 16, 2009 | 7:30 pm

That would only provide the average amplitude (of the previous seconds audio) once a second.

I want to know the average of the last X ms of audio every single sample….

December 16, 2009 | 9:01 pm

I haven’t used it personally, but there is an object out there (ircam??) called [accumulate~]. it might do what you need or at least get you closer…

December 16, 2009 | 9:29 pm

I think you’re referring to the CNMAT accumulate~ object, which very similar to the new +=~. It still has the 32-bit float accuracy limitation that +=~ has, so after about 6 mins at 44100sr, your patch would become inaccurate.

I do get what you’re saying that you need now, but I’m not sure how to go about doing it.

December 16, 2009 | 9:30 pm

actually, average~ does exactly what you are looking for. That is, the oldest value leaves the buffer as the newest sample value enters and then the average is recalculated, at every single sample. Thus giving a continuously updated average value. what you described is the avg~ object, not average~.

Using the CNMAT accumulate~ external, you can create a memoryless system to analyze the instantaneous energy in a signal. Look at the leaky-integrator example provided in the accumulate~ help file if you’re interested.

December 16, 2009 | 9:40 pm

I nearly suggested that example, but couldn’t think how to go about changing the number of samples that the integrator "averages" over.

December 17, 2009 | 12:31 pm

Thanks for all the suggestions. It looks like [average~] actually does exactly what I want.

Looking at the helpfile for it reveals it produces a signal output. The reference manual states a float output, hence my initial reluctance in using it. Cheers

November 13, 2013 | 3:32 pm

Has anyone implemented a different type of average other than equal weights? I’ve been looking for an exponential average where the most recent elements are weighted higher than the rest. I wonder if it’s feasible to implement in gen~.

November 13, 2013 | 5:54 pm

that would be very simple to implement in gen, as what you are describing is a single-sample delay (history) with attenuation (*) feeding back into itself (ie iir filter). it probably exists in an example patch

November 14, 2013 | 5:55 am

yeah, just an echo with feedback, really.

Viewing 11 posts - 1 through 11 (of 11 total)

Forums > MaxMSP