took utility/test code out of main source file and moved down to sample_code

This commit is contained in:
Mark Borgerding 2003-08-10 15:39:15 +00:00
parent b95e3ea18a
commit a962dc2681
7 changed files with 313 additions and 81 deletions

View File

@ -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) void * kiss_fft_alloc(int nfft,int inverse_fft)
{ {
kiss_fft_state * st=NULL; kiss_fft_state * st=NULL;
// allocate one large buffer to hold the state and the buffers for twiddles and bit-rev indices // 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*sizeof(int); int size = sizeof(kiss_fft_state) + (nfft>>1)*sizeof(kiss_fft_cpx) + (nfft>>1)*sizeof(int);
st = ( kiss_fft_state *)malloc(size); st = ( kiss_fft_state *)malloc(size);
if (st) { if (st) {
st->twiddle = (kiss_fft_cpx*)(st+1); st->twiddle = (kiss_fft_cpx*)(st+1); // pointer just beyond the kiss_fft_state struct
st->swap_indices = (int*)( st->twiddle + (nfft>>1) ); st->swap_indices = (int*)( st->twiddle + (nfft>>1) );// pointer just beyond the twiddle buffer
st->nfft = nfft; st->nfft = nfft;
// initialize the twiddle factors
make_twiddles(st->twiddle,nfft,inverse_fft); make_twiddles(st->twiddle,nfft,inverse_fft);
// create list of index-swaps
st->nswap = make_bit_reverse_indices(st->swap_indices,nfft); st->nswap = make_bit_reverse_indices(st->swap_indices,nfft);
} }
return st; return st;
} }
void kiss_fft(const void * cfg,kiss_fft_cpx *f) void kiss_fft(const void * cfg,kiss_fft_cpx *f)
{ {
const kiss_fft_state * st = cfg; const kiss_fft_state * st = cfg;
fft_work(st->nfft, f, st->twiddle , 1 ); fft_work(st->nfft, f, st->twiddle , 1 );
undo_bit_rev(f,st->swap_indices,st->nswap); undo_bit_rev(f,st->swap_indices,st->nswap);
} }
#ifdef FFT_UTIL
#include <stdio.h>
#include <string.h>
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<nfft;++k) {
buf[k].r = buf[k].r * scaling.r;
buf[k].i = buf[k].i * scaling.i;
}
}
fwrite( buf , sizeof(kiss_fft_cpx) , nfft , stdout );
}
free(st);
free(buf);
return 0;
}
#endif

View File

@ -32,15 +32,15 @@ void* kiss_fft_alloc(int nfft,int inverse_fft);
// free() the state when done using it // free() the state when done using it
/* /*
* kiss_fft * kiss_fft(cfg,in_out_buf)
* *
* Perform an in-place FFT on a complex input buffer. * Perform an in-place FFT on a complex input buffer.
* * for a forward FFT,
* the input buffer should be real(f[0]) , imag(f[0]), ... ,real(f[nfft-1]) , imag(f[nfft-1]) * the input should be f[0] , f[1] , ... ,f[nfft-1]
* the the output will be real(F[0]) , imag(F[0]), ... ,real(F[nfft-1]) , imag(F[nfft-1]) * the output will be F[0] , F[1] , ... ,F[nfft-1]
* * Note that each element is complex.
* */ * */
void kiss_fft( const void* cfg , kiss_fft_cpx *f ); // call for each buffer void kiss_fft( const void* cfg_from_alloc , kiss_fft_cpx *f ); // call for each buffer
// when done with the cfg for a given fft size and direction, simply free it // when done with the cfg for a given fft size and direction, simply free it
#define kiss_fft_free free #define kiss_fft_free free

59
test/Makefile Normal file
View File

