getting ready for next release

This commit is contained in:
Mark Borgerding 2003-10-26 04:07:32 +00:00
parent 52b4b9ab5c
commit 1db3d91ee5
9 changed files with 68 additions and 314 deletions

View File

@ -2,9 +2,9 @@ message:
@echo "Nothing to make here. Move on down to sample_code for ... you guessed it! Sample Code!"
tarball: clean
tar --exclude CVS -czf kiss_fft.tar.gz .
tar --exclude CVS --exclude .cvsignore -cvzf kiss_fft.tar.gz .
clean:
cd sample_code && make clean
rm -f kiss_fft.tar.gz *~
rm -f kiss_fft.tar.gz *~ *.pyc

21
README
View File

@ -1,4 +1,4 @@
KISS FFT - A power-of-two Fast Fourier Transform based up on the principle,
KISS FFT - A mixed-radix Fast Fourier Transform based up on the principle,
"Keep It Simple, Stupid."
There are many great fft libraries already around. Kiss FFT is not trying
@ -26,8 +26,8 @@ two functions you'll need to use. Code definitions are in kiss_fft.c, along
with sample usage code.
The code can be easily recompiled to work with 16bit fixed point data,
or various floating point types. The default is float.
The code can be compiled to use float, double or 16bit short samples.
The default is float.
BACKGROUND:
@ -35,22 +35,23 @@ BACKGROUND:
I started coding this because I couldn't find a fixed point FFT that didn't
use assembly code. I started with floating point numbers so I could get the
theory straight before working on fixed point issues. In the end, I had a
little bit of code that could be recompiled easily to do ffts with short, float,
little bit of code that could be recompiled easily to do ffts with short, float
or double (other types should be easy too).
Once I got my FFT working, I wanted to get some performance numbers against
a well respected and highly optimized fft library. I don't want to criticize
this great library, so let's call it FFT_BRANDX.
this great library, so let's call it FFT_BRANDX.
During this process, I learned:
1. FFT_BRANDX has 500 times as many lines of code as Kiss
(and that's just the C code).
2. It took me an embarrassingly long time to get FFT_BRANDX working.
3. FFT_BRANDX is almost 3 times faster than Kiss
3. A simple program using FFT_BRANDX is 500K. A similar program using kiss_fft is 18k.
4. FFT_BRANDX is about 3-4 times faster than Kiss
It is wonderful that free, highly optimized libraries like FFT_BRANDX exist.
But such libraries carry a huge burden of complexity necessary to extract every
last bit of performance.
last bit of performance.
Sometimes simpler is better, even if it's not better.
@ -62,13 +63,12 @@ PERFORMANCE:
DO NOT:
... use Kiss if you need the absolute fastest fft in the world
... use Kiss if you need mixed radix FFTs
... ask me to add features that will bloat the code
UNDER THE HOOD:
Kiss uses a complex-only, frequency decimation, radix 2, in-place FFT. Bit reversed
addressing is corrected as the last step in the transform. No scaling is done.
Kiss uses a complex-only, time decimation, mixed-radix , out-of-place FFT.
No scaling is done.
LICENSE:
BSD, see COPYING for details. Basically, "free to use, give credit where due, no guarantees"
@ -76,7 +76,6 @@ LICENSE:
TODO:
*) Add sample code for parallel ffts (stereo) packed into re,im components of time sequence.
*) Add simple windowing function, e.g. Hamming : w(i)=.54-.46*cos(2pi*i/(n-1))
*) Could mixed-radix FFTs be made simple enough to stand by the KISS principle?
*) Make the fixed point scaling and bit shifts more easily configurable.
*) Document/revisit the input/output fft scaling
*) See if the fixed point code can be optimized a little without adding complexity.

View File

