Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of IIR by
Revision 0:a7405ea45d27, committed 2016-07-29
- Comitter:
- benson516
- Date:
- Fri Jul 29 14:26:54 2016 +0000
- Child:
- 1:3d8226c5abfe
- Commit message:
- Infinite impulse response filter
Changed in this revision
| IIR.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/IIR.h Fri Jul 29 14:26:54 2016 +0000
@@ -0,0 +1,83 @@
+#include <vector>
+using std::vector;
+
+class Circular_buffer{
+private:
+ vector<float> A;
+ // vector<float>::iterator it;
+ int ind; // The index of the newest element
+
+public:
+ Circular_buffer(){
+ ind = 0;
+ }
+ Circular_buffer(int n){
+ A.resize(n);
+ for (size_t i=0; i< A.size(); i++)
+ A[i] = 0;
+
+ ind = 0;
+ }
+
+ void Init(int n){ // Input: buffer size
+ A.resize(n);
+ for (size_t i=0; i< A.size(); i++)
+ A[i] = 0;
+ ind = 0;
+ }
+
+ float Get(int i){ // Get the value of the (t-i)th element, where t stands for the current sample
+ int ind_e = ind-i;
+ while (ind_e < 0){
+ ind_e += A.size();
+ }
+ return A[ind_e];
+ }
+ void Insert(float x_new){ // Pop the oldest element and push a new element
+ ind++;
+ while (ind >= A.size()){
+ ind -= A.size();
+ }
+ A[ind] = x_new;
+ }
+};
+
+class IIR{
+private:
+
+ int m; // the order of numerator
+ int n; // the order of denominator
+ Circular_buffer x,y;
+ vector<float> b; // parameters of numerator, highest order --> lowest order (newest -> oldest)
+ vector<float> a; // parameters of denominator, highest order --> lowest order (newest -> oldest)
+ float gain;
+
+public:
+ IIR(int m, int n): m(m),n(n) {
+ x.Init(m+1);
+ y.Init(n);
+ }
+ void Assign_parameters(float b_in[], float a_in[], float gain_in){
+ b.assign(b_in,b_in+m+1);
+ a.assign(a_in,a_in+n);
+ gain = gain_in;
+ for (size_t i = 0; i < b.size(); i++){
+ b[i] *= gain;
+ }
+ }
+ float Iterate_once(float x_in){
+ x.Insert(x_in);
+ float y_new = 0;
+ // Numerator
+ for (int i = 0; i <= m; i++ ){
+ y_new += b[i]*x.Get(i);
+ }
+
+ // Denominator
+ for (int i = 0; i < n; i++){
+ y_new -= a[i]*y.Get(i);
+ }
+ y.Insert(y_new);
+ return y_new;
+ }
+};
