Author Topic: Moving RMS calculation  (Read 1006 times)

0 Members and 1 Guest are viewing this topic.

Offline Psi

  • Super Contributor
  • ***
  • Posts: 6686
  • Country: nz
Moving RMS calculation
« on: February 12, 2019, 09:04:49 pm »
Does anyone know if it's possible to do a moving window type RMS calculation from ADC data without having to store all the values in memory?
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 1708
  • Country: us
Re: Moving RMS calculation
« Reply #1 on: February 12, 2019, 09:59:09 pm »
You can't do a moving window average without a memory buffer.  Can you use an exponentially decaying average instead?
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 8651
  • Country: us
  • DavidH
Re: Moving RMS calculation
« Reply #2 on: February 12, 2019, 10:14:45 pm »
It can be done with a FIFO type data structure which allows not recomputing from the entire list of values but this still requires storing every value.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 6686
  • Country: nz
Re: Moving RMS calculation
« Reply #3 on: February 12, 2019, 10:21:53 pm »
It can be done with a FIFO type data structure which allows not recomputing from the entire list of values but this still requires storing every value.

yeah, ya can speed it up if you have all the data by subtracting (oldest value*oldest value) from the RMS sum.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 6686
  • Country: nz
Re: Moving RMS calculation
« Reply #4 on: February 12, 2019, 10:22:53 pm »
You can't do a moving window average without a memory buffer. 

Thanks, that's what i needed to know.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 1244
  • Country: ro
Re: Moving RMS calculation
« Reply #5 on: February 12, 2019, 10:28:17 pm »
Depending on how much compromise can be made, if a FIFO must be avoided at all costs, I've seen the trick of remembering only the average, and subtracting Avg/N before adding each new value, where N is the number of the samples (the length) of the averaging window.

Online ebastler

  • Super Contributor
  • ***
  • Posts: 2385
  • Country: de
Re: Moving RMS calculation
« Reply #6 on: February 12, 2019, 10:39:09 pm »
Can you use an exponentially decaying average instead?

@Psi, did you note this part of ejeffrey's reply? In many cases this will do the job, and is easy and efficient to implement:

mean_square := p * mean_square + (1-p) * new_sample^2
with a weight factor p,  0<p<1.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 6686
  • Country: nz
Re: Moving RMS calculation
« Reply #7 on: February 12, 2019, 10:43:26 pm »
Yep, i will have a look into exponential decay tomorrow.
It may work, just want to have a play with RMS first.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 3045
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Moving RMS calculation
« Reply #8 on: February 12, 2019, 10:52:22 pm »
Note that with exponential decay you still have to match the number of samples in the buffer with the period time. If you calculate RMS over 1.5 period, the result is off.

A fifo is a easy way since you can also use it to decimate samples to match the period time.

That is, if the frequency of the signal you measure is variable.
 

Online Kleinstein

  • Super Contributor
  • ***
  • Posts: 5288
  • Country: de
Re: Moving RMS calculation
« Reply #9 on: February 12, 2019, 11:31:10 pm »
One can do the RMS calculation for short periods, like about 1 period at a time and store these values. For the final result one can than do moving average on these recalculated values  (this could directly RMS, but maybe better the power and maybe DC contribution). So one would come close to moving average RMS with storing less data, though at a limited update rate (e.g. one period).
 

Online ebastler

  • Super Contributor
  • ***
  • Posts: 2385
  • Country: de
Re: Moving RMS calculation
« Reply #10 on: February 12, 2019, 11:35:01 pm »
@Jeroen3, Kleinstein: It is not clear to me from the OP whether the intent is to deal with a periodic signal at all? I was thinking of RMS noise when I read the OP, where period duration is not a relevant parameter. But that was just me jumping to conclusions as well...
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 1690
  • Country: lv
Re: Moving RMS calculation
« Reply #11 on: February 13, 2019, 12:34:49 am »
Right. We shall not assume periodic (and bipolar) signal. Most likely mentioned moving average confused many. It is indeed possible to calculate RMS "on the fly", w/o data buffer, using just two global variables.

samples = 0;
sum = 0;
while(1) {
  adc_data = ADC();
  samples = samples +1;
  sum = sum + adc_data*adc_data;
  RMS = sqrt(sum/samples);
}


