diff --git a/kiss_fft.c b/kiss_fft.c index ecb1987..4eeb1b2 100644 --- a/kiss_fft.c +++ b/kiss_fft.c @@ -168,96 +168,37 @@ void fft_work(int N,kiss_fft_cpx *f,const kiss_fft_cpx * twid,int twid_step) } +/* + * void * kiss_fft_alloc(int nfft,int inverse_fft) + * + * User-callable function to allocate all necessary scratch space for the fft. + * + * The return value is a contiguous block of memory, allocated with malloc. As such, + * It can be freed with free(), rather than a kiss_fft-specific function. + * */ void * kiss_fft_alloc(int nfft,int inverse_fft) { kiss_fft_state * st=NULL; - // allocate one large buffer to hold the state and the buffers for twiddles and bit-rev indices - int size = sizeof(kiss_fft_state) + (nfft>>1)*sizeof(kiss_fft_cpx) + nfft*sizeof(int); + // allocate one large buffer to hold the state, twiddle buffers, and bit-rev indices + int size = sizeof(kiss_fft_state) + (nfft>>1)*sizeof(kiss_fft_cpx) + (nfft>>1)*sizeof(int); st = ( kiss_fft_state *)malloc(size); - if (st) { - st->twiddle = (kiss_fft_cpx*)(st+1); - st->swap_indices = (int*)( st->twiddle + (nfft>>1) ); + if (st) { + st->twiddle = (kiss_fft_cpx*)(st+1); // pointer just beyond the kiss_fft_state struct + st->swap_indices = (int*)( st->twiddle + (nfft>>1) );// pointer just beyond the twiddle buffer st->nfft = nfft; + // initialize the twiddle factors make_twiddles(st->twiddle,nfft,inverse_fft); + // create list of index-swaps st->nswap = make_bit_reverse_indices(st->swap_indices,nfft); } return st; } + void kiss_fft(const void * cfg,kiss_fft_cpx *f) { const kiss_fft_state * st = cfg; fft_work(st->nfft, f, st->twiddle , 1 ); undo_bit_rev(f,st->swap_indices,st->nswap); } - -#ifdef FFT_UTIL - -#include -#include - -int main(int argc,char ** argv) -{ - kiss_fft_cpx * buf=NULL; - void *st; - int nfft=1024; - int inverse_fft=0; - int scale=0; - kiss_fft_cpx scaling; - - fprintf(stderr,"sizeof(kiss_fft_cpx) == %d\n",sizeof(kiss_fft_cpx) ); - - // parse args - while (argc>1 && argv[1][0]=='-'){ - if (0==strcmp("-i",argv[1])) { - inverse_fft = 1; - }else if(0==strcmp("-s",argv[1])) { - scale = 1; - } - --argc;++argv; - } - if (argc>1) { - nfft = atoi(argv[1]); - } - - // do we need to scale? - if (scale) { -#ifdef FIXED_POINT - if ( ! inverse_fft ) { - scaling.r = nfft; - scaling.i = nfft; - fprintf(stderr,"scaling by %d\n",scaling.r); - } -#else - if (inverse_fft ){ - scaling.r = 1.0/nfft; - scaling.i = 1.0/nfft; - fprintf(stderr,"scaling by %e\n",scaling.r); - } -#endif - else - scale = 0; // no need - } - - buf=(kiss_fft_cpx*)malloc( sizeof(kiss_fft_cpx) * nfft ); - st = kiss_fft_alloc( nfft ,inverse_fft ); - while ( fread( buf , sizeof(kiss_fft_cpx) , nfft , stdin ) > 0 ) { - kiss_fft( st , buf ); - if (scale) { - int k; - for (k=0;k $ +randdouble.dat: randshort.dat s2d + ./s2d < $< > $@ + +data: randshort.dat randfloat.dat randdouble.dat + +test: all data + @echo "####################Testing double fft|ifft" + @./kfftd < randdouble.dat | ./kifftd | ./scaled -d 1024 | ./sigdiffd randdouble.dat - + @echo "####################Testing float fft|ifft" + @./kfft < randfloat.dat | ./kifft | ./scale -d 1024 | ./sigdiff randfloat.dat - + @echo "####################Testing short fft|ifft" + @./kffts < randshort.dat | ./kiffts | ./scales -m 1024 | ./sigdiffs randshort.dat - + +clean: + rm -f $(ALLUTILS) *.dat *~ + diff --git a/test/sigdiff.c b/test/sigdiff.c new file mode 100644 index 0000000..08bc0aa --- /dev/null +++ b/test/sigdiff.c @@ -0,0 +1,80 @@ +/* +Copyright (c) 2003, 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 +#include +#include +#include + + +#ifndef BUFSIZE +# define BUFSIZE 1024 +#endif + +int main(int argc, char ** argv) +{ + FILE * f1=stdin; + FILE * f2=stdin; + + int i,n; + TYPE buf1[BUFSIZE]; + TYPE buf2[BUFSIZE]; + + double sigpower=0; + double noisepower=0; + double snrdb=0; + double scale=0; + long ntotal=0; + + if (argc>1 && strcmp( argv[1] ,"-") != 0 ) { + f1 = fopen(argv[1],"rb"); + } + if (argc>2 && strcmp( argv[2] ,"-") != 0 ) { + f2 = fopen(argv[2],"rb"); + } + + // TODO LEFT OFF HERE + while ( ( n = fread( buf1 , sizeof(TYPE) , BUFSIZE , f1 ) ) > 0 ) { + if ( fread( buf2 , sizeof(TYPE) , BUFSIZE , f2 ) != n ) { + fprintf(stderr,"premature end of file 2\n"); + exit(1); + } + for (i=0;i 0 ) { + fprintf(stderr,"premature end of file 1\n"); + exit(1); + } + scale /= ntotal; + + if (noisepower>0) + snrdb = 10*log10( sigpower / noisepower ); + else + snrdb = 200; + + printf("snr = %.2f dB\n",snrdb); + printf("average output/input = %.5e\n",scale); + + return 0; +} diff --git a/test/typeconvert.c b/test/typeconvert.c new file mode 100644 index 0000000..9b9fa35 --- /dev/null +++ b/test/typeconvert.c @@ -0,0 +1,46 @@ +/* +Copyright (c) 2003, 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 +#include +#include +#include + + +#define BUFSIZE 1024 +int main(int argc, char ** argv ) +{ + int i,n; + TYPE1 buf1[BUFSIZE]; + TYPE2 buf2[BUFSIZE]; + double mult=1.0; + + for (i=1;i 0 ) { + for (i=0;i $ +randdouble.dat: randshort.dat s2d + ./s2d < $< > $@ + +data: randshort.dat randfloat.dat randdouble.dat + +test: all data + @echo "####################Testing double fft|ifft" + @./kfftd < randdouble.dat | ./kifftd | ./scaled -d 1024 | ./sigdiffd randdouble.dat - + @echo "####################Testing float fft|ifft" + @./kfft < randfloat.dat | ./kifft | ./scale -d 1024 | ./sigdiff randfloat.dat - + @echo "####################Testing short fft|ifft" + @./kffts < randshort.dat | ./kiffts | ./scales -m 1024 | ./sigdiffs randshort.dat - + +clean: + rm -f $(ALLUTILS) *.dat *~ + diff --git a/tools/fftutil.c b/tools/fftutil.c new file mode 100644 index 0000000..2c09918 --- /dev/null +++ b/tools/fftutil.c @@ -0,0 +1,47 @@ +/* +Copyright (c) 2003, 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 +#include +#include +#include + +#include "kiss_fft.h" + +#ifndef NFFT +# define NFFT 1024 +#endif + +int main(int argc,char ** argv) +{ + kiss_fft_cpx buf[NFFT]; + void *st; + +#ifdef INVERSE_FFT + int inverse_fft=1; +#else + int inverse_fft=0; +#endif + + st = kiss_fft_alloc( NFFT ,inverse_fft ); + + while ( fread( buf , sizeof(kiss_fft_cpx) , NFFT , stdin ) > 0 ) { + kiss_fft( st , buf ); + fwrite( buf , sizeof(kiss_fft_cpx) , NFFT , stdout ); + } + + free(st); + + return 0; +}