aboutsummaryrefslogtreecommitdiff
path: root/modules/filters.h
blob: 8485bec25f2a59be49178fd40702408fde7e3a4e (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* this file contains a 37th attempt to write a general purpose iir filter toolset */

/* defined as inline functions with call by reference to enable compiler ref/deref optim */

/* the typedef */
#ifndef T
#define T t_float
#endif


/* the prototype for a word */
#define P static inline void
#define PP static void


/* the 'reference' arguments */
#define A *a
#define B *b
#define C *c
#define D *d
#define X *x
#define Y *y
#define S *s


/* the opcodes */

/* add */
P cadd  (T X, T Y, T A, T B, T C, T D) { X = A + C; Y = B + D;}
P cadd2 (T A, T B, T C, T D)           { A += C; B += D;}
P vcadd  (T X, T A, T C)                { cadd(x,x+1,a,a+1,c,c+1); }
P vcadd2 (T A, T C)                     { cadd2(a,a+1,c,c+1); }


/* mul */
P cmul_r (T X, T A, T B, T C, T D)    { X = A * C - B * D;}
P cmul_i (T Y, T A, T B, T C, T D)    { Y = A * D + B * C;}
P cmul (T X, T Y, T A, T B, T C, T D) 
{
    cmul_r (x, a, b, c, d);
    cmul_i (y, a, b, c, d);
}
P cmul2 (T A, T B, T C, T D)
{
    T x = A;
    T y = B;
    cmul (&x, &y, a, b, c, d);
    A = x;
    B = y;
}

P vcmul  (T X, T A, T C)  { cmul(x,x+1,a,a+1,c,c+1); }
P vcmul2 (T A, T C)       { cmul2(a,a+1,c,c+1); }


/* norm */
static inline t_float vcnorm(T X) { return hypot(x[0], x[1]); }



/* swap */
P vcswap(T Y, T X)
{
    t_float t[2] = {x[0], x[1]};
    x[0] = y[0];
    x[1] = y[1];
    y[0] = t[0];
    y[1] = t[1];
}


/* inverse */
P vcinv(T Y, T X)
{
    t_float scale = 1.0 / vcnorm(x);
    y[0] = scale * x[0];
    y[1] = scale * x[1];
}

P vcinv1(T X)
{
    t_float scale = 1.0 / vcnorm(x);
    x[0] *= scale;
    x[1] *= scale;
}

/* exp */

/* X = exp(Y) */
P vcexp2 (T Y, T X)
{
    T r = exp(x[0]);
    y[0] = cos (x[1]);
    y[1] = sin (x[1]);
    y[0] *= r;
    y[1] *= r;
}

P vcexp1 (T X)
{
    T y[2];
    vcexp2(y,x);
    x[0] = y[0];
    x[1] = y[1];
}

/* 
   FILTERS

   the transfer function is defined in terms of the "engineering" 
   bilateral z-transform of the discrete impulse response h[n].

   H(z) = Sum{n = -inf -> inf} h[n] z^(-n)

   a unit delay operating on a singnal S(z) is therefore 
   represented as z^(-1) S(z)

*/







/* biquads */

/* biquad, orthogonal (poles & zeros), real in, out, state, pole, output */
P biq_orth_r (T X, T Y, T S, T A, T C)
{
    Y = X + c[0] * s[0] - c[1] * s[1]; /* mind sign of c[1] */
    vcmul2(s, a);
    S += X;
}


/* biquad, orthogonal, complex one-pole, with scaling */

/* complex one pole: (output = s[0] + is[1]): C / (1-A z^(-1))  */

P one_pole_complex (T X, T Y, T S, T A, T C)
{
    vcmul(y, s, a);
    vcadd2(y, x);
    s[0] = y[0];
    s[1] = y[1];
    vcmul(y, s, c);
}

/* complex conj two pole:  (output = s[0]  : (Re(C) - Re(C*Conj(A))) / (1 - A z^(-1)) (1 - Conj(A) z^(-1)) */

P two_pole_complex_conj (T X, T Y, T S, T A, T C)
{
    vcmul2(s, a);
    s[0] += x[0];
    y[0] = s[0] * c[0] - s[1] * c[1];
}



/* support functions for IIR filter design */

/* evaluate pole and allzero TF in z^-1 given the complex zeros/poles:
   p(z) (or p(z)^-1) = \product (1-z_i z^-1) */
PP eval_zero_poly(t_float *val, t_float *arg, t_float *zeros, int nb_zeros)
{
    int i;
    t_float a[2] = {arg[0], arg[1]};
    vcinv1(a);
    val[0] = 1.0;
    val[1] = 0.0;
    a[0] *= -1;
    a[1] *= -1;
    for (i=0; i<nb_zeros; i++){
	t_float t[2];
	vcmul(t, a, zeros + 2*i);
	t[0] += 1.0;
	vcmul2(val, t);
    }
}

PP eval_pole_poly(t_float *val, t_float *arg, t_float *poles, int nb_poles)
{
    eval_zero_poly(val, arg, poles, nb_poles);
    vcinv1(val);
}


/* since it's more efficient to store half of the poles for a real impulse
   response, these functions compute p(z) conj(p(conj(z)))  */

PP eval_conj_zero_poly(t_float *val, t_float *arg, t_float *zeros, int nb_zeros)
{
    t_float t[2];
    t_float a[2] = {arg[0], arg[1]};
    eval_zero_poly(t, a, zeros, nb_zeros);
    a[1] *= -1;
    eval_zero_poly(val, a, zeros, nb_zeros);
    val[1] *= -1;
    vcmul2(val, t);
}

PP eval_conj_pole_poly(t_float *val, t_float *arg, t_float *poles, int nb_poles)
{
    eval_conj_zero_poly(val, arg, poles, nb_poles);
    vcinv1(val);
}

PP eval_conj_pole_zero_ratfunc(t_float *val, t_float *arg, t_float *poles, t_float *zeros, int nb_poles, int nb_zeros)
{
    t_float t[2];
    eval_conj_zero_poly(t, arg, zeros, nb_zeros);
    eval_conj_pole_poly(val, arg, poles, nb_zeros);
    vcmul2(val, t);
}



/* bandlimited IIR impulse:

   * design analog butterworth filter
   * obtain the partial fraction expansion of the transfer function
   * determine the state increment as a function of fractional delay of impulse location 
   (sample the impulse response)

*/