Note that accumulator overflow/saturation shall be addressed, solution depends on application.
 

Offline ali_asadzadeh

  • Frequent Contributor
  • **
  • Posts: 708
  • Country: ir
    • ASiD Designer
Re: Moving RMS calculation
« Reply #12 on: February 13, 2019, 02:52:43 am »
Quote
Right. We shall not assume periodic (and bipolar) signal. Most likely mentioned moving average confused many. It is indeed possible to calculate RMS "on the fly", w/o data buffer, using just two global variables.

samples = 0;
sum = 0;
while(1) {
  adc_data = ADC();
  samples = samples +1;
  sum = sum + adc_data*adc_data;
  RMS = sqrt(sum/samples);
}


Note that accumulator overflow/saturation shall be addressed, solution depends on application.

That's good solution, but the problem with this code is the overflow cases! and when samples roll over to zero ;) it can be corrected though!
You can order parts from www.ASiDesigner.com
we are a wire-based company
 

Offline Doctorandus_P

  • Frequent Contributor
  • **
  • Posts: 459
  • Country: nl
Re: Moving RMS calculation
« Reply #13 on: February 13, 2019, 03:06:50 am »
The combination of RMS and moving average does not make much sens to me.

RMS implies a periodic signal.
Without a periodic signal you have to add some kind of windowing function to guestimate your RMS value, and that implies a full buffer to work with.

If you have a periodic signal you can do some intermediate tricks.
Just assume you're sampling a 50Hz signal @ 10ksps.
You can then calculate a RMS value based on 10000 / 50 = 200 samples.
You can then put the calculated RMS values of each set of 200 samples into a relatively small buffer, and do a moving average or exponential decaying on that.

Depending on your sampling technique and doing AC only or AC+DC you ave to manage offsets carefully.

Something else to think about:
It is intuitive to start those 200 samples at a zero crossing, and continue untill the next zero crossing, and in a perfect world this is fine.
However, in the real world this leads to relatively big errors, if the ratio does not fit perfectly.

You may have upto one sample to many, or too less of your periodic signal without knowing it.
That sample will be near the "zero" value of your RMS, which means that you effectively would have calculated a better RMS value if you divided by 199 or 201.

However. If you start the sample period of those 200 samples near the (guestimated) RMS value of your signal, then one sample to few or too many does not matter much, because the "extra" sample has (almost) the same value as the RMS value and therefore does not add an error.

You can safely calculate the RMS value from a 201 sample buffer if that 201-th sample is the same as the RMS value.
But if that 201-th sample is (near to) 0, it will introcue an error of 0.5% in your RMS calculation.
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 1244
  • Country: ro
Re: Moving RMS calculation
« Reply #14 on: February 13, 2019, 03:15:15 am »
RMS implies a periodic signal.

RMS does not imply a periodic signal.
 
The following users thanked this post: ebastler, ogden

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 1708
  • Country: us
Re: Moving RMS calculation
« Reply #15 on: February 13, 2019, 03:56:22 am »
The combination of RMS and moving average does not make much sens to me.

RMS implies a periodic signal.
Without a periodic signal you have to add some kind of windowing function to guestimate your RMS value, and that implies a full buffer to work with.

If you have a periodic signal you can do some intermediate tricks.
Just assume you're sampling a 50Hz signal @ 10ksps.
You can then calculate a RMS value based on 10000 / 50 = 200 samples.
You can then put the calculated RMS values of each set of 200 samples into a relatively small buffer, and do a moving average or exponential decaying on that.

No you don't have to do any of this.

sqrt of the exponentially weighted moving average of the amplitude squared is perfectly fine by itself.  No matter what you choose you have to make sure you are averaging over a time appropriate to your application.  Yes, if your signal is at a known frequency and synchronous with your sampling rate you can get faster settling by using a square window filter synchronized to the period.  But there is no reason you have to do that or assume anything like that is possible.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 1571
  • Country: fr
Re: Moving RMS calculation
« Reply #16 on: February 13, 2019, 07:07:14 am »
Sorry if this is obvious to you - but just making sure what you meant by "store all the values in memory".

