added test code for kiss_fftnd

FFTs can now have an arbitrary # of dimensions
Also, buffer copies are eliminated.
This commit is contained in:
Mark Borgerding 2003-12-30 05:56:50 +00:00
parent 377ac796f0
commit ec3b64a62e
8 changed files with 192 additions and 50 deletions

View File

@ -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

View File

@ -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:

View File

@ -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__":

View File

@ -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<FFT_SIZE;++i) {
printf(stderr,"good[%d]=",i);pcpx(buf1out+i);
printf(stderr,"bad [%d]=",i);pcpx(buf2out+i);
printf("good[%d]=",i);pcpx(buf1out+i);
printf("bad [%d]=",i);pcpx(buf2out+i);
}
free(cfg);
return 1;
@ -178,6 +178,7 @@ int test_stride()
int test2d()
{
void *cfg;
int dims[]={3,9};
kiss_fft_cpx in[3*9]={
{1,0},{0,0},{0,0},{0,0},{0,0},{0,0},{3,0},{0,0},{0,0},
{0,0},{0,0},{0,0},{0,0},{5,0},{0,0},{0,0},{0,0},{0,0},
@ -186,8 +187,8 @@ int test2d()
kiss_fft_cpx ver[3*9]= {
{16.00000,0.00000},{0.16385,5.38749},{4.54576,7.50952},{-2.00000,1.73205},{-6.20961,9.91626},{-6.20961,-9.91626},{-2.00000,-1.73205},{4.54576,-7.50952},{0.16385,-5.38749},{-2.00000,1.73205},{-6.20961,9.91626},{-6.20961,-9.91626},{-2.00000,-1.73205},{4.54576,-7.50952},{0.16385,-5.38749},{16.00000,0.00000},{0.16385,5.38749},{4.54576,7.50952},{-2.00000,-1.73205},{4.54576,-7.50952},{0.16385,-5.38749},{16.00000,-0.00000},{0.16385,5.38749},{4.54576,7.50952},{-2.00000,1.73205},{-6.20961,9.91626},{-6.20961,-9.91626} };
cfg = kiss_fft2d_alloc(3,9,0,0,0);
kiss_fft2d(cfg,in,out);
cfg = kiss_fftnd_alloc(dims,2,0,0,0);
kiss_fftnd(cfg,in,out);
free(cfg);
if ( snr_compare( out,ver, 3*9) < 100 ) {

View File

@ -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

View File

@ -19,7 +19,7 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
#include <unistd.h>
#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<ndims;++i)
dimprod *= dims[i];
buf = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * nfft * nrows);
bufout = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * nfft * nrows);
st = kiss_fft2d_alloc (nrows, nfft, isinverse, 0, 0);
buf = (kiss_fft_cpx *) malloc (sizeof (kiss_fft_cpx) * dimprod);
bufout = (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) * nfft * nrows, 1, fin) > 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);

107
tools/kiss_fftnd.c Normal file
View File

@ -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;i<ndims;++i) {
size_t sublen;
kiss_fft_alloc (dims[i], inverse_fft, NULL, &sublen);
memneeded += sublen; /* st->states[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;i<ndims;++i) {
size_t len;
st->dims[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;i<stride;++i)
kiss_fft_stride( st->states[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;
}
}
}

9
tools/kiss_fftnd.h Normal file
View File

@ -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