Debian Codegraph @

fon/Sound_and_Spectrogram.cpp

raw symbols SCIP
1 /* Sound_and_Spectrogram.cpp
2 *
3 * Copyright (C) 1992-2011,2014-2020,2023,2025 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 3 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 #include "Sound_and_Spectrogram.h"cxx . . $ `<file>/fon/Sound_and_Spectrogram.h`/
20 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
21
22 #include "enums_getText.h"cxx . . $ `<file>/melder/enums_getText.h`/
23 #include "Sound_and_Spectrogram_enums.h"cxx . . $ `<file>/fon/Sound_and_Spectrogram_enums.h`/
24 #include "enums_getValue.h"cxx . . $ `<file>/melder/enums_getValue.h`/
25 #include "Sound_and_Spectrogram_enums.h"cxx . . $ `<file>/fon/Sound_and_Spectrogram_enums.h`/
26
27 autoSpectrogramcxx . . $ autoSpectrogram# Sound_to_Spectrogram_ecxx . . $ Sound_to_Spectrogram_e(2715189e1964953). (
28 const constSoundcxx . . $ constSound# me,
29 const double effectiveAnalysisWidth,
30 /* mutable clip */ double fmax,
31 const double minimumTimeStep1,
32 const double minimumFreqStep1,
33 const kSound_to_Spectrogram_windowShape windowType,
34 const double maximumTimeOversampling,
35 const double maximumFreqOversampling
36 ) {
37 try {
38 const double nyquist = 0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
39 const double physicalAnalysisWidth =
40 ( windowType == kSound_to_Spectrogram_windowShape::GAUSSIAN ? 2.0 * effectiveAnalysisWidth : effectiveAnalysisWidth );
41 const double effectiveTimeWidth = effectiveAnalysisWidth / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (NUMpicxx . . $ NUMpi.);
42 const double effectiveFreqWidth = 1.0 / effectiveTimeWidth;
43 const double minimumTimeStep2 = effectiveTimeWidth / maximumTimeOversampling;
44 const double minimumFreqStep2 = effectiveFreqWidth / maximumFreqOversampling;
45 const double timeStep = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (minimumTimeStep1, minimumTimeStep2);
46 double freqStep = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (minimumFreqStep1, minimumFreqStep2);
47 volatile const double physicalDuration = mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; // volatile, because we need to truncate to 64 bits
48
49 /*
50 Compute the time sampling.
51 */
52 const integercxx . . $ integer# approximateNumberOfSamplesPerWindow = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (physicalAnalysisWidth / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.);
53 const integercxx . . $ integer# halfnsamp_window = approximateNumberOfSamplesPerWindow / 2 - 1;
54 const integercxx . . $ integer# nsamp_window = halfnsamp_window * 2;
55 if (nsamp_window < 1)
56 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Your analysis window is too short: less than two samples.");
57 if (physicalAnalysisWidth > physicalDuration)
58 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Your sound is too short:\n"
59 U"it should be at least as long as ",
60 windowType == kSound_to_Spectrogram_windowShape::GAUSSIAN ? U"two window lengths." : U"one window length.");
61 const integercxx . . $ integer# numberOfTimes = 1 + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((physicalDuration - physicalAnalysisWidth) / timeStep); // >= 1
62 const double t1 = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + 0.5 * ((mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1) * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. - (numberOfTimes - 1) * timeStep); // centre of first frame
63
64 /*
65 Compute the frequency sampling of the FFT spectrum.
66 */
67 if (fmax <= 0.0 || fmax > nyquist)
68 fmax = nyquist;
69 integercxx . . $ integer# numberOfFreqs = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (fmax / freqStep);
70 if (numberOfFreqs < 1)
71 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"No frequency bins in spectrogram analysis. "
72 U"Raise the maximum frequency, or raise the window length. (Or lower the frequency step, if you have that.)");
73 integercxx . . $ integer# nsampFFT = 1;
74 while (nsampFFT < nsamp_window || nsampFFT < 2 * numberOfFreqs * (nyquist / fmax))
75 nsampFFT *= 2;
76 const integercxx . . $ integer# half_nsampFFT = nsampFFT / 2;
77
78 /*
79 Compute the frequency sampling of the spectrogram.
80 */
81 const integercxx . . $ integer# binWidth_samples = stdcxx . . $ std/::maxcxx . . $ std/max(2a338fce5842aa6a). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (freqStep * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * nsampFFT));
82 double binWidth_hertz = 1.0 / (mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * nsampFFT);
83 freqStep = binWidth_samples * binWidth_hertz;
84 numberOfFreqs = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (fmax / freqStep);
85 //Melder_assert (numberOfFreqs >= 1);
86 if (numberOfFreqs < 1)
87 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"No frequency bins in spectrogram analysis. "
88 U"Raise the maximum frequency, or raise the window length. (Or lower the frequency step, if you have that.)");
89
90 autoSpectrogramcxx . . $ autoSpectrogram# thee = Spectrogram_createcxx . . $ Spectrogram_create(afddb115247e4bb0). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfTimes, timeStep, t1,
91 0.0, fmax, numberOfFreqs, freqStep, 0.5 * (freqStep - binWidth_hertz));
92
93 autoVECcxx . . $ autoVEC# window = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (nsamp_window);
94 longdoublecxx . . $ longdouble# windowssq = 0.0;
95 for (integercxx . . $ integer# i = 1; i <= nsamp_window; i ++) {
96 const double nSamplesPerWindow_f = physicalAnalysisWidth / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
97 switch (windowType) {
98 case kSound_to_Spectrogram_windowShape::SQUARE: {
99 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = 1.0;
100 } break;
101 case kSound_to_Spectrogram_windowShape::HAMMING: {
102 const double phase = (double) i / nSamplesPerWindow_f; // 0 .. 1
103 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = 0.54 - 0.46 * coscxx . . $ cos(fee4f1d22bc8e617). (2.0 * NUMpicxx . . $ NUMpi. * phase);
104 } break;
105 case kSound_to_Spectrogram_windowShape::BARTLETT: {
106 const double phase = (double) i / nSamplesPerWindow_f; // 0 .. 1
107 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = 1.0 - fabscxx . . $ fabs(fee4f1d22bc8e617). ((2.0 * phase - 1.0));
108 } break;
109 case kSound_to_Spectrogram_windowShape::WELCH: {
110 const double phase = (double) i / nSamplesPerWindow_f; // 0 .. 1
111 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = 1.0 - (2.0 * phase - 1.0) * (2.0 * phase - 1.0);
112 } break;
113 case kSound_to_Spectrogram_windowShape::HANNING: {
114 const double phase = (double) i / nSamplesPerWindow_f; // 0 .. 1
115 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = 0.5 * (1.0 - coscxx . . $ cos(fee4f1d22bc8e617). (2.0 * NUMpicxx . . $ NUMpi. * phase));
116 } break;
117 case kSound_to_Spectrogram_windowShape::GAUSSIAN: {
118 const double imid = 0.5 * (double) (nsamp_window + 1), edge = expcxx . . $ exp(fee4f1d22bc8e617). (-12.0);
119 const double phase = ((double) i - imid) / nSamplesPerWindow_f; // -0.5 .. +0.5
120 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = (expcxx . . $ exp(fee4f1d22bc8e617). (-48.0 * phase * phase) - edge) / (1.0 - edge);
121 break;
122 }
123 break; default:
124 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = 1.0;
125 }
126 windowssq += window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] * window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i];
127 }
128 const double oneByBinWidth = 1.0 / double (windowssq) / binWidth_samples;
129
130 autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Sound to Spectrogram...");
131
132 MelderThread_PARALLELcxx . . $ `melder/MelderThread.h:173:9`! (numberOfTimes, 15) {
133 autoVECcxx . . $ autoVEC# data = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (nsampFFT);
134 autoVECcxx . . $ autoVEC# spectrum = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (half_nsampFFT + 1);
135 autoNUMFourierTablecxx . . $ autoNUMFourierTable# fftTable = NUMFourierTable_createcxx . . $ NUMFourierTable_create(10012c71e147318e). (nsampFFT);
136 MelderThread_FORcxx . . $ `melder/MelderThread.h:183:9`! (iframe) {
137 const double t = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe);
138 const integercxx . . $ integer# leftSample = Sampled_xToLowIndexcxx . . $ Sampled_xToLowIndex(405c2491bc3636e9). (me, t), rightSample = leftSample + 1;
139 const integercxx . . $ integer# startSample = rightSample - halfnsamp_window;
140 const integercxx . . $ integer# endSample = leftSample + halfnsamp_window;
141 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (startSample >= 1);
142 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (endSample <= mycxx . . $ `melder/melder.h:44:9`! nx);
143
144 spectrum.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0;
145 /*
146 For multichannel sounds, the power spectrogram should represent the
147 average power in the channels,
148 so that the result for a stereo sound in which the
149 left channel has the same waveform as the right channel,
150 is identical to the result for the corresponding mono (= averaged) sound.
151 Averaging starts by adding up the powers of the channels.
152 */
153 for (integercxx . . $ integer# channel = 1; channel <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; channel ++) {
154 for (integercxx . . $ integer# j = 1, i = startSample; j <= nsamp_window; j ++)
155 data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).channel] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i ++] * window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j];
156 for (integercxx . . $ integer# j = nsamp_window + 1; j <= nsampFFT; j ++)
157 data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] = 0.0;
158
159 if (MelderThread_IS_MASTERcxx . . $ `melder/MelderThread.h:199:9`!) { // then we can interact with the GUI
160 const double estimatedProgress = MelderThread_ESTIMATED_PROGRESScxx . . $ `melder/MelderThread.h:202:9`!;
161 Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). (estimatedProgress,
162 U"Sound to Spectrogram: analysed approximately ", Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (numberOfTimes * estimatedProgress),
163 U" out of ", numberOfTimes, U" frames"
164 );
165 }
166
167 /*
168 Compute the Fast Fourier Transform of the frame.
169 */
170 NUMfft_forwardcxx . . $ NUMfft_forward(aea41fd94d07eee). (fftTable.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), data.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); // data := complex spectrum
171
172 /*
173 Convert from complex to power spectrum,
174 accumulating the power spectra of the channels.
175 */
176 spectrum [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] += data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] * data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; // DC component
177 for (integercxx . . $ integer# i = 2; i <= half_nsampFFT; i ++)
178 spectrum [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] += data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + i - 2] * data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + i - 2] + data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + i - 1] * data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + i - 1];
179 spectrum [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).half_nsampFFT + 1] += data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).nsampFFT] * data [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).nsampFFT]; // Nyquist frequency. Correct??
180 }
181 /*
182 Power averaging ends by dividing the summed power by the number of channels,
183 */
184 if (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. > 1 )
185 spectrum.allcxx . . $ autovector#all(a26726dad87555bb).() /=cxx . . $ `operator/=`(c555d170d72ff256). mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.;
186
187 /*
188 Binning.
189 */
190 for (integercxx . . $ integer# iband = 1; iband <= numberOfFreqs; iband ++) {
191 const integercxx . . $ integer# lowerSample = (iband - 1) * binWidth_samples + 1;
192 const integercxx . . $ integer# higherSample = lowerSample + binWidth_samples;
193 const double power = NUMsumcxx . . $ NUMsum(4bb9c3e3a8dd5442). (spectrumcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (lowerSample, higherSample - 1));
194 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).iband] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = power * oneByBinWidth;
195 }
196 }
197 } MelderThread_ENDPARALLELcxx . . $ `melder/MelderThread.h:189:9`!
198 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
199 } catch (MelderErrorcxx . . $ MelderError#) {
200 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": spectrogram analysis not performed.");
201 }
202 }
203
204 autoSoundcxx . . $ autoSound# Spectrogram_to_Soundcxx . . $ Spectrogram_to_Sound(2ae5a3f2895be910). (Spectrogramcxx . . $ Spectrogram# me, double fsamp) {
205 try {
206 const double dt = 1.0 / fsamp;
207 const integercxx . . $ integer# n = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.) / dt);
208 if (n < 0)
209 return autoSoundcxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8). ();
210 autoSoundcxx . . $ autoSound# you = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (1, mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., n, dt, 0.5 * dt);
211 for (integercxx . . $ integer# i = 1; i <= n; i ++) {
212 const double t = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (you.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), i);
213 const double rframe = Sampled_xToIndexcxx . . $ Sampled_xToIndex(b395a896ba2eff9a). (me, t);
214 if (rframe < 1 || rframe >= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.)
215 continue;
216 const integercxx . . $ integer# leftFrame = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (rframe);
217 const integercxx . . $ integer# rightFrame = leftFrame + 1;
218 const double phase = rframe - leftFrame;
219 longdoublecxx . . $ longdouble# value = 0.0;
220 for (integercxx . . $ integer# j = 1; j <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; j ++) {
221 const double f = Matrix_rowToYcxx . . $ Matrix_rowToY(89d6d8659270b8b7). (me, j);
222 const double power = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).leftFrame] * (1.0 - phase) + mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).j] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).rightFrame] * phase;
223 value += sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (power) * sincxx . . $ sin(fee4f1d22bc8e617). (2 * NUMpicxx . . $ NUMpi. * f * t);
224 }
225 yourcxx . . $ `melder/melder.h:46:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = double (value);
226 }
227 return youcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
228 } catch (MelderErrorcxx . . $ MelderError#) {
229 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Sound.");
230 }
231 }
232
233 /* End of file Sound_and_Spectrogram.cpp */