mdct-test.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #define SKIP_CONFIG_H
  5. #ifndef CUSTOM_MODES
  6. #define CUSTOM_MODES
  7. #endif
  8. #include <stdio.h>
  9. #include "mdct.h"
  10. #define CELT_C
  11. #include "../celt/stack_alloc.h"
  12. #include "../celt/kiss_fft.c"
  13. #include "../celt/mdct.c"
  14. #include "../celt/mathops.c"
  15. #include "../celt/entcode.c"
  16. #ifndef M_PI
  17. #define M_PI 3.141592653
  18. #endif
  19. #ifdef FIXED_DEBUG
  20. long long celt_mips=0;
  21. #endif
  22. int ret = 0;
  23. void check(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
  24. {
  25. int bin,k;
  26. double errpow=0,sigpow=0;
  27. double snr;
  28. for (bin=0;bin<nfft/2;++bin) {
  29. double ansr = 0;
  30. double difr;
  31. for (k=0;k<nfft;++k) {
  32. double phase = 2*M_PI*(k+.5+.25*nfft)*(bin+.5)/nfft;
  33. double re = cos(phase);
  34. re /= nfft/4;
  35. ansr += in[k] * re;
  36. }
  37. /*printf ("%f %f\n", ansr, out[bin]);*/
  38. difr = ansr - out[bin];
  39. errpow += difr*difr;
  40. sigpow += ansr*ansr;
  41. }
  42. snr = 10*log10(sigpow/errpow);
  43. printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
  44. if (snr<60) {
  45. printf( "** poor snr: %f **\n", snr);
  46. ret = 1;
  47. }
  48. }
  49. void check_inv(kiss_fft_scalar * in,kiss_fft_scalar * out,int nfft,int isinverse)
  50. {
  51. int bin,k;
  52. double errpow=0,sigpow=0;
  53. double snr;
  54. for (bin=0;bin<nfft;++bin) {
  55. double ansr = 0;
  56. double difr;
  57. for (k=0;k<nfft/2;++k) {
  58. double phase = 2*M_PI*(bin+.5+.25*nfft)*(k+.5)/nfft;
  59. double re = cos(phase);
  60. /*re *= 2;*/
  61. ansr += in[k] * re;
  62. }
  63. /*printf ("%f %f\n", ansr, out[bin]);*/
  64. difr = ansr - out[bin];
  65. errpow += difr*difr;
  66. sigpow += ansr*ansr;
  67. }
  68. snr = 10*log10(sigpow/errpow);
  69. printf("nfft=%d inverse=%d,snr = %f\n",nfft,isinverse,snr );
  70. if (snr<60) {
  71. printf( "** poor snr: %f **\n", snr);
  72. ret = 1;
  73. }
  74. }
  75. void test1d(int nfft,int isinverse)
  76. {
  77. mdct_lookup cfg;
  78. size_t buflen = sizeof(kiss_fft_scalar)*nfft;
  79. kiss_fft_scalar * in = (kiss_fft_scalar*)malloc(buflen);
  80. kiss_fft_scalar * in_copy = (kiss_fft_scalar*)malloc(buflen);
  81. kiss_fft_scalar * out= (kiss_fft_scalar*)malloc(buflen);
  82. opus_val16 * window= (opus_val16*)malloc(sizeof(opus_val16)*nfft/2);
  83. int k;
  84. clt_mdct_init(&cfg, nfft, 0);
  85. for (k=0;k<nfft;++k) {
  86. in[k] = (rand() % 32768) - 16384;
  87. }
  88. for (k=0;k<nfft/2;++k) {
  89. window[k] = Q15ONE;
  90. }
  91. for (k=0;k<nfft;++k) {
  92. in[k] *= 32768;
  93. }
  94. if (isinverse)
  95. {
  96. for (k=0;k<nfft;++k) {
  97. in[k] /= nfft;
  98. }
  99. }
  100. for (k=0;k<nfft;++k)
  101. in_copy[k] = in[k];
  102. /*for (k=0;k<nfft;++k) printf("%d %d ", in[k].r, in[k].i);printf("\n");*/
  103. if (isinverse)
  104. {
  105. for (k=0;k<nfft;++k)
  106. out[k] = 0;
  107. clt_mdct_backward(&cfg,in,out, window, nfft/2, 0, 1);
  108. check_inv(in,out,nfft,isinverse);
  109. } else {
  110. clt_mdct_forward(&cfg,in,out,window, nfft/2, 0, 1);
  111. check(in_copy,out,nfft,isinverse);
  112. }
  113. /*for (k=0;k<nfft;++k) printf("%d %d ", out[k].r, out[k].i);printf("\n");*/
  114. free(in);
  115. free(out);
  116. clt_mdct_clear(&cfg);
  117. }
  118. int main(int argc,char ** argv)
  119. {
  120. ALLOC_STACK;
  121. if (argc>1) {
  122. int k;
  123. for (k=1;k<argc;++k) {
  124. test1d(atoi(argv[k]),0);
  125. test1d(atoi(argv[k]),1);
  126. }
  127. }else{
  128. test1d(32,0);
  129. test1d(32,1);
  130. test1d(256,0);
  131. test1d(256,1);
  132. test1d(512,0);
  133. test1d(512,1);
  134. #ifndef RADIX_TWO_ONLY
  135. test1d(40,0);
  136. test1d(40,1);
  137. test1d(120,0);
  138. test1d(120,1);
  139. test1d(240,0);
  140. test1d(240,1);
  141. test1d(480,0);
  142. test1d(480,1);
  143. #endif
  144. }
  145. return ret;
  146. }