Skip to main content
Senior III
October 8, 2026
Question

Sqrtf function for RMS calculation

  • October 8, 2026
  • 8 replies
  • 54 views

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?

8 replies

Associate II
October 8, 2026

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
Principal II
October 8, 2026

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
October 8, 2026

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
October 8, 2026

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
October 8, 2026

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
October 8, 2026

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

STuser2Author
Senior III
October 8, 2026

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
October 8, 2026

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.