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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/* hacktv - Analogue video transmitter for the HackRF */
/*=======================================================================*/
/* Copyright 2017 Philip Heron <phil@sanslogic.co.uk> */
/* */
/* 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 <string.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)
{
int i;
s->type = 1;
s->ntaps = ntaps;
s->itaps = malloc(s->ntaps * sizeof(int16_t));
s->qtaps = NULL;
for(i = 0; i < ntaps; i++)
{
s->itaps[i] = taps[i];
}
s->win = calloc(s->ntaps * 2, sizeof(int16_t));
s->owin = 0;
return(0);
}
size_t fir_int16_process(fir_int16_t *s, int16_t *out, const int16_t *in, size_t samples)
{
int a;
int x;
int y;
int p;
if(s->type == 0) return(0);
else if(s->type == 2) return(fir_int16_complex_process(s, out, in, samples));
else if(s->type == 3) return(fir_int16_scomplex_process(s, out, in, samples));
for(x = 0; x < samples; x++)
{
/* Append the next input sample to the round buffer */
s->win[s->owin] = s->win[s->owin + s->ntaps] = *in;
if(++s->owin == s->ntaps) s->owin = 0;
/* Calculate the next output sample */
a = 0;
p = s->owin;
for(y = 0; y < s->ntaps; y++, p++)
{
a += s->win[p] * s->itaps[y];
}
*out = a >> 15;
out += 2;
in += 2;
}
return(samples);
}
void fir_int16_free(fir_int16_t *s)
{
free(s->win);
free(s->itaps);
free(s->qtaps);
memset(s, 0, sizeof(fir_int16_t));
}
/* 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)
{
int i;
s->type = 2;
s->ntaps = ntaps;
s->itaps = malloc(s->ntaps * sizeof(int16_t) * 2);
s->qtaps = malloc(s->ntaps * sizeof(int16_t) * 2);
/* Copy the taps in the order and format they are to be used */
for(i = 0; i < ntaps; i++)
{
s->itaps[i * 2 + 0] = taps[i * 2 + 0];
s->itaps[i * 2 + 1] = -taps[i * 2 + 1];
s->qtaps[i * 2 + 0] = taps[i * 2 + 1];
s->qtaps[i * 2 + 1] = taps[i * 2 + 0];
}
s->win = calloc(s->ntaps * 2, sizeof(int16_t) * 2);
s->owin = 0;
return(0);
}
size_t fir_int16_complex_process(fir_int16_t *s, int16_t *out, const int16_t *in, size_t samples)
{
int32_t ai, aq;
int x;
int y;
int p;
for(x = 0; x < samples; x++)
{
/* Append the next input sample to the sliding window */
s->win[s->owin * 2 + 0] = s->win[(s->owin + s->ntaps) * 2 + 0] = in[0];
s->win[s->owin * 2 + 1] = s->win[(s->owin + s->ntaps) * 2 + 1] = in[1];
if(++s->owin == s->ntaps) s->owin = 0;
/* Calculate the next output sample */
ai = aq = 0;
for(p = s->owin * 2, y = 0; y < s->ntaps * 2; y++, p++)
{
ai += s->win[p] * s->itaps[y];
aq += s->win[p] * s->qtaps[y];
}
out[0] = aq >> 15;
out[1] = ai >> 15;
out += 2;
in += 2;
}
return(samples);
}
int fir_int16_scomplex_init(fir_int16_t *s, const int16_t *taps, unsigned int ntaps)
{
int i;
s->type = 3;
s->ntaps = ntaps;
s->itaps = calloc(s->ntaps, sizeof(int16_t));
s->qtaps = calloc(s->ntaps, sizeof(int16_t));
/* Copy the taps in the order and format they are to be used */
for(i = 0; i < ntaps; i++)
{
s->itaps[i] = taps[i * 2 + 0];
s->qtaps[i] = taps[i * 2 + 1];
}
s->win = calloc(s->ntaps * 2, sizeof(int16_t));
s->owin = 0;
return(0);
}
size_t fir_int16_scomplex_process(fir_int16_t *s, int16_t *out, const int16_t *in, size_t samples)
{
int32_t ai, aq;
int x;
int y;
int p;
for(x = 0; x < samples; x++)
{
/* Append the next input sample to the sliding window */
s->win[s->owin] = s->win[(s->owin + s->ntaps)] = in[0];
if(++s->owin == s->ntaps) s->owin = 0;
/* Calculate the next output sample */
ai = aq = 0;
for(p = s->owin, y = 0; y < s->ntaps; y++, p++)
{
ai += s->win[p] * s->itaps[y];
aq += s->win[p] * s->qtaps[y];
}
out[0] = aq >> 15;
out[1] = ai >> 15;
out += 2;
in += 2;
}
return(samples);
}
|