/src/dsp123/libgdither/tests/noisetest.c

https://bitbucket.org/glassman/pm123 · C · 107 lines · 76 code · 13 blank · 18 comment · 16 complexity · 32c35dddbaae55e0ea6f6bc72cfc2b80 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: runcheck.c $
  15. */
  16. #include <stdio.h>
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <assert.h>
  20. #include <complex.h>
  21. #include <fftw3.h>
  22. #define SIZE 1024
  23. #include "gdither.h"
  24. #include "noise.h"
  25. #include "compare.h"
  26. #include "gettime.h"
  27. #define REPS 10000
  28. #define CYCLES 500.0
  29. #define BINS 65536
  30. int main(int argc, char *argv[])
  31. {
  32. unsigned int i, j;
  33. fftw_complex *out = fftw_malloc(sizeof(fftw_complex) * (SIZE + 1));
  34. fftw_plan plan_rc;
  35. double in[SIZE], sum[SIZE/2];
  36. int distrib[BINS];
  37. double min = 1e10;
  38. double max = 0.0;
  39. int min_f = 1000;
  40. int max_f = 0;
  41. for (i=0; i<SIZE/2; i++) {
  42. sum[i] = 0.0;
  43. }
  44. plan_rc = fftw_plan_dft_r2c_1d(SIZE, in, out, 0);
  45. for (j=0; j<REPS; j++) {
  46. for (i=0; i<SIZE; i++) {
  47. in[i] = gdither_noise();
  48. //in[i] = random() / (float)RAND_MAX;
  49. }
  50. fftw_execute(plan_rc);
  51. for (i=1; i<SIZE/2; i++) {
  52. sum[i] += cabs(out[i]);
  53. }
  54. }
  55. for (i=1; i<SIZE/2; i++) {
  56. if (sum[i] > max) {
  57. max = sum[i];
  58. } else if (sum[i] < min) {
  59. min = sum[i];
  60. }
  61. //printf("%d\t%f\n", i, sum[i]);
  62. }
  63. min /= REPS*16;
  64. max /= REPS*16;
  65. printf("random amp/frequecy range = [%.2f,%.2f]\n", min, max);
  66. if (max - min > 0.03) {
  67. printf("min-max range exceeds 0.03, seems excessive, failing\n");
  68. exit(1);
  69. }
  70. for (i=0; i<BINS; i++) {
  71. distrib[i] = 0;
  72. }
  73. for (i=0; i<BINS*CYCLES; i++) {
  74. int bin = gdither_noise() * (BINS-1);
  75. distrib[bin]++;
  76. }
  77. for (i=0; i<BINS-1; i++) {
  78. if (distrib[i] > max_f) {
  79. max_f = distrib[i];
  80. }
  81. if (distrib[i] < min_f) {
  82. min_f = distrib[i];
  83. }
  84. }
  85. printf("16bit distribution range = [%.2f,%.2f]\n", min_f/CYCLES,
  86. max_f/CYCLES);
  87. if (min_f/CYCLES < 0.8 || max_f/CYCLES > 1.25) {
  88. printf("range seems excessive, failing\n");
  89. exit(1);
  90. }
  91. printf("ok\n");
  92. return 0;
  93. }
  94. /* vi:set ts=8 sts=4 sw=4: */