shun_iwasawa a35b8f
#include "kiss_fftr.h"
shun_iwasawa a35b8f
#include "_kiss_fft_guts.h"
shun_iwasawa a35b8f
#include <sys times.h=""></sys>
shun_iwasawa a35b8f
#include <time.h></time.h>
shun_iwasawa a35b8f
#include <unistd.h></unistd.h>
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static double cputime(void)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    struct tms t;
shun_iwasawa a35b8f
    times(&t);
shun_iwasawa a35b8f
    return (double)(t.tms_utime + t.tms_stime)/  sysconf(_SC_CLK_TCK) ;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static
shun_iwasawa a35b8f
kiss_fft_scalar rand_scalar(void) 
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
#ifdef USE_SIMD
shun_iwasawa a35b8f
    return _mm_set1_ps(rand()-RAND_MAX/2);
shun_iwasawa a35b8f
#else
shun_iwasawa a35b8f
    kiss_fft_scalar s = (kiss_fft_scalar)(rand() -RAND_MAX/2);
shun_iwasawa a35b8f
    return s/2;
shun_iwasawa a35b8f
#endif
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
static
shun_iwasawa a35b8f
double snr_compare( kiss_fft_cpx * vec1,kiss_fft_cpx * vec2, int n)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int k;
shun_iwasawa a35b8f
    double sigpow=1e-10,noisepow=1e-10,err,snr,scale=0;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
#ifdef USE_SIMD
shun_iwasawa a35b8f
    float *fv1 = (float*)vec1;
shun_iwasawa a35b8f
    float *fv2 = (float*)vec2;
