digitalFilter.cpp
Go to the documentation of this file.
1 /*
2  * ----------------------------------------------------------------------------
3  * "THE BEER-WARE LICENSE" (Revision 42):
4  * <joeromano@gmail.com> wrote this file. As long as you retain this notice you
5  * can do whatever you want with this stuff. If we meet some day, and you think
6  * this stuff is worth it, you can buy me a beer in return
7  * Joe Romano and Will McMahan
8  * ----------------------------------------------------------------------------
9  */
10 //@author Joe Romano
11 //@author Will McMahan
12 //@email joeromano@gmail.com
13 //@brief digitalFilter.cpp - class to create IIR and FIR digital filter
14 // coefficients in the Matlab (2010) style. This style being vectors
15 // of coefficients for the numberator (b) and denominator (a)
16 // respectively.
17 // Please refer to the matlab documentation page for implementation
18 // details: http://www.mathworks.com/access/helpdesk/help/techdoc/ref/filter.html
19 
21 
22 digitalFilter::digitalFilter(int filterOrder_userdef, bool isIIR)
23 {
24  filterOrder = filterOrder_userdef;
25  IIR = isIIR;
26 
27  b = new float [filterOrder + 1];
28  a = new float [filterOrder + 1];
29 
30  x = new float [filterOrder + 1];
31  u = new float [filterOrder + 1];
32 
33  // Initialize the arrays with zeros
34  for(int i = 0; i < (filterOrder + 1); i++)
35  {
36  b[i] = 0.0;
37  a[i] = 0.0;
38  x[i] = 0.0;
39  u[i] = 0.0;
40  }
41 }
42 
43 digitalFilter::digitalFilter(int filterOrder_userdef, bool isIIR, float *b_userdef, float *a_userdef)
44 {
45 
46  filterOrder = filterOrder_userdef;
47  IIR = isIIR;
48 
49  b = new float [filterOrder + 1];
50  a = new float [filterOrder + 1];
51 
52  x = new float [filterOrder + 1];
53  u = new float [filterOrder + 1];
54 
55  // Initialize the arrays
56 
57  for(int i = 0; i < (filterOrder + 1); i++)
58  {
59  b[i] = b_userdef[i];
60  a[i] = a_userdef[i];
61  x[i] = 0.0;
62  u[i] = 0.0;
63  }
64 
65 }
66 
68 {
69  /* Shift x2 and u2 vectors, losing the last elements and putting new u2 value in zeroth spot. */
70  for (int i = filterOrder ; i > 0 ; i--) {
71  x[i] = x[i-1];
72  u[i] = u[i-1];
73  }
74  u[0] = u_current;
75 
76  /* Simulate system. */
77  float output = b[0] * u[0];
78 
79  // if we have an IIR filter
80  if(IIR)
81  {
82  for (int i = 1 ; i < (filterOrder+1) ; i++) {
83  output += b[i] * u[i] - a[i] * x[i];
84  }
85  }
86 
87  // if we have an FIR filter
88  else
89  {
90  for (int i = 1 ; i < (filterOrder+1) ; i++) {
91  output += b[i] * u[i];
92  }
93  }
94 
95  /* Put the result in shared memory and in the x2 vector. */
96  x[0] = output;
97 
98  return output;
99 }
100 
102 {
103  delete[] x;
104  delete[] u;
105  delete[] a;
106  delete[] b;
107 }
digitalFilter(int filterOrder_userdef, bool isIIR)
float getNextFilteredValue(float u_current)


pr2_gripper_sensor_controller
Author(s): Joe Romano
autogenerated on Wed Apr 1 2020 03:58:23