test_unit_mdct.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. #include <stdio.h>
  28. #include "mdct.h"
  29. #include "stack_alloc.h"
  30. #include "kiss_fft.h"
  31. #include "mdct.h"
  32. #include "modes.h"
  33. #ifndef M_PI
  34. #define M_PI 3.141592653
  35. #endif
  36. int ret = 0;
  37. void check(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
  38. {
  39. int bin,k;
  40. double errpow=0,sigpow=0;
  41. double snr;
  42. for (bin=0;bin<nfft/2;++bin) {
  43. double ansr = 0;
  44. double difr;
  45. for (k=0;k<nfft;++k) {
  46. double phase = 2*M_PI*(k+.5+.25*nfft)*(bin+.5)/nfft;
  47. double re = cos(phase);
  48. re /= nfft/4;
  49. ansr += in[k] * re;
  50. }
  51. /*printf ("%f %f\n", ansr, out[bin]);*/
  52. difr = ansr - out[bin];
  53. errpow += difr*difr;
  54. sigpow += ansr*ansr;
  55. }
  56. snr = 10*log10(sigpow/errpow);
  57. printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
  58. if (snr<60) {
  59. printf( "** poor snr: %f **\n", snr);
  60. ret = 1;
  61. }
  62. }
  63. void check_inv(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
  64. {
  65. int bin,k;
  66. double errpow=0,sigpow=0;
  67. double snr;
  68. for (bin=0;bin<nfft;++bin) {
  69. double ansr = 0;
  70. double difr;
  71. for (k=0;k<nfft/2;++k) {
  72. double phase = 2*M_PI*(bin+.5+.25*nfft)*(k+.5)/nfft;
  73. double re = cos(phase);
  74. /*re *= 2;*/
  75. ansr += in[k] * re;
  76. }
  77. /*printf ("%f %f\n", ansr, out[bin]);*/
  78. difr = ansr - out[bin];
  79. errpow += difr*difr;
  80. sigpow += ansr*ansr;
  81. }
  82. snr = 10*log10(sigpow/errpow);
  83. printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
  84. if (snr<60) {
  85. printf( "** poor snr: %f **\n", snr);
  86. ret = 1;
  87. }
  88. }
  89. void test1d(int nfft,int isinverse,int arch)
  90. {
  91. size_t buflen = sizeof(kiss_fft_scalar)*nfft;
  92. kiss_fft_scalar *in;
  93. kiss_fft_scalar *in_copy;
  94. kiss_fft_scalar *out;
  95. opus_val16 *window;
  96. int k;
  97. #ifdef CUSTOM_MODES
  98. int shift = 0;
  99. const mdct_lookup *cfg;
  100. mdct_lookup _cfg;
  101. clt_mdct_init(&_cfg, nfft, 0, arch);
  102. cfg = &_cfg;
  103. #else
  104. int shift;
  105. const mdct_lookup *cfg;
  106. CELTMode *mode = opus_custom_mode_create(48000, 960, NULL);
  107. if (nfft == 1920) shift = 0;
  108. else if (nfft == 960) shift = 1;
  109. else if (nfft == 480) shift = 2;
  110. else if (nfft == 240) shift = 3;
  111. else return;
  112. cfg = &mode->mdct;
  113. #endif
  114. in = (kiss_fft_scalar*)malloc(buflen);
  115. in_copy = (kiss_fft_scalar*)malloc(buflen);
  116. out = (kiss_fft_scalar*)malloc(buflen);
  117. window = (opus_val16*)malloc(sizeof(opus_val16)*nfft/2);
  118. for (k=0;k<nfft;++k) {
  119. in[k] = (rand() % 32768) - 16384;
  120. }
  121. for (k=0;k<nfft/2;++k) {
  122. window[k] = Q15ONE;
  123. }
  124. for (k=0;k<nfft;++k) {
  125. in[k] *= 32768;
  126. }
  127. if (isinverse)
  128. {
  129. for (k=0;k<nfft;++k) {
  130. in[k] /= nfft;
  131. }
  132. }
  133. for (k=0;k<nfft;++k)
  134. in_copy[k] = in[k];
  135. /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
  136. if (isinverse)
  137. {
  138. for (k=0;k<nfft;++k)
  139. out[k] = 0;
  140. clt_mdct_backward(cfg,in,out, window, nfft/2, shift, 1, arch);
  141. /* apply TDAC because clt_mdct_backward() no longer does that */
  142. for (k=0;k<nfft/4;++k)
  143. out[nfft-k-1] = out[nfft/2+k];
  144. check_inv(in,out,nfft,isinverse);
  145. } else {
  146. clt_mdct_forward(cfg,in,out,window, nfft/2, shift, 1, arch);
  147. check(in_copy,out,nfft,isinverse);
  148. }
  149. /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
  150. free(in);
  151. free(in_copy);
  152. free(out);
  153. free(window);
  154. #ifdef CUSTOM_MODES
  155. clt_mdct_clear(&_cfg, arch);
  156. #endif
  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. }