|
| 1 | +/* Copyright (c) 2008 Xiph.Org Foundation |
| 2 | + Written by Jean-Marc Valin */ |
| 3 | +/* |
| 4 | + Redistribution and use in source and binary forms, with or without |
| 5 | + modification, are permitted provided that the following conditions |
| 6 | + are met: |
| 7 | +
|
| 8 | + - Redistributions of source code must retain the above copyright |
| 9 | + notice, this list of conditions and the following disclaimer. |
| 10 | +
|
| 11 | + - Redistributions in binary form must reproduce the above copyright |
| 12 | + notice, this list of conditions and the following disclaimer in the |
| 13 | + documentation and/or other materials provided with the distribution. |
| 14 | +
|
| 15 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 16 | + ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 17 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 18 | + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER |
| 19 | + OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 20 | + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 21 | + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 22 | + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 23 | + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 24 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | +*/ |
| 27 | + |
| 28 | +#ifdef HAVE_CONFIG_H |
| 29 | +#include "config.h" |
| 30 | +#endif |
| 31 | + |
| 32 | +#include <stdio.h> |
| 33 | + |
| 34 | +#include "mini_kfft.c" |
| 35 | + |
| 36 | +#ifndef M_PI |
| 37 | +#define M_PI 3.141592653 |
| 38 | +#endif |
| 39 | + |
| 40 | +int ret = 0; |
| 41 | + |
| 42 | +void check(mini_kiss_fft_cpx * in,mini_kiss_fft_cpx * out,int nfft,int isinverse) |
| 43 | +{ |
| 44 | + int bin,k; |
| 45 | + double errpow=0,sigpow=0, snr; |
| 46 | + |
| 47 | + for (bin=0;bin<nfft;++bin) { |
| 48 | + double ansr = 0; |
| 49 | + double ansi = 0; |
| 50 | + double difr; |
| 51 | + double difi; |
| 52 | + |
| 53 | + for (k=0;k<nfft;++k) { |
| 54 | + double phase = -2*M_PI*bin*k/nfft; |
| 55 | + double re = cos(phase); |
| 56 | + double im = sin(phase); |
| 57 | + if (isinverse) |
| 58 | + im = -im; |
| 59 | + |
| 60 | + if (0&&isinverse) |
| 61 | + { |
| 62 | + re /= nfft; |
| 63 | + im /= nfft; |
| 64 | + } |
| 65 | + |
| 66 | + ansr += in[k].r * re - in[k].i * im; |
| 67 | + ansi += in[k].r * im + in[k].i * re; |
| 68 | + } |
| 69 | + /*printf ("%d %d ", (int)ansr, (int)ansi);*/ |
| 70 | + difr = ansr - out[bin].r; |
| 71 | + difi = ansi - out[bin].i; |
| 72 | + errpow += difr*difr + difi*difi; |
| 73 | + sigpow += ansr*ansr+ansi*ansi; |
| 74 | + } |
| 75 | + snr = 10*log10(sigpow/errpow); |
| 76 | + printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr ); |
| 77 | + if (snr<60) { |
| 78 | + printf( "** poor snr: %f ** \n", snr); |
| 79 | + ret = 1; |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +void test1d(int nfft,int isinverse) |
| 84 | +{ |
| 85 | + size_t buflen = sizeof(mini_kiss_fft_cpx)*nfft; |
| 86 | + mini_kiss_fft_cpx *in; |
| 87 | + mini_kiss_fft_cpx *out; |
| 88 | + int k; |
| 89 | + mini_kiss_fft_state *fft; |
| 90 | + mini_kiss_fft_state *ifft; |
| 91 | + fft = mini_kiss_fft_alloc(nfft,0,NULL,NULL); |
| 92 | + ifft = mini_kiss_fft_alloc(nfft,1,NULL,NULL); |
| 93 | + |
| 94 | + in = (mini_kiss_fft_cpx*)malloc(buflen); |
| 95 | + out = (mini_kiss_fft_cpx*)malloc(buflen); |
| 96 | + |
| 97 | + for (k=0;k<nfft;++k) { |
| 98 | + in[k].r = (rand() % 32767) - 16384; |
| 99 | + in[k].i = (rand() % 32767) - 16384; |
| 100 | + } |
| 101 | + |
| 102 | + for (k=0;k<nfft;++k) { |
| 103 | + in[k].r *= 32768; |
| 104 | + in[k].i *= 32768; |
| 105 | + } |
| 106 | + |
| 107 | + if (isinverse) |
| 108 | + { |
| 109 | + for (k=0;k<nfft;++k) { |
| 110 | + in[k].r /= nfft; |
| 111 | + in[k].i /= nfft; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/ |
| 116 | + |
| 117 | + if (isinverse) |
| 118 | + mini_kiss_fft(ifft,in,out); |
| 119 | + else |
| 120 | + mini_kiss_fft(fft,in,out); |
| 121 | + |
| 122 | + /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/ |
| 123 | + |
| 124 | + check(in,out,nfft,isinverse); |
| 125 | + |
| 126 | + free(in); |
| 127 | + free(out); |
| 128 | + free(fft); |
| 129 | + free(ifft); |
| 130 | +} |
| 131 | + |
| 132 | +int main(int argc,char ** argv) |
| 133 | +{ |
| 134 | + if (argc>1) { |
| 135 | + int k; |
| 136 | + for (k=1;k<argc;++k) { |
| 137 | + test1d(atoi(argv[k]),0); |
| 138 | + test1d(atoi(argv[k]),1); |
| 139 | + } |
| 140 | + }else{ |
| 141 | + test1d(32,0); |
| 142 | + test1d(32,1); |
| 143 | + test1d(128,0); |
| 144 | + test1d(128,1); |
| 145 | + test1d(256,0); |
| 146 | + test1d(256,1); |
| 147 | + test1d(36,0); |
| 148 | + test1d(36,1); |
| 149 | + test1d(50,0); |
| 150 | + test1d(50,1); |
| 151 | + test1d(60,0); |
| 152 | + test1d(60,1); |
| 153 | + test1d(120,0); |
| 154 | + test1d(120,1); |
| 155 | + test1d(240,0); |
| 156 | + test1d(240,1); |
| 157 | + test1d(480,0); |
| 158 | + test1d(480,1); |
| 159 | + } |
| 160 | + return ret; |
| 161 | +} |
0 commit comments