test_unit_dft.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. #define SKIP_CONFIG_H
  28. #ifndef CUSTOM_MODES
  29. #define CUSTOM_MODES
  30. #endif
  31. #include <stdio.h>
  32. #define CELT_C
  33. #define TEST_UNIT_DFT_C
  34. #include "stack_alloc.h"
  35. #include "kiss_fft.h"
  36. #include "kiss_fft.c"
  37. #include "mathops.c"
  38. #include "entcode.c"
  39. #if defined(OPUS_X86_MAY_HAVE_SSE2) || defined(OPUS_X86_MAY_HAVE_SSE4_1)
  40. # include "x86/x86cpu.c"
  41. #elif defined(OPUS_ARM_ASM) || defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  42. # include "arm/armcpu.c"
  43. # include "celt_lpc.c"
  44. # include "pitch.c"
  45. # if defined(OPUS_ARM_MAY_HAVE_NEON_INTR)
  46. # include "arm/celt_neon_intr.c"
  47. # if defined(HAVE_ARM_NE10)
  48. # include "mdct.c"
  49. # include "arm/celt_ne10_fft.c"
  50. # include "arm/celt_ne10_mdct.c"
  51. # endif
  52. # endif
  53. # include "arm/arm_celt_map.c"
  54. #endif
  55. #ifndef M_PI
  56. #define M_PI 3.141592653
  57. #endif
  58. int ret = 0;
  59. void check(kiss_fft_cpx * in,kiss_fft_cpx * out,int nfft,int isinverse)
  60. {
  61. int bin,k;
  62. double errpow=0,sigpow=0, snr;
  63. for (bin=0;bin<nfft;++bin) {
  64. double ansr = 0;
  65. double ansi = 0;
  66. double difr;
  67. double difi;
  68. for (k=0;k<nfft;++k) {
  69. double phase = -2*M_PI*bin*k/nfft;
  70. double re = cos(phase);
  71. double im = sin(phase);
  72. if (isinverse)
  73. im = -im;
  74. if (!isinverse)
  75. {
  76. re /= nfft;
  77. im /= nfft;
  78. }
  79. ansr += in[k].r * re - in[k].i * im;
  80. ansi += in[k].r * im + in[k].i * re;
  81. }
  82. /*printf ("%d %d ", (int)ansr, (int)ansi);*/
  83. difr = ansr - out[bin].r;
  84. difi = ansi - out[bin].i;
  85. errpow += difr*difr + difi*difi;
  86. sigpow += ansr*ansr+ansi*ansi;
  87. }
  88. snr = 10*log10(sigpow/errpow);
  89. printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
  90. if (snr<60) {
  91. printf( "** poor snr: %f ** \n", snr);
  92. ret = 1;
  93. }
  94. }
  95. void test1d(int nfft,int isinverse,int arch)
  96. {
  97. size_t buflen = sizeof(kiss_fft_cpx)*nfft;
  98. kiss_fft_cpx * in = (kiss_fft_cpx*)malloc(buflen);
  99. kiss_fft_cpx * out= (kiss_fft_cpx*)malloc(buflen);
  100. kiss_fft_state *cfg = opus_fft_alloc(nfft,0,0,arch);
  101. int k;
  102. for (k=0;k<nfft;++k) {
  103. in[k].r = (rand() % 32767) - 16384;
  104. in[k].i = (rand() % 32767) - 16384;
  105. }
  106. for (k=0;k<nfft;++k) {
  107. in[k].r *= 32768;
  108. in[k].i *= 32768;
  109. }
  110. if (isinverse)
  111. {
  112. for (k=0;k<nfft;++k) {
  113. in[k].r /= nfft;
  114. in[k].i /= nfft;
  115. }
  116. }
  117. /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
  118. if (isinverse)
  119. opus_ifft(cfg,in,out, arch);
  120. else
  121. opus_fft(cfg,in,out, arch);
  122. /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
  123. check(in,out,nfft,isinverse);
  124. free(in);
  125. free(out);
  126. opus_fft_free(cfg, arch);
  127. }
  128. int main(int argc,char ** argv)
  129. {
  130. ALLOC_STACK;
  131. int arch = opus_select_arch();
  132. if (argc>1) {
  133. int k;
  134. for (k=1;k<argc;++k) {
  135. test1d(atoi(argv[k]),0,arch);
  136. test1d(atoi(argv[k]),1,arch);
  137. }
  138. }else{
  139. test1d(32,0,arch);
  140. test1d(32,1,arch);
  141. test1d(128,0,arch);
  142. test1d(128,1,arch);
  143. test1d(256,0,arch);
  144. test1d(256,1,arch);
  145. #ifndef RADIX_TWO_ONLY
  146. test1d(36,0,arch);
  147. test1d(36,1,arch);
  148. test1d(50,0,arch);
  149. test1d(50,1,arch);
  150. test1d(60,0,arch);
  151. test1d(60,1,arch);
  152. test1d(120,0,arch);
  153. test1d(120,1,arch);
  154. test1d(240,0,arch);
  155. test1d(240,1,arch);
  156. test1d(480,0,arch);
  157. test1d(480,1,arch);
  158. #endif
  159. }
  160. return ret;
  161. }