https://ja.wikipedia.org/wiki/%E7%A7%BB%E5%8B%95%E5%B9%B3%E5%9D%87
N が大きいほど、周波数に対する振幅が小さくなる。
1/√2G のとき
N = 0.443/fc * 1/ts = 0.443*fs/fc.
N : Point(s) of moving average[-]
fs: Sampling freqency[Hz]
ts: Sampling time[s]
fc: Cut-off frequency[Hz
Ex) fs = 1kHz, fc = 100 Hz
N = 4.43 = 5
int moving_average(int dat[], size_t size) { long tmp = 0; size_t i; for (i = 0; i < size; i++) { tmp += dat[i]; } return tmp/size; } float moving_averagef(int dat[], size_t size) { long tmp = 0; size_t i; for (i = 0; i < size; i++) { tmp += dat[i]; } return (float)tmp/size; }Arduino Nano で使う。
テストプログラム一部(リングバッファ使用)
#define AD_BUF_SIZE 5
CIRCULAR_BUFF ad = { AD_BUF_SIZE };
c_buff_push(ad, analogRead(xxx)); // Execute every 1 ms(1kHz).
float ad_value_sma = moving_averagef(c_buff_get_data(ad.dat, ad.size));
// Add funtion to my circulation buffer(ring buffer).
*int c_buff_str(CIRCULAR_BUFF *ps)
{
return ps.dat;
}
size_t c_buff_size(CIRCULAR_BUFF *ps)
{
return ps.size;
}
If these functions add, test program is rewrite as follows.
float ad_value_sma = moving_averagef(c_buff_str(ad), c_buff_size(ad));