Keep a running average of the last N samples
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….
----------begin_max5_patcher---------- 1722.3oc2a00aaaCE84Tf9efvn.cCI0U7KYoBzG5S6kUf.jMfATTLPay3nU8 ggj7RZKV9sOJRIGkT6PRaJSsAzJEynPc3g264d4kze+ku3rIyKtiWMA7Nvm. mc12EsblrslVNqqgyljwtaQJqR9fSVTjkwyqmbQ6urleWs7W76UbvmN+82+Y .KeInZy75R1hZvs2vpAI0UfhM0q2H9LqB7GfrJ.aUwzoS21OoI47EEaxkcFp q0qKxqyYYb4a3CkIrzs+AIKkMVL+udyLx1VWWxqDviUmTj+mk7E0pQGAFOM3 B.In4Z2Evm6+ZpR9l70z7fsMmuIKIOkWKG4vsuBV8haRxW0u6ox9DKuBwzla nY8eChtRP.c8Ufr0+4kun4t31EFS+7usjs39cRB3I6D2nmA2HJUgaIvIztqt G2h9YNu7Afa3DKR2DKdFQBZLZ2yrUIqj.+gIUSlq2xYYEK45HQ7rVdS91QPI RfOyr+1NS0V8WWyU8TCZyaXBvjqSKXh9+yGLguW+ze6FdmuXw0sdr2ljlBVU VbKHI+5j7jZd5WmBtTP4axXyS+Jn9ljJf3eyYKmN0YtsvItxAraNHPdC1NUL Hdf6kXujWlTrDTW.X+MujsRPyh6feJq5mcBcENSqJWLTM7ICiLGUoehT2hGP RNmeqXH+ibLa4Ba0PBoNyJifB5YdIjbnGuid6OcDd56iqp3oBjWTdO.YKkQr hxvOmiYaLwH0sfXchiPcb1.vTBs1kY7pJPfsDEVGQcSxxk77GMvLWTSwcgxq zYGNyMDb14.nsjEZxAEC9YrpL0Qb+TSR9QEkstX0pT9NGsPcp0HrbJEEFsa0 ZasSfcA+5tdxYiCKIOZr13Zs4mFi8VRdjXUlxpbjinihb7zp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OQTG Vz1mT9omyp03gTyA.Q+PpYeZsaH4Dv8zW6tAWyoUG.O4fKjZL3PmdvQLZZk5 ko0PjwLmG.G1HvA2G3fCJ3LxaMD4Elq4bw12Ob2fq4H14AuUI3zxbMGEcevb FoyQ7D3hLh4BGwL2Sm7OUguhMZZclWbHHQiXaNyRz5oCgSkMmQQHn9I7EwLc N7XdZ0OdqHi.GxOfyLr4mz4PONC2ciMH0KILgLIkDHwaXS6bJD6ErYjDGD4E r0rYU5AmeT3hGuPKZ7BsYiWnYl3l3gvdAZPSfFxKPKvDnc5y60nUP6m.oFI5 hFuPCdzPqoAws+EvcbAIP -----------end_max5_patcher-----------
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.
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….
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…
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.
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.
I nearly suggested that example, but couldn’t think how to go about changing the number of samples that the integrator "averages" over.
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
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~.
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
yeah, just an echo with feedback, really.
Forums > MaxMSP