From ec3b64a62e6307604e22653e84fb036824b328ad Mon Sep 17 00:00:00 2001 From: Mark Borgerding Date: Tue, 30 Dec 2003 05:56:50 +0000 Subject: [PATCH] added test code for kiss_fftnd FFTs can now have an arbitrary # of dimensions Also, buffer copies are eliminated. --- test/Makefile | 4 +- test/compfft.py | 30 ++++++++----- test/fft.py | 15 +++---- test/selftest.c | 11 ++--- tools/Makefile | 4 +- tools/fftutil.c | 62 +++++++++++++++++--------- tools/kiss_fftnd.c | 107 +++++++++++++++++++++++++++++++++++++++++++++ tools/kiss_fftnd.h | 9 ++++ 8 files changed, 192 insertions(+), 50 deletions(-) create mode 100644 tools/kiss_fftnd.c create mode 100644 tools/kiss_fftnd.h diff --git a/test/Makefile b/test/Makefile index 284d0d0..4bfc09a 100644 --- a/test/Makefile +++ b/test/Makefile @@ -39,10 +39,10 @@ CFLAGS=-Wall -O3 -pedantic -march=pentiumpro -ffast-math -fomit-frame-pointer # If the above flags do not work, try the following #CFLAGS=-Wall -O3 -$(FFTUTIL): ../kiss_fft.c fftutil.c kiss_fft2d.c kiss_fftr.c +$(FFTUTIL): ../kiss_fft.c fftutil.c kiss_fftnd.c kiss_fftr.c $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) -lm $+ -$(SELFTEST): ../kiss_fft.c $(SELFTESTSRC) kiss_fft2d.c +$(SELFTEST): ../kiss_fft.c $(SELFTESTSRC) kiss_fftnd.c $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) -lm $+ $(TESTKFC): ../kiss_fft.c kfc.c diff --git a/test/compfft.py b/test/compfft.py index ae7eb00..e815fec 100755 --- a/test/compfft.py +++ b/test/compfft.py @@ -7,6 +7,7 @@ import math import random import sys import struct +import fft pi=math.pi e=math.e @@ -28,23 +29,24 @@ def main(): util = opts.get('-u','./kf_float') try: - n = int(opts['-n']) + dims = [ int(d) for d in opts['-n'].split(',')] cpx = opts.get('-R') is None fmt=opts.get('-t','f') except KeyError: sys.stderr.write(""" usage: compfft.py - -n nfft - -u utilname : see sample_code/fftutil.c + -n d1[,d2,d3...] : FFT dimension(s) + -u utilname : see sample_code/fftutil.c, default = ./kf_float -R : real-optimized version\n""") sys.exit(1) - x = randbuf(n,cpx) + x = fft.make_random( dims ) - cmd = '%s -n %d ' % ( util, n ) + cmd = '%s -n %s ' % ( util, ','.join([ str(d) for d in dims]) ) if cpx: - xout = FFT.fft(x) - else: + xout = FFT.fftnd(x) + xout = reshape(xout,(size(xout),)) + else: cmd += '-R ' xout = FFT.real_fft(x) @@ -53,18 +55,22 @@ def main(): proc.tochild.write( dopack( x , fmt ,cpx ) ) proc.tochild.close() xoutcomp = dounpack( proc.fromchild.read( ) , fmt ,1 ) + #xoutcomp = reshape( xoutcomp , dims ) + + sig = xout * conjugate(xout) + sigpow = sum( sig ) - sigpow = sum( xout * conjugate(xout) ) - print len(xout) - print len(xoutcomp) - diff = xout-xoutcomp noisepow = sum( diff * conjugate(diff) ) snr = 10 * math.log10(abs( sigpow / noisepow ) ) - print 'NFFT=%d,SNR = %f dB' % (n,snr) + if snr<100: + print xout + print xoutcomp + print 'NFFT=%s,SNR = %f dB' % (str(dims),snr) def dopack(x,fmt,cpx): + x = reshape( x, ( size(x),) ) if cpx: s = ''.join( [ struct.pack('ff',c.real,c.imag) for c in x ] ) else: diff --git a/test/fft.py b/test/fft.py index e39b49c..ca03496 100755 --- a/test/fft.py +++ b/test/fft.py @@ -148,7 +148,6 @@ def randmat( ndims ): def test_fftnd(ndims=3): import FFT import Numeric - random.seed(2) x=randmat( ndims ) print 'dimensions=%s' % str( Numeric.shape(x) ) @@ -174,15 +173,15 @@ def myfftnd(x): def fftndwork(x,dims): import Numeric - gg=Numeric.product( dims ) + dimprod=Numeric.product( dims ) for k in range( len(dims) ): - d=dims[ k ] - g=gg/d - next_stage = [complex(0,0)]*len(x) - for i in range(g): - next_stage[i*d:(i+1)*d] = fft(x[i:(i+d)*g:g],0) - x = next_stage + cur_dim=dims[ k ] + stride=dimprod/cur_dim + next_x = [complex(0,0)]*len(x) + for i in range(stride): + next_x[i*cur_dim:(i+1)*cur_dim] = fft(x[i:(i+cur_dim)*stride:stride],0) + x = next_x return x if __name__ == "__main__": diff --git a/test/selftest.c b/test/selftest.c index c09c15c..6427811 100644 --- a/test/selftest.c +++ b/test/selftest.c @@ -1,5 +1,5 @@ #include "kiss_fft.h" -#include "kiss_fft2d.h" +#include "kiss_fftnd.h" #include "_kiss_fft_guts.h" #define xstr(s) str(s) @@ -164,8 +164,8 @@ int test_stride() if ( memcmp(buf1out,buf2out,sizeof(buf1out) ) ){ printf("kiss_fft_stride not working for stride =%d\n",SKIP_FACTOR); for (i=0;i #include "kiss_fft.h" -#include "kiss_fft2d.h" +#include "kiss_fftnd.h" #include "kiss_fftr.h" void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse) @@ -41,19 +41,22 @@ void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse) free(bufout); } -void fft_file2d(FILE * fin,FILE * fout,int nfft,int nrows,int isinverse) +void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse) { void *st; kiss_fft_cpx *buf; kiss_fft_cpx *bufout; + int dimprod=1,i; + for (i=0;i 0) { - kiss_fft2d (st, buf, bufout); - fwrite (bufout, sizeof (kiss_fft_cpx), nfft * nrows, fout); + while (fread (buf, sizeof (kiss_fft_cpx) * dimprod, 1, fin) > 0) { + kiss_fftnd (st, buf, bufout); + fwrite (bufout, sizeof (kiss_fft_cpx), dimprod, fout); } free (st); free (buf); @@ -86,27 +89,44 @@ void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse) free(cbuf); } +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 nfft=1024; int isinverse=0; int isreal=0; FILE *fin=stdin; FILE *fout=stdout; - int nrows=1; + int ndims=1; + int dims[32]; + dims[0] = 1024; /*default fft size*/ while (1) { - int c=getopt(argc,argv,"n:ir:R"); + int c=getopt(argc,argv,"n:iR"); if (c==-1) break; switch (c) { - case 'n':nfft = atoi(optarg);break; - case 'r':nrows = atoi(optarg);break; + 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 NFFT: fft size\n" - "\t-r nrows: # rows (implies 2d FFT)\n" + "\t-n d1[,d2,d3...]: fft dimension(s)\n" "\t-i : inverse\n" "\t-R : real input samples, not complex\n"); exit (1); @@ -126,12 +146,12 @@ int main(int argc,char ** argv) ++optind; } - if (nrows>1 && !isreal) - fft_file2d(fin,fout,nfft,nrows,isinverse); - else if (nrows==1 && !isreal) - fft_file(fin,fout,nfft,isinverse); - else if (nrows==1 && isreal) - fft_file_real(fin,fout,nfft,isinverse); + if (ndims>1 && !isreal) + fft_filend(fin,fout,dims,ndims,isinverse); + else if (ndims==1 && !isreal) + fft_file(fin,fout,dims[0],isinverse); + else if (ndims==1 && isreal) + fft_file_real(fin,fout,dims[0],isinverse); if (fout!=stdout) fclose(fout); if (fin!=stdin) fclose(fin); diff --git a/tools/kiss_fftnd.c b/tools/kiss_fftnd.c new file mode 100644 index 0000000..4a3a292 --- /dev/null +++ b/tools/kiss_fftnd.c @@ -0,0 +1,107 @@ +/* +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 "_kiss_fft_guts.h" + +typedef struct { + int minus4; /*magic to signify a N-d transform*/ + int dimprod; /* dimsum would be mighty tasty right now */ + int ndims; + int *dims; + void **states; + kiss_fft_cpx * tmpbuf; +}kiss_fftnd_state; + +void * kiss_fftnd_alloc(int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem) +{ + kiss_fftnd_state *st = NULL; + int i; + int dimprod=1; + size_t memneeded = sizeof(kiss_fftnd_state); + char * ptr; + + for (i=0;istates[i] */ + dimprod *= dims[i]; + } + memneeded += sizeof(int) * ndims;/* st->dims */ + memneeded += sizeof(void*) * ndims;/* st->states */ + memneeded += sizeof(kiss_fft_cpx) * dimprod; /* st->tmpbuf */ + + if (lenmem == NULL) { + st = (kiss_fftnd_state *) malloc (memneeded); + } else { + if (*lenmem >= memneeded) + st = (kiss_fftnd_state *) mem; + *lenmem = memneeded; + } + if (!st) + return NULL; + + st->minus4 = -4; + st->dimprod = dimprod; + st->ndims = ndims; + ptr=(char*)(st+1); + + st->states = (void**)ptr; + ptr += sizeof(void*) * ndims; + + st->dims = (int*)ptr; + ptr += sizeof(int) * ndims; + + st->tmpbuf = (kiss_fft_cpx*)ptr; + ptr += sizeof(kiss_fft_cpx) * dimprod; + + for (i=0;idims[i] = dims[i]; + kiss_fft_alloc (st->dims[i], inverse_fft, NULL, &len); + st->states[i] = ptr; + ptr += len; + kiss_fft_alloc (st->dims[i], inverse_fft, st->states[i], &len); + } + return st; +} + +void kiss_fftnd(const void * cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) +{ + int i,k; + kiss_fftnd_state *st = ( kiss_fftnd_state *)cfg; + const kiss_fft_cpx * bufin=fin; + kiss_fft_cpx * bufout; + + /*arrange it so the last bufout == fout*/ + if ( st->ndims & 1 ) + bufout = fout; + else + bufout = st->tmpbuf; + + for ( k=0; k < st->ndims; ++k) { + int curdim = st->dims[k]; + int stride = st->dimprod / curdim; + for (i=0;istates[k], bufin+i , bufout+i*curdim,stride ); + + /*toggle back and forth between the two buffers*/ + if (bufout == st->tmpbuf){ + bufout = fout; + bufin = st->tmpbuf; + }else{ + bufout = st->tmpbuf; + bufin = fout; + } + } +} diff --git a/tools/kiss_fftnd.h b/tools/kiss_fftnd.h new file mode 100644 index 0000000..bc0f45f --- /dev/null +++ b/tools/kiss_fftnd.h @@ -0,0 +1,9 @@ +#ifndef KISS_FFTND_H +#define KISS_FFTND_H + +#include "kiss_fft.h" + +void * kiss_fftnd_alloc(int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem); +void kiss_fftnd(const void * cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); + +#endif