Sound_to_SPINET.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /* Sound_to_SPINET.cpp
  2. *
  3. * Copyright (C) 1993-2017 David Weenink
  4. *
  5. * This code is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This code is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. djmw 20020813 GPL header
  20. djmw 20070103 Sound interface changes
  21. */
  22. #include "Sound_to_SPINET.h"
  23. #include "NUM2.h"
  24. static double fgamma (double x, integer n) {
  25. double x2p1 = 1.0 + x * x, d = x2p1;
  26. for (integer i = 2; i <= n; i ++) {
  27. d *= x2p1;
  28. }
  29. return 1.0 / d;
  30. }
  31. /*
  32. precondition:
  33. 0 < minimumFrequencyHz < maximumFrequencyHz
  34. */
  35. autoSPINET Sound_to_SPINET (Sound me, double timeStep, double windowDuration, double minimumFrequencyHz, double maximumFrequencyHz, integer nFilters, double excitationErbProportion, double inhibitionErbProportion) {
  36. try {
  37. const double b = 1.02, samplingFrequency = 1.0 / my dx;
  38. if (timeStep < my dx) {
  39. timeStep = my dx;
  40. }
  41. if (maximumFrequencyHz > samplingFrequency / 2.0) {
  42. maximumFrequencyHz = samplingFrequency / 2.0;
  43. }
  44. integer numberOfFrames;
  45. double firstTime;
  46. Sampled_shortTermAnalysis (me, windowDuration, timeStep, & numberOfFrames, & firstTime);
  47. autoSPINET thee = SPINET_create (my xmin, my xmax, numberOfFrames, timeStep, firstTime, minimumFrequencyHz, maximumFrequencyHz, nFilters, excitationErbProportion, inhibitionErbProportion);
  48. autoSound window = Sound_createGaussian (windowDuration, samplingFrequency);
  49. autoSound frame = Sound_createSimple (1, windowDuration, samplingFrequency);
  50. autoNUMvector<double> f (1, nFilters);
  51. autoNUMvector<double> bw (1, nFilters);
  52. autoNUMvector<double> aex (1, nFilters);
  53. autoNUMvector<double> ain (1, nFilters);
  54. /*
  55. Cochlear filterbank: gammatone.
  56. */
  57. for (integer i = 1; i <= nFilters; i ++) {
  58. f [i] = NUMerbToHertz (thy y1 + (i - 1) * thy dy);
  59. bw [i] = 2.0 * NUMpi * b * (f [i] * (6.23e-6 * f [i] + 93.39e-3) + 28.52);
  60. }
  61. autoMelderProgress progress (U"SPINET analysis");
  62. for (integer i = 1; i <= nFilters; i ++) {
  63. double bb = (f [i] / 1000.0) * exp (- f [i] / 1000.0); // outer & middle ear and phase locking
  64. double tgammaMax = (thy gamma - 1) / bw [i]; // the time where the gamma function envelope has its maximum
  65. double gammaMaxAmplitude = pow ((thy gamma - 1) / (NUMe * bw [i]), thy gamma - 1); // tgammaMax
  66. double timeCorrection = tgammaMax - windowDuration / 2.0;
  67. autoSound gammaTone = Sound_createGammaTone (0.0, 0.1, samplingFrequency, thy gamma, b, f [i], 0.0, 0.0, 0);
  68. autoSound filtered = Sounds_convolve (me, gammaTone.get(), kSounds_convolve_scaling::SUM, kSounds_convolve_signalOutsideTimeDomain::ZERO);
  69. /*
  70. To energy measure: weigh with broad-band transfer function.
  71. */
  72. for (integer j = 1; j <= numberOfFrames; j ++) {
  73. Sound_into_Sound (filtered.get(), frame.get(), Sampled_indexToX (thee.get(), j) + timeCorrection);
  74. Sounds_multiply (frame.get(), window.get());
  75. thy y [i] [j] = Sound_power (frame.get()) * bb / gammaMaxAmplitude;
  76. }
  77. Melder_progress ((double) i / nFilters, U"SPINET: filter ", i, U" from ", nFilters, U".");
  78. }
  79. /*
  80. Excitatory and inhibitory area functions.
  81. */
  82. for (integer i = 1; i <= nFilters; i ++) {
  83. for (integer k = 1; k <= nFilters; k ++) {
  84. double fr = (f [k] - f [i]) / bw [i];
  85. aex [i] += fgamma (fr / thy excitationErbProportion, thy gamma);
  86. ain [i] += fgamma (fr / thy inhibitionErbProportion, thy gamma);
  87. }
  88. }
  89. // On-center off-surround interactions
  90. for (integer j = 1; j <= numberOfFrames; j ++)
  91. for (integer i = 1; i <= nFilters; i ++) {
  92. longdouble a = 0.0;
  93. for (integer k = 1; k <= nFilters; k ++) {
  94. double fr = (f [k] - f [i]) / bw [i];
  95. double hexsq = fgamma (fr / thy excitationErbProportion, thy gamma);
  96. double hinsq = fgamma (fr / thy inhibitionErbProportion, thy gamma);
  97. a += thy y [k] [j] * (hexsq / aex [i] - hinsq / ain [i]);
  98. }
  99. thy s [i] [j] = a > 0.0 ? (double) a : 0.0;
  100. }
  101. return thee;
  102. } catch (MelderError) {
  103. Melder_throw (me, U": no SPINET created.");
  104. }
  105. }
  106. /* End of file Sound_to_SPINET.cpp */