Calculating mean instantly

seabed's icon

Hello world,

There are many ways in MaxMsp to keep a running average of a signal. However, I would like to calculate instantly the mean and the variance of a whole signal. So by the time I load a sample in a buffer to have its mean and variance.

Is that possible somehow? This can be done easily in non real-time process programs like Matlab, but I want to do the calculations with MaxMsp.

Wetterberg's icon

What do you mean by " a whole signal"? Are you talking about a loaded sample?

And what do you mean by "instantly"?

broc's icon

You can get all signal values of the sample/buffer with [uzi] and [peek].

pdelges's icon

Have a look at mxj buf.Op.

Wetterberg's icon

@broc yeah, but it isn't instant as such.

Roman Thilenius's icon

for peak, divide the putput of +=~ through the output of index~.

for average, divie the output of average~ into +=~ through the output of the average window.

seabed's icon

Hey thanks for the tips.

Weetterberg, yes I mean a loaded sample and by instantly I mean as fast as possible. I made a patch using uzi and peek, it takes a couple of seconds to do the calculations. Is there a faster way?

Actually Roman I didn't understand clearly what do you suggest.
Cheers

broc's icon

The calculation involves Addition, Subtraction and Multiplication of each signal value.
So it may take some time depending on the buffer size.

But the Java external 'mxj buf.Op' will probably be faster.
(although personally I try to avoid Java due to possible issues with Mac OS X)

seabed's icon

Hi again,

Well as you said Broc I am having a few issues with Java and Max. Anyways, I am using uzi and peek to do my job.

So here is another question. After I calculate the mean I calculate how many samples exceed that value during 4 secs. Then I do the same for the next 4 secs of my sample. I want to scale that numbers in a range of 30 - 2000. The problem is that I don't know the range of that values before scaling. Any ideas or tricks that can help?

Jesse's icon

signal -> jit.catch~ -> jit.3m

This would give you "instant" min, max, and mean values. There is definitely something to be said for using jitter matrices in such cases.

seabed's icon

Nice I will look it up. But now the problem is that I don't know in advance how many values exceed the mean per one second or four seconds. Any ideas about that?

Jesse's icon

jit.op @op >p would be a good place to start

seabed's icon

Sorry, I don't get it.

Jesse's icon
Max Patch
Copy patch and select New From Clipboard in Max.