13
0

remove cruft

This commit is contained in:
Robin Gareus 2016-10-07 13:51:58 +02:00
parent 2ac7c5d3e2
commit 9f342ceef7
5 changed files with 0 additions and 1075 deletions

View File

@ -1,208 +0,0 @@
/*
Copyright (c) 2003-2004, Mark Borgerding
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "kiss_fft.h"
#include "kiss_fftndr.h"
static
void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse)
{
kiss_fft_cfg st;
kiss_fft_cpx * buf;
kiss_fft_cpx * bufout;
buf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
bufout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft );
st = kiss_fft_alloc( nfft ,isinverse ,0,0);
while ( fread( buf , sizeof(kiss_fft_cpx) * nfft ,1, fin ) > 0 ) {
kiss_fft( st , buf ,bufout);
fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout );
}
free(st);
free(buf);
free(bufout);
}
static
void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
{
kiss_fftnd_cfg st;
kiss_fft_cpx *buf;
int dimprod=1,i;
for (i=0;i<ndims;++i)
dimprod *= dims[i];
buf = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * dimprod);
st = kiss_fftnd_alloc (dims, ndims, isinverse, 0, 0);
while (fread (buf, sizeof (kiss_fft_cpx) * dimprod, 1, fin) > 0) {
kiss_fftnd (st, buf, buf);
fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout);
}
free (st);
free (buf);
}
static
void fft_filend_real(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse)
{
int dimprod=1,i;
kiss_fftndr_cfg st;
void *ibuf;
void *obuf;
int insize,outsize; // size in bytes
for (i=0;i<ndims;++i)
dimprod *= dims[i];
insize = outsize = dimprod;
int rdim = dims[ndims-1];
if (isinverse)
insize = insize*2*(rdim/2+1)/rdim;
else
outsize = outsize*2*(rdim/2+1)/rdim;
ibuf = malloc(insize*sizeof(kiss_fft_scalar));
obuf = malloc(outsize*sizeof(kiss_fft_scalar));
st = kiss_fftndr_alloc(dims, ndims, isinverse, 0, 0);
while ( fread (ibuf, sizeof(kiss_fft_scalar), insize, fin) > 0) {
if (isinverse) {
kiss_fftndri(st,
(kiss_fft_cpx*)ibuf,
(kiss_fft_scalar*)obuf);
}else{
kiss_fftndr(st,
(kiss_fft_scalar*)ibuf,
(kiss_fft_cpx*)obuf);
}
fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout);
}
free(st);
free(ibuf);
free(obuf);
}
static
void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse)
{
kiss_fftr_cfg st;
kiss_fft_scalar * rbuf;
kiss_fft_cpx * cbuf;
rbuf = (kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar) * nfft );
cbuf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * (nfft/2+1) );
st = kiss_fftr_alloc( nfft ,isinverse ,0,0);
if (isinverse==0) {
while ( fread( rbuf , sizeof(kiss_fft_scalar) * nfft ,1, fin ) > 0 ) {
kiss_fftr( st , rbuf ,cbuf);
fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout );
}
}else{
while ( fread( cbuf , sizeof(kiss_fft_cpx) * (nfft/2+1) ,1, fin ) > 0 ) {
kiss_fftri( st , cbuf ,rbuf);
fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout );
}
}
free(st);
free(rbuf);
free(cbuf);
}
static
int get_dims(char * arg,int * dims)
{
char *p0;
int ndims=0;
do{
p0 = strchr(arg,',');
if (p0)
*p0++ = '\0';
dims[ndims++] = atoi(arg);
// fprintf(stderr,"dims[%d] = %d\n",ndims-1,dims[ndims-1]);
arg = p0;
}while (p0);
return ndims;
}
int main(int argc,char ** argv)
{
int isinverse=0;
int isreal=0;
FILE *fin=stdin;
FILE *fout=stdout;
int ndims=1;
int dims[32];
dims[0] = 1024; /*default fft size*/
while (1) {
int c=getopt(argc,argv,"n:iR");
if (c==-1) break;
switch (c) {
case 'n':
ndims = get_dims(optarg,dims);
break;
case 'i':isinverse=1;break;
case 'R':isreal=1;break;
case '?':
fprintf(stderr,"usage options:\n"
"\t-n d1[,d2,d3...]: fft dimension(s)\n"
"\t-i : inverse\n"
"\t-R : real input samples, not complex\n");
exit (1);
default:fprintf(stderr,"bad %c\n",c);break;
}
}
if ( optind < argc ) {
if (strcmp("-",argv[optind]) !=0)
fin = fopen(argv[optind],"rb");
++optind;
}
if ( optind < argc ) {
if ( strcmp("-",argv[optind]) !=0 )
fout = fopen(argv[optind],"wb");
++optind;
}
if (ndims==1) {
if (isreal)
fft_file_real(fin,fout,dims[0],isinverse);
else
fft_file(fin,fout,dims[0],isinverse);
}else{
if (isreal)
fft_filend_real(fin,fout,dims,ndims,isinverse);
else
fft_filend(fin,fout,dims,ndims,isinverse);
}
if (fout!=stdout) fclose(fout);
if (fin!=stdin) fclose(fin);
return 0;
}

