Skip to main content
Senior III
Oct 8, 2026 7:14 AM UTC
Solved

Sqrtf function for RMS calculation

Main goal is to measure the RMS value of the mains, first question is can i perform the calculations using floating point or fixed point? The RMS requires the sqrtf function, can i use the function due to its number of cycles?

LCE
LCEBest answer
Principal III
Oct 8, 2026 10:40 AM UTC

It sounds as if you already have the STM32 at hand - so why don’t you just try different methods ?

Use either the ARM cycle counter or a timer running on the highest clock possible (no interrupts, just let it run).

Then:

  • disable IRQs
  • get start time from timer / cycle counter
  • do calculations
  • get stop time from timer / cycle counter
  • enable IRQs
  • check time

10 replies

Associate II
Oct 8, 2026 8:36 AM UTC

Both FP and fixed point are possible.

If the tag “STM32G4 series” is correct you will have a single precision FPU. Then float should be quite fast.

 

What suits better for your needs is not possible to tell from your question. See your previous question 

 

LCE
LCEBest answer
Principal III
Oct 8, 2026 10:40 AM UTC

It sounds as if you already have the STM32 at hand - so why don’t you just try different methods ?

Use either the ARM cycle counter or a timer running on the highest clock possible (no interrupts, just let it run).

Then:

  • disable IRQs
  • get start time from timer / cycle counter
  • do calculations
  • get stop time from timer / cycle counter
  • enable IRQs
  • check time
Andrew Neil
Super User
Oct 8, 2026 10:48 AM UTC

As ​@mfgkw said, your question is too vague to give any definite answers - you can do any calculation with fixed-point, floating-point or even integer arithmetic depending on your requirements.

 

Due you really need a true RMS measurement, or is an approximation sufficient?

eg, if you assume that the waveform is sinusoidal, the RMS is just 0.7 times the peak;

You might be be able to use a pre-calculated lookup table;

etc, etc, …

 

the sqrtf function

You mean the standard C library function?

As previously noted, that uses double-precision floats - but the Cortex-M4 has only a single-precision FPU ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
STuser2Author
Senior III
Oct 8, 2026 11:26 AM UTC

Thank you all for the feedback, my main concern is if i am running at 10kHz (i would like to do at 20k0Hz) sampling for capturing the data and applying the RMS formula squaring, averaging, then sqrt apart from other basic things, quite intense calculations are involved as all of you are aware, so if i can fit in 100uS (10kHz) is what worries me. In case if i start floating point and again reworking to fixed point will be quite challenging, so i am getting the best opinion from experts. The requirement is True RMS calculation from the customer. 

  

Andrew Neil
Super User
Oct 8, 2026 11:40 AM UTC

As ​@LCE suggested, why don’t you just try it ?

 

Then you can see exactly how it all behaves in your specific circumstances.

 

PS:

CMSIS-DSP provides RMS and a “fast” square root function - specifically targetted for Cortex-M microcontrollers:

https://arm-software.github.io/CMSIS_5/DSP/html/group__RMS.html

https://arm-software.github.io/CMSIS_5/DSP/html/group__SQRT.html

 

 

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
STuser2Author
Senior III
Oct 8, 2026 12:05 PM UTC

As I am planning to do what ​@LCE suggested I have never done it before that kind of detailed analysis.

STuser2Author
Senior III
Oct 8, 2026 2:46 PM UTC

If i temporarily calculate using the peak value, is the below algorithm corent? Every 10kHz detect the max ADC count for 200 samples subtract the DC offset value so for example if 1.65V is midpoint and i get for example 3000 so 3000 - 2048 = 952 peak value, the RMS value is 952 * 0.7 = 666. Is the algorithm correct? I am not sure is it required to count the samples?

 

Andrew Neil
Super User
Oct 8, 2026 4:02 PM UTC

I am not sure is it required to count the samples?

 

Why would you count the samples ?

You’re just looking for the peak value of the waveform.

 

You can test this out in your favourite spreadsheet ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
STuser2Author
Senior III
Oct 18, 2026 4:46 PM UTC

I have used the floating-point calculations, luckily i did not observe any time delays, the program runs normally.  I have used the RMS equation. Thank you all for support. 

Piranha
Principal III
Oct 20, 2026 10:44 PM UTC

Does anyone here understands any digital signal processing at all? Combined true RMS with exponential filtering is ridiculously efficient and easy!

// decay_factor = error ^^ (freq_out / freq_in)

#define DECAY_FACTOR (1023 / (double)1024)

static float nFilter_;

void TrueRMS_Add(float nSample) // Capture
{
nFilter_ = fmaf(nFilter_, (float)DECAY_FACTOR, nSample * nSample);
}

float TrueRMS_Get(void) // Display
{
return sqrtf(nFilter_ * (1 - (float)DECAY_FACTOR));
}

The fmaf() function will use VFMA.F32 instruction taking 3 cycles. The sqrtf() function will use VSQRT.F32 instruction taking 14 cycles and will be called at a low rate. This way the Cortex-M4 at 170 MHz can process up to few Msps. Processing 20 ksps should use less than 1 % of the CPU. The example exponential filter decay factor is set up for 20 kHz input (capture) rate and 4 Hz output (display) rate with 0,756 % error at the output.