/src/dsp123/libgdither/tests/distortion.c

https://bitbucket.org/glassman/pm123 · C · 180 lines · 132 code · 25 blank · 23 comment · 23 complexity · fe2d06eeefb6e1edd941851c863166dc MD5 · raw file

  1. /*
  2. * Copyright (C) 2004 Steve Harris
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * $Id: distortion-test.c $
  15. */
  16. #include <unistd.h>
  17. #include <string.h>
  18. #include <math.h>
  19. #include <stdio.h>
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <complex.h>
  23. #include <fftw3.h>
  24. #include "gdither.h"
  25. #define SIZE 16384
  26. #ifndef M_PI
  27. #define M_PI 3.1415926
  28. #endif
  29. float in[SIZE];
  30. int32_t out[SIZE];
  31. double sin_tbl[SIZE];
  32. double sinless_tbl[SIZE];
  33. void find_snr(GDitherType dt, const char *desc);
  34. void find_res(GDitherType dt, const char *desc);
  35. int main(int argc, char *argv[])
  36. {
  37. int i;
  38. for (i=0; i<SIZE; i++) {
  39. sin_tbl[i] = sin((double)i * M_PI * 0.05) * 0.01;
  40. in[i] = (float)sin_tbl[i];
  41. }
  42. find_snr(GDitherNone, "no dithering");
  43. find_snr(GDitherRect, "rectangular dithering");
  44. find_snr(GDitherTri, "triangular dithering");
  45. find_snr(GDitherShaped, "noise-shaped dithering");
  46. printf("ok\n\n");
  47. find_res(GDitherNone, "no dithering");
  48. find_res(GDitherRect, "rectangular dithering");
  49. find_res(GDitherTri, "triangular dithering");
  50. find_res(GDitherShaped, "noise-shaped dithering");
  51. printf("ok\n");
  52. return 0;
  53. }
  54. void find_res(GDitherType dt, const char *desc)
  55. {
  56. int i, j;
  57. GDither *ds = gdither_new(dt, 1, 16, 0);
  58. int16_t out16[SIZE];
  59. fftw_plan plan_rc;
  60. fftw_complex *freq = fftw_malloc(sizeof(fftw_complex) * (SIZE + 1));
  61. double amp[SIZE];
  62. double floor_peak = -1000.0;
  63. double floor_sum = 0.0;
  64. double floor_cnt = 0.0;
  65. double harm_peak = -1000.0;
  66. double harm_sum = 0.0;
  67. double harm_cnt = 0.0;
  68. int harmonic[SIZE];
  69. //char tmp[256];
  70. plan_rc = fftw_plan_dft_r2c_1d(SIZE, sinless_tbl, freq, FFTW_PRESERVE_INPUT);
  71. gdither_runf(ds, 0, SIZE, in, out16);
  72. for (i=0; i<SIZE; i++) {
  73. sinless_tbl[i] = ((double)out16[i] / 32768.0) - sin_tbl[i];
  74. sinless_tbl[i] *= -0.5 * cos(2.0f * M_PI * (float) i /
  75. (float) SIZE) + 0.5;
  76. }
  77. fftw_execute(plan_rc);
  78. /* divide bins into ones that are where harmonic peaks could be
  79. and ones that aren't */
  80. for (i=0; i<SIZE/2; i++) {
  81. harmonic[i] = 0;
  82. }
  83. for (i=1; i<20; i++) {
  84. for (j=-4; j<5; j++) {
  85. harmonic[i*SIZE/40 + j] = 1;
  86. }
  87. }
  88. for (i=2; i<SIZE/2; i++) {
  89. amp[i] = 20.0 * log10(cabs(freq[i]));
  90. if (harmonic[i]) {
  91. if (amp[i] > harm_peak) harm_peak = amp[i];
  92. if (1 || amp[i] > -100.0) {
  93. harm_sum += amp[i];
  94. harm_cnt++;
  95. }
  96. } else {
  97. if (amp[i] > floor_peak) floor_peak = amp[i];
  98. floor_sum += amp[i];
  99. floor_cnt++;
  100. }
  101. //sprintf(tmp, "%f\t%g\n", 48000.0 / (double)SIZE * (double)i, amp[i]);
  102. //write(5, tmp, strlen(tmp));
  103. //printf("%d\t%g\n", i, amp_db);
  104. }
  105. printf("%s mean harmonic resonace = %.1fdB\n", desc,
  106. harm_sum / harm_cnt - floor_sum / floor_cnt);
  107. printf("%s peak harmonic resonance = %.1fdB\n", desc,
  108. harm_peak - floor_peak);
  109. if (dt != GDitherNone && harm_sum / harm_cnt - floor_sum / floor_cnt > 6.0) {
  110. printf("excessive resonance peaks, failing\n");
  111. exit(1);
  112. }
  113. if (dt != GDitherNone && harm_peak - floor_peak > 6.0) {
  114. printf("excessive resonance peaks, failing\n");
  115. exit(1);
  116. }
  117. }
  118. void find_snr(GDitherType dt, const char *desc)
  119. {
  120. GDither *ds = gdither_new(dt, 1, GDither32bit, 24);
  121. int i;
  122. double sum_sq = 0.0;
  123. double val;
  124. gdither_runf(ds, 0, SIZE, in, out);
  125. for (i=0; i<SIZE; i++) {
  126. sinless_tbl[i] = ((double)out[i] / 2147483648.0) - sin_tbl[i];
  127. sum_sq += sinless_tbl[i] * sinless_tbl[i];
  128. //printf("%g\n", sinless_tbl[i]);
  129. }
  130. val = 20.0 * log10(sqrt(sum_sq / (double)SIZE));
  131. printf("RMS SNR for %s = %.0fdB\n", desc, val);
  132. switch (dt) {
  133. case GDitherNone:
  134. break;
  135. case GDitherRect:
  136. if (val > -141.0) {
  137. printf("excessive noise, failed\n");
  138. exit(1);
  139. }
  140. break;
  141. case GDitherTri:
  142. if (val > -144.0) {
  143. printf("excessive noise, failed\n");
  144. exit(1);
  145. }
  146. break;
  147. case GDitherShaped:
  148. if (val > -130.0) {
  149. printf("excessive noise, failed\n");
  150. exit(1);
  151. }
  152. break;
  153. }
  154. }
  155. /* vi:set ts=8 sts=4 sw=4: */