| 1 | /* Sound_to_Cochleagram.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2011,2014-2018,2021,2023 Paul Boersma |
| 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. |
| 13 | * See the GNU 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 | * pb 1997/09/03 BUG: exponential decay only for iframe > 1 |
| 21 | * pb 1998/01/05 forward masking time |
| 22 | * pb 2002/07/16 GPL |
| 23 | * pb 2002/08/26 correct handling of zero forwardMaskingTime (bug found by djmw) |
| 24 | * pb 2004/11/22 simplified Sound_to_Spectrum () |
| 25 | * pb 2006/12/30 new Sound_create API |
| 26 | * pb 2007/01/28 made compatible with stereo sounds |
| 27 | * pb 2008/01/19 double |
| 28 | * pb 2011/06/08 C++ |
| 29 | */ |
| 30 | |
| 31 | #include "Sound_to_Cochleagram.h"cxx . . $ `<file>/fon/Sound_to_Cochleagram.h`/ |
| 32 | #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/ |
| 33 | #include "Spectrum_to_Excitation.h"cxx . . $ `<file>/fon/Spectrum_to_Excitation.h`/ |
| 34 | |
| 35 | autoCochleagramcxx . . $ autoCochleagram# Sound_to_Cochleagramcxx . . $ Sound_to_Cochleagram(6ee7f771f14cbecb). (Soundcxx . . $ Sound# me, double dt, double df, double dt_window, double forwardMaskingTime) { |
| 36 | try { |
| 37 | double duration = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 38 | integercxx . . $ integer# nFrames = 1 + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((duration - dt_window) / dt); |
| 39 | integercxx . . $ integer# nsamp_window = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (dt_window / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.), halfnsamp_window = nsamp_window / 2 - 1; |
| 40 | integercxx . . $ integer# nf = Melder_iround_tieDowncxx . . $ Melder_iround_tieDown(a6e4a4c8b00c35a3). (25.6 / df); |
| 41 | double dampingFactor = forwardMaskingTime > 0.0 ? expcxx . . $ exp(fee4f1d22bc8e617). (- dt / forwardMaskingTime) : 0.0; // default 30 ms |
| 42 | double integrationCorrection = 1.0 - dampingFactor; |
| 43 | |
| 44 | nsamp_window = halfnsamp_window * 2; |
| 45 | if (nFrames < 2) return autoCochleagramcxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8). (); |
| 46 | double t1 = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + 0.5 * (duration - mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. - (nFrames - 1) * dt); // centre of first frame |
| 47 | autoCochleagramcxx . . $ autoCochleagram# thee = Cochleagram_createcxx . . $ Cochleagram_create(77b228ef82268e40). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., nFrames, dt, t1, df, nf); |
| 48 | autoSoundcxx . . $ autoSound# window = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, nsamp_window * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.); |
| 49 | for (integercxx . . $ integer# iframe = 1; iframe <= nFrames; iframe ++) { |
| 50 | double t = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe); |
| 51 | integercxx . . $ integer# leftSample = Sampled_xToLowIndexcxx . . $ Sampled_xToLowIndex(405c2491bc3636e9). (me, t); |
| 52 | integercxx . . $ integer# rightSample = leftSample + 1; |
| 53 | integercxx . . $ integer# startSample = rightSample - halfnsamp_window; |
| 54 | integercxx . . $ integer# endSample = rightSample + halfnsamp_window; |
| 55 | if (startSample < 1) { |
| 56 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"Start sample too small: ", startSample, |
| 57 | U" instead of 1."); |
| 58 | startSample = 1; |
| 59 | } |
| 60 | if (endSample > mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.) { |
| 61 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"End sample too small: ", endSample, |
| 62 | U" instead of ", mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., |
| 63 | U"."); |
| 64 | endSample = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; |
| 65 | } |
| 66 | |
| 67 | /* Copy a window to a frame. */ |
| 68 | for (integercxx . . $ integer# i = 1; i <= nsamp_window; i ++) |
| 69 | window ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = |
| 70 | ( mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. == 1 ? mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i+startSample-1] : 0.5 * (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i+startSample-1] + mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i+startSample-1]) ) * |
| 71 | (0.5 - 0.5 * coscxx . . $ cos(fee4f1d22bc8e617). (2.0 * NUMpicxx . . $ NUMpi. * i / (nsamp_window + 1))); |
| 72 | autoSpectrumcxx . . $ autoSpectrum# spec = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (window.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true); |
| 73 | autoExcitationcxx . . $ autoExcitation# excitation = Spectrum_to_Excitationcxx . . $ Spectrum_to_Excitation(921ae313082a41f3). (spec.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), df); |
| 74 | for (integercxx . . $ integer# ifreq = 1; ifreq <= nf; ifreq ++) |
| 75 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = excitation ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq] + ( iframe > 1 ? dampingFactor * thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe - 1] : 0 ); |
| 76 | } |
| 77 | for (integercxx . . $ integer# iframe = 1; iframe <= nFrames; iframe ++) |
| 78 | for (integercxx . . $ integer# ifreq = 1; ifreq <= nf; ifreq ++) |
| 79 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] *= integrationCorrection; |
| 80 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 81 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 82 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Cochleagram."); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | static autoSoundcxx . . $ autoSound# createGammatonecxx . . $ createGammatone(c2815364717639a8). (double midFrequency_Hertz, double samplingFrequency) { |
| 87 | double lengthOfGammatone_seconds = 50.0 / midFrequency_Hertz; // 50 periods |
| 88 | integercxx . . $ integer# lengthOfGammatone_samples; |
| 89 | /* EdB's alfa1: */ |
| 90 | double latency = 1.95e-3 * powcxx . . $ pow(b71928d39fde4295). (midFrequency_Hertz / 1000, -0.725) + 0.6e-3; |
| 91 | /* EdB's beta: */ |
| 92 | double decayTime = 1e-3 * powcxx . . $ pow(b71928d39fde4295). (midFrequency_Hertz / 1000, -0.663); |
| 93 | /* EdB's omega: */ |
| 94 | double midFrequency_radPerSecond = 2 * NUMpicxx . . $ NUMpi. * midFrequency_Hertz; |
| 95 | autoSoundcxx . . $ autoSound# gammatone = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, lengthOfGammatone_seconds, samplingFrequency); |
| 96 | lengthOfGammatone_samples = gammatone ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; |
| 97 | for (integercxx . . $ integer# itime = 1; itime <= lengthOfGammatone_samples; itime ++) { |
| 98 | double time_seconds = (itime - 0.5) / samplingFrequency; |
| 99 | double timeAfterLatency = time_seconds - latency; |
| 100 | double x = timeAfterLatency / decayTime; |
| 101 | if (time_seconds > latency) gammatone ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itime] = |
| 102 | x * x * x * expcxx . . $ exp(fee4f1d22bc8e617). (- x) * coscxx . . $ cos(fee4f1d22bc8e617). (midFrequency_radPerSecond * timeAfterLatency); |
| 103 | } |
| 104 | return gammatonecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 105 | } |
| 106 | |
| 107 | autoCochleagramcxx . . $ autoCochleagram# Sound_to_Cochleagram_edbcxx . . $ Sound_to_Cochleagram_edb(71483ab7d3404602). |
| 108 | (Soundcxx . . $ Sound# me, double dtime, double dfreq, int hasSynapse, double replenishmentRate, |
| 109 | double lossRate, double returnRate, double reprocessingRate) |
| 110 | { |
| 111 | try { |
| 112 | double duration_seconds = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.; |
| 113 | if (dtime < mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.) dtime = mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 114 | integercxx . . $ integer# ntime = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (duration_seconds / dtime); |
| 115 | if (ntime < 2) return autoCochleagramcxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8). (); |
| 116 | integercxx . . $ integer# nfreq = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (25.6 / dfreq); // 25.6 Bark = highest frequency |
| 117 | |
| 118 | autoCochleagramcxx . . $ autoCochleagram# thee = Cochleagram_createcxx . . $ Cochleagram_create(77b228ef82268e40). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., ntime, dtime, 0.5 * dtime, dfreq, nfreq); |
| 119 | |
| 120 | /* Stages 1 and 2: outer- and middle-ear filtering. */ |
| 121 | /* From acoustic sound to oval window. */ |
| 122 | |
| 123 | for (integercxx . . $ integer# ifreq = 1; ifreq <= nfreq; ifreq ++) { |
| 124 | double *response = & thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).0]; |
| 125 | |
| 126 | /* Stage 3: basilar membrane filtering by gammatones. */ |
| 127 | /* From oval window to basilar membrane response. */ |
| 128 | |
| 129 | double midFrequency_Bark = (ifreq - 0.5) * dfreq; |
| 130 | double midFrequency_Hertz = NUMbarkToHertzcxx . . $ NUMbarkToHertz(447c67c6676f75c3). (midFrequency_Bark); |
| 131 | autoSoundcxx . . $ autoSound# gammatone = createGammatonecxx . . $ createGammatone(c2815364717639a8). (midFrequency_Hertz, 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.); |
| 132 | autoSoundcxx . . $ autoSound# basil = 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.); |
| 133 | |
| 134 | /* Stage 4: detection = rectify + integrate + low-pass 500 Hz. */ |
| 135 | /* From basilar membrane response to firing rate. */ |
| 136 | |
| 137 | if (hasSynapse) { |
| 138 | double dt = mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 139 | double M = 1.0; // maximum free transmitter |
| 140 | double A = 5.0, B = 300.0, g = 2000.0; // determine permeability |
| 141 | double y = replenishmentRate; // Meddis: 5.05 |
| 142 | double l = lossRate, r = returnRate; // Meddis: 2500, 6580 |
| 143 | double x = reprocessingRate; // Meddis: 66.31 |
| 144 | double h = 50000; // convert cleft contents to firing rate |
| 145 | double gdt = 1.0 - expcxx . . $ exp(fee4f1d22bc8e617). (- g * dt); |
| 146 | double ydt = 1.0 - expcxx . . $ exp(fee4f1d22bc8e617). (- y * dt); |
| 147 | double ldt = (1.0 - expcxx . . $ exp(fee4f1d22bc8e617). (- (l + r) * dt)) * l / (l + r); |
| 148 | double rdt = (1.0 - expcxx . . $ exp(fee4f1d22bc8e617). (- (l + r) * dt)) * r / (l + r); |
| 149 | double xdt = 1.0 - expcxx . . $ exp(fee4f1d22bc8e617). (- x * dt); |
| 150 | double kt = g * A / (A + B); // membrane permeability |
| 151 | double c = M * y * kt / (l * kt + y * (l + r)); // cleft contents |
| 152 | double q = c * (l + r) / kt; // free transmitter |
| 153 | double w = c * r / x; // reprocessing store |
| 154 | for (integercxx . . $ integer# itime = 1; itime <= basil ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; itime ++) { |
| 155 | double splusA = basil ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itime] * 10.0 + A; |
| 156 | double replenish = ( M > q ? ydt * (M - q) : 0.0 ); |
| 157 | kt = ( splusA > 0.0 ? gdt * splusA / (splusA + B) : 0.0 ); |
| 158 | double eject = kt * q; |
| 159 | double loss = ldt * c; |
| 160 | double reuptake = rdt * c; |
| 161 | double reprocess = xdt * w; |
| 162 | q = q + replenish - eject + reprocess; |
| 163 | c = c + eject - loss - reuptake; |
| 164 | w = w + reuptake - reprocess; |
| 165 | basil ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itime] = h * c; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if (dtime == mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.) { |
| 170 | for (integercxx . . $ integer# itime = 1; itime <= ntime; itime ++) |
| 171 | response [itime] = basil ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).itime]; |
| 172 | } else { |
| 173 | double d = dtime / basil ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dxcxx . . $ structSampled#dx. / 2.0; |
| 174 | double factor = -6 / d / d; |
| 175 | double area = d * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (NUMpicxx . . $ NUMpi. / 6); |
| 176 | double expmin6 = expcxx . . $ exp(7759b11ec49fe502). (-6), onebyoneminexpmin6 = 1 / (1 - expmin6); |
| 177 | for (integercxx . . $ integer# itime = 1; itime <= ntime; itime ++) { |
| 178 | double t1 = (itime - 1) * dtime; |
| 179 | double t2 = t1 + dtime; |
| 180 | double mean = 0.0; |
| 181 | integercxx . . $ integer# i1, i2; |
| 182 | integercxx . . $ integer# n = Matrix_getWindowSamplesXcxx . . $ Matrix_getWindowSamplesX(c738979be11bd51a). (basil.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t1, t2, & i1, & i2); |
| 183 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (n >= 1); |
| 184 | if (n <= 2) { |
| 185 | for (integercxx . . $ integer# isamp = i1; isamp <= i2; isamp ++) |
| 186 | mean += basil ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp]; |
| 187 | mean /= n; |
| 188 | } else { |
| 189 | integercxx . . $ integer# muint = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((i1 + i2) / 2.0), dint = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (d); |
| 190 | for (integercxx . . $ integer# isamp = muint - dint; isamp <= muint + dint; isamp ++) { |
| 191 | double y = 0; |
| 192 | if (isamp < 1 || isamp > basil ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.) |
| 193 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"isamp ", isamp); |
| 194 | else |
| 195 | y = basil ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp]; |
| 196 | mean += y * onebyoneminexpmin6 * (expcxx . . $ exp(fee4f1d22bc8e617). (factor * (isamp - muint) * |
| 197 | (isamp - muint)) - expmin6); |
| 198 | } |
| 199 | mean /= area; |
| 200 | } |
| 201 | response [itime] = mean; |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 206 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 207 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Cochleagram (edb)."); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* End of file Sound_to_Cochleagram.cpp */ |