Debian Codegraph @

dwtools/Sound_and_Spectrogram_extensions.cpp

raw symbols SCIP
1 /* Sound_and_Spectrogram_extensions.cpp
2 *
3 * Copyright (C) 1993-2024 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 20010718
21 djmw 20020813 GPL header.
22 djmw 20041124 Changed call to Sound_to_Spectrum.
23 djmw 20070103 Sound interface changes
24 djmw 20071107 Errors/warnings text changes
25 djmw 20071202 Melder_warning<n>
26 */
27
28 #include "Sound_and_Spectrogram_extensions.h"cxx . . $ `<file>/dwtools/Sound_and_Spectrogram_extensions.h`/
29 #include "Sound_extensions.h"cxx . . $ `<file>/dwtools/Sound_extensions.h`/
30 #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/
31 #include "Sound_to_Pitch.h"cxx . . $ `<file>/fon/Sound_to_Pitch.h`/
32 #include "Vector.h"cxx . . $ `<file>/fon/Vector.h`/
33 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
34
35 autoSoundcxx . . $ autoSound# BandFilterSpectrogram_as_Soundcxx . . $ BandFilterSpectrogram_as_Sound(816802e9f10cd434). (BandFilterSpectrogramcxx . . $ BandFilterSpectrogram# me, int to_dB);
36
37 /*
38 The gaussian(x) = (exp(-48*((i-(n+1)/2)/(n+1))^2)-exp(-12))/(1-exp(-12));
39 For power we need the area under the square of this window:
40 Integrate (gaussian(i)^2,i=1..n) =
41 (sqrt(Pi)*sqrt(3)*sqrt(2)*erf(2*(n-1)*sqrt(3)*sqrt(2)/(n+1))*(n+1) + 24*exp(-24)*(n-1)+
42 -4*sqrt(Pi)*sqrt(3)*exp(-12)*erf(2*(n-1)*sqrt(3)/(n+1))*(n+1))/ (24 * (-1+exp(-12))^2),
43 where erf(x) = 1 - erfc(x) and n is the windowLength in samples.
44 To compare with the rectangular window we need to divide this by the window width (n -1) x 1^2.
45 */
46 static void _Spectrogram_windowCorrectioncxx . . $ _Spectrogram_windowCorrection(3b0637a0e0d21b36). (Spectrogramcxx . . $ Spectrogram# me, integercxx . . $ integer# numberOfSamples_window) {
47 double windowFactor = 1.0;
48 if (numberOfSamples_window > 1) {
49 const double e12 = expcxx . . $ exp(7759b11ec49fe502). (-12);
50 const double denum = (e12 - 1) * (e12 - 1.0) * 24 * (numberOfSamples_window - 1);
51 const double arg1 = 2.0 * NUMsqrt3cxx . . $ NUMsqrt3. * (numberOfSamples_window - 1) / (numberOfSamples_window + 1);
52 const double arg2 = arg1 * NUMsqrt2cxx . . $ NUMsqrt2.;
53 const double p2 = NUMsqrtpicxx . . $ NUMsqrtpi. * NUMsqrt3cxx . . $ NUMsqrt3. * NUMsqrt2cxx . . $ NUMsqrt2. * (1 - NUMerfcccxx . . $ NUMerfcc(447c67c6676f75c3). (arg2)) * (numberOfSamples_window + 1);
54 const double p1 = 4 * NUMsqrtpicxx . . $ NUMsqrtpi. * NUMsqrt3cxx . . $ NUMsqrt3. * e12 * (1 - NUMerfcccxx . . $ NUMerfcc(447c67c6676f75c3). (arg1)) * (numberOfSamples_window + 1);
55 windowFactor = (p2 - p1 + 24 * (numberOfSamples_window - 1) * e12 * e12) / denum;
56 }
57 mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..getcxx . . $ automatrix#get(b2b81450b5bdda66).() /=cxx . . $ `operator/=`(50b469ae42a42f64). windowFactor;
58 }
59
60 static autoSpectrumcxx . . $ autoSpectrum# Sound_to_Spectrum_powercxx . . $ Sound_to_Spectrum_power(ca0e2140ea6ec12a). (Soundcxx . . $ Sound# me) {
61 try {
62 autoSpectrumcxx . . $ autoSpectrum# thee = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (me, true);
63 double scale = 2.0 * thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx. / (mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.);
64 /*
65 factor '2' because we combine positive and negative frequencies
66 thy dx : width of frequency bin
67 my xmax - my xmin : duration of sound
68 */
69 VECcxx . . $ VEC# re = thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1), im = thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2);
70 for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; i ++) {
71 const double power = scale * (re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]);
72 re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = power;
73 im [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 0.0;
74 }
75 /*
76 Correction of frequency bins at 0 Hz and nyquist: don't count for two.
77 */
78 re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] *= 0.5;
79 re [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.] *= 0.5;
80 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
81 } catch (MelderErrorcxx . . $ MelderError#) {
82 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Spectrum with spectral power created.");
83 }
84 }
85
86 static void Sound_into_BarkSpectrogram_framecxx . . $ Sound_into_BarkSpectrogram_frame(a76285bdff54fac). (Soundcxx . . $ Sound# me, BarkSpectrogramcxx . . $ BarkSpectrogram# thee, integercxx . . $ integer# frame) {
87 autoSpectrumcxx . . $ autoSpectrum# him = Sound_to_Spectrum_powercxx . . $ Sound_to_Spectrum_power(ca0e2140ea6ec12a). (me);
88 integercxx . . $ integer# numberOfFrequencies = hiscxx . . $ `melder/melder.h:47:9`! nxcxx . . $ structSampled#nx.;
89
90 autoVECcxx . . $ autoVEC# z = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfFrequencies);
91 for (integercxx . . $ integer# ifreq = 1; ifreq <= numberOfFrequencies; ifreq ++) {
92 const double frequency_Hz = hiscxx . . $ `melder/melder.h:47:9`! x1cxx . . $ structSampled#x1. + (ifreq - 1) * hiscxx . . $ `melder/melder.h:47:9`! dxcxx . . $ structSampled#dx.;
93 z [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ifreq] = thycxx . . $ `melder/melder.h:45:9`! v_hertzToFrequencycxx . . $ structBarkSpectrogram#v_hertzToFrequency(447c67c6676f75c3). (frequency_Hz);
94 }
95
96 for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! nycxx . . $ structSampledXY#ny.; i ++) {
97 const double z0 = thycxx . . $ `melder/melder.h:45:9`! y1cxx . . $ structSampledXY#y1. + (i - 1) * thycxx . . $ `melder/melder.h:45:9`! dycxx . . $ structSampledXY#dy.;
98 constVECcxx . . $ constVEC# pow = hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); // TODO ??
99 longdoublecxx . . $ longdouble# p = 0.0;
100 for (integercxx . . $ integer# ifreq = 1; ifreq <= numberOfFrequencies; ifreq ++) {
101 /*
102 Sekey & Hanson filter is defined in the power domain.
103 We therefore multiply the power with a (and not a^2).
104 integral (F(z),z=0..25) = 1.58/9
105 */
106 const double a = NUMsekeyhansonfilter_amplitudecxx . . $ NUMsekeyhansonfilter_amplitude(d8e7e0733fa41c87). (z0, z [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ifreq]);
107 p += a * pow [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ifreq] ;
108 }
109 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).frame] = double (p);
110 }
111 }
112
113 autoBarkSpectrogramcxx . . $ autoBarkSpectrogram# Sound_to_BarkSpectrogramcxx . . $ Sound_to_BarkSpectrogram(2a7e1fd26093cab). (Soundcxx . . $ Sound# me, double analysisWidth, double dt, double f1_bark, double fmax_bark, double df_bark) {
114 try {
115 const double samplingFrequency = 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., nyquist = 0.5 * samplingFrequency;
116 const double windowDuration = 2.0 * analysisWidth; /* gaussian window */
117 const double zmax = NUMhertzToBarkcxx . . $ NUMhertzToBark(447c67c6676f75c3). (nyquist);
118 double fmin_bark = 0.0;
119
120 // Check defaults.
121
122 if (f1_bark <= 0.0)
123 f1_bark = 1.0;
124 if (fmax_bark <= 0.0)
125 fmax_bark = zmax;
126 if (df_bark <= 0.0)
127 df_bark = 1.0;
128
129 fmax_bark = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (fmax_bark, zmax);
130 const integercxx . . $ integer# numberOfFilters = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! ( (fmax_bark - f1_bark) / df_bark);
131 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (numberOfFilters > 0,
132 U"The combination of filter parameters is not valid.");
133
134 integercxx . . $ integer# numberOfFrames;
135 double t1;
136 Sampled_shortTermAnalysiscxx . . $ Sampled_shortTermAnalysis(77f703705034c8a). (me, windowDuration, dt, & numberOfFrames, & t1);
137 autoSoundcxx . . $ autoSound# sframe = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, windowDuration, samplingFrequency);
138 autoSoundcxx . . $ autoSound# window = Sound_createGaussiancxx . . $ Sound_createGaussian(c2815364717639a8). (windowDuration, samplingFrequency);
139 autoBarkSpectrogramcxx . . $ autoBarkSpectrogram# thee = BarkSpectrogram_createcxx . . $ BarkSpectrogram_create(b560be76b93ca01d). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfFrames, dt, t1, fmin_bark, fmax_bark, numberOfFilters, df_bark, f1_bark);
140
141 autoMelderProgresscxx . . $ autoMelderProgress# progesscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Sound to BarkSpectrogram...");
142
143 for (integercxx . . $ integer# iframe = 1; iframe <= numberOfFrames; iframe ++) {
144 const double t = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe);
145
146 Sound_into_Soundcxx . . $ Sound_into_Sound(9b5e4c6d6787168f). (me, sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t - windowDuration / 2.0);
147 Sounds_multiplycxx . . $ Sounds_multiply(3740d3b02ef916dc). (sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), window.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
148 Sound_into_BarkSpectrogram_framecxx . . $ Sound_into_BarkSpectrogram_frame(a76285bdff54fac). (sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe);
149
150 if (iframe % 10 == 1)
151 Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). ( (double) iframe / numberOfFrames, U"BarkSpectrogram analysis: frame ",
152 iframe, U" from ", numberOfFrames, U".");
153 }
154
155 _Spectrogram_windowCorrectioncxx . . $ _Spectrogram_windowCorrection(3b0637a0e0d21b36). ((Spectrogramcxx . . $ Spectrogram#) thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), window ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.);
156
157 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
158 } catch (MelderErrorcxx . . $ MelderError#) {
159 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no BarkSpectrogram created.");
160 }
161 }
162
163 static void Sound_into_MelSpectrogram_framecxx . . $ Sound_into_MelSpectrogram_frame(a045d3cbe0b71a7d). (Soundcxx . . $ Sound# me, MelSpectrogramcxx . . $ MelSpectrogram# thee, integercxx . . $ integer# frame) {
164 autoSpectrumcxx . . $ autoSpectrum# him = Sound_to_Spectrum_powercxx . . $ Sound_to_Spectrum_power(ca0e2140ea6ec12a). (me);
165
166 for (integercxx . . $ integer# ifilter = 1; ifilter <= thycxx . . $ `melder/melder.h:45:9`! nycxx . . $ structSampledXY#ny.; ifilter ++) {
167 longdoublecxx . . $ longdouble# power = 0.0;
168 const double fc_mel = thycxx . . $ `melder/melder.h:45:9`! y1cxx . . $ structSampledXY#y1. + (ifilter - 1) * thycxx . . $ `melder/melder.h:45:9`! dycxx . . $ structSampledXY#dy.;
169 const double fc_hz = thycxx . . $ `melder/melder.h:45:9`! v_frequencyToHertzcxx . . $ structMelSpectrogram#v_frequencyToHertz(447c67c6676f75c3). (fc_mel);
170 const double fl_hz = thycxx . . $ `melder/melder.h:45:9`! v_frequencyToHertzcxx . . $ structMelSpectrogram#v_frequencyToHertz(447c67c6676f75c3). (stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (fc_mel - thycxx . . $ `melder/melder.h:45:9`! dycxx . . $ structSampledXY#dy., 0.0));
171 const double fh_hz = thycxx . . $ `melder/melder.h:45:9`! v_frequencyToHertzcxx . . $ structMelSpectrogram#v_frequencyToHertz(447c67c6676f75c3). (stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (fc_mel + thycxx . . $ `melder/melder.h:45:9`! dycxx . . $ structSampledXY#dy., hiscxx . . $ `melder/melder.h:47:9`! xmaxcxx . . $ structFunction#xmax.));
172 integercxx . . $ integer# ifrom, ito;
173 Sampled_getWindowSamplescxx . . $ Sampled_getWindowSamples(fe99094ed5bb3f6a). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), fl_hz, fh_hz, & ifrom, & ito);
174 for (integercxx . . $ integer# i = ifrom; i <= ito; i ++) {
175 /*
176 Bin with a triangular filter the power (= amplitude-squared)
177 */
178 const double f = hiscxx . . $ `melder/melder.h:47:9`! x1cxx . . $ structSampled#x1. + (i - 1) * hiscxx . . $ `melder/melder.h:47:9`! dxcxx . . $ structSampled#dx.;
179 const double a = NUMtriangularfilter_amplitudecxx . . $ NUMtriangularfilter_amplitude(6ba6c9f826937c6e). (fl_hz, fc_hz, fh_hz, f);
180 power += a * hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
181 }
182 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifilter] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).frame] = double (power);
183 }
184 }
185
186 autoMelSpectrogramcxx . . $ autoMelSpectrogram# Sound_to_MelSpectrogramcxx . . $ Sound_to_MelSpectrogram(528140ad90e7d0ad). (Soundcxx . . $ Sound# me, double analysisWidth, double dt, double f1_mel, double fmax_mel, double df_mel) {
187 try {
188 const double samplingFrequency = 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., nyquist = 0.5 * samplingFrequency;
189 const double windowDuration = 2.0 * analysisWidth; // Gaussian window
190 double fmin_mel = 0.0;
191 const double fbottom = NUMhertzToMel2cxx . . $ NUMhertzToMel2(447c67c6676f75c3). (100.0), fceiling = NUMhertzToMel2cxx . . $ NUMhertzToMel2(447c67c6676f75c3). (nyquist);
192
193 // Check defaults.
194
195 if (fmax_mel <= 0.0 || fmax_mel > fceiling)
196 fmax_mel = fceiling;
197 if (fmax_mel <= f1_mel) {
198 f1_mel = fbottom;
199 fmax_mel = fceiling;
200 }
201 if (f1_mel <= 0.0)
202 f1_mel = fbottom;
203 if (df_mel <= 0.0)
204 df_mel = 100.0;
205
206 // Determine the number of filters.
207
208 const integercxx . . $ integer# numberOfFilters = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! ((fmax_mel - f1_mel) / df_mel);
209
210
211 integercxx . . $ integer# numberOfFrames;
212 double t1;
213 Sampled_shortTermAnalysiscxx . . $ Sampled_shortTermAnalysis(77f703705034c8a). (me, windowDuration, dt, & numberOfFrames, & t1);
214 autoSoundcxx . . $ autoSound# sframe = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, windowDuration, samplingFrequency);
215 autoSoundcxx . . $ autoSound# window = Sound_createGaussiancxx . . $ Sound_createGaussian(c2815364717639a8). (windowDuration, samplingFrequency);
216 autoMelSpectrogramcxx . . $ autoMelSpectrogram# thee = MelSpectrogram_createcxx . . $ MelSpectrogram_create(ac799e7c17ea0a99). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfFrames, dt, t1, fmin_mel, fmax_mel, numberOfFilters, df_mel, f1_mel);
217
218 autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Sound to MelSpectrogram...");
219
220 for (integercxx . . $ integer# iframe = 1; iframe <= numberOfFrames; iframe ++) {
221 const double t = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe);
222 Sound_into_Soundcxx . . $ Sound_into_Sound(9b5e4c6d6787168f). (me, sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t - windowDuration / 2.0);
223 Sounds_multiplycxx . . $ Sounds_multiply(3740d3b02ef916dc). (sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), window.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
224 Sound_into_MelSpectrogram_framecxx . . $ Sound_into_MelSpectrogram_frame(a045d3cbe0b71a7d). (sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe);
225
226 if (iframe % 10 == 1)
227 Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). ((double) iframe / numberOfFrames, U"Frame ", iframe, U" out of ", numberOfFrames, U".");
228 }
229
230 _Spectrogram_windowCorrectioncxx . . $ _Spectrogram_windowCorrection(3b0637a0e0d21b36). ((Spectrogramcxx . . $ Spectrogram#) thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), window ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.);
231
232 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
233 } catch (MelderErrorcxx . . $ MelderError#) {
234 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": No MelSpectrogram created.");
235 }
236 }
237
238 /*
239 Analog formant filter response :
240 H(f) = i f B / (f1^2 - f^2 + i f B)
241 */
242 static int Sound_into_Spectrogram_framecxx . . $ Sound_into_Spectrogram_frame(91b8dca92893443f). (Soundcxx . . $ Sound# me, Spectrogramcxx . . $ Spectrogram# thee, integercxx . . $ integer# frame, double bw) {
243 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (bw > 0.0);
244 autoSpectrumcxx . . $ autoSpectrum# him = Sound_to_Spectrum_powercxx . . $ Sound_to_Spectrum_power(ca0e2140ea6ec12a). (me);
245
246 for (integercxx . . $ integer# ifilter = 1; ifilter <= thycxx . . $ `melder/melder.h:45:9`! nycxx . . $ structSampledXY#ny.; ifilter ++) {
247 const double fc = thycxx . . $ `melder/melder.h:45:9`! y1cxx . . $ structSampledXY#y1. + (ifilter - 1) * thycxx . . $ `melder/melder.h:45:9`! dycxx . . $ structSampledXY#dy.;
248 constVECcxx . . $ constVEC# pow = hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1);
249 double p = 0.0;
250 for (integercxx . . $ integer# ifreq = 1; ifreq <= hiscxx . . $ `melder/melder.h:47:9`! nxcxx . . $ structSampled#nx.; ifreq ++) {
251 /*
252 H(f) = ifB / (fc^2 - f^2 + ifB)
253 H(f)| = fB / sqrt ((fc^2 - f^2)^2 + f^2B^2)
254 |H(f)|^2 = f^2B^2 / ((fc^2 - f^2)^2 + f^2B^2)
255 = 1 / (((fc^2 - f^2) /fB)^2 + 1)
256 */
257 const double f = hiscxx . . $ `melder/melder.h:47:9`! x1cxx . . $ structSampled#x1. + (ifreq - 1) * hiscxx . . $ `melder/melder.h:47:9`! dxcxx . . $ structSampled#dx.;
258 const double a = NUMformantfilter_amplitudecxx . . $ NUMformantfilter_amplitude(7678bba071383ab). (fc, bw, f);
259 p += a * pow [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).ifreq];
260 }
261 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifilter] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).frame] = p;
262 }
263 return 1;
264 }
265
266 autoSpectrogramcxx . . $ autoSpectrogram# Sound_to_Spectrogram_pitchDependentcxx . . $ Sound_to_Spectrogram_pitchDependent(2be3a662e82e66d1). (Soundcxx . . $ Sound# me, double analysisWidth, double dt, double f1_hz, double fmax_hz, double df_hz, double relative_bw,
267 double pitchFloor, double pitchCeiling)
268 {
269 try {
270 autoPitchcxx . . $ autoPitch# thee = Sound_to_Pitchcxx . . $ Sound_to_Pitch(c773ddec40eb7e2b). (me, dt, pitchFloor, pitchCeiling);
271 return Sound_Pitch_to_Spectrogramcxx . . $ Sound_Pitch_to_Spectrogram(2f4bc0119ecc15c8). (me, thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), analysisWidth, dt, f1_hz, fmax_hz, df_hz, relative_bw);
272 } catch (MelderErrorcxx . . $ MelderError#) {
273 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Spectrogram created.");
274 }
275 }
276
277 autoSpectrogramcxx . . $ autoSpectrogram# Sound_Pitch_to_Spectrogramcxx . . $ Sound_Pitch_to_Spectrogram(2f4bc0119ecc15c8). (Soundcxx . . $ Sound# me, Pitchcxx . . $ Pitch# thee, double analysisWidth, double dt, double f1_hz, double fmax_hz, double df_hz, double relative_bw) {
278 try {
279 const double windowDuration = 2.0 * analysisWidth; /* gaussian window */
280 const double nyquist = 0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., samplingFrequency = 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., fmin_hz = 0.0;
281
282 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! xmin >= thycxx . . $ `melder/melder.h:45:9`! xmin && mycxx . . $ `melder/melder.h:44:9`! xmax <= thycxx . . $ `melder/melder.h:45:9`! xmax,
283 U"The domain of the Sound should be included in the domain of the Pitch.");
284
285 double f0_median = Pitch_getQuantilecxx . . $ Pitch_getQuantile(5072ddcaf167d3af). (thee, thycxx . . $ `melder/melder.h:45:9`! xmincxx . . $ structFunction#xmin., thycxx . . $ `melder/melder.h:45:9`! xmaxcxx . . $ structFunction#xmax., 0.5, kPitch_unitcxx . . $ kPitch_unit#::HERTZcxx . . $ kPitch_unit#HERTZ.);
286
287 if (isundefcxx . . $ isundef(f8197d536af80c96). (f0_median) || f0_median == 0.0) {
288 f0_median = 100.0;
289 Melder_warningcxx . . $ Melder_warning(6298c93241566110). (U"Pitch values undefined. Bandwidth fixed to 100 Hz. ");
290 }
291
292 if (f1_hz <= 0.0)
293 f1_hz = 100.0;
294 if (fmax_hz <= 0.0)
295 fmax_hz = nyquist;
296 if (df_hz <= 0.0)
297 df_hz = f0_median / 2.0;
298 if (relative_bw <= 0.0)
299 relative_bw = 1.1;
300
301 fmax_hz = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (fmax_hz, nyquist);
302 const integercxx . . $ integer# numberOfFilters = Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! ( (fmax_hz - f1_hz) / df_hz);
303
304 double t1;
305 integercxx . . $ integer# numberOfFrames, numberOfUndefinedPitchFrames = 0;
306 Sampled_shortTermAnalysiscxx . . $ Sampled_shortTermAnalysis(77f703705034c8a). (me, windowDuration, dt, & numberOfFrames, & t1);
307 autoSpectrogramcxx . . $ autoSpectrogram# him = Spectrogram_createcxx . . $ Spectrogram_create(afddb115247e4bb0). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfFrames, dt, t1, fmin_hz, fmax_hz, numberOfFilters, df_hz, f1_hz);
308
309 // Temporary objects
310
311 autoSoundcxx . . $ autoSound# sframe = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, windowDuration, samplingFrequency);
312 autoSoundcxx . . $ autoSound# window = Sound_createGaussiancxx . . $ Sound_createGaussian(c2815364717639a8). (windowDuration, samplingFrequency);
313 autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Sound & Pitch: To Spectrogram...");
314 for (integercxx . . $ integer# iframe = 1; iframe <= numberOfFrames; iframe ++) {
315 const double t = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe);
316 double f0 = Pitch_getValueAtTimecxx . . $ Pitch_getValueAtTime(b0f5a36024d7459c). (thee, t, kPitch_unitcxx . . $ kPitch_unit#::HERTZcxx . . $ kPitch_unit#HERTZ., 0);
317
318 if (isundefcxx . . $ isundef(f8197d536af80c96). (f0) || f0 == 0.0) {
319 numberOfUndefinedPitchFrames ++;
320 f0 = f0_median;
321 }
322 const double b = relative_bw * f0;
323 Sound_into_Soundcxx . . $ Sound_into_Sound(9b5e4c6d6787168f). (me, sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), t - windowDuration / 2.0);
324 Sounds_multiplycxx . . $ Sounds_multiply(3740d3b02ef916dc). (sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), window.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
325
326 Sound_into_Spectrogram_framecxx . . $ Sound_into_Spectrogram_frame(91b8dca92893443f). (sframe.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe, b);
327
328 if (iframe % 10 == 1)
329 Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). ((double) iframe / numberOfFrames, U"Frame ", iframe, U" out of ",
330 numberOfFrames, U".");
331 }
332
333 _Spectrogram_windowCorrectioncxx . . $ _Spectrogram_windowCorrection(3b0637a0e0d21b36). (him.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), window ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.);
334
335 return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
336 } catch (MelderErrorcxx . . $ MelderError#) {
337 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"FormantFilter not created from Pitch & FormantFilter.");
338 }
339 }
340
341 autoSoundcxx . . $ autoSound# BandFilterSpectrogram_as_Soundcxx . . $ BandFilterSpectrogram_as_Sound(816802e9f10cd434). (BandFilterSpectrogramcxx . . $ BandFilterSpectrogram# me, int unit) {
342 try {
343 autoSoundcxx . . $ autoSound# thee = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny., mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.);
344 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; i ++)
345 for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; j ++)
346 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).i] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).j] = mycxx . . $ `melder/melder.h:44:9`! v_getValueAtSamplecxx . . $ structBandFilterSpectrogram#v_getValueAtSample(8bbfc6ce471470c9). (j, i, unit);
347 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
348 } catch (MelderErrorcxx . . $ MelderError#) {
349 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Sound created.");
350 }
351 }
352
353 autoSoundcxx . . $ autoSound# BandFilterSpectrograms_crossCorrelatecxx . . $ BandFilterSpectrograms_crossCorrelate(16c861ee153843f6). (BandFilterSpectrogramcxx . . $ BandFilterSpectrogram# me, BandFilterSpectrogramcxx . . $ BandFilterSpectrogram# thee, enum kSounds_convolve_scalingcxx . . $ kSounds_convolve_scaling# scaling, enum kSounds_convolve_signalOutsideTimeDomaincxx . . $ kSounds_convolve_signalOutsideTimeDomain# signalOutsideTimeDomain) {
354 try {
355 autoSoundcxx . . $ autoSound# sme = BandFilterSpectrogram_as_Soundcxx . . $ BandFilterSpectrogram_as_Sound(816802e9f10cd434). (me, 1); // to dB
356 autoSoundcxx . . $ autoSound# sthee = BandFilterSpectrogram_as_Soundcxx . . $ BandFilterSpectrogram_as_Sound(816802e9f10cd434). (thee, 1);
357 autoSoundcxx . . $ autoSound# cc = Sounds_crossCorrelatecxx . . $ Sounds_crossCorrelate(b7360aaa447e63e). (sme.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), sthee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), scaling, signalOutsideTimeDomain);
358 return cccxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
359 } catch (MelderErrorcxx . . $ MelderError#) {
360 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" and ", thee, U" not cross-correlated.");
361 }
362 }
363
364 autoSoundcxx . . $ autoSound# BandFilterSpectrograms_convolvecxx . . $ BandFilterSpectrograms_convolve(16c861ee153843f6). (BandFilterSpectrogramcxx . . $ BandFilterSpectrogram# me, BandFilterSpectrogramcxx . . $ BandFilterSpectrogram# thee, enum kSounds_convolve_scalingcxx . . $ kSounds_convolve_scaling# scaling, enum kSounds_convolve_signalOutsideTimeDomaincxx . . $ kSounds_convolve_signalOutsideTimeDomain# signalOutsideTimeDomain) {
365 try {
366 autoSoundcxx . . $ autoSound# sme = BandFilterSpectrogram_as_Soundcxx . . $ BandFilterSpectrogram_as_Sound(816802e9f10cd434). (me, 1); // to dB
367 autoSoundcxx . . $ autoSound# sthee = BandFilterSpectrogram_as_Soundcxx . . $ BandFilterSpectrogram_as_Sound(816802e9f10cd434). (thee, 1);
368 autoSoundcxx . . $ autoSound# cc = Sounds_convolvecxx . . $ Sounds_convolve(b7360aaa447e63e). (sme.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), sthee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), scaling, signalOutsideTimeDomain);
369 return cccxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
370 } catch (MelderErrorcxx . . $ MelderError#) {
371 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U" and ", thee, U" not convolved.");
372 }
373 }
374
375 /* End of file Sound_and_Spectrogram_extensions.cpp */