test_unit_mdct.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /* Copyright (c) 2008-2011 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. #include "mdct.h"
  34. #include "stack_alloc.h"
  35. #include "kiss_fft.c"
  36. #include "mdct.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 "pitch.c"
  44. # include "celt_lpc.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 "arm/celt_ne10_fft.c"
  49. # include "arm/celt_ne10_mdct.c"
  50. # endif
  51. # endif
  52. # include "arm/arm_celt_map.c"
  53. #endif
  54. #ifndef M_PI
  55. #define M_PI 3.141592653
  56. #endif
  57. int ret = 0;
  58. void check(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
  59. {
  60. int bin,k;
  61. double errpow=0,sigpow=0;
  62. double snr;
  63. for (bin=0;bin<nfft/2;++bin) {
  64. double ansr = 0;
  65. double difr;
  66. for (k=0;k<nfft;++k) {
  67. double phase = 2*M_PI*(k+.5+.25*nfft)*(bin+.5)/nfft;
  68. double re = cos(phase);
  69. re /= nfft/4;
  70. ansr += in[k] * re;
  71. }
  72. /*printf ("%f %f\n", ansr, out[bin]);*/
  73. difr = ansr - out[bin];
  74. errpow += difr*difr;
  75. sigpow += ansr*ansr;
  76. }
  77. snr = 10*log10(sigpow/errpow);
  78. printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
  79. if (snr<60) {
  80. printf( "** poor snr: %f **\n", snr);
  81. ret = 1;
  82. }
  83. }
  84. void check_inv(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
  85. {
  86. int bin,k;
  87. double errpow=0,sigpow=0;
  88. double snr;
  89. for (bin=0;bin<nfft;++bin) {
  90. double ansr = 0;
  91. double difr;
  92. for (k=0;k<nfft/2;++k) {
  93. double phase = 2*M_PI*(bin+.5+.25*nfft)*(k+.5)/nfft;
  94. double re = cos(phase);
  95. /*re *= 2;*/
  96. ansr += in[k] * re;
  97. }
  98. /*printf ("%f %f\n", ansr, out[bin]);*/
  99. difr = ansr - out[bin];
  100. errpow += difr*difr;
  101. sigpow += ansr*ansr;
  102. }
  103. snr = 10*log10(sigpow/errpow);
  104. printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
  105. if (snr<60) {
  106. printf( "** poor snr: %f **\n", snr);
  107. ret = 1;
  108. }
  109. }
  110. void test1d(int nfft,int isinverse,int arch)
  111. {
  112. mdct_lookup cfg;
  113. size_t buflen = sizeof(kiss_fft_scalar)*nfft;
  114. kiss_fft_scalar * in = (kiss_fft_scalar*)malloc(buflen);
  115. kiss_fft_scalar * in_copy = (kiss_fft_scalar*)malloc(buflen);
  116. kiss_fft_scalar * out= (kiss_fft_scalar*)malloc(buflen);
  117. opus_val16 * window= (opus_val16*)malloc(sizeof(opus_val16)*nfft/2);
  118. int k;
  119. clt_mdct_init(&cfg, nfft, 0, arch);
  120. for (k=0;k<nfft;++k) {
  121. in[k] = (rand() % 32768) - 16384;
  122. }
  123. for (k=0;k<nfft/2;++k) {
  124. window[k] = Q15ONE;
  125. }
  126. for (k=0;k<nfft;++k) {
  127. in[k] *= 32768;
  128. }
  129. if (isinverse)
  130. {
  131. for (k=0;k<nfft;++k) {
  132. in[k] /= nfft;
  133. }
  134. }
  135. for (k=0;k<nfft;++k)
  136. in_copy[k] = in[k];
  137. /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
  138. if (isinverse)
  139. {
  140. for (k=0;k<nfft;++k)
  141. out[k] = 0;
  142. clt_mdct_backward(&cfg,in,out, window, nfft/2, 0, 1, arch);
  143. /* apply TDAC because clt_mdct_backward() no longer does that */
  144. for (k=0;k<nfft/4;++k)
  145. out[nfft-k-1] = out[nfft/2+k];
  146. check_inv(in,out,nfft,isinverse);
  147. } else {
  148. clt_mdct_forward(&cfg,in,out,window, nfft/2, 0, 1, arch);
  149. check(in_copy,out,nfft,isinverse);
  150. }
  151. /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
  152. free(in);
  153. free(in_copy);
  154. free(out);
  155. free(window);
  156. clt_mdct_clear(&cfg, arch);
  157. }
  158. int main(int argc,char ** argv)
  159. {
  160. ALLOC_STACK;
  161. int arch = opus_select_arch();
  162. if (argc>1) {
  163. int k;
  164. for (k=1;k<argc;++k) {
  165. test1d(atoi(argv[k]),0,arch);
  166. test1d(atoi(argv[k]),1,arch);
  167. }
  168. }else{
  169. test1d(32,0,arch);
  170. test1d(32,1,arch);
  171. test1d(256,0,arch);
  172. test1d(256,1,arch);
  173. test1d(512,0,arch);
  174. test1d(512,1,arch);
  175. test1d(1024,0,arch);
  176. test1d(1024,1,arch);
  177. test1d(2048,0,arch);
  178. test1d(2048,1,arch);
  179. #ifndef RADIX_TWO_ONLY
  180. test1d(36,0,arch);
  181. test1d(36,1,arch);
  182. test1d(40,0,arch);
  183. test1d(40,1,arch);
  184. test1d(60,0,arch);
  185. test1d(60,1,arch);
  186. test1d(120,0,arch);
  187. test1d(120,1,arch);
  188. test1d(240,0,arch);
  189. test1d(240,1,arch);
  190. test1d(480,0,arch);
  191. test1d(480,1,arch);
  192. test1d(960,0,arch);
  193. test1d(960,1,arch);
  194. test1d(1920,0,arch);
  195. test1d(1920,1,arch);
  196. #endif
  197. }
  198. return ret;
  199. }