shun_iwasawa a35b8f
    for (k=0;k<8*n;++k) {
shun_iwasawa a35b8f
        sigpow += *fv1 * *fv1;
shun_iwasawa a35b8f
        err = *fv1 - *fv2;
shun_iwasawa a35b8f
        noisepow += err*err;
shun_iwasawa a35b8f
        ++fv1;
shun_iwasawa a35b8f
        ++fv2;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
#else
shun_iwasawa a35b8f
    for (k=0;k
shun_iwasawa a35b8f
        sigpow += (double)vec1[k].r * (double)vec1[k].r + 
shun_iwasawa a35b8f
                  (double)vec1[k].i * (double)vec1[k].i;
shun_iwasawa a35b8f
        err = (double)vec1[k].r - (double)vec2[k].r;
shun_iwasawa a35b8f
        noisepow += err * err;
shun_iwasawa a35b8f
        err = (double)vec1[k].i - (double)vec2[k].i;
shun_iwasawa a35b8f
        noisepow += err * err;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
        if (vec1[k].r)
shun_iwasawa a35b8f
            scale +=(double) vec2[k].r / (double)vec1[k].r;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
#endif    
shun_iwasawa a35b8f
    snr = 10*log10( sigpow / noisepow );
shun_iwasawa a35b8f
    scale /= n;
shun_iwasawa a35b8f
    if (snr<10) {
shun_iwasawa a35b8f
        printf( "\npoor snr, try a scaling factor %f\n" , scale );
shun_iwasawa a35b8f
        exit(1);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    return snr;
shun_iwasawa a35b8f
}
shun_iwasawa a35b8f
shun_iwasawa a35b8f
#ifndef NUMFFTS
shun_iwasawa a35b8f
#define NUMFFTS 10000
shun_iwasawa a35b8f
#endif
shun_iwasawa a35b8f
shun_iwasawa a35b8f
shun_iwasawa a35b8f
int main(int argc,char ** argv)
shun_iwasawa a35b8f
{
shun_iwasawa a35b8f
    int nfft = 8*3*5;
shun_iwasawa a35b8f
    double ts,tfft,trfft;
shun_iwasawa a35b8f
    int i;
shun_iwasawa a35b8f
    if (argc>1)
shun_iwasawa a35b8f
        nfft = atoi(argv[1]);
shun_iwasawa a35b8f
    kiss_fft_cpx cin[nfft];
shun_iwasawa a35b8f
    kiss_fft_cpx cout[nfft];
shun_iwasawa a35b8f
    kiss_fft_cpx sout[nfft];
shun_iwasawa a35b8f
    kiss_fft_cfg  kiss_fft_state;
shun_iwasawa a35b8f
    kiss_fftr_cfg  kiss_fftr_state;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    kiss_fft_scalar rin[nfft+2];
shun_iwasawa a35b8f
    kiss_fft_scalar rout[nfft+2];
shun_iwasawa a35b8f
    kiss_fft_scalar zero;
shun_iwasawa a35b8f
    memset(&zero,0,sizeof(zero) ); // ugly way of setting short,int,float,double, or __m128 to zero
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    srand(time(0));
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    for (i=0;i
shun_iwasawa a35b8f
        rin[i] = rand_scalar();
shun_iwasawa a35b8f
        cin[i].r = rin[i];
shun_iwasawa a35b8f
        cin[i].i = zero;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    kiss_fft_state = kiss_fft_alloc(nfft,0,0,0);
shun_iwasawa a35b8f
    kiss_fftr_state = kiss_fftr_alloc(nfft,0,0,0);
shun_iwasawa a35b8f
    kiss_fft(kiss_fft_state,cin,cout);
shun_iwasawa a35b8f
    kiss_fftr(kiss_fftr_state,rin,sout);
shun_iwasawa a35b8f
    /*
shun_iwasawa a35b8f
    printf(" results from kiss_fft : (%f,%f), (%f,%f), (%f,%f) ...\n "
shun_iwasawa a35b8f
            , (float)cout[0].r , (float)cout[0].i
shun_iwasawa a35b8f
            , (float)cout[1].r , (float)cout[1].i
shun_iwasawa a35b8f
            , (float)cout[2].r , (float)cout[2].i); 
shun_iwasawa a35b8f
    printf(" results from kiss_fftr: (%f,%f), (%f,%f), (%f,%f) ...\n "
shun_iwasawa a35b8f
            , (float)sout[0].r , (float)sout[0].i
shun_iwasawa a35b8f
            , (float)sout[1].r , (float)sout[1].i
shun_iwasawa a35b8f
            , (float)sout[2].r , (float)sout[2].i); 
shun_iwasawa a35b8f
    */
shun_iwasawa a35b8f
        
shun_iwasawa a35b8f
    printf( "nfft=%d, inverse=%d, snr=%g\n",
shun_iwasawa a35b8f
            nfft,0, snr_compare(cout,sout,(nfft/2)+1) );
shun_iwasawa a35b8f
    ts = cputime();
shun_iwasawa a35b8f
    for (i=0;i
shun_iwasawa a35b8f
        kiss_fft(kiss_fft_state,cin,cout);
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    tfft = cputime() - ts;
shun_iwasawa a35b8f
    
shun_iwasawa a35b8f
    ts = cputime();
shun_iwasawa a35b8f
    for (i=0;i
shun_iwasawa a35b8f
        kiss_fftr( kiss_fftr_state, rin, cout );
shun_iwasawa a35b8f
        /* kiss_fftri(kiss_fftr_state,cout,rin); */
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
    trfft = cputime() - ts;
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    printf("%d complex ffts took %gs, real took %gs\n",NUMFFTS,tfft,trfft);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    free(kiss_fft_state);
shun_iwasawa a35b8f
    free(kiss_fftr_state);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    kiss_fft_state = kiss_fft_alloc(nfft,1,0,0);
shun_iwasawa a35b8f
    kiss_fftr_state = kiss_fftr_alloc(nfft,1,0,0);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    memset(cin,0,sizeof(cin));
shun_iwasawa a35b8f
#if 1
shun_iwasawa a35b8f
    for (i=1;i< nfft/2;++i) {
shun_iwasawa a35b8f
        //cin[i].r = (kiss_fft_scalar)(rand()-RAND_MAX/2);
shun_iwasawa a35b8f
        cin[i].r = rand_scalar();
shun_iwasawa a35b8f
        cin[i].i = rand_scalar();
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
#else
shun_iwasawa a35b8f
    cin[0].r = 12000;
shun_iwasawa a35b8f
    cin[3].r = 12000;
shun_iwasawa a35b8f
    cin[nfft/2].r = 12000;
shun_iwasawa a35b8f
#endif
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    // conjugate symmetry of real signal 
shun_iwasawa a35b8f
    for (i=1;i< nfft/2;++i) {
shun_iwasawa a35b8f
        cin[nfft-i].r = cin[i].r;
shun_iwasawa a35b8f
        cin[nfft-i].i = - cin[i].i;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    kiss_fft(kiss_fft_state,cin,cout);
shun_iwasawa a35b8f
    kiss_fftri(kiss_fftr_state,cin,rout);
shun_iwasawa a35b8f
    /*
shun_iwasawa a35b8f
    printf(" results from inverse kiss_fft : (%f,%f), (%f,%f), (%f,%f), (%f,%f), (%f,%f) ...\n "
shun_iwasawa a35b8f
            , (float)cout[0].r , (float)cout[0].i , (float)cout[1].r , (float)cout[1].i , (float)cout[2].r , (float)cout[2].i , (float)cout[3].r , (float)cout[3].i , (float)cout[4].r , (float)cout[4].i
shun_iwasawa a35b8f
            ); 
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    printf(" results from inverse kiss_fftr: %f,%f,%f,%f,%f ... \n"
shun_iwasawa a35b8f
            ,(float)rout[0] ,(float)rout[1] ,(float)rout[2] ,(float)rout[3] ,(float)rout[4]);
shun_iwasawa a35b8f
*/
shun_iwasawa a35b8f
    for (i=0;i
shun_iwasawa a35b8f
        sout[i].r = rout[i];
shun_iwasawa a35b8f
        sout[i].i = zero;
shun_iwasawa a35b8f
    }
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    printf( "nfft=%d, inverse=%d, snr=%g\n",
shun_iwasawa a35b8f
            nfft,1, snr_compare(cout,sout,nfft/2) );
shun_iwasawa a35b8f
    free(kiss_fft_state);
shun_iwasawa a35b8f
    free(kiss_fftr_state);
shun_iwasawa a35b8f
shun_iwasawa a35b8f
    return 0;
shun_iwasawa a35b8f
}