forked from fsphil/hacktv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fir.c
325 lines (251 loc) · 6.92 KB
/
fir.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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* hacktv - Analogue video transmitter for the HackRF */
/*=======================================================================*/
/* Copyright 2017 Philip Heron <[email protected]> */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include "fir.h"
/* Some of the filter design functions contained within here where taken
from or are based on those within gnuradio's gr-filter/lib/firdes.cc */
static double i_zero(double x)
{
double sum, u, halfx, temp;
int n;
sum = u = n = 1;
halfx = x / 2.0;
do
{
temp = halfx / (double) n;
n += 1;
temp *= temp;
u *= temp;
sum += u;
}
while(u >= 1e-21 * sum);
return(sum);
}
static void kaiser(double *taps, size_t ntaps, double beta)
{
double i_beta = 1.0 / i_zero(beta);
double inm1 = 1.0 / ((double) (ntaps - 1));
double temp;
int i;
taps[0] = i_beta;
for(i = 1; i < ntaps - 1; i++)
{
temp = 2 * i * inm1 - 1;
taps[i] = i_zero(beta * sqrt(1.0 - temp * temp)) * i_beta;
}
taps[ntaps - 1] = i_beta;
}
void fir_low_pass(double *taps, size_t ntaps, double sample_rate, double cutoff, double width, double gain)
{
int n;
int M = (ntaps - 1) / 2;
double fmax;
double fwT0 = 2 * M_PI * cutoff / sample_rate;
/* Create the window */
kaiser(taps, ntaps, 7.0);
/* Generate the filter taps */
for(n = -M; n <= M; n++)
{
if(n == 0)
{
taps[n + M] *= fwT0 / M_PI;
}
else
{
taps[n + M] *= sin(n * fwT0) / (n * M_PI);
}
}
/* find the factor to normalize the gain, fmax.
* For low-pass, gain @ zero freq = 1.0 */
fmax = taps[0 + M];
for(n = 1; n <= M; n++)
{
fmax += 2 * taps[n + M];
}
/* Normalise */
gain /= fmax;
for(n = 0; n < ntaps; n++)
{
taps[n] *= gain;
}
}
void fir_complex_band_pass(double *taps, size_t ntaps, double sample_rate, double low_cutoff, double high_cutoff, double width, double gain)
{
double *lptaps;
double freq = M_PI * (high_cutoff + low_cutoff) / sample_rate;
double phase;
int i;
lptaps = &taps[ntaps];
fir_low_pass(lptaps, ntaps, sample_rate, (high_cutoff - low_cutoff) / 2, width, gain);
if(ntaps & 1)
{
phase = -freq * (ntaps >> 1);
}
else
{
phase = -freq / 2.0 * ((1 + 2 * ntaps) >> 1);
}
for(i = 0; i < ntaps; i++, phase += freq)
{
taps[i * 2 + 0] = lptaps[i] * cos(phase);
taps[i * 2 + 1] = lptaps[i] * sin(phase);
}
}
/* int16_t */
void fir_int16_low_pass(int16_t *taps, size_t ntaps, double sample_rate, double cutoff, double width, double gain)
{
double *dtaps;
int i;
int a;
dtaps = calloc(ntaps, sizeof(double));
fir_low_pass(dtaps, ntaps, sample_rate, cutoff, width, gain);
for(a = i = 0; i < ntaps; i++)
{
taps[i] = round(dtaps[i] * 32767.0);
a += taps[i];
}
free(dtaps);
}
int fir_int16_init(fir_int16_t *s, const int16_t *taps, unsigned int ntaps, unsigned int interpolation, unsigned int decimation)
{
s->ntaps = ntaps;
s->taps = taps;
s->interpolation = interpolation;
s->decimation = decimation;
s->ds = 0;
s->lwin = (ntaps + s->interpolation - 1) / s->interpolation;
s->owin = 0;
s->win = calloc(s->lwin, sizeof(int16_t));
return(0);
}
size_t fir_int16_process(fir_int16_t *s, int16_t *output, size_t ostep, const int16_t *input, size_t samples, size_t istep)
{
int32_t a;
int i;
int x;
int y;
int p;
int osamples;
osamples = 0;
for(x = 0; x < samples; x++)
{
/* Append the next input sample to the round buffer */
s->win[s->owin++] = *input;
input += istep;
if(s->owin == s->lwin) s->owin = 0;
for(i = 0; i < s->interpolation; i++)
{
/* Calculate the next output sample */
if(s->ds == 0)
{
a = 0;
p = s->owin - 1;
if(p == -1) p = s->lwin - 1;
for(y = i; y < s->ntaps; y += s->interpolation)
{
a += s->win[p--] * s->taps[y];
if(p == -1) p = s->lwin - 1;
}
*output = a >> 16;
output += ostep;
osamples++;
s->ds = s->decimation;
}
s->ds--;
}
}
return(osamples);
}
void fir_int16_free(fir_int16_t *s)
{
if(s->win) free(s->win);
}
/* complex int16_t */
void fir_int16_complex_band_pass(int16_t *taps, size_t ntaps, double sample_rate, double low_cutoff, double high_cutoff, double width, double gain)
{
double *dtaps;
int i;
int a;
dtaps = calloc(ntaps, sizeof(double) * 2);
fir_complex_band_pass(dtaps, ntaps, sample_rate, low_cutoff, high_cutoff, width, gain);
for(a = i = 0; i < ntaps * 2; i++)
{
taps[i] = round(dtaps[i] * 32767.0);
a += taps[i];
}
free(dtaps);
}
int fir_int16_complex_init(fir_int16_t *s, const int16_t *taps, unsigned int ntaps, unsigned int interpolation, unsigned int decimation)
{
s->ntaps = ntaps;
s->taps = taps;
s->interpolation = interpolation;
s->decimation = decimation;
s->ds = 0;
s->lwin = (ntaps + s->interpolation - 1) / s->interpolation;
s->owin = 0;
s->win = calloc(s->lwin, sizeof(int16_t) * 2);
return(0);
}
size_t fir_int16_complex_process(fir_int16_t *s, int16_t *output, size_t ostep, const int16_t *input, size_t samples, size_t istep)
{
int32_t ai, aq;
int i;
int x;
int y;
int p;
int osamples;
osamples = 0;
for(x = 0; x < samples; x++)
{
/* Append the next input sample to the round buffer */
s->win[s->owin * 2 + 0] = input[0];
s->win[s->owin * 2 + 1] = input[1];
if(++s->owin == s->lwin) s->owin = 0;
input += istep * 2;
for(i = 0; i < s->interpolation; i++)
{
/* Calculate the next output sample */
if(s->ds == 0)
{
ai = 0;
aq = 0;
p = s->owin - 1;
if(p == -1) p = s->lwin - 1;
for(y = i; y < s->ntaps; y += s->interpolation)
{
ai += s->win[p * 2 + 0] * s->taps[y * 2 + 0] - s->win[p * 2 + 1] * s->taps[y * 2 + 1];
aq += s->win[p * 2 + 1] * s->taps[y * 2 + 0] + s->win[p * 2 + 0] * s->taps[y * 2 + 1];
if(--p == -1) p = s->lwin - 1;
}
output[0] = ai >> 16;
output[1] = aq >> 16;
output += ostep * 2;
osamples++;
s->ds = s->decimation;
}
s->ds--;
}
}
return(osamples);
}
void fir_int16_complex_free(fir_int16_t *s)
{
free(s->win);
}