mirror of
https://github.com/mborgerding/kissfft.git
synced 2025-05-27 21:20:27 -04:00
'make test' output: ### testing SNR for 1024 point FFTs #### DOUBLE snr_t2f = 296.78 snr_f2t = 317.11 #### FLOAT snr_t2f = 145.28 snr_f2t = 143.51 #### SHORT snr_t2f = 52.409 snr_f2t = 22.174 #### timing 10000 x 1024 point FFTs #### DOUBLE Elapsed:0:03.43 user:2.68 sys:0.25 #### FLOAT Elapsed:0:01.39 user:1.08 sys:0.11 #### SHORT Elapsed:0:02.01 user:1.39 sys:0.09
419 lines
11 KiB
C
419 lines
11 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;
|
|
*/
|
|
|
|
typedef struct {
|
|
int nfft;
|
|
int inverse;
|
|
int *factors;
|
|
kiss_fft_cpx * twiddles;
|
|
kiss_fft_cpx * tmpbuf;
|
|
kiss_fft_cpx * scratch;
|
|
}kiss_fft_state;
|
|
|
|
#ifdef FIXED_POINT
|
|
/*
|
|
#define C_SUB( res, a,b)\
|
|
do { (res).r=((a).r-b.r+1)>>1; (res).i=((a).i-b.i+1)>>1; }while(0)
|
|
#define C_ADDTO( res , a)\
|
|
do { (res).r=((res).r+(a).r+1)>>1; (res).i=((res).i+(a).i+1)>>1; }while(0)
|
|
#define C_SUBFROM( res , a)\
|
|
do { (res).r=((res).r-(a).r+1)>>1; (res).i=((res).i-(a).i+1)>>1; }while(0)
|
|
*/
|
|
/* We don't have to worry about overflow from multiplying by twiddle factors since they
|
|
* all have unity magnitude. Still need to shift away fractional bits after adding 1/2 for
|
|
* rounding. */
|
|
# define C_MUL(m,a,b) \
|
|
do{ (m).r = ( ( (a).r*(b).r - (a).i*(b).i) + (1<<14) ) >> 15;\
|
|
(m).i = ( ( (a).r*(b).i + (a).i*(b).r) + (1<<14) ) >> 15;\
|
|
}while(0)
|
|
#else // not FIXED_POINT
|
|
|
|
#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_SUB( res, a,b)\
|
|
do { (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; }while(0)
|
|
#define C_ADDTO( res , a)\
|
|
do { (res).r += (a).r; (res).i += (a).i; }while(0)
|
|
#define C_SUBFROM( res , a)\
|
|
do { (res).r -= (a).r; (res).i -= (a).i; }while(0)
|
|
|
|
static
|
|
kiss_fft_cpx cexp(double phase)
|
|
{
|
|
kiss_fft_cpx x;
|
|
#ifdef FIXED_POINT
|
|
x.r = (kiss_fft_scalar) ( 32767*cos(phase) );
|
|
x.i = (kiss_fft_scalar) ( -32767*sin(phase) );
|
|
#else
|
|
x.r = cos(phase);
|
|
x.i = -sin(phase);
|
|
#endif
|
|
return x;
|
|
}
|
|
|
|
void printcpx(const char * desc,kiss_fft_cpx c)
|
|
{
|
|
printf("%s = (%e,%e)\n",desc,(double)c.r,(double)c.i);
|
|
}
|
|
|
|
static
|
|
inline
|
|
kiss_fft_cpx crot(kiss_fft_cpx c, int numquads)
|
|
{
|
|
kiss_fft_cpx out;
|
|
switch (numquads&3) {
|
|
case 0:
|
|
out.r = c.r;
|
|
out.i = c.i;
|
|
break;
|
|
case 1:
|
|
out.r = c.i;
|
|
out.i = -c.r;
|
|
break;
|
|
case 2:
|
|
out.r = -c.r;
|
|
out.i = -c.i;
|
|
break;
|
|
case 3:
|
|
out.r = -c.i;
|
|
out.i = c.r;
|
|
break;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
#define C_ROTADDTO(sum,c,q) \
|
|
do{\
|
|
switch ((q)&3) {\
|
|
case 0:\
|
|
(sum).r += (c).r;\
|
|
(sum).i += (c).i;\
|
|
break;\
|
|
case 1:\
|
|
(sum).r += (c).i;\
|
|
(sum).i -= (c).r;\
|
|
break;\
|
|
case 2:\
|
|
(sum).r -= (c).r;\
|
|
(sum).i -= (c).i;\
|
|
break;\
|
|
case 3:\
|
|
(sum).r -= (c).i;\
|
|
(sum).i += (c).r;\
|
|
break;\
|
|
}\
|
|
}while(0)
|
|
|
|
|
|
static
|
|
void fft_work(
|
|
kiss_fft_cpx * Fout,
|
|
const kiss_fft_cpx * f,
|
|
int fstride,
|
|
int * factors,
|
|
const kiss_fft_state * st
|
|
);
|
|
|
|
static
|
|
void fft4work(
|
|
kiss_fft_cpx * Fout,
|
|
const kiss_fft_cpx * f,
|
|
int fstride,
|
|
int * factors,
|
|
const kiss_fft_state * st
|
|
)
|
|
{
|
|
kiss_fft_cpx * Fout1;
|
|
kiss_fft_cpx * Fout2;
|
|
kiss_fft_cpx * Fout3;
|
|
int m,u;
|
|
kiss_fft_cpx * scratch = st->scratch;
|
|
kiss_fft_cpx * twiddles = st->twiddles;
|
|
int phase_dir = st->inverse ? -1 : 1;
|
|
|
|
factors++;
|
|
m=*factors++;
|
|
|
|
Fout1 = Fout + m;
|
|
Fout2 = Fout + 2*m;
|
|
Fout3 = Fout + 3*m;
|
|
|
|
if (m==1) {
|
|
Fout[0] = f[0];
|
|
Fout[1] = f[fstride*1];
|
|
Fout[2] = f[fstride*2];
|
|
Fout[3] = f[fstride*3];
|
|
}else{
|
|
fft_work( Fout , f, fstride*4,factors,st);
|
|
fft_work( Fout1 ,f+fstride, fstride*4,factors,st);
|
|
fft_work( Fout2 ,f+fstride*2, fstride*4,factors,st);
|
|
fft_work( Fout3 ,f+fstride*3, fstride*4,factors,st);
|
|
}
|
|
|
|
for ( u=0; u<m; ++u ) {
|
|
kiss_fft_cpx t1,t2,t3;
|
|
scratch[1]= *Fout1;
|
|
scratch[2]= *Fout2;
|
|
scratch[3]= *Fout3;
|
|
|
|
#ifdef FIXED_POINT
|
|
Fout->r >>=2; Fout->i >>=2;
|
|
scratch[1].r >>=2; scratch[1].i >>=2;
|
|
scratch[2].r >>=2; scratch[2].i >>=2;
|
|
scratch[3].r >>=2; scratch[3].i >>=2;
|
|
#endif
|
|
*Fout3 = *Fout2 = *Fout1 = *Fout;
|
|
|
|
// start loop
|
|
C_MUL(t1,scratch[1] , twiddles[fstride * u * 1] );
|
|
C_MUL(t2,scratch[2] , twiddles[fstride * u * 2] );
|
|
C_MUL(t3,scratch[3] , twiddles[fstride * u * 3] );
|
|
|
|
C_ADDTO(*Fout,t1);
|
|
C_ADDTO(*Fout,t2);
|
|
C_ADDTO(*Fout,t3);
|
|
C_SUBFROM(*Fout2,t1);
|
|
C_SUBFROM(*Fout2,t3);
|
|
C_ADDTO(*Fout2,t2);
|
|
C_SUBFROM(*Fout3,t2);
|
|
C_SUBFROM(*Fout1,t2);
|
|
|
|
if(phase_dir==-1) {
|
|
C_ROTADDTO(*Fout1,t1,3);
|
|
C_ROTADDTO(*Fout1,t3,1);
|
|
C_ROTADDTO(*Fout3,t1,1);
|
|
C_ROTADDTO(*Fout3,t3,3);
|
|
}else{
|
|
C_ROTADDTO(*Fout1,t1,1);
|
|
C_ROTADDTO(*Fout1,t3,3);
|
|
C_ROTADDTO(*Fout3,t1,3);
|
|
C_ROTADDTO(*Fout3,t3,1);
|
|
}
|
|
++Fout; ++Fout1; ++Fout2; ++Fout3;
|
|
}
|
|
|
|
}
|
|
// the heart of the fft
|
|
static
|
|
void fft2work(
|
|
kiss_fft_cpx * Fout,
|
|
const kiss_fft_cpx * f,
|
|
int fstride,
|
|
int * factors,
|
|
const kiss_fft_state * st
|
|
)
|
|
{
|
|
int m;
|
|
kiss_fft_cpx * Fout2;
|
|
|
|
factors++;
|
|
m=*factors++;
|
|
Fout2 = Fout + m;
|
|
|
|
if (m==1) {
|
|
Fout[0] = f[0];
|
|
Fout[1] = f[fstride];
|
|
} else if (*factors == 2) {
|
|
fft2work(Fout,f,fstride*2,factors,st);
|
|
fft2work(Fout2,f+fstride,fstride*2,factors,st);
|
|
} else {
|
|
fft_work( Fout , f, fstride*2,factors,st);
|
|
fft_work( Fout + m, f+fstride, fstride*2,factors,st);
|
|
}
|
|
|
|
{
|
|
kiss_fft_cpx * twiddles = st->twiddles;
|
|
kiss_fft_cpx t;
|
|
do{
|
|
C_MUL (t, *Fout2 , *twiddles);
|
|
twiddles += fstride;
|
|
C_SUB( *Fout2 , *Fout , t );
|
|
C_ADDTO( *Fout , t );
|
|
++Fout2;
|
|
++Fout;
|
|
}while (--m);
|
|
}
|
|
}
|
|
|
|
|
|
static
|
|
void fft_work(
|
|
kiss_fft_cpx * Fout,
|
|
const kiss_fft_cpx * f,
|
|
int fstride,
|
|
int * factors,
|
|
const kiss_fft_state * st
|
|
)
|
|
{
|
|
int m,p,q,q1,u,k;
|
|
kiss_fft_cpx t;
|
|
kiss_fft_cpx * scratch = st->scratch;
|
|
kiss_fft_cpx * twiddles = st->twiddles;
|
|
|
|
#if 1
|
|
switch (*factors) {
|
|
case 4: fft4work(Fout,f,fstride,factors,st); return;
|
|
case 2: fft2work(Fout,f,fstride,factors,st); return;
|
|
default:
|
|
break;
|
|
}
|
|
#endif
|
|
p=*factors++;
|
|
m=*factors++;
|
|
|
|
for (q=0;q<p;++q) {
|
|
if (m==1)
|
|
Fout[q] = *f;
|
|
else
|
|
fft_work( Fout + m*q, f, fstride*p,factors,st);
|
|
f+= fstride;
|
|
}
|
|
|
|
for ( u=0; u<m; ++u ) {
|
|
k=u;
|
|
for ( q1=0 ; q1<p ; ++q1 ) {
|
|
scratch[q1] = Fout[ k ];
|
|
#ifdef FIXED_POINT
|
|
scratch[q1].r >>= 1;
|
|
scratch[q1].i >>= 1;
|
|
#endif
|
|
k += m;
|
|
}
|
|
|
|
k=u;
|
|
for ( q1=0 ; q1<p ; ++q1 ) {
|
|
int twidx=3;
|
|
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] );
|
|
Fout[ k ].r += t.r;
|
|
Fout[ k ].i += t.i;
|
|
}
|
|
k += m;
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
* 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 allocsize;
|
|
int nstages=0;
|
|
int i;
|
|
kiss_fft_state * st=NULL;
|
|
|
|
allocsize = sizeof(kiss_fft_state)
|
|
+ sizeof(kiss_fft_cpx)*nfft // twiddle factors
|
|
+ sizeof(kiss_fft_cpx)*nfft // tmpbuf
|
|
+ sizeof(int)*nfft // factors
|
|
+ sizeof(kiss_fft_cpx)*nfft; // scratch
|
|
|
|
st = ( kiss_fft_state *)malloc( allocsize );
|
|
if (!st)
|
|
return NULL;
|
|
|
|
st->nfft=nfft;
|
|
st->inverse = inverse_fft;
|
|
st->twiddles = (kiss_fft_cpx*)(st+1); // just beyond struct
|
|
st->tmpbuf = (kiss_fft_cpx*)(st->twiddles + nfft);// just after twiddles
|
|
st->scratch = (kiss_fft_cpx*)(st->tmpbuf + nfft);
|
|
st->factors = (int*)(st->scratch + nfft); // just after tmpbuf
|
|
|
|
|
|
for (i=0;i<nfft;++i) {
|
|
const double pi=3.14159265358979323846264338327;
|
|
double phase = ( 2*pi /nfft ) * i;
|
|
if (st->inverse)
|
|
phase *= -1;
|
|
st->twiddles[i] = cexp( phase );
|
|
}
|
|
|
|
while (nfft>1) {
|
|
const int primes[] = {4,2,3,5,7,11,13,17,-1};
|
|
int p=nfft;
|
|
i=0;
|
|
while ( primes[i] != -1 ) {
|
|
if ( nfft % primes[i] == 0){
|
|
p = primes[i];
|
|
break;
|
|
}
|
|
++i;
|
|
}
|
|
st->factors[2*nstages] = p;
|
|
nfft /= p;
|
|
st->factors[2*nstages+1] = nfft;
|
|
|
|
//printf("%d,%d ",p,nfft);
|
|
|
|
++nstages;
|
|
}
|
|
//printf("\n");
|
|
|
|
// reverse the factors list so that the 2s are packed to the back
|
|
nfft=st->nfft;
|
|
for ( i=0 ; i< nstages ;i+=2 ) {
|
|
int p;
|
|
p = st->factors[i];
|
|
st->factors[i] = st->factors[ 2*nstages-i-2];
|
|
st->factors[2*nstages-i-2 ] = p;
|
|
}
|
|
|
|
for ( i=0 ; i< nstages*2 ;i+=2 ) {
|
|
nfft /= st->factors[i];
|
|
st->factors[i+1] = nfft;
|
|
//printf("%d,%d ",st->factors[i], st->factors[i+1] );
|
|
}
|
|
|
|
memset(st->tmpbuf,0,sizeof(kiss_fft_cpx)*nfft);
|
|
return st;
|
|
}
|
|
|
|
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);
|
|
fft_work( f, st->tmpbuf, 1, st->factors,st );
|
|
}
|