annotate ext/kissfft/test/test_vs_dft.c @ 409:1f1999b0f577

Bring in kissfft into this repo (formerly a subrepo, but the remote is not responding)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 21 Jul 2015 07:34:15 +0100
parents
children
rev   line source
c@409 1 #include "kiss_fft.h"
c@409 2
c@409 3
c@409 4 void check(kiss_fft_cpx * in,kiss_fft_cpx * out,int nfft,int isinverse)
c@409 5 {
c@409 6 int bin,k;
c@409 7 double errpow=0,sigpow=0;
c@409 8
c@409 9 for (bin=0;bin<nfft;++bin) {
c@409 10 double ansr = 0;
c@409 11 double ansi = 0;
c@409 12 double difr;
c@409 13 double difi;
c@409 14
c@409 15 for (k=0;k<nfft;++k) {
c@409 16 double phase = -2*M_PI*bin*k/nfft;
c@409 17 double re = cos(phase);
c@409 18 double im = sin(phase);
c@409 19 if (isinverse)
c@409 20 im = -im;
c@409 21
c@409 22 #ifdef FIXED_POINT
c@409 23 re /= nfft;
c@409 24 im /= nfft;
c@409 25 #endif
c@409 26
c@409 27 ansr += in[k].r * re - in[k].i * im;
c@409 28 ansi += in[k].r * im + in[k].i * re;
c@409 29 }
c@409 30 difr = ansr - out[bin].r;
c@409 31 difi = ansi - out[bin].i;
c@409 32 errpow += difr*difr + difi*difi;
c@409 33 sigpow += ansr*ansr+ansi*ansi;
c@409 34 }
c@409 35 printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,10*log10(sigpow/errpow) );
c@409 36 }
c@409 37
c@409 38 void test1d(int nfft,int isinverse)
c@409 39 {
c@409 40 size_t buflen = sizeof(kiss_fft_cpx)*nfft;
c@409 41
c@409 42 kiss_fft_cpx * in = (kiss_fft_cpx*)malloc(buflen);
c@409 43 kiss_fft_cpx * out= (kiss_fft_cpx*)malloc(buflen);
c@409 44 kiss_fft_cfg cfg = kiss_fft_alloc(nfft,isinverse,0,0);
c@409 45 int k;
c@409 46
c@409 47 for (k=0;k<nfft;++k) {
c@409 48 in[k].r = (rand() % 65536) - 32768;
c@409 49 in[k].i = (rand() % 65536) - 32768;
c@409 50 }
c@409 51
c@409 52 kiss_fft(cfg,in,out);
c@409 53
c@409 54 check(in,out,nfft,isinverse);
c@409 55
c@409 56 free(in);
c@409 57 free(out);
c@409 58 free(cfg);
c@409 59 }
c@409 60
c@409 61 int main(int argc,char ** argv)
c@409 62 {
c@409 63 if (argc>1) {
c@409 64 int k;
c@409 65 for (k=1;k<argc;++k) {
c@409 66 test1d(atoi(argv[k]),0);
c@409 67 test1d(atoi(argv[k]),1);
c@409 68 }
c@409 69 }else{
c@409 70 test1d(32,0);
c@409 71 test1d(32,1);
c@409 72 }
c@409 73 return 0;
c@409 74 }