A true moving average or moving RMS doesn't need to store "all values" (whatever that means) but only the last N values, N*Ts being your moving window's period (Ts being your sampling period).
We don't know anything about your sample rate and moving window, but in a lot of cases, only a relatively small (circular) buffer is needed. For instance, @44100Hz, a 100ms moving RMS would only need a ~4K samples circular buffer.

Now if this is too much for your intended target (but if it is, I'm not sure your target would be appropriate anyway, but again we don't know anything about your application as a whole...), you can use exponential decay filtering as already suggested for instance. That may fit your requirements. Keep in mind that exponential decay filtering is essentially an IIR filter, whereas a moving average/RMS is essentially an FIR.
 

Offline snarkysparky

  • Regular Contributor
  • *
  • Posts: 158
  • Country: us
Re: Moving RMS calculation
« Reply #17 on: February 15, 2019, 04:58:34 am »
at each sample point calculate the squared value of the signal.

T = input^2;

now exponential filter this stream of squared values.

Ta = Ta + K*(T - Ta);    //  0 < K < 1

When the RMS is desired just square root this value

RMS = Ta^(1/2);
 

Online ebastler

  • Super Contributor
  • ***
  • Posts: 2385
  • Country: de
Re: Moving RMS calculation
« Reply #18 on: February 15, 2019, 06:02:21 am »
T = input^2;
Ta = Ta + K*(T - Ta);    //  0 < K < 1

You had me wondering for a moment, but it's indeed the same as my formula in post #6.

But I like mine better,  :P
since it makes the decay of the memory term more transparent (for me at least).
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 1372
  • Country: ca
Re: Moving RMS calculation
« Reply #19 on: February 15, 2019, 06:36:06 am »
T = input^2;
Ta = Ta + K*(T - Ta);    //  0 < K < 1

You had me wondering for a moment, but it's indeed the same as my formula in post #6.

Depending what you want to accomplish, the exponential smoothing may or may not replace the moving average.

For example, if you have a 50 Hz periodic signal, you can choose the window of moving average to match the cycle, which is 20 ms for 50 Hz. In this case, your moving average will give you very nice flat estimate, which will also respond very fast to the changes.

In the same situation, exponential smoothing will produce a periodic 50 Hz curve, and to suppress the periodicity you will have to make K very small. But the small K will make your exponential smoothing very slow to respond to changes.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 6686
  • Country: nz
Re: Moving RMS calculation
« Reply #20 on: February 15, 2019, 09:19:59 am »
The signal is audio.
I need to make a decision based on live data but also make a decision based on the RMS value at any given point.
The RMS value will obviously be for a set period, likely 1-10 ms (TBD)
The ADC will be running at max speed so 1-10ms is still a lot of samples to buffer in memory.
I have the ram to buffer them, i was more just curious if it was possible to do an moving RMS calc without needing all the ram.
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline NorthGuy

  • Super Contributor
  • ***
  • Posts: 1372
  • Country: ca
Re: Moving RMS calculation
« Reply #21 on: February 15, 2019, 09:27:46 am »
The signal is audio.
I need to make a decision based on live data but also make a decision based on the RMS value at any given point.
The RMS value will obviously be for a set period, likely 1-10 ms (TBD)
The ADC will be running at max speed so 1-10ms is still a lot of samples to buffer in memory.
I have the ram to buffer them, i was more just curious if it was possible to do an moving RMS calc without needing all the ram.

Just calculate the sum of squares for the burst, then move to the next burst etc. No need for memory.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 6686
  • Country: nz
Re: Moving RMS calculation
« Reply #22 on: February 15, 2019, 09:31:17 am »
When RMS data is needed it needs to be for the last 1-10ms
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline ogden

  • Super Contributor
  • ***
  • Posts: 1690
  • Country: lv
Re: Moving RMS calculation
« Reply #23 on: February 15, 2019, 09:43:15 am »
When RMS data is needed it needs to be for the last 1-10ms

You may calculate RMS for 1ms blocks and save results in circular buffer for last 10ms or even longer time. I believe that for many applications 1ms granularity may be ok.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 6686
  • Country: nz
Re: Moving RMS calculation
« Reply #24 on: February 15, 2019, 10:30:17 am »
thanks, i will give it a try
Greek letter 'Psi' (not Pounds per Square Inch)
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf