icub-basic-demos
IIRFilt.cpp
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 
3 // Copyright: (C) 2006-2007 Alex Bernardino, ISR-IST
4 // Authors: Alex Bernardino
5 // CopyPolicy: Released under the terms of the GNU GPL v2.0.
6 
17 #include <iCub/IIRFilt.h>
18 
19 void iir_filt_forward(float *in, int stepin, float *out, int stepout, int length, float *coeffs, float *i0)
20 {
21  int j;
22  float b0 = coeffs[0];
23  float a1 = coeffs[1];
24  float a2 = coeffs[2];
25  float a3 = coeffs[3];
26 
27  out[0] = b0*in[0] - a1*i0[0] - a2*i0[1] - a3*i0[2];
28  out[1*stepout] = b0*in[stepin] - a1*out[0] - a2*i0[0] - a3*i0[1];
29  out[2*stepout] = b0*in[2*stepin] - a1*out[stepout] - a2*out[0] - a3*i0[0];
30  for(j = 3; j < length; j++)
31  out[j*stepout] = b0*in[j*stepin] - a1*out[(j-1)*stepout] - a2*out[(j-2)*stepout] - a3*out[(j-3)*stepout];
32 }
33 
34 
35 void iir_filt_backward(float *in, int stepin, float *out, int stepout, int length, float *coeffs, float *i0)
36 {
37  int j;
38  float b0 = coeffs[0];
39  float a1 = coeffs[1];
40  float a2 = coeffs[2];
41  float a3 = coeffs[3];
42 
43  out[(length-1)*stepout] = b0*in[(length-1)*stepin] - a1*i0[0] - a2*i0[1] - a3*i0[2];
44  out[(length-2)*stepout] = b0*in[(length-2)*stepin] - a1*out[(length-1)*stepout] - a2*i0[0] - a3*i0[1];
45  out[(length-3)*stepout] = b0*in[(length-3)*stepin] - a1*out[(length-2)*stepout] - a2*out[(length-1)*stepout] - a3*i0[0];
46  for(j = length-4; j >= 0; j--)
47  out[j*stepout] = b0*in[j*stepin] - a1*out[(j+1)*stepout] - a2*out[(j+2)*stepout] - a3*out[(j+3)*stepout];
48 }
49 
50 void iir_filt_forward(float *in, float *out, int length, float *coeffs, float *i0)
51 {
52  int j;
53  float b0 = coeffs[0];
54  float a1 = coeffs[1];
55  float a2 = coeffs[2];
56  float a3 = coeffs[3];
57 
58  out[0] = b0*in[0] - a1*i0[0] - a2*i0[1] - a3*i0[2];
59  out[1] = b0*in[1] - a1*out[0] - a2*i0[0] - a3*i0[1];
60  out[2] = b0*in[2] - a1*out[1] - a2*out[0] - a3*i0[0];
61  for(j = 3; j < length; j++)
62  out[j] = b0*in[j] - a1*out[j-1] - a2*out[j-2] - a3*out[j-3];
63 }
64 
65 
66 void iir_filt_backward(float *in, float *out, int length, float *coeffs, float *i0)
67 {
68  int j;
69  float b0 = coeffs[0];
70  float a1 = coeffs[1];
71  float a2 = coeffs[2];
72  float a3 = coeffs[3];
73 
74  out[length-1] = b0*in[length-1] - a1*i0[0] - a2*i0[1] - a3*i0[2];
75  out[length-2] = b0*in[length-2] - a1*out[length-1] - a2*i0[0] - a3*i0[1];
76  out[length-3] = b0*in[length-3] - a1*out[length-2] - a2*out[length-1] - a3*i0[0];
77  for(j = length-4; j >= 0; j--)
78  out[j] = b0*in[j] - a1*out[j+1] - a2*out[j+2] - a3*out[j+3];
79 }
80 
81 void iir_filt_forward(float *in, float *out, int stepout, int length, float *coeffs, float *i0)
82 {
83  int j;
84  float b0 = coeffs[0];
85  float a1 = coeffs[1];
86  float a2 = coeffs[2];
87  float a3 = coeffs[3];
88 
89  out[0] = b0*in[0] - a1*i0[0] - a2*i0[1] - a3*i0[2];
90  out[1*stepout] = b0*in[1] - a1*out[0] - a2*i0[0] - a3*i0[1];
91  out[2*stepout] = b0*in[2] - a1*out[stepout] - a2*out[0] - a3*i0[0];
92  for(j = 3; j < length; j++)
93  out[j*stepout] = b0*in[j] - a1*out[(j-1)*stepout] - a2*out[(j-2)*stepout] - a3*out[(j-3)*stepout];
94 }
95 
96 
97 void iir_filt_backward(float *in, float *out, int stepout, int length, float *coeffs, float *i0)
98 {
99  int j;
100  float b0 = coeffs[0];
101  float a1 = coeffs[1];
102  float a2 = coeffs[2];
103  float a3 = coeffs[3];
104 
105  out[(length-1)*stepout] = b0*in[length-1] - a1*i0[0] - a2*i0[1] - a3*i0[2];
106  out[(length-2)*stepout] = b0*in[length-2] - a1*out[(length-1)*stepout] - a2*i0[0] - a3*i0[1];
107  out[(length-3)*stepout] = b0*in[length-3] - a1*out[(length-2)*stepout] - a2*out[(length-1)*stepout] - a3*i0[0];
108  for(j = length-4; j >= 0; j--)
109  out[j*stepout] = b0*in[j] - a1*out[(j+1)*stepout] - a2*out[(j+2)*stepout] - a3*out[(j+3)*stepout];
110 }
111 
112 void iir_filt_forward(float *in, int stepin, float *out, int length, float *coeffs, float *i0)
113 {
114  int j;
115  float b0 = coeffs[0];
116  float a1 = coeffs[1];
117  float a2 = coeffs[2];
118  float a3 = coeffs[3];
119 
120  out[0] = b0*in[0] - a1*i0[0] - a2*i0[1] - a3*i0[2];
121  out[1] = b0*in[stepin] - a1*out[0] - a2*i0[0] - a3*i0[1];
122  out[2] = b0*in[2*stepin] - a1*out[1] - a2*out[0] - a3*i0[0];
123  for(j = 3; j < length; j++)
124  out[j] = b0*in[j*stepin] - a1*out[j-1] - a2*out[j-2] - a3*out[j-3];
125 }
126 
127 
128 void iir_filt_backward(float *in, int stepin, float *out, int length, float *coeffs, float *i0)
129 {
130  int j;
131  float b0 = coeffs[0];
132  float a1 = coeffs[1];
133  float a2 = coeffs[2];
134  float a3 = coeffs[3];
135 
136  out[length-1] = b0*in[(length-1)*stepin] - a1*i0[0] - a2*i0[1] - a3*i0[2];
137  out[length-2] = b0*in[(length-2)*stepin] - a1*out[length-1] - a2*i0[0] - a3*i0[1];
138  out[length-3] = b0*in[(length-3)*stepin] - a1*out[length-2] - a2*out[length-1] - a3*i0[0];
139  for(j = length-4; j >= 0; j--)
140  out[j] = b0*in[j*stepin] - a1*out[j+1] - a2*out[j+2] - a3*out[j+3];
141 }
Functions for generic, 3 tap, iir filtering.
void iir_filt_forward(float *in, int stepin, float *out, int stepout, int length, float *coeffs, float *i0)
Functions iir_filt_forward and iir_filt_backward do a 3 tap recursive filtering operation on floating...
Definition: IIRFilt.cpp:19