【发布时间】:2021-09-23 15:39:32
【问题描述】:
这是我引用的参考帖子:Any Faster RMS Value Calculation in C?
#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;
}
-
参数
sample是否已经是定点值并且已经被S缩放了? -
sum_squares是否在以下行中更改为定点值?
static uint32_t sum_squares = 1UL * SAMPLES * INITIAL * INITIAL;
- 下面这行是为了抵消上面
sample的平方吗? 此外,这是整数除法,这意味着小数部分将被截断。这个可以吗?我们不会失去精度吗?
sum_squares / SAMPLES
- 如果
sum_squares是定点值,倒数第二行的2不也应该改成定点值吗?
【问题讨论】:
-
由于
rms被初始化为512(中间样本值),调用rms_filter样本值为512,您应该会得到相同的返回数字。同样,如果您继续使用相同的值 x 调用rms_filter,则返回值应该收敛到 x。 (这可能需要一段时间,因为它是一个无限响应过滤器,而不是一个有限响应过滤器。) -
(1) No. (2)
SAMPLES是比例因子,因为它的值为 512,所以它相当于一个 9 个小数位的定点 1.0。INITIAL是初始 RMS 值,您可以将其更改为您想要的任何值。 (3) 可以改为rms = (rms * 1UL * SAMPLES + sum_squares / rms) / (2 * SAMPLES);,这样可能会更准确一些。 (4) 这实际上是我在 (3) 中的“改进”版本中所做的。
标签: c signal-processing fixed-point fixed-point-iteration