2018-01-1901:55:42
arduino濾波算法--轉載至極客工坊 ----http://www.geek-workshop.com/thread-7694-1-1.html
卡爾曼濾波
1 #include <Wire.h> // I2C library, gyroscope 2 3 // Accelerometer ADXL345 4 #define ACC (0x53) //ADXL345 ACC address 5 #define A_TO_READ (6) //num of bytes we are going to read each time (two bytes for each axis) 6 7 8 // Gyroscope ITG3200 9 #define GYRO 0x68 // gyro address, binary = 11101000 when AD0 is connected to Vcc (see schematics of your breakout board) 10 #define G_SMPLRT_DIV 0x15 11 #define G_DLPF_FS 0x16 12 #define G_INT_CFG 0x17 13 #define G_PWR_MGM 0x3E 14 15 #define G_TO_READ 8 // 2 bytes for each axis x, y, z 16 17 18 // offsets are chip specific. 19 int a_offx = 0; 20 int a_offy = 0; 21 int a_offz = 0; 22 23 int g_offx = 0; 24 int g_offy = 0; 25 int g_offz = 0; 26 //////////////////////// 27 28 //////////////////////// 29 char str[512]; 30 31 void initAcc() { 32 //Turning on the ADXL345 33 writeTo(ACC, 0x2D, 0); 34 writeTo(ACC, 0x2D, 16); 35 writeTo(ACC, 0x2D, 8); 36 //by default the device is in +-2g range reading 37 } 38 39 void getAccelerometerData(int* result) { 40 int regAddress = 0x32; //first axis-acceleration-data register on the ADXL345 41 byte buff[A_TO_READ]; 42 43 readFrom(ACC, regAddress, A_TO_READ, buff); //read the acceleration data from the ADXL345 44 45 //each axis reading comes in 10 bit resolution, ie 2 bytes. Least Significat Byte first!! 46 //thus we are converting both bytes in to one int 47 result[0] = (((int)buff[1]) << 8) | buff[0] + a_offx; 48 result[1] = (((int)buff[3]) << 8) | buff[2] + a_offy; 49 result[2] = (((int)buff[5]) << 8) | buff[4] + a_offz; 50 } 51 52 //initializes the gyroscope 53 void initGyro() 54 { 55 /***************************************** 56 * ITG 3200 57 * power management set to: 58 * clock select = internal oscillator 59 * no reset, no sleep mode 60 * no standby mode 61 * sample rate to = 125Hz 62 * parameter to +/- 2000 degrees/sec 63 * low pass filter = 5Hz 64 * no interrupt 65 ******************************************/ 66 writeTo(GYRO, G_PWR_MGM, 0x00); 67 writeTo(GYRO, G_SMPLRT_DIV, 0x07); // EB, 50, 80, 7F, DE, 23, 20, FF 68 writeTo(GYRO, G_DLPF_FS, 0x1E); // +/- 2000 dgrs/sec, 1KHz, 1E, 19 69 writeTo(GYRO, G_INT_CFG, 0x00); 70 } 71 72 73 void getGyroscopeData(int * result) 74 { 75 /************************************** 76 Gyro ITG-3200 I2C 77 registers: 78 temp MSB = 1B, temp LSB = 1C 79 x axis MSB = 1D, x axis LSB = 1E 80 y axis MSB = 1F, y axis LSB = 20 81 z axis MSB = 21, z axis LSB = 22 82 *************************************/ 83 84 int regAddress = 0x1B; 85 int temp, x, y, z; 86 byte buff[G_TO_READ]; 87 88 readFrom(GYRO, regAddress, G_TO_READ, buff); //read the gyro data from the ITG3200 89 90 result[0] = ((buff[2] << 8) | buff[3]) + g_offx; 91 result[1] = ((buff[4] << 8) | buff[5]) + g_offy; 92 result[2] = ((buff[6] << 8) | buff[7]) + g_offz; 93 result[3] = (buff[0] << 8) | buff[1]; // temperature 94 95 } 96 97 98 float xz=0,yx=0,yz=0; 99 float p_xz=1,p_yx=1,p_yz=1; 100 float q_xz=0.0025,q_yx=0.0025,q_yz=0.0025; 101 float k_xz=0,k_yx=0,k_yz=0; 102 float r_xz=0.25,r_yx=0.25,r_yz=0.25; 103 //int acc_temp[3]; 104 //float acc[3]; 105 int acc[3]; 106 int gyro[4]; 107 float Axz; 108 float Ayx; 109 float Ayz; 110 float t=0.025; 111 void setup() 112 { 113 Serial.begin(9600); 114 Wire.begin(); 115 initAcc(); 116 initGyro(); 117 118 } 119 120 //unsigned long timer = 0; 121 //float o; 122 void loop() 123 { 124 125 getAccelerometerData(acc); 126 getGyroscopeData(gyro); 127 //timer = millis(); 128 sprintf(str, "%d,%d,%d,%d,%d,%d", acc[0],acc[1],acc[2],gyro[0],gyro[1],gyro[2]); 129 130 //acc[0]=acc[0]; 131 //acc[2]=acc[2]; 132 //acc[1]=acc[1]; 133 //r=sqrt(acc[0]*acc[0]+acc[1]*acc[1]+acc[2]*acc[2]); 134 gyro[0]=gyro[0]/ 14.375; 135 gyro[1]=gyro[1]/ (-14.375); 136 gyro[2]=gyro[2]/ 14.375; 137 138 139 Axz=(atan2(acc[0],acc[2]))*180/PI; 140 Ayx=(atan2(acc[0],acc[1]))*180/PI; 141 /*if((acc[0]!=0)&&(acc[1]!=0)) 142 { 143 Ayx=(atan2(acc[0],acc[1]))*180/PI; 144 } 145 else 146 { 147 Ayx=t*gyro[2]; 148 }*/ 149 Ayz=(atan2(acc[1],acc[2]))*180/PI; 150 151 152 //kalman filter 153 calculate_xz(); 154 calculate_yx(); 155 calculate_yz(); 156 157 //sprintf(str, "%d,%d,%d", xz_1, xy_1, x_1); 158 //Serial.print(xz);Serial.print(","); 159 //Serial.print(yx);Serial.print(","); 160 //Serial.print(yz);Serial.print(","); 161 //sprintf(str, "%d,%d,%d,%d,%d,%d", acc[0],acc[1],acc[2],gyro[0],gyro[1],gyro[2]); 162 //sprintf(str, "%d,%d,%d",gyro[0],gyro[1],gyro[2]); 163 Serial.print(Axz);Serial.print(","); 164 //Serial.print(Ayx);Serial.print(","); 165 //Serial.print(Ayz);Serial.print(","); 166 //Serial.print(str); 167 //o=gyro[2];//w=acc[2]; 168 //Serial.print(o);Serial.print(","); 169 //Serial.print(w);Serial.print(","); 170 Serial.print("\n"); 171 172 173 //delay(50); 174 } 175 void calculate_xz() 176 { 177 178 xz=xz+t*gyro[1]; 179 p_xz=p_xz+q_xz; 180 k_xz=p_xz/(p_xz+r_xz); 181 xz=xz+k_xz*(Axz-xz); 182 p_xz=(1-k_xz)*p_xz; 183 } 184 void calculate_yx() 185 { 186 187 yx=yx+t*gyro[2]; 188 p_yx=p_yx+q_yx; 189 k_yx=p_yx/(p_yx+r_yx); 190 yx=yx+k_yx*(Ayx-yx); 191 p_yx=(1-k_yx)*p_yx; 192 193 } 194 void calculate_yz() 195 { 196 yz=yz+t*gyro[0]; 197 p_yz=p_yz+q_yz; 198 k_yz=p_yz/(p_yz+r_yz); 199 yz=yz+k_yz*(Ayz-yz); 200 p_yz=(1-k_yz)*p_yz; 201 202 } 203 204 205 //---------------- Functions 206 //Writes val to address register on ACC 207 void writeTo(int DEVICE, byte address, byte val) { 208 Wire.beginTransmission(DEVICE); //start transmission to ACC 209 Wire.write(address); // send register address 210 Wire.write(val); // send value to write 211 Wire.endTransmission(); //end transmission 212 } 213 214 215 //reads num bytes starting from address register on ACC in to buff array 216 void readFrom(int DEVICE, byte address, int num, byte buff[]) { 217 Wire.beginTransmission(DEVICE); //start transmission to ACC 218 Wire.write(address); //sends address to read from 219 Wire.endTransmission(); //end transmission 220 221 Wire.beginTransmission(DEVICE); //start transmission to ACC 222 Wire.requestFrom(DEVICE, num); // request 6 bytes from ACC 223 224 int i = 0; 225 while(Wire.available()) //ACC may send less than requested (abnormal) 226 { 227 buff[i] = Wire.read(); // receive a byte 228 i++; 229 } 230 Wire.endTransmission(); //end transmission 231 }
十大濾波算法
1、限幅濾波法(又稱程序判斷濾波法) 2、中位值濾波法 3、算術平均濾波法 4、遞推平均濾波法(又稱滑動平均濾波法) 5、中位值平均濾波法(又稱防脈沖干擾平均濾波法) 6、限幅平均濾波法 7、一階滯后濾波法 8、加權遞推平均濾波法 9、消抖濾波法 10、限幅消抖濾波法 11、新增加 卡爾曼濾波(非擴展卡爾曼),代碼在17樓(點擊這里)感謝zhangzhe0617分享 程序默認對int類型數據進行濾波,如需要對其他類型進行濾波,只需要把程序中所有int替換成long、float或者double即可。 1、限幅濾波法(又稱程序判斷濾波法) ARDUINO 代碼復制打印 /* A、名稱:限幅濾波法(又稱程序判斷濾波法) B、方法: 根據經驗判斷,確定兩次采樣允許的最大偏差值(設為A), 每次檢測到新值時判斷: 如果本次值與上次值之差<=A,則本次值有效, 如果本次值與上次值之差>A,則本次值無效,放棄本次值,用上次值代替本次值。 C、優點: 能有效克服因偶然因素引起的脈沖干擾。 D、缺點: 無法抑制那種周期性的干擾。 平滑度差。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; int Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 Value = 300; } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Value = Filter_Value; // 最近一次有效采樣的值,該變量為全局變量 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 限幅濾波法(又稱程序判斷濾波法) #define FILTER_A 1 int Filter() { int NewValue; NewValue = Get_AD(); if(((NewValue - Value) > FILTER_A) || ((Value - NewValue) > FILTER_A)) return Value; else return NewValue; } 2、中位值濾波法 ARDUINO 代碼復制打印 /* A、名稱:中位值濾波法 B、方法: 連續采樣N次(N取奇數),把N次采樣值按大小排列, 取中間值為本次有效值。 C、優點: 能有效克服因偶然因素引起的波動干擾; 對溫度、液位的變化緩慢的被測參數有良好的濾波效果。 D、缺點: 對流量、速度等快速變化的參數不宜。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 中位值濾波法 #define FILTER_N 101 int Filter() { int filter_buf[FILTER_N]; int i, j; int filter_temp; for(i = 0; i < FILTER_N; i++) { filter_buf[i] = Get_AD(); delay(1); } // 采樣值從小到大排列(冒泡法) for(j = 0; j < FILTER_N - 1; j++) { for(i = 0; i < FILTER_N - 1 - j; i++) { if(filter_buf[i] > filter_buf[i + 1]) { filter_temp = filter_buf[i]; filter_buf[i] = filter_buf[i + 1]; filter_buf[i + 1] = filter_temp; } } } return filter_buf[(FILTER_N - 1) / 2]; } 3、算術平均濾波法 ARDUINO 代碼復制打印 /* A、名稱:算術平均濾波法 B、方法: 連續取N個采樣值進行算術平均運算: N值較大時:信號平滑度較高,但靈敏度較低; N值較小時:信號平滑度較低,但靈敏度較高; N值的選取:一般流量,N=12;壓力:N=4。 C、優點: 適用於對一般具有隨機干擾的信號進行濾波; 這種信號的特點是有一個平均值,信號在某一數值范圍附近上下波動。 D、缺點: 對於測量速度較慢或要求數據計算速度較快的實時控制不適用; 比較浪費RAM。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 算術平均濾波法 #define FILTER_N 12 int Filter() { int i; int filter_sum = 0; for(i = 0; i < FILTER_N; i++) { filter_sum += Get_AD(); delay(1); } return (int)(filter_sum / FILTER_N); } 4、遞推平均濾波法(又稱滑動平均濾波法) ARDUINO 代碼復制打印 /* A、名稱:遞推平均濾波法(又稱滑動平均濾波法) B、方法: 把連續取得的N個采樣值看成一個隊列,隊列的長度固定為N, 每次采樣到一個新數據放入隊尾,並扔掉原來隊首的一次數據(先進先出原則), 把隊列中的N個數據進行算術平均運算,獲得新的濾波結果。 N值的選取:流量,N=12;壓力,N=4;液面,N=4-12;溫度,N=1-4。 C、優點: 對周期性干擾有良好的抑制作用,平滑度高; 適用於高頻振盪的系統。 D、缺點: 靈敏度低,對偶然出現的脈沖性干擾的抑制作用較差; 不易消除由於脈沖干擾所引起的采樣值偏差; 不適用於脈沖干擾比較嚴重的場合; 比較浪費RAM。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 遞推平均濾波法(又稱滑動平均濾波法) #define FILTER_N 12 int filter_buf[FILTER_N + 1]; int Filter() { int i; int filter_sum = 0; filter_buf[FILTER_N] = Get_AD(); for(i = 0; i < FILTER_N; i++) { filter_buf[i] = filter_buf[i + 1]; // 所有數據左移,低位仍掉 filter_sum += filter_buf[i]; } return (int)(filter_sum / FILTER_N); } 5、中位值平均濾波法(又稱防脈沖干擾平均濾波法) ARDUINO 代碼復制打印 /* A、名稱:中位值平均濾波法(又稱防脈沖干擾平均濾波法) B、方法: 采一組隊列去掉最大值和最小值后取平均值, 相當於“中位值濾波法”+“算術平均濾波法”。 連續采樣N個數據,去掉一個最大值和一個最小值, 然后計算N-2個數據的算術平均值。 N值的選取:3-14。 C、優點: 融合了“中位值濾波法”+“算術平均濾波法”兩種濾波法的優點。 對於偶然出現的脈沖性干擾,可消除由其所引起的采樣值偏差。 對周期干擾有良好的抑制作用。 平滑度高,適於高頻振盪的系統。 D、缺點: 計算速度較慢,和算術平均濾波法一樣。 比較浪費RAM。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 中位值平均濾波法(又稱防脈沖干擾平均濾波法)(算法1) #define FILTER_N 100 int Filter() { int i, j; int filter_temp, filter_sum = 0; int filter_buf[FILTER_N]; for(i = 0; i < FILTER_N; i++) { filter_buf[i] = Get_AD(); delay(1); } // 采樣值從小到大排列(冒泡法) for(j = 0; j < FILTER_N - 1; j++) { for(i = 0; i < FILTER_N - 1 - j; i++) { if(filter_buf[i] > filter_buf[i + 1]) { filter_temp = filter_buf[i]; filter_buf[i] = filter_buf[i + 1]; filter_buf[i + 1] = filter_temp; } } } // 去除最大最小極值后求平均 for(i = 1; i < FILTER_N - 1; i++) filter_sum += filter_buf[i]; return filter_sum / (FILTER_N - 2); } // 中位值平均濾波法(又稱防脈沖干擾平均濾波法)(算法2) /* #define FILTER_N 100 int Filter() { int i; int filter_sum = 0; int filter_max, filter_min; int filter_buf[FILTER_N]; for(i = 0; i < FILTER_N; i++) { filter_buf[i] = Get_AD(); delay(1); } filter_max = filter_buf[0]; filter_min = filter_buf[0]; filter_sum = filter_buf[0]; for(i = FILTER_N - 1; i > 0; i--) { if(filter_buf[i] > filter_max) filter_max=filter_buf[i]; else if(filter_buf[i] < filter_min) filter_min=filter_buf[i]; filter_sum = filter_sum + filter_buf[i]; filter_buf[i] = filter_buf[i - 1]; } i = FILTER_N - 2; filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是為了四舍五入 filter_sum = filter_sum / i; return filter_sum; }*/ 6、限幅平均濾波法 ARDUINO 代碼復制打印 /* A、名稱:限幅平均濾波法 B、方法: 相當於“限幅濾波法”+“遞推平均濾波法”; 每次采樣到的新數據先進行限幅處理, 再送入隊列進行遞推平均濾波處理。 C、優點: 融合了兩種濾波法的優點; 對於偶然出現的脈沖性干擾,可消除由於脈沖干擾所引起的采樣值偏差。 D、缺點: 比較浪費RAM。 E、整理:shenhaiyu 2013-11-01 */ #define FILTER_N 12 int Filter_Value; int filter_buf[FILTER_N]; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 filter_buf[FILTER_N - 2] = 300; } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 限幅平均濾波法 #define FILTER_A 1 int Filter() { int i; int filter_sum = 0; filter_buf[FILTER_N - 1] = Get_AD(); if(((filter_buf[FILTER_N - 1] - filter_buf[FILTER_N - 2]) > FILTER_A) || ((filter_buf[FILTER_N - 2] - filter_buf[FILTER_N - 1]) > FILTER_A)) filter_buf[FILTER_N - 1] = filter_buf[FILTER_N - 2]; for(i = 0; i < FILTER_N - 1; i++) { filter_buf[i] = filter_buf[i + 1]; filter_sum += filter_buf[i]; } return (int)filter_sum / (FILTER_N - 1); } 7、一階滯后濾波法 ARDUINO 代碼復制打印 /* A、名稱:一階滯后濾波法 B、方法: 取a=0-1,本次濾波結果=(1-a)*本次采樣值+a*上次濾波結果。 C、優點: 對周期性干擾具有良好的抑制作用; 適用於波動頻率較高的場合。 D、缺點: 相位滯后,靈敏度低; 滯后程度取決於a值大小; 不能消除濾波頻率高於采樣頻率1/2的干擾信號。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; int Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 Value = 300; } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 一階滯后濾波法 #define FILTER_A 0.01 int Filter() { int NewValue; NewValue = Get_AD(); Value = (int)((float)NewValue * FILTER_A + (1.0 - FILTER_A) * (float)Value); return Value; } 8、加權遞推平均濾波法 ARDUINO 代碼復制打印 /* A、名稱:加權遞推平均濾波法 B、方法: 是對遞推平均濾波法的改進,即不同時刻的數據加以不同的權; 通常是,越接近現時刻的數據,權取得越大。 給予新采樣值的權系數越大,則靈敏度越高,但信號平滑度越低。 C、優點: 適用於有較大純滯后時間常數的對象,和采樣周期較短的系統。 D、缺點: 對於純滯后時間常數較小、采樣周期較長、變化緩慢的信號; 不能迅速反應系統當前所受干擾的嚴重程度,濾波效果差。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 加權遞推平均濾波法 #define FILTER_N 12 int coe[FILTER_N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // 加權系數表 int sum_coe = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12; // 加權系數和 int filter_buf[FILTER_N + 1]; int Filter() { int i; int filter_sum = 0; filter_buf[FILTER_N] = Get_AD(); for(i = 0; i < FILTER_N; i++) { filter_buf[i] = filter_buf[i + 1]; // 所有數據左移,低位仍掉 filter_sum += filter_buf[i] * coe[i]; } filter_sum /= sum_coe; return filter_sum; } 9、消抖濾波法 ARDUINO 代碼復制打印 /* A、名稱:消抖濾波法 B、方法: 設置一個濾波計數器,將每次采樣值與當前有效值比較: 如果采樣值=當前有效值,則計數器清零; 如果采樣值<>當前有效值,則計數器+1,並判斷計數器是否>=上限N(溢出); 如果計數器溢出,則將本次值替換當前有效值,並清計數器。 C、優點: 對於變化緩慢的被測參數有較好的濾波效果; 可避免在臨界值附近控制器的反復開/關跳動或顯示器上數值抖動。 D、缺點: 對於快速變化的參數不宜; 如果在計數器溢出的那一次采樣到的值恰好是干擾值,則會將干擾值當作有效值導入系統。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; int Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 Value = 300; } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 消抖濾波法 #define FILTER_N 12 int i = 0; int Filter() { int new_value; new_value = Get_AD(); if(Value != new_value) { i++; if(i > FILTER_N) { i = 0; Value = new_value; } } else i = 0; return Value; } 10、限幅消抖濾波法 ARDUINO 代碼復制打印 /* A、名稱:限幅消抖濾波法 B、方法: 相當於“限幅濾波法”+“消抖濾波法”; 先限幅,后消抖。 C、優點: 繼承了“限幅”和“消抖”的優點; 改進了“消抖濾波法”中的某些缺陷,避免將干擾值導入系統。 D、缺點: 對於快速變化的參數不宜。 E、整理:shenhaiyu 2013-11-01 */ int Filter_Value; int Value; void setup() { Serial.begin(9600); // 初始化串口通信 randomSeed(analogRead(0)); // 產生隨機種子 Value = 300; } void loop() { Filter_Value = Filter(); // 獲得濾波器輸出值 Serial.println(Filter_Value); // 串口輸出 delay(50); } // 用於隨機產生一個300左右的當前值 int Get_AD() { return random(295, 305); } // 限幅消抖濾波法 #define FILTER_A 1 #define FILTER_N 5 int i = 0; int Filter() { int NewValue; int new_value; NewValue = Get_AD(); if(((NewValue - Value) > FILTER_A) || ((Value - NewValue) > FILTER_A)) new_value = Value; else new_value = NewValue; if(Value != new_value) { i++; if(i > FILTER_N) { i = 0; Value = new_value; } } else i = 0; return Value; }
1、限幅濾波法(又稱程序判斷濾波法)
2、中位值濾波法
3、算術平均濾波法
4、遞推平均濾波法(又稱滑動平均濾波法)
5、中位值平均濾波法(又稱防脈沖干擾平均濾波法)
6、限幅平均濾波法
7、一階滯后濾波法
8、加權遞推平均濾波法
9、消抖濾波法
10、限幅消抖濾波法
11、新增加 卡爾曼濾波(非擴展卡爾曼),代碼在17樓(點擊這里)感謝zhangzhe0617分享
程序默認對int類型數據進行濾波,如需要對其他類型進行濾波,只需要把程序中所有int替換成long、float或者double即可。
1、限幅濾波法(又稱程序判斷濾波法)
2、中位值濾波法
3、算術平均濾波法
4、遞推平均濾波法(又稱滑動平均濾波法)
5、中位值平均濾波法(又稱防脈沖干擾平均濾波法)
6、限幅平均濾波法
7、一階滯后濾波法
8、加權遞推平均濾波法
9、消抖濾波法
10、限幅消抖濾波法
