kissfft/kiss_fft.c
Mark Borgerding 61571342a5 uses lookup table for twiddle factors
'make test' output: (Elapsed time is inflated, realplayer was running at time)

### testing SNR for  1024 point FFTs
#### DOUBLE
snr_t2f = 295.41
snr_f2t = 307.88
#### FLOAT
snr_t2f = 144.63
snr_f2t = 143.48
#### SHORT
snr_t2f = -30.111
snr_f2t = -61.637

#### timing 10000 x 1024 point FFTs
#### DOUBLE
Elapsed:0:25.19 user:20.22 sys:0.30
#### FLOAT
Elapsed:0:07.16 user:6.00 sys:0.09
#### SHORT
Elapsed:0:05.89 user:4.66 sys:0.11
2003-10-11 13:38:37 +00:00

223 lines
6.2 KiB
C

/*
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 <stdio.h>
#include <math.h>
#include <memory.h>
#include "kiss_fft.h"
/*
* kiss_fft.h
* defines kiss_fft_scalar as either short or a float type
* and defines
* typedef struct {
* kiss_fft_scalar r;
* kiss_fft_scalar i;
* }kiss_fft_cpx;
*/
const double pi=3.14159265358979323846264338327;
#define MAX_STAGES 20
typedef struct {
int nfft;
int inverse;
kiss_fft_cpx * twiddles;
}kiss_fft_state;
#define C_ADD(x,a,b) \
do{ (x).r = (a).r+(b).r;\
(x).i = (a).i+(b).i;}while(0)
#define C_SUB(x,a,b) \
do{ (x).r = (a).r-(b).r;\
(x).i = (a).i-(b).i;}while(0)
#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)
static
kiss_fft_cpx cexp(double phase)
{
kiss_fft_cpx x;
x.r = cos(phase);
x.i = -sin(phase);
return x;
}
static kiss_fft_cpx csub(kiss_fft_cpx a,kiss_fft_cpx b)
{
kiss_fft_cpx c;
C_SUB(c,a,b);
return c;
}
static kiss_fft_cpx cadd(kiss_fft_cpx a,kiss_fft_cpx b)
{
kiss_fft_cpx c;
C_ADD(c,a,b);
return c;
}
static kiss_fft_cpx cmul(kiss_fft_cpx a,kiss_fft_cpx b)
{
kiss_fft_cpx c;
C_MUL(c,a,b);
return c;
}
// the heart of the fft
static
void fft_work(
kiss_fft_cpx * Fout,
const kiss_fft_cpx * f,
int fstride,
int n,
int Norig,
int inverse,
kiss_fft_cpx * scratch,
kiss_fft_cpx * twiddles
)
{
int m,p=0,q,q1,u,k;
kiss_fft_cpx t;
if (n==1) {
*Fout = *f;
return;
}
/*
double two_pi_divN;
two_pi_divN = 2*pi/n;
if (inverse)
two_pi_divN *= -1;
*/
if (n%2 == 0) p=2;
else if(n%3 == 0) p=3;
else if(n%5 == 0) p=5;
else if(n%7 == 0) p=7;
else {
fprintf(stderr,"%d is not divisible by %d\n",n,p);
p=n;
exit(1);
}
m = n/p;
/* n = stage->n; m = stage->m; p = stage->p; */
#ifdef LOUD
printf("n=%d,p=%d\n",n,p);
for (k=0;k<n;++k) {
t=f[k*fstride];
printf("(%.3f,%.3f) ",t.r,t.i);
}
printf(" <<< fin \n");
#endif
for (q=0;q<p;++q) {
#ifdef LOUD
for (k=0;k<m;++k) {
t = (f+q*fstride)[fstride*p*k];
printf("(%.3f,%.3f) ",t.r,t.i);
}
printf(" <<< f[fstride*k+q] \n");
#endif
fft_work( Fout + m*q, f+q*fstride, fstride*p, m,Norig,inverse, scratch ,twiddles);
}
#ifdef LOUD
printf("twiddling n=%d,p=%d\n",n,p);
#endif
for ( u=0; u<m; ++u ) {
for ( q1=0 ; q1<p ; ++q1 ) {
scratch[q1] = Fout[ u+q1*m ];
#ifdef LOUD
printf("(%.3f,%.3f) ",scratch[q1].r,scratch[q1].i);
#endif
}
#ifdef LOUD
printf(" <<< scratch \n");
#endif
for ( q1=0 ; q1<p ; ++q1 ) {
int twidx=0;
k=q1*m+u;
Fout[ k ] = scratch[0];
for (q=1;q<p;++q ) {
twidx += fstride * k;
if (twidx>=Norig)
twidx-=Norig;
t = twiddles[twidx];
/*
kiss_fft_cpx diff,tref;
tref = cexp( two_pi_divN * k * q );
diff = csub(t,tref);
if (
diff.r * diff.r + diff.i*diff.i * 100
>
tref.r * tref.r + tref.i*tref.i)
{
printf("twiddling n=%d,p=%d\n",n,p);
printf("t=(%.3f,%.3f) ",t.r,t.i);
printf("tref=(%.3f,%.3f) ",tref.r,tref.i);
printf("diff=(%.3f,%.3f) \n",diff.r,diff.i);
}
*/
Fout[ k ] = cadd( Fout[ k ] ,
cmul( scratch[q] , t ) );
}
}
}
}
/*
* 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)
{
int i;
kiss_fft_state * st=NULL;
st = ( kiss_fft_state *)malloc( sizeof(kiss_fft_state) );
st->nfft=nfft;
st->inverse = inverse_fft;
st->twiddles = (kiss_fft_cpx*)malloc( sizeof(kiss_fft_cpx)*nfft );
for (i=0;i<nfft;++i) {
double phase = ( 2*pi /nfft ) * i;
if (st->inverse)
phase *= -1;
st->twiddles[i] = cexp( phase );
}
return st;
}
void kiss_fft(const void * cfg,kiss_fft_cpx *f)
{
int i,n;
const kiss_fft_state * st = cfg;
kiss_fft_cpx tmpbuf[1024];
kiss_fft_cpx scratch[1024];
n = st->nfft;
for (i=0;i<n;++i)
tmpbuf[i] = f[i];
fft_work( f, tmpbuf, 1, n,n, st->inverse, scratch ,st->twiddles);
}