View File

@ -1,116 +0,0 @@
#include "kfc.h"
/*
Copyright (c) 2003-2004, Mark Borgerding
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
typedef struct cached_fft *kfc_cfg;
struct cached_fft
{
int nfft;
int inverse;
kiss_fft_cfg cfg;
kfc_cfg next;
};
static kfc_cfg cache_root=NULL;
static int ncached=0;
static kiss_fft_cfg find_cached_fft(int nfft,int inverse)
{
size_t len;
kfc_cfg cur=cache_root;
kfc_cfg prev=NULL;
while ( cur ) {
if ( cur->nfft == nfft && inverse == cur->inverse )
break;/*found the right node*/
prev = cur;
cur = prev->next;
}
if (cur== NULL) {
/* no cached node found, need to create a new one*/
kiss_fft_alloc(nfft,inverse,0,&len);
#ifdef USE_SIMD
int padding = (16-sizeof(struct cached_fft)) & 15;
// make sure the cfg aligns on a 16 byte boundary
len += padding;
#endif
cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len ));
if (cur == NULL)
return NULL;
cur->cfg = (kiss_fft_cfg)(cur+1);
#ifdef USE_SIMD
cur->cfg = (kiss_fft_cfg) ((char*)(cur+1)+padding);
#endif
kiss_fft_alloc(nfft,inverse,cur->cfg,&len);
cur->nfft=nfft;
cur->inverse=inverse;
cur->next = NULL;
if ( prev )
prev->next = cur;
else
cache_root = cur;
++ncached;
}
return cur->cfg;
}
void kfc_cleanup(void)
{
kfc_cfg cur=cache_root;
kfc_cfg next=NULL;
while (cur){
next = cur->next;
free(cur);
cur=next;
}
ncached=0;
cache_root = NULL;
}
void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
{
kiss_fft( find_cached_fft(nfft,0),fin,fout );
}
void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout)
{
kiss_fft( find_cached_fft(nfft,1),fin,fout );
}
#ifdef KFC_TEST
static void check(int nc)
{
if (ncached != nc) {
fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached);
exit(1);
}
}
int main(void)
{
kiss_fft_cpx buf1[1024],buf2[1024];
memset(buf1,0,sizeof(buf1));
check(0);
kfc_fft(512,buf1,buf2);
check(1);
kfc_fft(512,buf1,buf2);
check(1);
kfc_ifft(512,buf1,buf2);
check(2);
kfc_cleanup();
check(0);
return 0;
}
#endif

View File

