Electronics > Projects, Designs, and Technical Stuff

RMS calculation in cheap microcontroller

<< < (3/3)

ejeffrey:

--- Quote from: Ian.M on June 10, 2020, 02:19:48 pm ---if there is an internal or external ADC with enough precision and a max sample rate two or more orders of magnitude faster than the period of highest significant harmonic in the input signal,

--- End quote ---

That is probably dramatic overkill in 99% of situations.  Its not a big deal because even that is fairly modest by modern MCU standards: to look at the 7th harmonic of 60 Hz with 100x oversampling is still only 48 kHz, so essentially audio speed and even low end microprocessors will have hundreds of clock cycles per sample.   But if you are pressed for cycles for whatever reason you could usually go much slower than that.  To figure out exactly how low you can go you would need more specifications such as the required accuracy, expected amount of harmonic content, and so on.

nfmax:
If you are happy with an exponentially-filtered estimate of the RMS of the AC component of a signal - more or less what an old analogue RMS-reading meter would indicate - and your data points are sampled at constant intervals, you can use a fast recursive algorithm to calculate the exponentially-weighted mean (DC level) and variance (square of the AC RMS value), sample by sample:


--- Code: ---  diff := x - mean
  incr := alpha * diff
  mean := mean + incr
  variance := (1 - alpha) * (variance + diff * incr)

--- End code ---

To start up, set the value of mean to the first sample value x, and set the value of variance to zero, then iterate starting from the second sample. Constant alpha (0 < alpha < 1) sets the 'time constant' of the filter; variables diff and incr are used for temporary storage. Take the square root of variance if and when you need the actual RMS value.

Source: https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf

Implementation using fixed-point arithmetic is left as an excercise for the reader  ;D

Bassman59:

--- Quote from: nfmax on June 10, 2020, 05:57:37 pm ---If you are happy with an exponentially-filtered estimate of the RMS of the AC component of a signal - more or less what an old analogue RMS-reading meter would indicate - and your data points are sampled at constant intervals, you can use a fast recursive algorithm to calculate the exponentially-weighted mean (DC level) and variance (square of the AC RMS value), sample by sample:


--- Code: ---  diff := x - mean
  incr := alpha * diff
  mean := mean + incr
  variance := (1 - alpha) * (variance + diff * incr)

--- End code ---

To start up, set the value of mean to the first sample value x, and set the value of variance to zero, then iterate starting from the second sample. Constant alpha (0 < alpha < 1) sets the 'time constant' of the filter; variables diff and incr are used for temporary storage. Take the square root of variance if and when you need the actual RMS value.

Source: https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf

Implementation using fixed-point arithmetic is left as an excercise for the reader  ;D

--- End quote ---

Ah, you beat me to it -- I'm doing the same thing now!

coppice:

--- Quote from: nfmax on June 10, 2020, 05:57:37 pm ---If you are happy with an exponentially-filtered estimate of the RMS of the AC component of a signal - more or less what an old analogue RMS-reading meter would indicate - and your data points are sampled at constant intervals, you can use a fast recursive algorithm to calculate the exponentially-weighted mean (DC level) and variance (square of the AC RMS value), sample by sample:


--- Code: ---  diff := x - mean
  incr := alpha * diff
  mean := mean + incr
  variance := (1 - alpha) * (variance + diff * incr)

--- End code ---

To start up, set the value of mean to the first sample value x, and set the value of variance to zero, then iterate starting from the second sample. Constant alpha (0 < alpha < 1) sets the 'time constant' of the filter; variables diff and incr are used for temporary storage. Take the square root of variance if and when you need the actual RMS value.

Source: https://fanf2.user.srcf.net/hermes/doc/antiforgery/stats.pdf

Implementation using fixed-point arithmetic is left as an excercise for the reader  ;D

--- End quote ---
What's fast about that? At 3 multiplies per sample its not faster than other ways of achieving the same outcome.

pardo-bsso:

--- Quote from: coppice on June 10, 2020, 08:24:31 pm ---What's fast about that? At 3 multiplies per sample its not faster than other ways of achieving the same outcome.

--- End quote ---

In the linked paper they do a nice derivation of why it works and also avoids loss of precision and numerical stability errors for large sample sets.

But as with almost everything, it depends on your needs and use case.

Navigation

[0] Message Index

[*] Previous page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod