RMS Averaging?

maxmike mike's icon

I'm looking for a gen~ alternative to average~ for rms averaging.

Anyone have a simple solution?

Thanks!

hz37's icon
// Gen RMS calculator.
// henszimmerman@gmail.com.

// Input buffer.
Delay input(8192, 1, feedback=0);

// Amount of samples to average.
Param n;

// Accumulator.
acc = 0;

// Step through all samples.
for(idx = 0; idx < n; idx += 1)
{
    // Read next value from input buffer.
    v = input.read(idx, 0, interp="spline");
    
    // Add square of value to accumulator.
    acc += (v * v);
}

// Store next value we read from the input.
input.write(in1);

// Output RMS of n samples.
out1 = sqrt(acc / n);


Evan's icon

Nice one!

maxmike mike's icon

This is perfect, thanks HZ37!

Likemushi's icon

Hello , can u say , if i do at first sqrt(pow(in,2)) and after do averange - is that right ?

volker böhm's icon

Hello , can u say , if i do at first sqrt(pow(in,2)) and after do averange – is that right ?

no.

volker böhm's icon

// Gen RMS calculator.
// henszimmerman@gmail.com.
...

while this works correctly, it gets insanely expensive for larger averaging windows.
i would propose something like the code below.
caveat: averaging size is fixed, as i don't know an easy way to reset a delay line in gen~.
you could make it work with a buffer, though.

Delay d(2000);
History y(0);
n = 2000;
rdiv = 1 / n;

sq = in1 * in1;
xn = d.read(n, interp="none");
avg = sq + y - xn;

out1 = sqrt(avg*rdiv);

y = avg;
d.write(sq);

Roman Thilenius's icon

for many applications you might want to/can downsample the analysis. in this particular situation the order rms->average can make sense.

jayrope's icon

Does HZ37's code provide RMS updated every sample, whereas Volker Böhm's code provides RMS value only every 2000 samples?

A continous RMS, updated per sample, would be more desirable for example for a peak versus RMS comparison, threshold measurement and such.

Thanx for any insight!