在C中的任何更快的RMS值计算? [英] Any Faster RMS Value Calculation in C?

查看:601
本文介绍了在C中的任何更快的RMS值计算?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个软件,一个小的8位微控制器中的code C.部分是读电流互感器(ZCT)的ADC值,然后计算RMS值。流经ZCT的电流为正弦波,但它可以被扭曲。我的code如下:

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

然而,它有许多的浮点计算。这增加了一个大的负担,我小的MCU。而且我发现,的sqrt()函数没有在我的编译工作。

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.

有没有code,可以运行得更快?

Is there any code that could run faster?

推荐答案

当你需要获得更快的上缺少FPU,你最好的处理器 办法是更换浮点运算与定点。结合 这与乔普的建议(一牛顿迭代平方根),你会得到 是这样的:

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;
}

只是通过这个过滤器运行你的原始ADC采样。你可以添加一些 位的变化在这里和那里得到更高的分辨率,但你必须要 注意不要溢出你的变量。我怀疑你真正需要的 额外的分辨率。

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.

滤波器的输出是在相同的单元作为其输入。在这种情况下, 这是你的ADC的单位: 2.5&NBSP; V&NBSP; /&NBSP; 1024 NBSP;&≈NBSP; 2.44&NBSP;毫伏。如果你能保持 本机在随后的计算中,你将节省的周期,避免 不必要的转换。如果你确实需要的价值是在伏(它 可能是I / O要求),那么你将不得不转换为浮动 点。如果你想毫伏,你可以留在整数境界:

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;

这篇关于在C中的任何更快的RMS值计算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