-
Notifications
You must be signed in to change notification settings - Fork 110
/
p_iir.c
158 lines (119 loc) · 4.96 KB
/
p_iir.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <pal.h>
/**
* Computes a cascaded IIR filter of 'nb' biquad sections using 32-bit
* coefficients and 32-bit delay buffers. Each biquad section is implemented
* using Direct-form II.
*
* @param x Pointer an input vector with 'nx' elements
*
* @param h Pointer to vector containing '5*nb' filter coefficient
*
* @param r Pointer an output vector of floats of 'nr' elements
*
* @param nb Number of biquads in filter
*
* @param nr Size of input and output vectors
*
* @return None
*
*/
#define _Ntaps 3
#define _Nstage 2
#define _Ndata 64
#define IIR 0
#define FIR 1
float coeffs[_Nstage][2][_Ntaps+1] =
{{/*IIR-0*/ {1, 0.5, 0, 0}, /*FIR-0*/ {0.5, 0.5, 0, 0}},
{/*IIR-1*/ {1, 0, 0, 0}, /*FIR-1*/ {0, 0, 0, 0}}};
void p_iir_f32(const float *x, const float *h, float *r, int nb, int nr)
{
register int rdp; // pointer to the I/O data's current position.
register int cp; // pointer to the coefficients array.
//TODO: Need to make this dynamic
float ca[_Nstage][_Ntaps]; // bwd coefficients
float cb[_Nstage][_Ntaps]; // fwd coefficients
float dl[_Nstage][_Ntaps]; // delay line
float iir[4]; // temp accumulator
for (cp=0; cp<_Ntaps; cp++){
dl[0][cp] = 0.0;
ca[0][cp] = coeffs[0][IIR][cp];
cb[0][cp] = coeffs[0][FIR][cp];
dl[1][cp] = 0.0;
ca[1][cp] = coeffs[1][IIR][cp];
cb[1][cp] = coeffs[1][FIR][cp];
}
for (rdp=0; rdp<_Ndata; rdp+=8){
iir[0] = ca[0][0] * (x[rdp+0] + ca[0][1] * dl[0][0] + ca[0][2] * dl[0][1]);
iir[2] = cb[0][0] * iir[0] + cb[0][1] * dl[0][0] + cb[0][2] * dl[0][1];
dl[0][1] = dl[0][0];
dl[0][0] = iir[0];
iir[1] = ca[1][0] * (x[rdp+0] + ca[1][1] * dl[1][0] + ca[1][2] * dl[1][1]);
iir[3] = cb[1][0] * iir[1] + cb[1][1] * dl[1][0] + cb[1][2] * dl[1][1];
dl[1][1] = dl[1][0];
dl[1][0] = iir[1];
r[rdp+0] = iir[2] + iir[3];
iir[0] = ca[0][0] * (x[rdp+1] + ca[0][1] * dl[0][0] + ca[0][2] * dl[0][1]);
iir[2] = cb[0][0] * iir[0] + cb[0][1] * dl[0][0] + cb[0][2] * dl[0][1];
dl[0][1] = dl[0][0];
dl[0][0] = iir[0];
iir[1] = ca[1][0] * (x[rdp+1] + ca[1][1] * dl[1][0] + ca[1][2] * dl[1][1]);
iir[3] = cb[1][0] * iir[1] + cb[1][1] * dl[1][0] + cb[1][2] * dl[1][1];
dl[1][1] = dl[1][0];
dl[1][0] = iir[1];
r[rdp+1] = iir[2] + iir[3];
iir[0] = ca[0][0] * (x[rdp+2] + ca[0][1] * dl[0][0] + ca[0][2] * dl[0][1]);
iir[2] = cb[0][0] * iir[0] + cb[0][1] * dl[0][0] + cb[0][2] * dl[0][1];
dl[0][1] = dl[0][0];
dl[0][0] = iir[0];
iir[1] = ca[1][0] * (x[rdp+2] + ca[1][1] * dl[1][0] + ca[1][2] * dl[1][1]);
iir[3] = cb[1][0] * iir[1] + cb[1][1] * dl[1][0] + cb[1][2] * dl[1][1];
dl[1][1] = dl[1][0];
dl[1][0] = iir[1];
r[rdp+2] = iir[2] + iir[3];
iir[0] = ca[0][0] * (x[rdp+3] + ca[0][1] * dl[0][0] + ca[0][2] * dl[0][1]);
iir[2] = cb[0][0] * iir[0] + cb[0][1] * dl[0][0] + cb[0][2] * dl[0][1];
dl[0][1] = dl[0][0];
dl[0][0] = iir[0];
iir[1] = ca[1][0] * (x[rdp+3] + ca[1][1] * dl[1][0] + ca[1][2] * dl[1][1]);
iir[3] = cb[1][0] * iir[1] + cb[1][1] * dl[1][0] + cb[1][2] * dl[1][1];
dl[1][1] = dl[1][0];
dl[1][0] = iir[1];
r[rdp+3] = iir[2] + iir[3];
iir[0] = ca[0][0] * (x[rdp+4] + ca[0][1] * dl[0][0] + ca[0][2] * dl[0][1]);
iir[2] = cb[0][0] * iir[0] + cb[0][1] * dl[0][0] + cb[0][2] * dl[0][1];
dl[0][1] = dl[0][0];
dl[0][0] = iir[0];
iir[1] = ca[1][0] * (x[rdp+4] + ca[1][1] * dl[1][0] + ca[1][2] * dl[1][1]);
iir[3] = cb[1][0] * iir[1] + cb[1][1] * dl[1][0] + cb[1][2] * dl[1][1];
dl[1][1] = dl[1][0];
dl[1][0] = iir[1];
r[rdp+4] = iir[2] + iir[3];
iir[0] = ca[0][0] * (x[rdp+5] + ca[0][1] * dl[0][0] + ca[0][2] * dl[0][1]);
iir[2] = cb[0][0] * iir[0] + cb[0][1] * dl[0][0] + cb[0][2] * dl[0][1];
dl[0][1] = dl[0][0];
dl[0][0] = iir[0];
iir[1] = ca[1][0] * (x[rdp+5] + ca[1][1] * dl[1][0] + ca[1][2] * dl[1][1]);
iir[3] = cb[1][0] * iir[1] + cb[1][1] * dl[1][0] + cb[1][2] * dl[1][1];
dl[1][1] = dl[1][0];
dl[1][0] = iir[1];
r[rdp+5] = iir[2] + iir[3];
iir[0] = ca[0][0] * (x[rdp+6] + ca[0][1] * dl[0][0] + ca[0][2] * dl[0][1]);
iir[2] = cb[0][0] * iir[0] + cb[0][1] * dl[0][0] + cb[0][2] * dl[0][1];
dl[0][1] = dl[0][0];
dl[0][0] = iir[0];
iir[1] = ca[1][0] * (x[rdp+6] + ca[1][1] * dl[1][0] + ca[1][2] * dl[1][1]);
iir[3] = cb[1][0] * iir[1] + cb[1][1] * dl[1][0] + cb[1][2] * dl[1][1];
dl[1][1] = dl[1][0];
dl[1][0] = iir[1];
r[rdp+6] = iir[2] + iir[3];
iir[0] = ca[0][0] * (x[rdp+7] + ca[0][1] * dl[0][0] + ca[0][2] * dl[0][1]);
iir[2] = cb[0][0] * iir[0] + cb[0][1] * dl[0][0] + cb[0][2] * dl[0][1];
dl[0][1] = dl[0][0];
dl[0][0] = iir[0];
iir[1] = ca[1][0] * (x[rdp+7] + ca[1][1] * dl[1][0] + ca[1][2] * dl[1][1]);
iir[3] = cb[1][0] * iir[1] + cb[1][1] * dl[1][0] + cb[1][2] * dl[1][1];
dl[1][1] = dl[1][0];
dl[1][0] = iir[1];
r[rdp+7] = iir[2] + iir[3];
}
}