Debian Codegraph @

fon/Sound_to_Cochleagram.cpp

raw
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"
32 #include "Sound_and_Spectrum.h"
33 #include "Spectrum_to_Excitation.h"
34
35 autoCochleagram Sound_to_Cochleagram (Sound me, double dt, double df, double dt_window, double forwardMaskingTime) {
36 try {
37 double duration = my nx * my dx;
38 integer nFrames = 1 + Melder_ifloor ((duration - dt_window) / dt);
39 integer nsamp_window = Melder_ifloor (dt_window / my dx), halfnsamp_window = nsamp_window / 2 - 1;
40 integer nf = Melder_iround_tieDown (25.6 / df);
41 double dampingFactor = forwardMaskingTime > 0.0 ? exp (- 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 autoCochleagram ();
46 double t1 = my x1 + 0.5 * (duration - my dx - (nFrames - 1) * dt); // centre of first frame
47 autoCochleagram thee = Cochleagram_create (my xmin, my xmax, nFrames, dt, t1, df, nf);
48 autoSound window = Sound_createSimple (1, nsamp_window * my dx, 1.0 / my dx);
49 for (integer iframe = 1; iframe <= nFrames; iframe ++) {
50 double t = Sampled_indexToX (thee.get(), iframe);
51 integer leftSample = Sampled_xToLowIndex (me, t);
52 integer rightSample = leftSample + 1;
53 integer startSample = rightSample - halfnsamp_window;
54 integer endSample = rightSample + halfnsamp_window;
55 if (startSample < 1) {
56 Melder_casual (U"Start sample too small: ", startSample,
57 U" instead of 1.");
58 startSample = 1;
59 }
60 if (endSample > my nx) {
61 Melder_casual (U"End sample too small: ", endSample,
62 U" instead of ", my nx,
63 U".");
64 endSample = my nx;
65 }
66
67 /* Copy a window to a frame. */
68 for (integer i = 1; i <= nsamp_window; i ++)
69 window -> z [1] [i] =
70 ( my ny == 1 ? my z [1] [i+startSample-1] : 0.5 * (my z [1] [i+startSample-1] + my z [2] [i+startSample-1]) ) *
71 (0.5 - 0.5 * cos (2.0 * NUMpi * i / (nsamp_window + 1)));
72 autoSpectrum spec = Sound_to_Spectrum (window.get(), true);
73 autoExcitation excitation = Spectrum_to_Excitation (spec.get(), df);
74 for (integer ifreq = 1; ifreq <= nf; ifreq ++)
75 thy z [ifreq] [iframe] = excitation -> z [1] [ifreq] + ( iframe > 1 ? dampingFactor * thy z [ifreq] [iframe - 1] : 0 );
76 }
77 for (integer iframe = 1; iframe <= nFrames; iframe ++)
78 for (integer ifreq = 1; ifreq <= nf; ifreq ++)
79 thy z [ifreq] [iframe] *= integrationCorrection;
80 return thee;
81 } catch (MelderError) {
82 Melder_throw (me, U": not converted to Cochleagram.");
83 }
84 }
85
86 static autoSound createGammatone (double midFrequency_Hertz, double samplingFrequency) {
87 double lengthOfGammatone_seconds = 50.0 / midFrequency_Hertz; // 50 periods
88 integer lengthOfGammatone_samples;
89 /* EdB's alfa1: */
90 double latency = 1.95e-3 * pow (midFrequency_Hertz / 1000, -0.725) + 0.6e-3;
91 /* EdB's beta: */
92 double decayTime = 1e-3 * pow (midFrequency_Hertz / 1000, -0.663);
93 /* EdB's omega: */
94 double midFrequency_radPerSecond = 2 * NUMpi * midFrequency_Hertz;
95 autoSound gammatone = Sound_createSimple (1, lengthOfGammatone_seconds, samplingFrequency);
96 lengthOfGammatone_samples = gammatone -> nx;
97 for (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 -> z [1] [itime] =
102 x * x * x * exp (- x) * cos (midFrequency_radPerSecond * timeAfterLatency);
103 }
104 return gammatone;
105 }
106
107 autoCochleagram Sound_to_Cochleagram_edb
108 (Sound me, double dtime, double dfreq, int hasSynapse, double replenishmentRate,
109 double lossRate, double returnRate, double reprocessingRate)
110 {
111 try {
112 double duration_seconds = my xmax;
113 if (dtime < my dx) dtime = my dx;
114 integer ntime = Melder_iround (duration_seconds / dtime);
115 if (ntime < 2) return autoCochleagram ();
116 integer nfreq = Melder_iround (25.6 / dfreq); // 25.6 Bark = highest frequency
117
118 autoCochleagram thee = Cochleagram_create (my xmin, my 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 (integer ifreq = 1; ifreq <= nfreq; ifreq ++) {
124 double *response = & thy z [ifreq] [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 = NUMbarkToHertz (midFrequency_Bark);
131 autoSound gammatone = createGammatone (midFrequency_Hertz, 1.0 / my dx);
132 autoSound basil = Sounds_convolve (me, gammatone.get(), kSounds_convolve_scaling::SUM, 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 = my 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 - exp (- g * dt);
146 double ydt = 1.0 - exp (- y * dt);
147 double ldt = (1.0 - exp (- (l + r) * dt)) * l / (l + r);
148 double rdt = (1.0 - exp (- (l + r) * dt)) * r / (l + r);
149 double xdt = 1.0 - exp (- 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 (integer itime = 1; itime <= basil -> nx; itime ++) {
155 double splusA = basil -> z [1] [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 -> z [1] [itime] = h * c;
166 }
167 }
168
169 if (dtime == my dx) {
170 for (integer itime = 1; itime <= ntime; itime ++)
171 response [itime] = basil -> z [1] [itime];
172 } else {
173 double d = dtime / basil -> dx / 2.0;
174 double factor = -6 / d / d;
175 double area = d * sqrt (NUMpi / 6);
176 double expmin6 = exp (-6), onebyoneminexpmin6 = 1 / (1 - expmin6);
177 for (integer itime = 1; itime <= ntime; itime ++) {
178 double t1 = (itime - 1) * dtime;
179 double t2 = t1 + dtime;
180 double mean = 0.0;
181 integer i1, i2;
182 integer n = Matrix_getWindowSamplesX (basil.get(), t1, t2, & i1, & i2);
183 Melder_assert (n >= 1);
184 if (n <= 2) {
185 for (integer isamp = i1; isamp <= i2; isamp ++)
186 mean += basil -> z [1] [isamp];
187 mean /= n;
188 } else {
189 integer muint = Melder_ifloor ((i1 + i2) / 2.0), dint = Melder_ifloor (d);
190 for (integer isamp = muint - dint; isamp <= muint + dint; isamp ++) {
191 double y = 0;
192 if (isamp < 1 || isamp > basil -> nx)
193 Melder_casual (U"isamp ", isamp);
194 else
195 y = basil -> z [1] [isamp];
196 mean += y * onebyoneminexpmin6 * (exp (factor * (isamp - muint) *
197 (isamp - muint)) - expmin6);
198 }
199 mean /= area;
200 }
201 response [itime] = mean;
202 }
203 }
204 }
205 return thee;
206 } catch (MelderError) {
207 Melder_throw (me, U": not converted to Cochleagram (edb).");
208 }
209 }
210
211 /* End of file Sound_to_Cochleagram.cpp */