mirror of
https://github.com/mborgerding/kissfft.git
synced 2025-05-27 21:20:27 -04:00
Fixed point sums are divided by 2 each stage. This will never overflow for radix 2 ffts. For mixed radix, it may overflow, but will usually give better SNR. 'make test' output: ### testing SNR for 1024 point FFTs #### DOUBLE snr_t2f = 295.30 snr_f2t = 308.25 #### FLOAT snr_t2f = 146.92 snr_f2t = 143.25 #### SHORT snr_t2f = 54.645 snr_f2t = 24.677 #### timing 10000 x 1024 point FFTs #### DOUBLE Elapsed:0:25.96 user:19.77 sys:0.22 #### FLOAT Elapsed:0:06.62 user:5.48 sys:0.11 #### SHORT Elapsed:0:06.01 user:4.75 sys:0.12
221 lines
6.3 KiB
C
221 lines
6.3 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;
|
|
int *factors;
|
|
kiss_fft_cpx * twiddles;
|
|
kiss_fft_cpx * tmpbuf;
|
|
kiss_fft_cpx * scratch;
|
|
}kiss_fft_state;
|
|
|
|
#ifdef FIXED_POINT
|
|
|
|
# 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)
|
|
/* 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_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)
|
|
#endif
|
|
|
|
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;
|
|
}
|
|
|
|
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 * factors
|
|
)
|
|
{
|
|
int m,p=0,q,q1,u,k;
|
|
kiss_fft_cpx t;
|
|
|
|
if (n==1) {
|
|
*Fout = *f;
|
|
return;
|
|
}
|
|
p=*factors++;
|
|
m=*factors++;//m = n/p;
|
|
|
|
for (q=0;q<p;++q) {
|
|
fft_work( Fout + m*q, f+q*fstride, fstride*p, m,Norig,inverse, scratch ,twiddles,factors);
|
|
}
|
|
|
|
for ( u=0; u<m; ++u ) {
|
|
for ( q1=0 ; q1<p ; ++q1 ) {
|
|
#ifdef FIXED_POINT
|
|
scratch[q1].r = Fout[ u+q1*m ].r>>1;
|
|
scratch[q1].i = Fout[ u+q1*m ].i>>1;
|
|
#else
|
|
scratch[q1] = Fout[ u+q1*m ];
|
|
#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];
|
|
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 nstages=0;
|
|
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 );
|
|
st->tmpbuf = (kiss_fft_cpx*)malloc( sizeof(kiss_fft_cpx)*nfft );
|
|
st->scratch = (kiss_fft_cpx*)malloc( sizeof(kiss_fft_cpx)*nfft );
|
|
|
|
st->factors = (int*)malloc( sizeof(int)*nfft );
|
|
|
|
for (i=0;i<nfft;++i) {
|
|
double phase = ( 2*pi /nfft ) * i;
|
|
if (st->inverse)
|
|
phase *= -1;
|
|
st->twiddles[i] = cexp( phase );
|
|
}
|
|
|
|
while (nfft>1) {
|
|
const int primes[] = {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;
|
|
}
|
|
}
|
|
st->factors[2*nstages] = p;
|
|
nfft /= p;
|
|
st->factors[2*nstages+1] = nfft;
|
|
++nstages;
|
|
}
|
|
|
|
return st;
|
|
}
|
|
|
|
void kiss_fft(const void * cfg,kiss_fft_cpx *f)
|
|
{
|
|
int i,n;
|
|
const kiss_fft_state * st = cfg;
|
|
|
|
n = st->nfft;
|
|
|
|
for (i=0;i<n;++i)
|
|
st->tmpbuf[i] = f[i];
|
|
|
|
fft_work( f, st->tmpbuf, 1, n,n, st->inverse, st->scratch ,st->twiddles,st->factors);
|
|
}
|