| 1 | /* Sound_to_SPINET.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2019 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 | /* |
| 20 | djmw 20020813 GPL header |
| 21 | djmw 20070103 Sound interface changes |
| 22 | */ |
| 23 | |
| 24 | #include "Sound_to_SPINET.h"cxx . . $ `<file>/dwtools/Sound_to_SPINET.h`/ |
| 25 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 26 | |
| 27 | static double fgammacxx . . $ fgamma(3b6c2eefe8d0b5cf). (double x, integercxx . . $ integer# n) { |
| 28 | const double x2p1 = 1.0 + x * x; |
| 29 | double d = x2p1; |
| 30 | for (integercxx . . $ integer# i = 2; i <= n; i ++) |
| 31 | d *= x2p1; |
| 32 | return 1.0 / d; |
| 33 | } |
| 34 | |
| 35 | /* |
| 36 | precondition: |
| 37 | 0 < minimumFrequencyHz < maximumFrequencyHz |
| 38 | */ |
| 39 | |
| 40 | autoSPINETcxx . . $ autoSPINET# Sound_to_SPINETcxx . . $ Sound_to_SPINET(ab6a30a57ddbf21). (Soundcxx . . $ Sound# me, double timeStep, double windowDuration, double minimumFrequencyHz, double maximumFrequencyHz, integercxx . . $ integer# numberOfGammaFilters, double excitationErbProportion, double inhibitionErbProportion) { |
| 41 | try { |
| 42 | const double b = 1.02, samplingFrequency = 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 43 | |
| 44 | if (timeStep < mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.) |
| 45 | timeStep = mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 46 | if (maximumFrequencyHz > samplingFrequency / 2.0) |
| 47 | maximumFrequencyHz = samplingFrequency / 2.0; |
| 48 | |
| 49 | integercxx . . $ integer# numberOfFrames; |
| 50 | double firstTime; |
| 51 | Sampled_shortTermAnalysiscxx . . $ Sampled_shortTermAnalysis(77f703705034c8a). (me, windowDuration, timeStep, & numberOfFrames, & firstTime); |
| 52 | autoSPINETcxx . . $ autoSPINET# thee = SPINET_createcxx . . $ SPINET_create(71e7bc8bc4700c29). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfFrames, timeStep, firstTime, minimumFrequencyHz, maximumFrequencyHz, numberOfGammaFilters, excitationErbProportion, inhibitionErbProportion); |
| 53 | autoSoundcxx . . $ autoSound# window = Sound_createGaussiancxx . . $ Sound_createGaussian(c2815364717639a8). (windowDuration, samplingFrequency); |
| 54 | autoSoundcxx . . $ autoSound# frame = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, windowDuration, samplingFrequency); |
| 55 | autoVECcxx . . $ autoVEC# f = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfGammaFilters); |
| 56 | autoVECcxx . . $ autoVEC# bw = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfGammaFilters); |
| 57 | autoVECcxx . . $ autoVEC# aex = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfGammaFilters); |
| 58 | autoVECcxx . . $ autoVEC# ain = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfGammaFilters); |
| 59 | /* |
| 60 | Cochlear filterbank: gammatone. |
| 61 | */ |
| 62 | for (integercxx . . $ integer# i = 1; i <= numberOfGammaFilters; i ++) { |
| 63 | f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = NUMerbToHertzcxx . . $ NUMerbToHertz(447c67c6676f75c3). (thycxx . . $ `melder/melder.h:45:9`! y1cxx . . $ structSampledXY#y1. + (i - 1) * thycxx . . $ `melder/melder.h:45:9`! dycxx . . $ structSampledXY#dy.); |
| 64 | bw [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = NUM2picxx . . $ NUM2pi. * b * (f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] * (6.23e-6 * f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] + 93.39e-3) + 28.52); |
| 65 | } |
| 66 | |
| 67 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Sound to SPINET..."); |
| 68 | |
| 69 | for (integercxx . . $ integer# i = 1; i <= numberOfGammaFilters; i ++) { |
| 70 | const double bb = (f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] / 1000.0) * expcxx . . $ exp(fee4f1d22bc8e617). (- f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] / 1000.0); // outer & middle ear and phase locking |
| 71 | const double tgammaMax = (thycxx . . $ `melder/melder.h:45:9`! gammacxx . . $ structSPINET#gamma. - 1) / bw [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; // the time where the gamma function envelope has its maximum |
| 72 | const double gammaMaxAmplitude = powcxx . . $ pow(9454dda30a4b8335). ((thycxx . . $ `melder/melder.h:45:9`! gammacxx . . $ structSPINET#gamma. - 1) / (NUMecxx . . $ NUMe. * bw [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]), thycxx . . $ `melder/melder.h:45:9`! gammacxx . . $ structSPINET#gamma. - 1); |
| 73 | const double timeCorrection = tgammaMax - windowDuration / 2.0; |
| 74 | |
| 75 | autoSoundcxx . . $ autoSound# gammaTone = Sound_createGammaTonecxx . . $ Sound_createGammaTone(9a0130e8aee7eee5). (0.0, 0.1, samplingFrequency, thycxx . . $ `melder/melder.h:45:9`! gammacxx . . $ structSPINET#gamma., b, f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i], 0.0, 0.0, false); |
| 76 | autoSoundcxx . . $ autoSound# filtered = Sounds_convolvecxx . . $ Sounds_convolve(b7360aaa447e63e). (me, gammaTone.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), kSounds_convolve_scalingcxx . . $ kSounds_convolve_scaling#::SUMcxx . . $ kSounds_convolve_scaling#SUM., kSounds_convolve_signalOutsideTimeDomaincxx . . $ kSounds_convolve_signalOutsideTimeDomain#::ZEROcxx . . $ kSounds_convolve_signalOutsideTimeDomain#ZERO.); |
| 77 | /* |
| 78 | To energy measure: weigh with broad-band transfer function. |
| 79 | */ |
| 80 | for (integercxx . . $ integer# j = 1; j <= numberOfFrames; j ++) { |
| 81 | Sound_into_Soundcxx . . $ Sound_into_Sound(9b5e4c6d6787168f). (filtered.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), frame.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), j) + timeCorrection); |
| 82 | Sounds_multiplycxx . . $ Sounds_multiply(3740d3b02ef916dc). (frame.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), window.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 83 | thycxx . . $ `melder/melder.h:45:9`! ycxx . . $ structSPINET#y. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = Sound_powercxx . . $ Sound_power(152928a1b0c69725). (frame.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()) * bb / gammaMaxAmplitude; |
| 84 | } |
| 85 | Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). ((double) i / numberOfGammaFilters, U"SPINET: filter ", i, U" from ", numberOfGammaFilters, U"."); |
| 86 | } |
| 87 | /* |
| 88 | Excitatory and inhibitory area functions. |
| 89 | */ |
| 90 | for (integercxx . . $ integer# i = 1; i <= numberOfGammaFilters; i ++) { |
| 91 | for (integercxx . . $ integer# k = 1; k <= numberOfGammaFilters; k ++) { |
| 92 | const double fr = (f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] - f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]) / bw [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 93 | aex [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] += fgammacxx . . $ fgamma(3b6c2eefe8d0b5cf). (fr / thycxx . . $ `melder/melder.h:45:9`! excitationErbProportioncxx . . $ structSPINET#excitationErbProportion., thycxx . . $ `melder/melder.h:45:9`! gammacxx . . $ structSPINET#gamma.); |
| 94 | ain [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] += fgammacxx . . $ fgamma(3b6c2eefe8d0b5cf). (fr / thycxx . . $ `melder/melder.h:45:9`! inhibitionErbProportioncxx . . $ structSPINET#inhibitionErbProportion., thycxx . . $ `melder/melder.h:45:9`! gammacxx . . $ structSPINET#gamma.); |
| 95 | } |
| 96 | } |
| 97 | /* |
| 98 | On-center off-surround interactions |
| 99 | */ |
| 100 | for (integercxx . . $ integer# j = 1; j <= numberOfFrames; j ++) |
| 101 | for (integercxx . . $ integer# i = 1; i <= numberOfGammaFilters; i ++) { |
| 102 | longdoublecxx . . $ longdouble# a = 0.0; |
| 103 | for (integercxx . . $ integer# k = 1; k <= numberOfGammaFilters; k ++) { |
| 104 | const double fr = (f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).k] - f [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]) / bw [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 105 | const double hexsq = fgammacxx . . $ fgamma(3b6c2eefe8d0b5cf). (fr / thycxx . . $ `melder/melder.h:45:9`! excitationErbProportioncxx . . $ structSPINET#excitationErbProportion., thycxx . . $ `melder/melder.h:45:9`! gammacxx . . $ structSPINET#gamma.); |
| 106 | const double hinsq = fgammacxx . . $ fgamma(3b6c2eefe8d0b5cf). (fr / thycxx . . $ `melder/melder.h:45:9`! inhibitionErbProportioncxx . . $ structSPINET#inhibitionErbProportion., thycxx . . $ `melder/melder.h:45:9`! gammacxx . . $ structSPINET#gamma.); |
| 107 | a += thycxx . . $ `melder/melder.h:45:9`! ycxx . . $ structSPINET#y. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).k] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] * (hexsq / aex [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] - hinsq / ain [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]); |
| 108 | } |
| 109 | thycxx . . $ `melder/melder.h:45:9`! scxx . . $ structSPINET#s. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = a > 0.0 ? (double) a : 0.0; |
| 110 | } |
| 111 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 112 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 113 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no SPINET created."); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* End of file Sound_to_SPINET.cpp */ |