Debian Codegraph @

dwtools/Sound_to_SPINET.cpp

raw symbols SCIP
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"
25 #include "NUM2.h"
26
27 static double fgamma (double x, integer n) {
28 const double x2p1 = 1.0 + x * x;
29 double d = x2p1;
30 for (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 autoSPINET Sound_to_SPINET (Sound me, double timeStep, double windowDuration, double minimumFrequencyHz, double maximumFrequencyHz, integer numberOfGammaFilters, double excitationErbProportion, double inhibitionErbProportion) {
41 try {
42 const double b = 1.02, samplingFrequency = 1.0 / my dx;
43
44 if (timeStep < my dx)
45 timeStep = my dx;
46 if (maximumFrequencyHz > samplingFrequency / 2.0)
47 maximumFrequencyHz = samplingFrequency / 2.0;
48
49 integer numberOfFrames;
50 double firstTime;
51 Sampled_shortTermAnalysis (me, windowDuration, timeStep, & numberOfFrames, & firstTime);
52 autoSPINET thee = SPINET_create (my xmin, my xmax, numberOfFrames, timeStep, firstTime, minimumFrequencyHz, maximumFrequencyHz, numberOfGammaFilters, excitationErbProportion, inhibitionErbProportion);
53 autoSound window = Sound_createGaussian (windowDuration, samplingFrequency);
54 autoSound frame = Sound_createSimple (1, windowDuration, samplingFrequency);
55 autoVEC f = raw_VEC (numberOfGammaFilters);
56 autoVEC bw = raw_VEC (numberOfGammaFilters);
57 autoVEC aex = raw_VEC (numberOfGammaFilters);
58 autoVEC ain = raw_VEC (numberOfGammaFilters);
59 /*
60 Cochlear filterbank: gammatone.
61 */
62 for (integer i = 1; i <= numberOfGammaFilters; i ++) {
63 f [i] = NUMerbToHertz (thy y1 + (i - 1) * thy dy);
64 bw [i] = NUM2pi * b * (f [i] * (6.23e-6 * f [i] + 93.39e-3) + 28.52);
65 }
66
67 autoMelderProgress progress (U"Sound to SPINET...");
68
69 for (integer i = 1; i <= numberOfGammaFilters; i ++) {
70 const double bb = (f [i] / 1000.0) * exp (- f [i] / 1000.0); // outer & middle ear and phase locking
71 const double tgammaMax = (thy gamma - 1) / bw [i]; // the time where the gamma function envelope has its maximum
72 const double gammaMaxAmplitude = pow ((thy gamma - 1) / (NUMe * bw [i]), thy gamma - 1);
73 const double timeCorrection = tgammaMax - windowDuration / 2.0;
74
75 autoSound gammaTone = Sound_createGammaTone (0.0, 0.1, samplingFrequency, thy gamma, b, f [i], 0.0, 0.0, false);
76 autoSound filtered = Sounds_convolve (me, gammaTone.get(), kSounds_convolve_scaling::SUM, kSounds_convolve_signalOutsideTimeDomain::ZERO);
77 /*
78 To energy measure: weigh with broad-band transfer function.
79 */
80 for (integer j = 1; j <= numberOfFrames; j ++) {
81 Sound_into_Sound (filtered.get(), frame.get(), Sampled_indexToX (thee.get(), j) + timeCorrection);
82 Sounds_multiply (frame.get(), window.get());
83 thy y [i] [j] = Sound_power (frame.get()) * bb / gammaMaxAmplitude;
84 }
85 Melder_progress ((double) i / numberOfGammaFilters, U"SPINET: filter ", i, U" from ", numberOfGammaFilters, U".");
86 }
87 /*
88 Excitatory and inhibitory area functions.
89 */
90 for (integer i = 1; i <= numberOfGammaFilters; i ++) {
91 for (integer k = 1; k <= numberOfGammaFilters; k ++) {
92 const double fr = (f [k] - f [i]) / bw [i];
93 aex [i] += fgamma (fr / thy excitationErbProportion, thy gamma);
94 ain [i] += fgamma (fr / thy inhibitionErbProportion, thy gamma);
95 }
96 }
97 /*
98 On-center off-surround interactions
99 */
100 for (integer j = 1; j <= numberOfFrames; j ++)
101 for (integer i = 1; i <= numberOfGammaFilters; i ++) {
102 longdouble a = 0.0;
103 for (integer k = 1; k <= numberOfGammaFilters; k ++) {
104 const double fr = (f [k] - f [i]) / bw [i];
105 const double hexsq = fgamma (fr / thy excitationErbProportion, thy gamma);
106 const double hinsq = fgamma (fr / thy inhibitionErbProportion, thy gamma);
107 a += thy y [k] [j] * (hexsq / aex [i] - hinsq / ain [i]);
108 }
109 thy s [i] [j] = a > 0.0 ? (double) a : 0.0;
110 }
111 return thee;
112 } catch (MelderError) {
113 Melder_throw (me, U": no SPINET created.");
114 }
115 }
116
117 /* End of file Sound_to_SPINET.cpp */