00

Dependencies:   mcp2515 mbed-dev-f303

Committer:
yezhong
Date:
Tue Jan 11 02:19:55 2022 +0000
Revision:
5:2503c88a564f
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yezhong 5:2503c88a564f 1 #include "mbed.h"
yezhong 5:2503c88a564f 2 #include "Moving_Average.h"
yezhong 5:2503c88a564f 3
yezhong 5:2503c88a564f 4
yezhong 5:2503c88a564f 5
yezhong 5:2503c88a564f 6 // Source是不断变化的
yezhong 5:2503c88a564f 7 float Moving_Average(unsigned int NN, float *FILTER, float Source)
yezhong 5:2503c88a564f 8 {
yezhong 5:2503c88a564f 9
yezhong 5:2503c88a564f 10 float AVER = 0.0f;
yezhong 5:2503c88a564f 11 float SUM = 0;
yezhong 5:2503c88a564f 12 unsigned char i;
yezhong 5:2503c88a564f 13
yezhong 5:2503c88a564f 14
yezhong 5:2503c88a564f 15 FILTER[NN] = Source; // 将接受到的数据放在均值滤波数组的“最后(倒数第2个)”元素里面
yezhong 5:2503c88a564f 16
yezhong 5:2503c88a564f 17
yezhong 5:2503c88a564f 18 // 把第一次进入的 Source 赋值给 FILTER[0] ~ FILTER[N-1]
yezhong 5:2503c88a564f 19 if(FILTER[NN+1] == 0) // FILTER[N+1]等于0,表示第一次进入该函数
yezhong 5:2503c88a564f 20 {
yezhong 5:2503c88a564f 21 for(i = 0; i < NN; i++)
yezhong 5:2503c88a564f 22 {
yezhong 5:2503c88a564f 23 FILTER[i] = Source;
yezhong 5:2503c88a564f 24 }
yezhong 5:2503c88a564f 25
yezhong 5:2503c88a564f 26 FILTER[NN+1] = 1; // 设置标记,已经进入过该函数,以后不再全部赋值
yezhong 5:2503c88a564f 27 }
yezhong 5:2503c88a564f 28
yezhong 5:2503c88a564f 29
yezhong 5:2503c88a564f 30 for(i = 0; i < NN; i++)
yezhong 5:2503c88a564f 31 {
yezhong 5:2503c88a564f 32 FILTER[i] = FILTER[i + 1];
yezhong 5:2503c88a564f 33 SUM += FILTER[i];
yezhong 5:2503c88a564f 34 }
yezhong 5:2503c88a564f 35
yezhong 5:2503c88a564f 36 AVER = SUM / NN;
yezhong 5:2503c88a564f 37 SUM = 0;
yezhong 5:2503c88a564f 38
yezhong 5:2503c88a564f 39 return AVER;
yezhong 5:2503c88a564f 40 }