| 1 |
/* |
| 2 |
* Copyright (c) 2003-2004, Mark Borgerding. All rights reserved. |
| 3 |
* This file is part of KISS FFT - https://github.com/mborgerding/kissfft |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: BSD-3-Clause |
| 6 |
* See COPYING file for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "_kiss_fft_guts.h" |
| 10 |
|
| 11 |
|
| 12 |
/* |
| 13 |
Some definitions that allow real or complex filtering |
| 14 |
*/ |
| 15 |
#ifdef REAL_FASTFIR |
| 16 |
#define MIN_FFT_LEN 2048 |
| 17 |
#include "kiss_fftr.h" |
| 18 |
typedef kiss_fft_scalar kffsamp_t; |
| 19 |
typedef kiss_fftr_cfg kfcfg_t; |
| 20 |
#define FFT_ALLOC kiss_fftr_alloc |
| 21 |
#define FFTFWD kiss_fftr |
| 22 |
#define FFTINV kiss_fftri |
| 23 |
#else |
| 24 |
#define MIN_FFT_LEN 1024 |
| 25 |
typedef kiss_fft_cpx kffsamp_t; |
| 26 |
typedef kiss_fft_cfg kfcfg_t; |
| 27 |
#define FFT_ALLOC kiss_fft_alloc |
| 28 |
#define FFTFWD kiss_fft |
| 29 |
#define FFTINV kiss_fft |
| 30 |
#endif |
| 31 |
|
| 32 |
typedef struct kiss_fastfir_state *kiss_fastfir_cfg; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
kiss_fastfir_cfg kiss_fastfir_alloc(const kffsamp_t * imp_resp,size_t n_imp_resp, |
| 37 |
size_t * nfft,void * mem,size_t*lenmem); |
| 38 |
|
| 39 |
/* see do_file_filter for usage */ |
| 40 |
size_t kiss_fastfir( kiss_fastfir_cfg cfg, kffsamp_t * inbuf, kffsamp_t * outbuf, size_t n, size_t *offset); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static int verbose=0; |
| 45 |
|
| 46 |
|
| 47 |
struct kiss_fastfir_state{ |
| 48 |
size_t nfft; |
| 49 |
size_t ngood; |
| 50 |
kfcfg_t fftcfg; |
| 51 |
kfcfg_t ifftcfg; |
| 52 |
kiss_fft_cpx * fir_freq_resp; |
| 53 |
kiss_fft_cpx * freqbuf; |
| 54 |
size_t n_freq_bins; |
| 55 |
kffsamp_t * tmpbuf; |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
kiss_fastfir_cfg kiss_fastfir_alloc( |
| 60 |
const kffsamp_t * imp_resp,size_t n_imp_resp, |
| 61 |
size_t *pnfft, /* if <= 0, an appropriate size will be chosen */ |
| 62 |
void * mem,size_t*lenmem) |
| 63 |
{ |
| 64 |
kiss_fastfir_cfg st = NULL; |
| 65 |
size_t len_fftcfg,len_ifftcfg; |
| 66 |
size_t memneeded = sizeof(struct kiss_fastfir_state); |
| 67 |
char * ptr; |
| 68 |
size_t i; |
| 69 |
size_t nfft=0; |
| 70 |
float scale; |
| 71 |
size_t n_freq_bins; |
| 72 |
if (pnfft) |
| 73 |
nfft=*pnfft; |
| 74 |
|
| 75 |
if (nfft<=0) { |
| 76 |
/* determine fft size as next power of two at least 2x |
| 77 |
the impulse response length*/ |
| 78 |
i=n_imp_resp-1; |
| 79 |
nfft=2; |
| 80 |
do{ |
| 81 |
nfft<<=1; |
| 82 |
}while (i>>=1); |
| 83 |
#ifdef MIN_FFT_LEN |
| 84 |
if ( nfft < MIN_FFT_LEN ) |
| 85 |
nfft=MIN_FFT_LEN; |
| 86 |
#endif |
| 87 |
} |
| 88 |
if (pnfft) |
| 89 |
*pnfft = nfft; |
| 90 |
|
| 91 |
#ifdef REAL_FASTFIR |
| 92 |
n_freq_bins = nfft/2 + 1; |
| 93 |
#else |
| 94 |
n_freq_bins = nfft; |
| 95 |
#endif |
| 96 |
/*fftcfg*/ |
| 97 |
FFT_ALLOC (nfft, 0, NULL, &len_fftcfg); |
| 98 |
memneeded += len_fftcfg; |
| 99 |
/*ifftcfg*/ |
| 100 |
FFT_ALLOC (nfft, 1, NULL, &len_ifftcfg); |
| 101 |
memneeded += len_ifftcfg; |
| 102 |
/* tmpbuf */ |
| 103 |
memneeded += sizeof(kffsamp_t) * nfft; |
| 104 |
/* fir_freq_resp */ |
| 105 |
memneeded += sizeof(kiss_fft_cpx) * n_freq_bins; |
| 106 |
/* freqbuf */ |
| 107 |
memneeded += sizeof(kiss_fft_cpx) * n_freq_bins; |
| 108 |
|
| 109 |
if (lenmem == NULL) { |
| 110 |
st = (kiss_fastfir_cfg) malloc (memneeded); |
| 111 |
} else { |
| 112 |
if (*lenmem >= memneeded) |
| 113 |
st = (kiss_fastfir_cfg) mem; |
| 114 |
*lenmem = memneeded; |
| 115 |
} |
| 116 |
if (!st) |
| 117 |
return NULL; |
| 118 |
|
| 119 |
st->nfft = nfft; |
| 120 |
st->ngood = nfft - n_imp_resp + 1; |
| 121 |
st->n_freq_bins = n_freq_bins; |
| 122 |
ptr=(char*)(st+1); |
| 123 |
|
| 124 |
st->fftcfg = (kfcfg_t)ptr; |
| 125 |
ptr += len_fftcfg; |
| 126 |
|
| 127 |
st->ifftcfg = (kfcfg_t)ptr; |
| 128 |
ptr += len_ifftcfg; |
| 129 |
|
| 130 |
st->tmpbuf = (kffsamp_t*)ptr; |
| 131 |
ptr += sizeof(kffsamp_t) * nfft; |
| 132 |
|
| 133 |
st->freqbuf = (kiss_fft_cpx*)ptr; |
| 134 |
ptr += sizeof(kiss_fft_cpx) * n_freq_bins; |
| 135 |
|
| 136 |
st->fir_freq_resp = (kiss_fft_cpx*)ptr; |
| 137 |
ptr += sizeof(kiss_fft_cpx) * n_freq_bins; |
| 138 |
|
| 139 |
FFT_ALLOC (nfft,0,st->fftcfg , &len_fftcfg); |
| 140 |
FFT_ALLOC (nfft,1,st->ifftcfg , &len_ifftcfg); |
| 141 |
|
| 142 |
memset(st->tmpbuf,0,sizeof(kffsamp_t)*nfft); |
| 143 |
/*zero pad in the middle to left-rotate the impulse response |
| 144 |
This puts the scrap samples at the end of the inverse fft'd buffer */ |
| 145 |
st->tmpbuf[0] = imp_resp[ n_imp_resp - 1 ]; |
| 146 |
for (i=0;i<n_imp_resp - 1; ++i) { |
| 147 |
st->tmpbuf[ nfft - n_imp_resp + 1 + i ] = imp_resp[ i ]; |
| 148 |
} |
| 149 |
|
| 150 |
FFTFWD(st->fftcfg,st->tmpbuf,st->fir_freq_resp); |
| 151 |
|
| 152 |
/* TODO: this won't work for fixed point */ |
| 153 |
scale = 1.0f / st->nfft; |
| 154 |
|
| 155 |
for ( i=0; i < st->n_freq_bins; ++i ) { |
| 156 |
#ifdef USE_SIMD |
| 157 |
st->fir_freq_resp[i].r *= _mm_set1_ps(scale); |
| 158 |
st->fir_freq_resp[i].i *= _mm_set1_ps(scale); |
| 159 |
#else |
| 160 |
st->fir_freq_resp[i].r *= scale; |
| 161 |
st->fir_freq_resp[i].i *= scale; |
| 162 |
#endif |
| 163 |
} |
| 164 |
return st; |
| 165 |
} |
| 166 |
|
| 167 |
static void fastconv1buf(const kiss_fastfir_cfg st,const kffsamp_t * in,kffsamp_t * out) |
| 168 |
{ |
| 169 |
size_t i; |
| 170 |
/* multiply the frequency response of the input signal by |
| 171 |
that of the fir filter*/ |
| 172 |
FFTFWD( st->fftcfg, in , st->freqbuf ); |
| 173 |
for ( i=0; i<st->n_freq_bins; ++i ) { |
| 174 |
kiss_fft_cpx tmpsamp; |
| 175 |
C_MUL(tmpsamp,st->freqbuf[i],st->fir_freq_resp[i]); |
| 176 |
st->freqbuf[i] = tmpsamp; |
| 177 |
} |
| 178 |
|
| 179 |
/* perform the inverse fft*/ |
| 180 |
FFTINV(st->ifftcfg,st->freqbuf,out); |
| 181 |
} |
| 182 |
|
| 183 |
/* n : the size of inbuf and outbuf in samples |
| 184 |
return value: the number of samples completely processed |
| 185 |
n-retval samples should be copied to the front of the next input buffer */ |
| 186 |
static size_t kff_nocopy( |
| 187 |
kiss_fastfir_cfg st, |
| 188 |
const kffsamp_t * inbuf, |
| 189 |
kffsamp_t * outbuf, |
| 190 |
size_t n) |
| 191 |
{ |
| 192 |
size_t norig=n; |
| 193 |
while (n >= st->nfft ) { |
| 194 |
fastconv1buf(st,inbuf,outbuf); |
| 195 |
inbuf += st->ngood; |
| 196 |
outbuf += st->ngood; |
| 197 |
n -= st->ngood; |
| 198 |
} |
| 199 |
return norig - n; |
| 200 |
} |
| 201 |
|
| 202 |
static |
| 203 |
size_t kff_flush(kiss_fastfir_cfg st,const kffsamp_t * inbuf,kffsamp_t * outbuf,size_t n) |
| 204 |
{ |
| 205 |
size_t zpad=0,ntmp; |
| 206 |
|
| 207 |
ntmp = kff_nocopy(st,inbuf,outbuf,n); |
| 208 |
n -= ntmp; |
| 209 |
inbuf += ntmp; |
| 210 |
outbuf += ntmp; |
| 211 |
|
| 212 |
zpad = st->nfft - n; |
| 213 |
memset(st->tmpbuf,0,sizeof(kffsamp_t)*st->nfft ); |
| 214 |
memcpy(st->tmpbuf,inbuf,sizeof(kffsamp_t)*n ); |
| 215 |
|
| 216 |
fastconv1buf(st,st->tmpbuf,st->tmpbuf); |
| 217 |
|
| 218 |
memcpy(outbuf,st->tmpbuf,sizeof(kffsamp_t)*( st->ngood - zpad )); |
| 219 |
return ntmp + st->ngood - zpad; |
| 220 |
} |
| 221 |
|
| 222 |
size_t kiss_fastfir( |
| 223 |
kiss_fastfir_cfg vst, |
| 224 |
kffsamp_t * inbuf, |
| 225 |
kffsamp_t * outbuf, |
| 226 |
size_t n_new, |
| 227 |
size_t *offset) |
| 228 |
{ |
| 229 |
size_t ntot = n_new + *offset; |
| 230 |
if (n_new==0) { |
| 231 |
return kff_flush(vst,inbuf,outbuf,ntot); |
| 232 |
}else{ |
| 233 |
size_t nwritten = kff_nocopy(vst,inbuf,outbuf,ntot); |
| 234 |
*offset = ntot - nwritten; |
| 235 |
/*save the unused or underused samples at the front of the input buffer */ |
| 236 |
memcpy( inbuf , inbuf+nwritten , *offset * sizeof(kffsamp_t) ); |
| 237 |
return nwritten; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#ifdef FAST_FILT_UTIL |
| 242 |
#include <unistd.h> |
| 243 |
#include <sys/types.h> |
| 244 |
#include <sys/mman.h> |
| 245 |
#include <assert.h> |
| 246 |
|
| 247 |
static |
| 248 |
void direct_file_filter( |
| 249 |
FILE * fin, |
| 250 |
FILE * fout, |
| 251 |
const kffsamp_t * imp_resp, |
| 252 |
size_t n_imp_resp) |
| 253 |
{ |
| 254 |
size_t nlag = n_imp_resp - 1; |
| 255 |
|
| 256 |
const kffsamp_t *tmph; |
| 257 |
kffsamp_t *buf, *circbuf; |
| 258 |
kffsamp_t outval; |
| 259 |
size_t nread; |
| 260 |
size_t nbuf; |
| 261 |
size_t oldestlag = 0; |
| 262 |
size_t k, tap; |
| 263 |
#ifndef REAL_FASTFIR |
| 264 |
kffsamp_t tmp; |
| 265 |
#endif |
| 266 |
|
| 267 |
nbuf = 4096; |
| 268 |
buf = (kffsamp_t *) malloc ( sizeof (kffsamp_t) * nbuf); |
| 269 |
circbuf = (kffsamp_t *) malloc (sizeof (kffsamp_t) * nlag); |
| 270 |
if (!circbuf || !buf) { |
| 271 |
perror("circbuf allocation"); |
| 272 |
exit(1); |
| 273 |
} |
| 274 |
|
| 275 |
if ( fread (circbuf, sizeof (kffsamp_t), nlag, fin) != nlag ) { |
| 276 |
perror ("insufficient data to overcome transient"); |
| 277 |
exit (1); |
| 278 |
} |
| 279 |
|
| 280 |
do { |
| 281 |
nread = fread (buf, sizeof (kffsamp_t), nbuf, fin); |
| 282 |
if (nread <= 0) |
| 283 |
break; |
| 284 |
|
| 285 |
for (k = 0; k < nread; ++k) { |
| 286 |
tmph = imp_resp+nlag; |
| 287 |
#ifdef REAL_FASTFIR |
| 288 |
# ifdef USE_SIMD |
| 289 |
outval = _mm_set1_ps(0); |
| 290 |
#else |
| 291 |
outval = 0; |
| 292 |
#endif |
| 293 |
for (tap = oldestlag; tap < nlag; ++tap) |
| 294 |
outval += circbuf[tap] * *tmph--; |
| 295 |
for (tap = 0; tap < oldestlag; ++tap) |
| 296 |
outval += circbuf[tap] * *tmph--; |
| 297 |
outval += buf[k] * *tmph; |
| 298 |
#else |
| 299 |
# ifdef USE_SIMD |
| 300 |
outval.r = outval.i = _mm_set1_ps(0); |
| 301 |
#else |
| 302 |
outval.r = outval.i = 0; |
| 303 |
#endif |
| 304 |
for (tap = oldestlag; tap < nlag; ++tap){ |
| 305 |
C_MUL(tmp,circbuf[tap],*tmph); |
| 306 |
--tmph; |
| 307 |
C_ADDTO(outval,tmp); |
| 308 |
} |
| 309 |
|
| 310 |
for (tap = 0; tap < oldestlag; ++tap) { |
| 311 |
C_MUL(tmp,circbuf[tap],*tmph); |
| 312 |
--tmph; |
| 313 |
C_ADDTO(outval,tmp); |
| 314 |
} |
| 315 |
C_MUL(tmp,buf[k],*tmph); |
| 316 |
C_ADDTO(outval,tmp); |
| 317 |
#endif |
| 318 |
|
| 319 |
circbuf[oldestlag++] = buf[k]; |
| 320 |
buf[k] = outval; |
| 321 |
|
| 322 |
if (oldestlag == nlag) |
| 323 |
oldestlag = 0; |
| 324 |
} |
| 325 |
|
| 326 |
if (fwrite (buf, sizeof (buf[0]), nread, fout) != nread) { |
| 327 |
perror ("short write"); |
| 328 |
exit (1); |
| 329 |
} |
| 330 |
} while (nread); |
| 331 |
free (buf); |
| 332 |
free (circbuf); |
| 333 |
} |
| 334 |
|
| 335 |
static |
| 336 |
void do_file_filter( |
| 337 |
FILE * fin, |
| 338 |
FILE * fout, |
| 339 |
const kffsamp_t * imp_resp, |
| 340 |
size_t n_imp_resp, |
| 341 |
size_t nfft ) |
| 342 |
{ |
| 343 |
int fdout; |
| 344 |
size_t n_samps_buf; |
| 345 |
|
| 346 |
kiss_fastfir_cfg cfg; |
| 347 |
kffsamp_t *inbuf,*outbuf; |
| 348 |
size_t nread,nwrite; |
| 349 |
size_t idx_inbuf; |
| 350 |
|
| 351 |
fdout = fileno(fout); |
| 352 |
|
| 353 |
cfg=kiss_fastfir_alloc(imp_resp,n_imp_resp,&nfft,0,0); |
| 354 |
|
| 355 |
/* use length to minimize buffer shift*/ |
| 356 |
n_samps_buf = 8*4096/sizeof(kffsamp_t); |
| 357 |
n_samps_buf = nfft + 4*(nfft-n_imp_resp+1); |
| 358 |
|
| 359 |
if (verbose) fprintf(stderr,"bufsize=%d\n",(int)(sizeof(kffsamp_t)*n_samps_buf) ); |
| 360 |
|
| 361 |
|
| 362 |
/*allocate space and initialize pointers */ |
| 363 |
inbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf); |
| 364 |
outbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf); |
| 365 |
|
| 366 |
idx_inbuf=0; |
| 367 |
do{ |
| 368 |
/* start reading at inbuf[idx_inbuf] */ |
| 369 |
nread = fread( inbuf + idx_inbuf, sizeof(kffsamp_t), n_samps_buf - idx_inbuf,fin ); |
| 370 |
|
| 371 |
/* If nread==0, then this is a flush. |
| 372 |
The total number of samples in input is idx_inbuf + nread . */ |
| 373 |
nwrite = kiss_fastfir(cfg, inbuf, outbuf,nread,&idx_inbuf) * sizeof(kffsamp_t); |
| 374 |
/* kiss_fastfir moved any unused samples to the front of inbuf and updated idx_inbuf */ |
| 375 |
|
| 376 |
ssize_t written = write(fdout, outbuf, nwrite); |
| 377 |
if ( written < 0 || (size_t)written != nwrite ) { |
| 378 |
perror("short write"); |
| 379 |
exit(1); |
| 380 |
} |
| 381 |
}while ( nread ); |
| 382 |
free(cfg); |
| 383 |
free(inbuf); |
| 384 |
free(outbuf); |
| 385 |
} |
| 386 |
|
| 387 |
int main(int argc,char**argv) |
| 388 |
{ |
| 389 |
kffsamp_t * h; |
| 390 |
int use_direct=0; |
| 391 |
size_t nh,nfft=0; |
| 392 |
FILE *fin=stdin; |
| 393 |
FILE *fout=stdout; |
| 394 |
FILE *filtfile=NULL; |
| 395 |
while (1) { |
| 396 |
int c=getopt(argc,argv,"n:h:i:o:vd"); |
| 397 |
if (c==-1) break; |
| 398 |
switch (c) { |
| 399 |
case 'v': |
| 400 |
verbose=1; |
| 401 |
break; |
| 402 |
case 'n': |
| 403 |
nfft=atoi(optarg); |
| 404 |
break; |
| 405 |
case 'i': |
| 406 |
fin = fopen(optarg,"rb"); |
| 407 |
if (fin==NULL) { |
| 408 |
perror(optarg); |
| 409 |
exit(1); |
| 410 |
} |
| 411 |
break; |
| 412 |
case 'o': |
| 413 |
fout = fopen(optarg,"w+b"); |
| 414 |
if (fout==NULL) { |
| 415 |
perror(optarg); |
| 416 |
exit(1); |
| 417 |
} |
| 418 |
break; |
| 419 |
case 'h': |
| 420 |
filtfile = fopen(optarg,"rb"); |
| 421 |
if (filtfile==NULL) { |
| 422 |
perror(optarg); |
| 423 |
exit(1); |
| 424 |
} |
| 425 |
break; |
| 426 |
case 'd': |
| 427 |
use_direct=1; |
| 428 |
break; |
| 429 |
case '?': |
| 430 |
fprintf(stderr,"usage options:\n" |
| 431 |
"\t-n nfft: fft size to use\n" |
| 432 |
"\t-d : use direct FIR filtering, not fast convolution\n" |
| 433 |
"\t-i filename: input file\n" |
| 434 |
"\t-o filename: output(filtered) file\n" |
| 435 |
"\t-n nfft: fft size to use\n" |
| 436 |
"\t-h filename: impulse response\n"); |
| 437 |
exit (1); |
| 438 |
default:fprintf(stderr,"bad %c\n",c);break; |
| 439 |
} |
| 440 |
} |
| 441 |
if (filtfile==NULL) { |
| 442 |
fprintf(stderr,"You must supply the FIR coeffs via -h\n"); |
| 443 |
exit(1); |
| 444 |
} |
| 445 |
fseek(filtfile,0,SEEK_END); |
| 446 |
nh = ftell(filtfile) / sizeof(kffsamp_t); |
| 447 |
if (verbose) fprintf(stderr,"%d samples in FIR filter\n",(int)nh); |
| 448 |
h = (kffsamp_t*)malloc(sizeof(kffsamp_t)*nh); |
| 449 |
fseek(filtfile,0,SEEK_SET); |
| 450 |
if (fread(h,sizeof(kffsamp_t),nh,filtfile) != nh) |
| 451 |
fprintf(stderr,"short read on filter file\n"); |
| 452 |
|
| 453 |
fclose(filtfile); |
| 454 |
|
| 455 |
if (use_direct) |
| 456 |
direct_file_filter( fin, fout, h,nh); |
| 457 |
else |
| 458 |
do_file_filter( fin, fout, h,nh,nfft); |
| 459 |
|
| 460 |
if (fout!=stdout) fclose(fout); |
| 461 |
if (fin!=stdin) fclose(fin); |
| 462 |
|
| 463 |
return 0; |
| 464 |
} |
| 465 |
#endif |