test_unit_dft.c 4.7 KB

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