@ -23,13 +23,6 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */
#include "kiss_fft.h"
/* Try static inline. Who knows? You might get a speed improvement. I didn't. YMMV */
#if 0
# define FUNCDECL static inline
#else
# define FUNCDECL
#endif
typedef struct {
int nfft;
int inverse;
@ -47,25 +40,15 @@ typedef struct {
}while(0)
# define C_FIXDIV(c,div) \
do{ (c).r /= div; (c).i /=div; }while(0)
#define C_MUL_SCALAR(m,s) \
do{ (m).r = ( (m).r * (s) + (1<<14) ) >> 15;\
(m).i = ( (m).i * (s) + (1<<14) ) >> 15;\
}while(0)
#else /* not FIXED_POINT*/
#define C_MUL_SCALAR(m,s) \
do{ (m).r *= (s);\
(m).i *= (s);\
}while(0)
# define C_FIXDIV(c,div) /* NOOP */
#define C_MUL(m,a,b) \
do{ (m).r = (a).r*(b).r - (a).i*(b).i;\
(m).i = (a).r*(b).i + (a).i*(b).r; }while(0)
#endif
#define C_ADD( res, a,b)\
do { (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; }while(0)
#define C_SUB( res, a,b)\
do { (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; }while(0)
#define C_ADDTO( res , a)\
@ -93,7 +76,6 @@ kiss_fft_cpx cexp(double phase)
return x;
}
FUNCDECL
void bfly2(
kiss_fft_cpx * Fout,
int fstride,
@ -116,52 +98,6 @@ void bfly2(
}while (--m);
}
FUNCDECL
void bfly3(
kiss_fft_cpx * Fout,
int fstride,
const kiss_fft_state * st,
int m
)
{
kiss_fft_cpx *Fout0,*Fout1,*Fout2;
int u;
kiss_fft_cpx * scratch = st->scratch;
kiss_fft_cpx *tw1,*tw2;
tw2 = tw1 = st->twiddles;
Fout0=Fout;
Fout1=Fout0+m;
Fout2=Fout0+2*m;
scratch[3] = st->twiddles[ fstride*m ];
for ( u=0; u<m; ++u ) {
C_FIXDIV(*Fout0,3);
C_FIXDIV(*Fout1,3);
C_FIXDIV(*Fout2,3);
scratch[0] = *Fout0;
C_MUL(scratch[1],*Fout1 , *tw1 );
tw1 += fstride;
C_MUL(scratch[2],*Fout2 , *tw2 );
tw2 += 2*fstride;
C_ADD(scratch[5],scratch[1],scratch[2]);
C_SUB(scratch[6],scratch[1],scratch[2]);
C_ADDTO(*Fout0,scratch[5]);
C_MUL_SCALAR(scratch[5], scratch[3].r );
C_MUL_SCALAR(scratch[6], scratch[3].i );
scratch[4].r = scratch[5].r - scratch[6].i;
scratch[4].i = scratch[5].i + scratch[6].r;
C_ADD( *Fout1, scratch[0] , scratch[4] );
*Fout2 = *Fout1;
Fout2->r += 2*scratch[6].i;
Fout2->i -= 2*scratch[6].r;
++Fout0;++Fout1;++Fout2;
}
}
FUNCDECL
void bfly4(
kiss_fft_cpx * Fout,
int fstride,
@ -214,8 +150,7 @@ void bfly4(
}while(--m);
}
FUNCDECL
void bflyp(
void bfly_generic(
kiss_fft_cpx * Fout,
int fstride,
const kiss_fft_state * st,
@ -227,6 +162,7 @@ void bflyp(
kiss_fft_cpx * scratch = st->scratch;
kiss_fft_cpx * twiddles = st->twiddles;
kiss_fft_cpx t;
int Norig = st->nfft;
for ( u=0; u<m; ++u ) {
k=u;
@ -241,7 +177,6 @@ void bflyp(
int twidx=0;
Fout[ k ] = scratch[0];
for (q=1;q<p;++q ) {
int Norig = st->nfft;
twidx += fstride * k;
if (twidx>=Norig) twidx-=Norig;
C_MUL(t,scratch[q] , twiddles[twidx] );
@ -252,7 +187,6 @@ void bflyp(
}
}
FUNCDECL
void fft_work(
kiss_fft_cpx * Fout,
const kiss_fft_cpx * f,
@ -274,10 +208,9 @@ void fft_work(
}
switch (p) {
case 2: bfly2(Fout,fstride,st,m); break;
case 3: bfly3(Fout,fstride,st,m); break;
case 4: bfly4(Fout,fstride,st,m); break;
default: bflyp(Fout,fstride,st,m,p); break;
case 2: bfly2(Fout,fstride,st,m); break;
default: bfly_generic(Fout,fstride,st,m,p); break;
}
}
@ -324,7 +257,7 @@ void * kiss_fft_alloc(int nfft,int inverse_fft)
while (nfft>1) {
/* If you add a new radix, don't forget to put it here */
const int primes[] = {4,2,3,-1};
const int primes[] = {4,2,-1};
int p=nfft;
i=0;
while ( primes[i] != -1 ) {
@ -345,10 +278,13 @@ void * kiss_fft_alloc(int nfft,int inverse_fft)
void kiss_fft(const void * cfg,kiss_fft_cpx *f)
{
int n;
const kiss_fft_state * st = cfg;
n = st->nfft;
memcpy(st->tmpbuf,f,sizeof(kiss_fft_cpx)*n);
memcpy(st->tmpbuf,f,sizeof(kiss_fft_cpx)*st->nfft);
fft_work( f, st->tmpbuf, 1, st->factors,st );
}
void kiss_fft_io(const void * cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout)
{
const kiss_fft_state * st = cfg;
fft_work( fout, fin, 1, st->factors,st );
}

View File

@ -2,10 +2,7 @@
#define KISS_FFT_H
#ifdef FIXED_POINT
# define kiss_fft_scalar short
# define FIXED_POINT_FRAC_BITS 15
#else
# ifndef kiss_fft_scalar
# define kiss_fft_scalar float
@ -21,7 +18,7 @@ typedef struct {
/*
* fft_alloc
*
* Initialize a radix 2 FFT (or IFFT) algorithm.
* Initialize a FFT (or IFFT) algorithm.
*
* The return value from fft_alloc is a cfg buffer used internally
* by the fft routine.
@ -42,6 +39,9 @@ void* kiss_fft_alloc(int nfft,int inverse_fft);
* */
void kiss_fft( const void* cfg_from_alloc , kiss_fft_cpx *f ); /* call for each buffer */
/* two buffer version */
void kiss_fft_io(const void * cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout);
/* when done with the cfg for a given fft size and direction, simply free it*/
#define kiss_fft_free free

View File

@ -1,9 +1,17 @@
NFFT=840
ifeq "$(NFFT)" ""
NFFT=1024
endif
ALLUTILS=kfft kffts kfftd
NUMFFTS=10000
ifeq "$(NUMFFTS)" ""
NUMFFTS=10000
endif
UTILSRC=../kiss_fft.c fftutil.c
CFLAGS=-Wall -O3 -lm -I.. -ansi -DFUNCDECL= -pedantic
CFLAGS=-Wall -O3 -lm -I.. -ansi -pedantic
all: $(ALLUTILS)
@ -17,7 +25,7 @@ kfftd: $(UTILSRC)
time: all
@echo
@echo "#### timing $(NUMFFTS) x $(NFFT) point FFTs"
@echo -n "#### timing $(NUMFFTS) x $(NFFT) point FFTs. "; factor $(NFFT)
@echo "#### DOUBLE"
@time -f 'Elapsed:%E user:%U sys:%S' \
sh -c 'dd if=/dev/zero bs=$$((16*$(NFFT))) count=$(NUMFFTS) 2>/dev/null |./kfftd>/dev/null'
@ -28,6 +36,14 @@ time: all
@time -f 'Elapsed:%E user:%U sys:%S' \
sh -c 'dd if=/dev/zero bs=$$((4*$(NFFT))) count=$(NUMFFTS) 2>/dev/null |./kffts>/dev/null'
POW2=256 512 1024 2048
POW3=243 729 2187
mtime: all
@for n in $(POW2) ;do \
export NFFT=$$n;make time; \
done
snr: all
@which octave
@echo

View File

@ -1,87 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <malloc.h>
#include <stdio.h>
#include <math.h>
#include "kiss_fft.h"
#define NFFT 1024
int main(int argc, char ** argv)
{
int k;
void * st;
float fs=44100;
short sampsin[2*NFFT];
float lmag2[NFFT/2];
float rmag2[NFFT/2];
int peakr=0,peakl=0;
int removedc=1;
kiss_fft_cpx cbuf[NFFT];
int nbufs=0;
st = kiss_fft_alloc(NFFT,0);
memset( lmag2 , 0 , sizeof(lmag2) );
memset( rmag2 , 0 , sizeof(rmag2) );
while ( fread( sampsin , sizeof(short) * 2*NFFT, 1 , stdin ) == 1 ) {
//perform two ffts in parallel by packing the left&right channels into the real and imaginary
for (k=0;k<NFFT;++k) {
cbuf[k].r = sampsin[2*k];
cbuf[k].i = sampsin[2*k+1];
}
if (removedc) {
float dcr=0,dci=0;
for (k=0;k<NFFT;++k){
dcr += cbuf[k].r;
dci += cbuf[k].i;
}
dcr /= NFFT;
dci /= NFFT;
for (k=0;k<NFFT;++k) {
cbuf[k].r -= dcr;
cbuf[k].i -= dci;
}
}
// perform the fft on the L+R packed buffer
kiss_fft( st , cbuf );
// get the half-symmetric FFTs for the Left and Right Channels
for (k=0;k<NFFT/2;++k) {
int k2 = (NFFT-k)%NFFT;
kiss_fft_cpx r,l;
r.r = (cbuf[k].r + cbuf[k2].r) * 0.5;
r.i = (cbuf[k].i - cbuf[k2].i) * 0.5;
l.r = (cbuf[k].i + cbuf[k2].i) * 0.5;
l.i = (cbuf[k].r - cbuf[k2].r) * 0.5;
rmag2[k] += r.r * r.r + r.i * r.i;
lmag2[k] += l.r * l.r + l.i * l.i;
}
++nbufs;
}
free(st);
for (k=0;k<NFFT/2;++k) {
if (rmag2[peakr] < rmag2[k])
peakr = k;
if (lmag2[peakl] < lmag2[k])
peakl = k;
}
printf("%d buffers\n",nbufs);
printf("peak frequency R:%.3fdB @ %.1f Hz\n",10*log10(rmag2[peakr]) , peakr*fs/NFFT);
printf(" L:%.3fdB @ %.1f Hz\n",10*log10(lmag2[peakl]) , peakl*fs/NFFT);
return 0;
}

View File

@ -1,80 +0,0 @@
/*
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;
}

View File

@ -1,46 +0,0 @@
/*
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;
}

View File

@ -1,9 +1,17 @@
NFFT=840
ifeq "$(NFFT)" ""
NFFT=1024
endif
ALLUTILS=kfft kffts kfftd
NUMFFTS=10000
ifeq "$(NUMFFTS)" ""
NUMFFTS=10000
endif
UTILSRC=../kiss_fft.c fftutil.c
CFLAGS=-Wall -O3 -lm -I.. -ansi -DFUNCDECL= -pedantic
CFLAGS=-Wall -O3 -lm -I.. -ansi -pedantic
all: $(ALLUTILS)
@ -17,7 +25,7 @@ kfftd: $(UTILSRC)
time: all
@echo
@echo "#### timing $(NUMFFTS) x $(NFFT) point FFTs"
@echo -n "#### timing $(NUMFFTS) x $(NFFT) point FFTs. "; factor $(NFFT)
@echo "#### DOUBLE"
@time -f 'Elapsed:%E user:%U sys:%S' \
sh -c 'dd if=/dev/zero bs=$$((16*$(NFFT))) count=$(NUMFFTS) 2>/dev/null |./kfftd>/dev/null'
@ -28,6 +36,14 @@ time: all
@time -f 'Elapsed:%E user:%U sys:%S' \
sh -c 'dd if=/dev/zero bs=$$((4*$(NFFT))) count=$(NUMFFTS) 2>/dev/null |./kffts>/dev/null'
POW2=256 512 1024 2048
POW3=243 729 2187
mtime: all
@for n in $(POW2) ;do \
export NFFT=$$n;make time; \
done
snr: all
@which octave
@echo