Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any Faster RMS Value Calculation in C?

I am writing a software for a small 8-bit microcontroller in C. Part of the code is to read the ADC value of a current transformer (ZCT), and then calculate the RMS value. The current flowing through the ZCT is sinusoidal but it can be distorted. My code as follow:

float       adc_value, inst_current;
float       acc_load_current;           // accumulator = (I1*I1 + I2*I2 + ... + In*In)
double      rms_current;

// Calculate the real instantanous value from the ADC reading
inst_current = (adc_value/1024)*2.5;    // 10bit ADC, Voltage ref. 2.5V, so formula is: x=(adc/1024)*2.5V                           

// Update the RMS value with the new instananous value:
// Substract 1 sample from the accumulator (sample size is 512, so divide accumulator by 512 and substract it from the accumulator)
acc_load_current -= (acc_load_current / 512);       
inst_current *= inst_current;          // square the instantanous current
acc_load_current += inst_current;      // Add it to the accumulator

rms_current = (acc_load_current / 512);  // Get the mean square value. (sample size is 512)
rms_current = sqrt(rms_current);         // Get RMS value

// Now the < rms_current > is the real RMS current

However, it has many floating point calculations. This adds a large burden to my small MCU. And I found that the sqrt() function does not work in my compiler.

Is there any code that could run faster?

like image 568
eepty Avatar asked Mar 02 '15 10:03

eepty


People also ask

How do you calculate effective RMS value?

RMS Voltage Equation Then the RMS voltage (VRMS) of a sinusoidal waveform is determined by multiplying the peak voltage value by 0.7071, which is the same as one divided by the square root of two ( 1/√2 ).

Is RMS value effective value?

An rms value is also known as the effective value and is defined in terms of the equivalent heating effect of direct current.

What is RMS current formula?

Following is the formula of RMS value of AC: I r . m . s = I 0 √ 2 = 0.707 I 0.

Why RMS value is used instead of average?

The RMS value is most important in the case of an AC signal. Because the instantaneous value of an AC signal varies continuously with respect to time. Unlike a DC signal, which is relatively constant. Therefore, the instantaneous value of voltage cannot directly be used for the calculation.


1 Answers

When you need to get faster on an processor that lacks an FPU, your best bet is to replace floating point calculations with fixed point. Combine this with joop's suggestion (a one Newton-Raphson sqrt) and you get something like this:

#define INITIAL 512  /* Initial value of the filter memory. */
#define SAMPLES 512

uint16_t rms_filter(uint16_t sample)
{
    static uint16_t rms = INITIAL;
    static uint32_t sum_squares = 1UL * SAMPLES * INITIAL * INITIAL;

    sum_squares -= sum_squares / SAMPLES;
    sum_squares += (uint32_t) sample * sample;
    if (rms == 0) rms = 1;    /* do not divide by zero */
    rms = (rms + sum_squares / SAMPLES / rms) / 2;
    return rms;
}

Just run your raw ADC samples through this filter. You may add a few bit-shifts here and there to get more resolution, but you have to be careful not to overflow your variables. And I doubt you really need the extra resolution.

The output of the filter is in the same unit as its input. In this case, it is the unit of your ADC: 2.5 V / 1024 ≈ 2.44 mV. If you can keep this unit in subsequent calculations, you will save cycles by avoiding unnecessary conversions. If you really need the value to be in volts (it may be an I/O requirement), then you will have to convert to floating point. If you want millivolts, you can stay in the integer realm:

uint16_t rms_in_mV = rms_filter(raw_sample) * 160000UL >> 16;
like image 110
Edgar Bonet Avatar answered Nov 15 '22 19:11

Edgar Bonet