@ -1,46 +0,0 @@
#ifndef KFC_H
#define KFC_H
#include "kiss_fft.h"
#ifdef __cplusplus
extern "C" {
#endif
/*
KFC -- Kiss FFT Cache
Not needing to deal with kiss_fft_alloc and a config
object may be handy for a lot of programs.
KFC uses the underlying KISS FFT functions, but caches the config object.
The first time kfc_fft or kfc_ifft for a given FFT size, the cfg
object is created for it. All subsequent calls use the cached
configuration object.
NOTE:
You should probably not use this if your program will be using a lot
of various sizes of FFTs. There is a linear search through the
cached objects. If you are only using one or two FFT sizes, this
will be negligible. Otherwise, you may want to use another method
of managing the cfg objects.
There is no automated cleanup of the cached objects. This could lead
to large memory usage in a program that uses a lot of *DIFFERENT*
sized FFTs. If you want to force all cached cfg objects to be freed,
call kfc_cleanup.
*/
/*forward complex FFT */
void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout);
/*reverse complex FFT */
void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout);
/*free all cached objects*/
void kfc_cleanup(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,470 +0,0 @@
/*
Copyright (c) 2003-2004, Mark Borgerding
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "_kiss_fft_guts.h"
/*
Some definitions that allow real or complex filtering
*/
#ifdef REAL_FASTFIR
#define MIN_FFT_LEN 2048
#include "kiss_fftr.h"
typedef kiss_fft_scalar kffsamp_t;
typedef kiss_fftr_cfg kfcfg_t;
#define FFT_ALLOC kiss_fftr_alloc
#define FFTFWD kiss_fftr
#define FFTINV kiss_fftri
#else
#define MIN_FFT_LEN 1024
typedef kiss_fft_cpx kffsamp_t;
typedef kiss_fft_cfg kfcfg_t;
#define FFT_ALLOC kiss_fft_alloc
#define FFTFWD kiss_fft
#define FFTINV kiss_fft
#endif
typedef struct kiss_fastfir_state *kiss_fastfir_cfg;
kiss_fastfir_cfg kiss_fastfir_alloc(const kffsamp_t * imp_resp,size_t n_imp_resp,
size_t * nfft,void * mem,size_t*lenmem);
/* see do_file_filter for usage */
size_t kiss_fastfir( kiss_fastfir_cfg cfg, kffsamp_t * inbuf, kffsamp_t * outbuf, size_t n, size_t *offset);
static int verbose=0;
struct kiss_fastfir_state{
size_t nfft;
size_t ngood;
kfcfg_t fftcfg;
kfcfg_t ifftcfg;
kiss_fft_cpx * fir_freq_resp;
kiss_fft_cpx * freqbuf;
size_t n_freq_bins;
kffsamp_t * tmpbuf;
};
kiss_fastfir_cfg kiss_fastfir_alloc(
const kffsamp_t * imp_resp,size_t n_imp_resp,
size_t *pnfft, /* if <= 0, an appropriate size will be chosen */
void * mem,size_t*lenmem)
{
kiss_fastfir_cfg st = NULL;
size_t len_fftcfg,len_ifftcfg;
size_t memneeded = sizeof(struct kiss_fastfir_state);
char * ptr;
size_t i;
size_t nfft=0;
float scale;
int n_freq_bins;
if (pnfft)
nfft=*pnfft;
if (nfft<=0) {
/* determine fft size as next power of two at least 2x
the impulse response length*/
i=n_imp_resp-1;
nfft=2;
do{
nfft<<=1;
}while (i>>=1);
#ifdef MIN_FFT_LEN
if ( nfft < MIN_FFT_LEN )
nfft=MIN_FFT_LEN;
#endif
}
if (pnfft)
*pnfft = nfft;
#ifdef REAL_FASTFIR
n_freq_bins = nfft/2 + 1;
#else
n_freq_bins = nfft;
#endif
/*fftcfg*/
FFT_ALLOC (nfft, 0, NULL, &len_fftcfg);
memneeded += len_fftcfg;
/*ifftcfg*/
FFT_ALLOC (nfft, 1, NULL, &len_ifftcfg);
memneeded += len_ifftcfg;
/* tmpbuf */
memneeded += sizeof(kffsamp_t) * nfft;
/* fir_freq_resp */
memneeded += sizeof(kiss_fft_cpx) * n_freq_bins;
/* freqbuf */
memneeded += sizeof(kiss_fft_cpx) * n_freq_bins;
if (lenmem == NULL) {
st = (kiss_fastfir_cfg) malloc (memneeded);
} else {
if (*lenmem >= memneeded)
st = (kiss_fastfir_cfg) mem;
*lenmem = memneeded;
}
if (!st)
return NULL;
st->nfft = nfft;
st->ngood = nfft - n_imp_resp + 1;
st->n_freq_bins = n_freq_bins;
ptr=(char*)(st+1);
st->fftcfg = (kfcfg_t)ptr;
ptr += len_fftcfg;
st->ifftcfg = (kfcfg_t)ptr;
ptr += len_ifftcfg;
st->tmpbuf = (kffsamp_t*)ptr;
ptr += sizeof(kffsamp_t) * nfft;
st->freqbuf = (kiss_fft_cpx*)ptr;
ptr += sizeof(kiss_fft_cpx) * n_freq_bins;
st->fir_freq_resp = (kiss_fft_cpx*)ptr;
ptr += sizeof(kiss_fft_cpx) * n_freq_bins;
FFT_ALLOC (nfft,0,st->fftcfg , &len_fftcfg);
FFT_ALLOC (nfft,1,st->ifftcfg , &len_ifftcfg);
memset(st->tmpbuf,0,sizeof(kffsamp_t)*nfft);
/*zero pad in the middle to left-rotate the impulse response
This puts the scrap samples at the end of the inverse fft'd buffer */
st->tmpbuf[0] = imp_resp[ n_imp_resp - 1 ];
for (i=0;i<n_imp_resp - 1; ++i) {
st->tmpbuf[ nfft - n_imp_resp + 1 + i ] = imp_resp[ i ];
}
FFTFWD(st->fftcfg,st->tmpbuf,st->fir_freq_resp);
/* TODO: this won't work for fixed point */
scale = 1.0 / st->nfft;
for ( i=0; i < st->n_freq_bins; ++i ) {
#ifdef USE_SIMD
st->fir_freq_resp[i].r *= _mm_set1_ps(scale);
st->fir_freq_resp[i].i *= _mm_set1_ps(scale);
#else
st->fir_freq_resp[i].r *= scale;
st->fir_freq_resp[i].i *= scale;
#endif
}
return st;
}
static void fastconv1buf(const kiss_fastfir_cfg st,const kffsamp_t * in,kffsamp_t * out)
{
size_t i;
/* multiply the frequency response of the input signal by
that of the fir filter*/
FFTFWD( st->fftcfg, in , st->freqbuf );
for ( i=0; i<st->n_freq_bins; ++i ) {
kiss_fft_cpx tmpsamp;
C_MUL(tmpsamp,st->freqbuf[i],st->fir_freq_resp[i]);
st->freqbuf[i] = tmpsamp;
}
/* perform the inverse fft*/
FFTINV(st->ifftcfg,st->freqbuf,out);
}
/* n : the size of inbuf and outbuf in samples
return value: the number of samples completely processed
n-retval samples should be copied to the front of the next input buffer */
static size_t kff_nocopy(
kiss_fastfir_cfg st,
const kffsamp_t * inbuf,
kffsamp_t * outbuf,
size_t n)
{
size_t norig=n;
while (n >= st->nfft ) {
fastconv1buf(st,inbuf,outbuf);
inbuf += st->ngood;
outbuf += st->ngood;
n -= st->ngood;
}
return norig - n;
}
static
size_t kff_flush(kiss_fastfir_cfg st,const kffsamp_t * inbuf,kffsamp_t * outbuf,size_t n)
{
size_t zpad=0,ntmp;
ntmp = kff_nocopy(st,inbuf,outbuf,n);
n -= ntmp;
inbuf += ntmp;
outbuf += ntmp;
zpad = st->nfft - n;
memset(st->tmpbuf,0,sizeof(kffsamp_t)*st->nfft );
memcpy(st->tmpbuf,inbuf,sizeof(kffsamp_t)*n );
fastconv1buf(st,st->tmpbuf,st->tmpbuf);
memcpy(outbuf,st->tmpbuf,sizeof(kffsamp_t)*( st->ngood - zpad ));
return ntmp + st->ngood - zpad;
}
size_t kiss_fastfir(
kiss_fastfir_cfg vst,
kffsamp_t * inbuf,
kffsamp_t * outbuf,
size_t n_new,
size_t *offset)
{
size_t ntot = n_new + *offset;
if (n_new==0) {
return kff_flush(vst,inbuf,outbuf,ntot);
}else{
size_t nwritten = kff_nocopy(vst,inbuf,outbuf,ntot);
*offset = ntot - nwritten;
/*save the unused or underused samples at the front of the input buffer */
memcpy( inbuf , inbuf+nwritten , *offset * sizeof(kffsamp_t) );
return nwritten;
}
}
#ifdef FAST_FILT_UTIL
#include <unistd.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <assert.h>
static
void direct_file_filter(
FILE * fin,
FILE * fout,
const kffsamp_t * imp_resp,
size_t n_imp_resp)
{
size_t nlag = n_imp_resp - 1;
const kffsamp_t *tmph;
kffsamp_t *buf, *circbuf;
kffsamp_t outval;
size_t nread;
size_t nbuf;
size_t oldestlag = 0;
size_t k, tap;
#ifndef REAL_FASTFIR
kffsamp_t tmp;
#endif
nbuf = 4096;
buf = (kffsamp_t *) malloc ( sizeof (kffsamp_t) * nbuf);
circbuf = (kffsamp_t *) malloc (sizeof (kffsamp_t) * nlag);
if (!circbuf || !buf) {
perror("circbuf allocation");
exit(1);
}
if ( fread (circbuf, sizeof (kffsamp_t), nlag, fin) != nlag ) {
perror ("insufficient data to overcome transient");
exit (1);
}
do {
nread = fread (buf, sizeof (kffsamp_t), nbuf, fin);
if (nread <= 0)
break;
for (k = 0; k < nread; ++k) {
tmph = imp_resp+nlag;
#ifdef REAL_FASTFIR
# ifdef USE_SIMD
outval = _mm_set1_ps(0);
#else
outval = 0;
#endif
for (tap = oldestlag; tap < nlag; ++tap)
outval += circbuf[tap] * *tmph--;
for (tap = 0; tap < oldestlag; ++tap)
outval += circbuf[tap] * *tmph--;
outval += buf[k] * *tmph;
#else
# ifdef USE_SIMD
outval.r = outval.i = _mm_set1_ps(0);
#else
outval.r = outval.i = 0;
#endif
for (tap = oldestlag; tap < nlag; ++tap){
C_MUL(tmp,circbuf[tap],*tmph);
--tmph;
C_ADDTO(outval,tmp);
}
for (tap = 0; tap < oldestlag; ++tap) {
C_MUL(tmp,circbuf[tap],*tmph);
--tmph;
C_ADDTO(outval,tmp);
}
C_MUL(tmp,buf[k],*tmph);
C_ADDTO(outval,tmp);
#endif
circbuf[oldestlag++] = buf[k];
buf[k] = outval;
if (oldestlag == nlag)
oldestlag = 0;
}
if (fwrite (buf, sizeof (buf[0]), nread, fout) != nread) {
perror ("short write");
exit (1);
}
} while (nread);
free (buf);
free (circbuf);
}
static
void do_file_filter(
FILE * fin,
FILE * fout,
const kffsamp_t * imp_resp,
size_t n_imp_resp,
size_t nfft )
{
int fdout;
size_t n_samps_buf;
kiss_fastfir_cfg cfg;
kffsamp_t *inbuf,*outbuf;
int nread,nwrite;
size_t idx_inbuf;
fdout = fileno(fout);
cfg=kiss_fastfir_alloc(imp_resp,n_imp_resp,&nfft,0,0);
/* use length to minimize buffer shift*/
n_samps_buf = 8*4096/sizeof(kffsamp_t);
n_samps_buf = nfft + 4*(nfft-n_imp_resp+1);
if (verbose) fprintf(stderr,"bufsize=%d\n",(int)(sizeof(kffsamp_t)*n_samps_buf) );
/*allocate space and initialize pointers */
inbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf);
outbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf);
idx_inbuf=0;
do{
/* start reading at inbuf[idx_inbuf] */
nread = fread( inbuf + idx_inbuf, sizeof(kffsamp_t), n_samps_buf - idx_inbuf,fin );
/* If nread==0, then this is a flush.
The total number of samples in input is idx_inbuf + nread . */
nwrite = kiss_fastfir(cfg, inbuf, outbuf,nread,&idx_inbuf) * sizeof(kffsamp_t);
/* kiss_fastfir moved any unused samples to the front of inbuf and updated idx_inbuf */
if ( write(fdout, outbuf, nwrite) != nwrite ) {
perror("short write");
exit(1);
}
}while ( nread );
free(cfg);
free(inbuf);
free(outbuf);
}
int main(int argc,char**argv)
{
kffsamp_t * h;
int use_direct=0;
size_t nh,nfft=0;
FILE *fin=stdin;
FILE *fout=stdout;
FILE *filtfile=NULL;
while (1) {
int c=getopt(argc,argv,"n:h:i:o:vd");
if (c==-1) break;
switch (c) {
case 'v':
verbose=1;
break;
case 'n':
nfft=atoi(optarg);
break;
case 'i':
fin = fopen(optarg,"rb");
if (fin==NULL) {
perror(optarg);
exit(1);
}
break;
case 'o':
fout = fopen(optarg,"w+b");
if (fout==NULL) {
perror(optarg);
exit(1);
}
break;
case 'h':
filtfile = fopen(optarg,"rb");
if (filtfile==NULL) {
perror(optarg);
exit(1);
}
break;
case 'd':
use_direct=1;
break;
case '?':
fprintf(stderr,"usage options:\n"
"\t-n nfft: fft size to use\n"
"\t-d : use direct FIR filtering, not fast convolution\n"
"\t-i filename: input file\n"
"\t-o filename: output(filtered) file\n"
"\t-n nfft: fft size to use\n"
"\t-h filename: impulse response\n");
exit (1);
default:fprintf(stderr,"bad %c\n",c);break;
}
}
if (filtfile==NULL) {
fprintf(stderr,"You must supply the FIR coeffs via -h\n");
exit(1);
}
fseek(filtfile,0,SEEK_END);
nh = ftell(filtfile) / sizeof(kffsamp_t);
if (verbose) fprintf(stderr,"%d samples in FIR filter\n",(int)nh);
h = (kffsamp_t*)malloc(sizeof(kffsamp_t)*nh);
fseek(filtfile,0,SEEK_SET);
if (fread(h,sizeof(kffsamp_t),nh,filtfile) != nh)
fprintf(stderr,"short read on filter file\n");
fclose(filtfile);
if (use_direct)
direct_file_filter( fin, fout, h,nh);
else
do_file_filter( fin, fout, h,nh,nfft);
if (fout!=stdout) fclose(fout);
if (fin!=stdin) fclose(fin);
return 0;
}
#endif