@ -0,0 +1,59 @@
NFFT=1024
ALLUTILS=kfft kifft kffts kiffts kfftd kifftd s2f s2d sigdiff sigdiffs sigdiffd scale scales scaled
NUMFFTS=1000
UTILSRC=../kiss_fft.c fftutil.c
all: $(ALLUTILS)
kfft: $(UTILSRC)
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) $(UTILSRC)
kifft: ../kiss_fft.h ../kiss_fft.c fftutil.c
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -DINVERSE_FFT $(UTILSRC)
kffts: $(UTILSRC)
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -DFIXED_POINT $(UTILSRC)
kiffts: $(UTILSRC)
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -DFIXED_POINT -DINVERSE_FFT $(UTILSRC)
kfftd: $(UTILSRC)
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -Dkiss_fft_scalar=double $(UTILSRC)
kifftd: ../kiss_fft.h ../kiss_fft.c fftutil.c
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -Dkiss_fft_scalar=double -DINVERSE_FFT $(UTILSRC)
s2f: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=short -DTYPE2=float
s2d: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=short -DTYPE2=double
scale: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=float -DTYPE2=float
scales: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=short -DTYPE2=short
scaled: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=double -DTYPE2=double
sigdiff: sigdiff.c
gcc -Wall -O3 -o $@ $^ -DTYPE=float -lm
sigdiffs: sigdiff.c
gcc -Wall -O3 -o $@ $^ -DTYPE=short -lm
sigdiffd: sigdiff.c
gcc -Wall -O3 -o $@ $^ -DTYPE=double -lm
randshort.dat:
dd if=/dev/urandom bs=$$((4 * $(NFFT) )) count=$(NUMFFTS) of=randshort.dat
randfloat.dat: randshort.dat s2f
./s2f < $< > $
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 *~

80
test/sigdiff.c Normal file
View File

@ -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 <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#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<n;++i) {
double s=buf1[i];
double n = s - buf2[i];
sigpower += s*s;
noisepower += n*n;
if (s!=0) {
++ntotal;
scale += buf2[i] / s;
}
}
}
if ( fread( buf2 , sizeof(TYPE) , BUFSIZE , f2 ) > 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;
}

46
test/typeconvert.c Normal file
View File

@ -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 <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#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<argc;++i){
if (strcmp("-m",argv[i])==0){
mult *= atof(argv[i+1] );
++i;
}else if (strcmp("-d",argv[i])==0){
mult /= atof(argv[i+1] );
++i;
}
}
while ( ( n = fread( buf1 , sizeof(TYPE1) , BUFSIZE , stdin ) ) > 0 ) {
for (i=0;i<n;++i)
buf2[i] = (TYPE1)(mult * buf1[i]);
fwrite( buf2 , sizeof(TYPE2) , n , stdout );
}
return 0;
}

59
tools/Makefile Normal file
View File

@ -0,0 +1,59 @@
NFFT=1024
ALLUTILS=kfft kifft kffts kiffts kfftd kifftd s2f s2d sigdiff sigdiffs sigdiffd scale scales scaled
NUMFFTS=1000
UTILSRC=../kiss_fft.c fftutil.c
all: $(ALLUTILS)
kfft: $(UTILSRC)
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) $(UTILSRC)
kifft: ../kiss_fft.h ../kiss_fft.c fftutil.c
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -DINVERSE_FFT $(UTILSRC)
kffts: $(UTILSRC)
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -DFIXED_POINT $(UTILSRC)
kiffts: $(UTILSRC)
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -DFIXED_POINT -DINVERSE_FFT $(UTILSRC)
kfftd: $(UTILSRC)
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -Dkiss_fft_scalar=double $(UTILSRC)
kifftd: ../kiss_fft.h ../kiss_fft.c fftutil.c
gcc -Wall -O3 -o $@ -lm -I.. -DNFFT=$(NFFT) -Dkiss_fft_scalar=double -DINVERSE_FFT $(UTILSRC)
s2f: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=short -DTYPE2=float
s2d: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=short -DTYPE2=double
scale: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=float -DTYPE2=float
scales: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=short -DTYPE2=short
scaled: typeconvert.c
gcc -Wall -O3 -o $@ $^ -DTYPE1=double -DTYPE2=double
sigdiff: sigdiff.c
gcc -Wall -O3 -o $@ $^ -DTYPE=float -lm
sigdiffs: sigdiff.c
gcc -Wall -O3 -o $@ $^ -DTYPE=short -lm
sigdiffd: sigdiff.c
gcc -Wall -O3 -o $@ $^ -DTYPE=double -lm
randshort.dat:
dd if=/dev/urandom bs=$$((4 * $(NFFT) )) count=$(NUMFFTS) of=randshort.dat
randfloat.dat: randshort.dat s2f
./s2f < $< > $
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 *~

47
tools/fftutil.c Normal file
View File

@ -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 <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#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;
}