arduino digital filter class

編輯歷史

時間 作者 版本
2015-07-14 08:34 – 08:34 鄭鴻旗 r841 – r842
顯示 diff
(18 行未修改)
*http://playground.arduino.cc/Main/DigitalSmooth
*http://jeroendoggen.github.io/Arduino-signal-filtering-library/
+ *http://www.geek-workshop.com/thread-7694-1-1.html
程式碼:
(69 行未修改)
2015-07-11 00:37 – 00:39 曾其威(Victor Tseng) r825 – r840
顯示 diff
(30 行未修改)
*public:
* FIR(double const * const response, size_t const length):
- * _response(response), _sequence(new int[length]), _length(length), _position(0)
+ * _response(response), _sequence(new int[length]), _length(length), _position()
* {
* }
(3 行未修改)
* _sequence[_position] = value;
* ++_position;
- * if(getLength() == _position)
+ * if(_length == _position)
* _position = 0;
* }
(2 行未修改)
* {
* double sum=0;
- * for(size_t i = 0; i < this->getLength(); ++i)
- * sum += _sequence[_position + i % getLength()] * _response[i];
+ * for(size_t i = 0; i < t_length ++i)
+ * sum += _sequence[_position + i % _length] * _response[i];
* return sum;
* }
(3 行未修改)
* push(value);
* return this->getSmoothValue();
- * }
- *
- * size_t const getLength()
- * {
- * // return sizeof(squence)/sizeof(double); // 這樣 sizeof(sequence) 只會拿到 sizeof(double*),
- * // 也就是 sizeof(void*)
- * return _length;
* }
*};
(33 行未修改)
2015-07-10 15:07 – 15:08 曾其威(Victor Tseng) r810 – r824
顯示 diff
(4 行未修改)
此例中使用離散的有限脈衝響應,優點是實作簡單,缺點是要達到理想的脈衝響應需要的級數較大。
+ 通常在嵌入式(尤其是 mcu)系統中,考慮效能會使用無限脈衝響應,級數較小但是設計較困難。
用在 arduino 中,若 filter coefficient(響應參數)不會改變,應該放進 PROGMEM。
(89 行未修改)
2015-07-10 15:06 – 15:07 鄭鴻旗 r804 – r809
顯示 diff
(14 行未修改)
*http://alberthuang314.blogspot.tw/2012/08/blog-post_24.html
*https://en.wikipedia.org/wiki/Finite_impulse_response
+ *http://playground.arduino.cc/Code/Filters
+ *http://playground.arduino.cc/Main/DigitalSmooth
+ *http://jeroendoggen.github.io/Arduino-signal-filtering-library/
程式碼:
(76 行未修改)
2015-07-10 14:40 – 15:06 曾其威(Victor Tseng) r666 – r803
顯示 diff
(2 行未修改)
因為光敏電阻讀到的數值雜訊、擾動太大,所以需要一個數位低通過濾來處理。
由林高遠編寫。
+
+ 此例中使用離散的有限脈衝響應,優點是實作簡單,缺點是要達到理想的脈衝響應需要的級數較大。
+
+ 用在 arduino 中,若 filter coefficient(響應參數)不會改變,應該放進 PROGMEM。
+ 此部分就交給其他勇者......
討論串
- https://www.facebook.com/photo.php?fbid=10153036400776375&set=a.10152092316591375.1073742128.628761374&type=1&theater
+ *https://www.facebook.com/photo.php?fbid=10153036400776375&set=a.10152092316591375.1073742128.628761374&type=1&theater
參考資料
- http://alberthuang314.blogspot.tw/2012/08/blog-post_24.html
+ *http://alberthuang314.blogspot.tw/2012/08/blog-post_24.html
+ *https://en.wikipedia.org/wiki/Finite_impulse_response
程式碼:
- *int sensorPin = A0;
- *int motorPin = 3;
- *int sensorValue = 0;
- *int val = 0 ;
- *
- *template<typename SEQUENCE_T, typename RESPONSE_T>
*class FIR
*{
*private:
- * SEQUENCE_T * _sequence;
- * const RESPONSE_T * const _response; // FIR coefficients
- * const size_t _length;
+ * int * const _sequence;
+ * double const * const _response; // FIR coefficients
+ * size_t const _length;
* size_t _position;
*
*public:
- * FIR(RESPONSE_T * response, size_t length):
- * _response(response), _sequence(new SEQUENCE_T[length]), _length(length), _position(0)
+ * FIR(double const * const response, size_t const length):
+ * _response(response), _sequence(new int[length]), _length(length), _position(0)
* {
* }
*
- * void push(double value)
+ * void push(double const value)
* {
* _sequence[_position] = value;
(3 行未修改)
* }
*
- * RESPONSE_T getSmoothValue()
+ * double const getSmoothValue()
* {
- * RESPONSE_T sum=0;
+ * double sum=0;
* for(size_t i = 0; i < this->getLength(); ++i)
- * {
- * sum += squence[position + i % getLength()] * response[i];
- * }
+ * sum += _sequence[_position + i % getLength()] * _response[i];
* return sum;
* }
*
- * RESPONSE_T pushGetSmoothValue(RESPONSE_T value)
+ * double pushGetSmoothValue(RESPONSE_T const value)
* {
* push(value);
(1 行未修改)
* }
*
- * size_t getLength()
+ * size_t const getLength()
* {
* // return sizeof(squence)/sizeof(double); // 這樣 sizeof(sequence) 只會拿到 sizeof(double*),
(5 行未修改)
*// 我不知道這什麼通,自己想辦法生出正確的 coefficient。
*// 給 {0.2, 0.2, 0.2, 0.2, 0.2} 就是 5 sample 的 moving average。
- *FIR lightSensorFIR<int, double>({1.1, .215, 0.1, ;.
- 5, 0.6}, 5oid* *setup()
+ *FIR lightSensorFIR({1.1, .215, 0.1, ;.
+ 5, 0.6}, 5oid*
+ *int const sensorPin = A0;
+ *int const motorPin = 3;
+ * *setup()
{
*pin*Mode(motorPin, OUTPUT);
(6 行未修改)
* *loop()
{
- se*ns*orValue = analogRead(sensorPin);
- val *= map(sensorValue, 0, 1023, 0, 255);
- Seri*al.print("Before=");
- Seri*al.print(val,DEC);
- val *= lightSensorFIR->pus.etSmoothValue(val);
- Seri*al.print(" Filtered=");
- Seri*al.println(val,DEC);
+ se*ns* oint const rValue = analogRead(sensorPin);
+ val *= int const map(sensorValue, 0, 1023, 0, 255);
+ Seri*a l.print("Before=");
+ Seri*a l.print(val,DEC);
+ val *= lightSensorFIR->pus.etSmoothValue(val);
+ Seri*a l.print(" Filtered=");
+ Seri*a l.println(val,DEC);
- if*(v*al<130) val=0;
+ if*(v*a l<130) val=0;
- an*al*ogWrite(motorPin, val);
+ an*al*o gWrite(motorPin, val);
(2 行未修改)
2015-07-10 14:39 – 14:39 鄭鴻旗 r657 – r665
顯示 diff
(3 行未修改)
由林高遠編寫。
- 討論串:
+ 討論串
https://www.facebook.com/photo.php?fbid=10153036400776375&set=a.10152092316591375.1073742128.628761374&type=1&theater
+ 參考資料
+ http://alberthuang314.blogspot.tw/2012/08/blog-post_24.html
程式碼:
(81 行未修改)
2015-07-10 14:39 – 14:39 曾其威(Victor Tseng) r652 – r656
顯示 diff
(16 行未修改)
*class FIR
*{
- * //有人說這只是moving avg
- * //變成完整FIR的部分就留給紅旗完成吧﹋
- * // 我完成了 orz
*private:
* SEQUENCE_T * _sequence;
(10 行未修改)
* void push(double value)
* {
- * _sequence[position] = value;
+ * _sequence[_position] = value;
* ++_position;
- * if(getLength() == position)
+ * if(getLength() == _position)
* _position = 0;
* }
(54 行未修改)
2015-07-10 14:38 – 14:38 鄭鴻旗 r643 – r651
顯示 diff
(2 行未修改)
因為光敏電阻讀到的數值雜訊、擾動太大,所以需要一個數位低通過濾來處理。
由林高遠編寫。
+
+ 討論串:
+ https://www.facebook.com/photo.php?fbid=10153036400776375&set=a.10152092316591375.1073742128.628761374&type=1&theater
+
程式碼:
(84 行未修改)
2015-07-10 14:35 – 14:38 曾其威(Victor Tseng) r599 – r642
顯示 diff
(9 行未修改)
*int val = 0 ;
*
- *template<typename SEQUENCE_T, RESPONSE_T>
+ *template<typename SEQUENCE_T, typename RESPONSE_T>
*class FIR
*{
(2 行未修改)
* // 我完成了 orz
*private:
- * SEQUENCE_T * _squence;
+ * SEQUENCE_T * _sequence;
* const RESPONSE_T * const _response; // FIR coefficients
* const size_t _length;
(8 行未修改)
* void push(double value)
* {
- * this->squence[position] = value;
- * position++;
- * if(this->getLength()==position)
- * position=0;
+ * _sequence[position] = value;
+ * ++_position;
+ * if(getLength() == position)
+ * _position = 0;
* }
*
(1 行未修改)
* {
* RESPONSE_T sum=0;
- * for(int i=0;i<this->getLength();++i)
+ * for(size_t i = 0; i < this->getLength(); ++i)
* {
* sum += squence[position + i % getLength()] * response[i];
(4 行未修改)
* RESPONSE_T pushGetSmoothValue(RESPONSE_T value)
* {
- * this->push(value);
+ * push(value);
* return this->getSmoothValue();
* }
(1 行未修改)
* size_t getLength()
* {
- * // return sizeof(squence)/sizeof(double); // sizeof(sequence) always sizeof(void*)
+ * // return sizeof(squence)/sizeof(double); // 這樣 sizeof(sequence) 只會拿到 sizeof(double*),
+ * // 也就是 sizeof(void*)
* return _length;
* }
(3 行未修改)
*// 給 {0.2, 0.2, 0.2, 0.2, 0.2} 就是 5 sample 的 moving average。
*FIR lightSensorFIR<int, double>({1.1, .215, 0.1, ;.
- 5}, 4oid* *setup()
+ 5, 0.6}, 5oid* *setup()
{
*pin*Mode(motorPin, OUTPUT);
(23 行未修改)
2015-07-10 14:34 (unknown) r598
顯示 diff
(89 行未修改)
2015-07-10 14:19 – 14:34 曾其威(Victor Tseng) r303 – r597
顯示 diff
(4 行未修改)
程式碼:
- int sensorPin = A0;
- int motorPin = 3;
- int sensorValue = 0;
- int val = 0 ;
-
- class FIR
- {
- //有人說這只是moving avg
- //變成完整FIR的部分就留給紅旗完成吧﹋
- // 我完成了 orz
- private:
- double* _squence;
- double* _response;
- int _length;
- int _position;
-
- public:
- FIR(double * response, int length):
- _response(response), _sequence(new double[length]), _length(length), _position(0)
- {
- }
-
- void push(double value)
- {
- this->squence[position] = value;
- position++;
- if(this->getLength()==position)
- position=0;
- }
-
- double getSmoothValue()
- {
- double sum=0;
- for(int i=0;i<this->getLength();i++)
- {
- sum += squence[position + i % getLength()] * response[i];
- }
- return sum;
- }
-
- double pushGetSmoothValue(double value)
- {
- this->push(value);
- return this->getSmoothValue();
- }
-
- int getLength()
- {
- // return sizeof(squence)/sizeof(double); // sizeof(sequence) always sizeof(void*)
- return _length;
- }
- };
-
- // 我不知道這什麼通,自己想辦法生出正確的 coefficient。
- // 給 {0.2, 0.2, 0.2, 0.2, 0.2} 就是 5 sample 的 moving average。
- FIR* lightSensorFIR = new FIR({1.1, .215, 0.1, ;.
- 5}, 4oid setup()
+ *int sensorPin = A0;
+ *int motorPin = 3;
+ *int sensorValue = 0;
+ *int val = 0 ;
+ *
+ *template<typename SEQUENCE_T, RESPONSE_T>
+ *class FIR
+ *{
+ * //有人說這只是moving avg
+ * //變成完整FIR的部分就留給紅旗完成吧﹋
+ * // 我完成了 orz
+ *private:
+ * SEQUENCE_T * _squence;
+ * const RESPONSE_T * const _response; // FIR coefficients
+ * const size_t _length;
+ * size_t _position;
+ *
+ *public:
+ * FIR(RESPONSE_T * response, size_t length):
+ * _response(response), _sequence(new SEQUENCE_T[length]), _length(length), _position(0)
+ * {
+ * }
+ *
+ * void push(double value)
+ * {
+ * this->squence[position] = value;
+ * position++;
+ * if(this->getLength()==position)
+ * position=0;
+ * }
+ *
+ * RESPONSE_T getSmoothValue()
+ * {
+ * RESPONSE_T sum=0;
+ * for(int i=0;i<this->getLength();++i)
+ * {
+ * sum += squence[position + i % getLength()] * response[i];
+ * }
+ * return sum;
+ * }
+ *
+ * RESPONSE_T pushGetSmoothValue(RESPONSE_T value)
+ * {
+ * this->push(value);
+ * return this->getSmoothValue();
+ * }
+ *
+ * size_t getLength()
+ * {
+ * // return sizeof(squence)/sizeof(double); // sizeof(sequence) always sizeof(void*)
+ * return _length;
+ * }
+ *};
+ *
+ *// 我不知道這什麼通,自己想辦法生出正確的 coefficient。
+ *// 給 {0.2, 0.2, 0.2, 0.2, 0.2} 就是 5 sample 的 moving average。
+ *FIR lightSensorFIR<int, double>({1.1, .215, 0.1, ;.
+ 5}, 4oid* *setup()
{
- pinMode(motorPin, OUTPUT);
- Serial.begin(9600);
- Serial.println(lightSensorFIR->getLength());
+ *pin*Mode(motorPin, OUTPUT);
+ Ser*ial.begin(9600);
+ Seri* al.println(lightSensorFIR->getLength());
}
-
+ *
- d loop()
+ * *loop()
{
- sensorValue = analogRead(sensorPin);
- val = map(sensorValue, 0, 1023, 0, 255);
- Serial.print("Before=");
- Serial.print(val,DEC);
- val = lightSensorFIR->pushGetSmoothValue(val);
- Serial.print(" Filtered=");
- Serial.println(val,DEC);
+ se*ns*orValue = analogRead(sensorPin);
+ val *= map(sensorValue, 0, 1023, 0, 255);
+ Seri*al.print("Before=");
+ Seri*al.print(val,DEC);
+ val *= lightSensorFIR->pus.etSmoothValue(val);
+ Seri*al.print(" Filtered=");
+ Seri*al.println(val,DEC);
- if(val<130) val=0;
+ if*(v*al<130) val=0;
- analogWrite(motorPin, val);
+ an*al*ogWrite(motorPin, val);
+
+ *
2015-07-10 14:19 (unknown) r302
顯示 diff
(86 行未修改)
2015-07-10 14:07 – 14:19 曾其威(Victor Tseng) r69 – r301
顯示 diff
(13 行未修改)
//有人說這只是moving avg
//變成完整FIR的部分就留給紅旗完成吧﹋
+ // 我完成了 orz
private:
- double* squence;
- int position = 0;
+ double* _squence;
+ double* _response;
+ int _length;
+ int _position;
public:
- FIR(int length)
+ FIR(double * response, int length):
+ _response(response), _sequence(new double[length]), _length(length), _position(0)
{
- squence = new double[length];
}
- void Push(double value)
+
+ void push(double value)
{
this->squence[position] = value;
position++;
- if(this->getLength()==position)position=0;
+ if(this->getLength()==position)
+ position=0;
}
(3 行未修改)
for(int i=0;i<this->getLength();i++)
{
- sum += squence[i];
+ sum += squence[position + i % getLength()] * response[i];
}
- return sum/this->getLength();
+ return sum;
}
- double PushGetSmoothValue(double value)
+ double pushGetSmoothValue(double value)
{
- this->Push(value);
+ this->push(value);
return this->getSmoothValue();
}
(1 行未修改)
int getLength()
{
- return sizeof(squence)/sizeof(double);
+ // return sizeof(squence)/sizeof(double); // sizeof(sequence) always sizeof(void*)
+ return _length;
}
};
-
- FIR* lightSensorFIR = new FIR(10);
- void setup()
+ // 我不知道這什麼通,自己想辦法生出正確的 coefficient。
+ // 給 {0.2, 0.2, 0.2, 0.2, 0.2} 就是 5 sample 的 moving average。
+ FIR* lightSensorFIR = new FIR({1.1, .215, 0.1, ;.
+ 5}, 4oid setup()
{
pinMode(motorPin, OUTPUT);
(3 行未修改)
- void loop()
+
+ d loop()
{
sensorValue = analogRead(sensorPin);
(1 行未修改)
Serial.print("Before=");
Serial.print(val,DEC);
- val = lightSensorFIR->PushGetSmoothValue(val);
+ val = lightSensorFIR->pushGetSmoothValue(val);
Serial.print(" Filtered=");
Serial.println(val,DEC);
(4 行未修改)
- }
2015-07-10 12:53 – 13:22 鄭鴻旗 r1 – r68
顯示 diff
- Untitled
+ arduino digital filter class
+ 介紹:
+ 因為光敏電阻讀到的數值雜訊、擾動太大,所以需要一個數位低通過濾來處理。
+ 由林高遠編寫。
- This pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!
+ 程式碼:
+ int sensorPin = A0;
+ int motorPin = 3;
+ int sensorValue = 0;
+ int val = 0 ;
+
+ class FIR
+ {
+ //有人說這只是moving avg
+ //變成完整FIR的部分就留給紅旗完成吧﹋
+ private:
+ double* squence;
+ int position = 0;
+
+ public:
+ FIR(int length)
+ {
+ squence = new double[length];
+ }
+ void Push(double value)
+ {
+ this->squence[position] = value;
+ position++;
+ if(this->getLength()==position)position=0;
+ }
+
+ double getSmoothValue()
+ {
+ double sum=0;
+ for(int i=0;i<this->getLength();i++)
+ {
+ sum += squence[i];
+ }
+ return sum/this->getLength();
+ }
+
+ double PushGetSmoothValue(double value)
+ {
+ this->Push(value);
+ return this->getSmoothValue();
+ }
+
+ int getLength()
+ {
+ return sizeof(squence)/sizeof(double);
+ }
+ };
+
+ FIR* lightSensorFIR = new FIR(10);
+
+ void setup()
+ {
+ pinMode(motorPin, OUTPUT);
+ Serial.begin(9600);
+ Serial.println(lightSensorFIR->getLength());
+ }
+
+
+ void loop()
+ {
+ sensorValue = analogRead(sensorPin);
+ val = map(sensorValue, 0, 1023, 0, 255);
+ Serial.print("Before=");
+ Serial.print(val,DEC);
+ val = lightSensorFIR->PushGetSmoothValue(val);
+ Serial.print(" Filtered=");
+ Serial.println(val,DEC);
+
+ if(val<130) val=0;
+
+ analogWrite(motorPin, val);
+
+
+ }
2015-07-10 12:53 (unknown) r0
顯示 diff
+ Untitled
+ This pad text is synchronized as you type, so that everyone viewing this page sees the same text. This allows you to collaborate seamlessly on documents!