View File

@ -1,235 +0,0 @@
/*
Copyright (c) 2003-2004, Mark Borgerding
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <png.h>
#include "kiss_fft.h"
#include "kiss_fftr.h"
int nfft=1024;
FILE * fin=NULL;
FILE * fout=NULL;
int navg=20;
int remove_dc=0;
int nrows=0;
float * vals=NULL;
int stereo=0;
static
void config(int argc,char** argv)
{
while (1) {
int c = getopt (argc, argv, "n:r:as");
if (c == -1)
break;
switch (c) {
case 'n': nfft=(int)atoi(optarg);break;
case 'r': navg=(int)atoi(optarg);break;
case 'a': remove_dc=1;break;
case 's': stereo=1;break;
case '?':
fprintf (stderr, "usage options:\n"
"\t-n d: fft dimension(s) [1024]\n"
"\t-r d: number of rows to average [20]\n"
"\t-a : remove average from each fft buffer\n"
"\t-s : input is stereo, channels will be combined before fft\n"
"16 bit machine format real input is assumed\n"
);
default:
fprintf (stderr, "bad %c\n", c);
exit (1);
break;
}
}
if ( optind < argc ) {
if (strcmp("-",argv[optind]) !=0)
fin = fopen(argv[optind],"rb");
++optind;
}
if ( optind < argc ) {
if ( strcmp("-",argv[optind]) !=0 )
fout = fopen(argv[optind],"wb");
++optind;
}
if (fin==NULL)
fin=stdin;
if (fout==NULL)
fout=stdout;
}
#define CHECKNULL(p) if ( (p)==NULL ) do { fprintf(stderr,"CHECKNULL failed @ %s(%d): %s\n",__FILE__,__LINE__,#p );exit(1);} while(0)
typedef struct
{
png_byte r;
png_byte g;
png_byte b;
} rgb_t;
static
void val2rgb(float x,rgb_t *p)
{
const double pi = 3.14159265358979;
p->g = (int)(255*sin(x*pi));
p->r = (int)(255*abs(sin(x*pi*3/2)));
p->b = (int)(255*abs(sin(x*pi*5/2)));
//fprintf(stderr,"%.2f : %d,%d,%d\n",x,(int)p->r,(int)p->g,(int)p->b);
}
static
void cpx2pixels(rgb_t * res,const float * fbuf,size_t n)
{
size_t i;
float minval,maxval,valrange;
minval=maxval=fbuf[0];
for (i = 0; i < n; ++i) {
if (fbuf[i] > maxval) maxval = fbuf[i];
if (fbuf[i] < minval) minval = fbuf[i];
}
fprintf(stderr,"min ==%f,max=%f\n",minval,maxval);
valrange = maxval-minval;
if (valrange == 0) {
fprintf(stderr,"min == max == %f\n",minval);
exit (1);
}
for (i = 0; i < n; ++i)
val2rgb( (fbuf[i] - minval)/valrange , res+i );
}
static
void transform_signal(void)
{
short *inbuf;
kiss_fftr_cfg cfg=NULL;
kiss_fft_scalar *tbuf;
kiss_fft_cpx *fbuf;
float *mag2buf;
int i;
int n;
int avgctr=0;
int nfreqs=nfft/2+1;
CHECKNULL( cfg=kiss_fftr_alloc(nfft,0,0,0) );
CHECKNULL( inbuf=(short*)malloc(sizeof(short)*2*nfft ) );
CHECKNULL( tbuf=(kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar)*nfft ) );
CHECKNULL( fbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*nfreqs ) );
CHECKNULL( mag2buf=(float*)malloc(sizeof(float)*nfreqs ) );
memset(mag2buf,0,sizeof(mag2buf)*nfreqs);
while (1) {
if (stereo) {
n = fread(inbuf,sizeof(short)*2,nfft,fin);
if (n != nfft )
break;
for (i=0;i<nfft;++i)
tbuf[i] = inbuf[2*i] + inbuf[2*i+1];
}else{
n = fread(inbuf,sizeof(short),nfft,fin);
if (n != nfft )
break;
for (i=0;i<nfft;++i)
tbuf[i] = inbuf[i];
}
if (remove_dc) {
float avg = 0;
for (i=0;i<nfft;++i) avg += tbuf[i];
avg /= nfft;
for (i=0;i<nfft;++i) tbuf[i] -= (kiss_fft_scalar)avg;
}
/* do FFT */
kiss_fftr(cfg,tbuf,fbuf);
for (i=0;i<nfreqs;++i)
mag2buf[i] += fbuf[i].r * fbuf[i].r + fbuf[i].i * fbuf[i].i;
if (++avgctr == navg) {
avgctr=0;
++nrows;
vals = (float*)realloc(vals,sizeof(float)*nrows*nfreqs);
float eps = 1;
for (i=0;i<nfreqs;++i)
vals[(nrows - 1) * nfreqs + i] = 10 * log10 ( mag2buf[i] / navg + eps );
memset(mag2buf,0,sizeof(mag2buf[0])*nfreqs);
}
}
free(cfg);
free(inbuf);
free(tbuf);
free(fbuf);
free(mag2buf);
}
static
void make_png(void)
{
png_bytepp row_pointers=NULL;
rgb_t * row_data=NULL;
int i;
int nfreqs = nfft/2+1;
png_structp png_ptr=NULL;
png_infop info_ptr=NULL;
CHECKNULL( png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,0,0,0) );
CHECKNULL( info_ptr = png_create_info_struct(png_ptr) );
png_init_io(png_ptr, fout );
png_set_IHDR(png_ptr, info_ptr ,nfreqs,nrows,8,PNG_COLOR_TYPE_RGB,PNG_INTERLACE_NONE,PNG_COMPRESSION_TYPE_DEFAULT,PNG_FILTER_TYPE_DEFAULT );
row_data = (rgb_t*)malloc(sizeof(rgb_t) * nrows * nfreqs) ;
cpx2pixels(row_data, vals, nfreqs*nrows );
row_pointers = realloc(row_pointers, nrows*sizeof(png_bytep));
for (i=0;i<nrows;++i) {
row_pointers[i] = (png_bytep)(row_data + i*nfreqs);
}
png_set_rows(png_ptr, info_ptr, row_pointers);
fprintf(stderr,"creating %dx%d png\n",nfreqs,nrows);
fprintf(stderr,"bitdepth %d \n",png_get_bit_depth(png_ptr,info_ptr ) );
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY , NULL);
}
int main(int argc,char ** argv)
{
config(argc,argv);
transform_signal();
make_png();
if (fout!=stdout) fclose(fout);
if (fin!=stdin) fclose(fin);
return 0;
}