Debian Codegraph @

dwtools/Spectrum_extensions.cpp

raw symbols SCIP
1 /* Spectrum_extensions.cpp
2 *
3 * Copyright (C) 1993-2021 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 20030929 Added a warning in Spectrum_drawPhases.
23 djmw 20031023 New: Spectra_multiply, Spectrum_conjugate
24 djmw 20040506 Changed warning message in Spectrum_drawPhases.
25 djmw 20041124 Changed call to Sound_to_Spectrum.
26 djmw 20061218 Introduction of Melder_information<12...9>
27 djmw 20071022 phase_unwrap initialize phase = 0.
28 djmw 20080122 float -> double
29 djmw 20080202 Warning in Spectrum_drawPhases to wchar
30 djmw 20080411 Removed define NUM2pi
31 */
32
33 #include "Ltas.h"cxx . . $ `<file>/fon/Ltas.h`/
34 #include "Spectrum_extensions.h"cxx . . $ `<file>/dwtools/Spectrum_extensions.h`/
35 #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/
36 #include "Sound_and_Spectrum_dft.h"cxx . . $ `<file>/dwtools/Sound_and_Spectrum_dft.h`/
37 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
38
39 #define SIGNcxx . . $ `dwtools/Spectrum_extensions.cpp:39:9`!(x,s) ((s) < 0 ? -fabs (x) : fabs(x))
40
41 #define THLCONcxx . . $ `dwtools/Spectrum_extensions.cpp:41:9`! 0.5
42 #define THLINCcxx . . $ `dwtools/Spectrum_extensions.cpp:42:9`! 1.5
43 #define EXP2cxx . . $ `dwtools/Spectrum_extensions.cpp:43:9`! 12
44
45 #define PPVPHAcxx . . $ `dwtools/Spectrum_extensions.cpp:45:9`!(x,y,test) ((test) ? atan2 (-(y),-(x)) : atan2 ((y),(x)))
46 #define PHADVTcxx . . $ `dwtools/Spectrum_extensions.cpp:46:9`!(xr,xi,yr,yi,xa) ((xa) > 0 ? ((xr)*(yr)+(xi)*(yi))/ (xa) : 0)
47
48 struct tribolet_structcxx . . $ tribolet_struct# {
49 double thlinccxx . . $ tribolet_struct#thlinc., thlconcxx . . $ tribolet_struct#thlcon.;
50 double ddfcxx . . $ tribolet_struct#ddf., dvtmn2cxx . . $ tribolet_struct#dvtmn2.;
51 double *xcxx . . $ tribolet_struct#x.;
52 integercxx . . $ integer# nxcxx . . $ tribolet_struct#nx., lcxx . . $ tribolet_struct#l., countcxx . . $ tribolet_struct#count.;
53 bool reverse_signcxx . . $ tribolet_struct#reverse_sign.;
54 };
55
56 /*
57 Perform modified Goertzel algorithm to calculate, at frequency 'freq_rad',
58 the real and imaginary part of the spectrum and the d/df of the
59 spectrum of x.
60 Reference: Bonzanigo (1978), IEEE Trans. ASSP, Vol. 26.
61 */
62 static void getSpectralValuescxx . . $ getSpectralValues(12b972f65bd98f83). (struct tribolet_structcxx . . $ tribolet_struct# *tbs, double freq_rad, double *xr, double *xi, double *nxr, double *nxi) {
63 const double cosf = coscxx . . $ cos(fee4f1d22bc8e617). (freq_rad), sinf = sincxx . . $ sin(fee4f1d22bc8e617). (freq_rad);
64 double a = 2 * cosf;
65 double b, u1 = 0, u2 = u1, w1 = u1, w2 = u1;
66 const double *x = tbs -> xcxx . . $ tribolet_struct#x.;
67 const integercxx . . $ integer# nx = tbs -> nxcxx . . $ tribolet_struct#nx.;
68
69 for (integercxx . . $ integer# j = 1; j <= nx; j ++) {
70 double xj = x [j];
71 double u0 = xj + a * u1 - u2;
72 double w0 = (j - 1) * xj + a * w1 - w2;
73 u2 = u1;
74 u1 = u0;
75 w2 = w1;
76 w1 = w0;
77 }
78
79 // Bonzanigo's phase correction
80
81 a = freq_rad * (nx - 1);
82 u1 = coscxx . . $ cos(fee4f1d22bc8e617). (a);
83 u2 = - sincxx . . $ sin(fee4f1d22bc8e617). (a);
84
85 a = u1 - u2 * cosf;
86 b = u2 * sinf;
87 *xr = u1 * a - u2 * b;
88 *xi = u2 * a + u1 * b;
89
90 a = w1 - w2 * cosf;
91 b = w2 * sinf;
92 *nxr = u1 * a - u2 * b;
93 *nxi = u2 * a + u1 * b;
94 tbs -> countcxx . . $ tribolet_struct#count. ++;
95 }
96
97 // Find the closest unwrapped phase estimate from the two admissible phase values (a1 & a2).
98
99 static int phase_checkcxx . . $ phase_check(b5f85b1d1412d6f4). (double pv, double *inout_phase, double thlcon) {
100 const double a0 = (*inout_phase - pv) / NUM2picxx . . $ NUM2pi.;
101 const integercxx . . $ integer# k = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (a0); // ppgb: instead of truncation toward zero
102 const double a1 = pv + k * NUM2picxx . . $ NUM2pi.;
103 const double a2 = a1 + SIGNcxx . . $ `dwtools/Spectrum_extensions.cpp:39:9`! (NUM2pi, a0);
104 const double a3 = fabscxx . . $ fabs(fee4f1d22bc8e617). (a1 - *inout_phase);
105 const double a4 = fabscxx . . $ fabs(fee4f1d22bc8e617). (a2 - *inout_phase);
106
107 if (a3 > thlcon && a4 > thlcon)
108 return 0;
109 *inout_phase = a3 > a4 ? a2 : a1;
110 return 1;
111 }
112
113 /*
114 Phase unwrapping based on Tribolet's adaptive integration method.
115 the unwrapped phase estimate is returned.
116 */
117 static double phase_unwrapcxx . . $ phase_unwrap(c48a7cd06ea9d4cb). (struct tribolet_structcxx . . $ tribolet_struct# *tbs, double pfreq, double ppv, double pdvt, double *pphase, double *ppdvt) {
118 double sdvt [25], sppv [25];
119 double freq, phase = 0.0;
120 double xr, xi, xmsq, nxr, nxi;
121 integercxx . . $ integer# k, sindex [25], pindex = 1, sp = 1;
122
123 sppv [sp] = ppv;
124 sdvt [sp] = pdvt;
125 sindex [sp] = tbs -> lcxx . . $ tribolet_struct#l. + 1;
126
127 goto p40;
128 p20:
129 /*
130 When the routine runs out of stack space, there probably is
131 a zero very near the unit circle that results in a jump of
132 pi in the phase.
133 */
134 if ((sindex [sp] - pindex) <= 1)
135 return phase;
136 /*
137 p30:
138 Get the intermediate frequency value and compute its phase
139 derivative and principal value.
140 */
141 k = (sindex [sp] + pindex) / 2;
142 freq = pfreq + (k - 1) * tbs -> ddfcxx . . $ tribolet_struct#ddf.;
143 getSpectralValuescxx . . $ getSpectralValues(12b972f65bd98f83). (tbs, freq, & xr, & xi, & nxr, & nxi);
144 sindex [ ++sp] = k;
145 sppv [sp] = PPVPHAcxx . . $ `dwtools/Spectrum_extensions.cpp:45:9`! (xr, xi, tbs -> reverse_sign);
146 xmsq = xr * xr + xi * xi;
147 sdvt [sp] = PHADVTcxx . . $ `dwtools/Spectrum_extensions.cpp:46:9`! (xr, xi, nxr, nxi, xmsq);
148
149 p40:
150 /*
151 Evaluate the phase increment.
152 If the phase increment, reduced by the expected linear phase
153 increment, is greater than the specified threshold, adapt step size.
154 */
155 double delta = 0.5 * tbs -> ddfcxx . . $ tribolet_struct#ddf. * (sindex [sp] - pindex);
156 double phase_inc = delta * (*ppdvt + sdvt [sp]);
157
158 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (phase_inc - delta * tbs -> dvtmn2cxx . . $ tribolet_struct#dvtmn2.) > tbs -> thlinccxx . . $ tribolet_struct#thlinc.)
159 goto p20;
160
161 phase = *pphase + phase_inc;
162
163 if (! phase_checkcxx . . $ phase_check(b5f85b1d1412d6f4). (sppv [sp], &phase, tbs -> thlconcxx . . $ tribolet_struct#thlcon.))
164 goto p20;
165 if (fabscxx . . $ fabs(fee4f1d22bc8e617). (phase - *pphase) > NUMpicxx . . $ NUMpi.)
166 goto p20;
167 if (sp == 1)
168 return phase;
169 /*
170 p10: Update previous estimate.
171 */
172 pindex = sindex [sp];
173 *pphase = phase;
174 *ppdvt = sdvt [sp--];
175
176 goto p40;
177 }
178
179 autoMatrixcxx . . $ autoMatrix# Spectrum_unwrapcxx . . $ Spectrum_unwrap(bce19a848610ec35). (Spectrumcxx . . $ Spectrum# me) {
180 try {
181 struct tribolet_structcxx . . $ tribolet_struct# tbs;
182 int remove_linear_part = 1;
183
184 const integercxx . . $ integer# nfft = 2 * Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (2_integercxx . . $ `operator""_integer`(85240eb457c3721a)., Melder_iroundUpToPowerOfTwocxx . . $ Melder_iroundUpToPowerOfTwo(8ce0d1b217db7fd1). (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1)); // TODO: explain edge case
185 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (nfft / 2 == mycxx . . $ `melder/melder.h:44:9`! nx - 1,
186 U"Dimension of Spectrum should be a power of 2 - 1.");
187
188 autoSoundcxx . . $ autoSound# x = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (me);
189 autoSoundcxx . . $ autoSound# nx = Data_copycxx . . $ Data_copy(97a542a26794c80e). (x.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
190
191 for (integercxx . . $ integer# i = 1; i <= x ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; i ++)
192 nx ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] *= (i - 1);
193
194 autoSpectrumcxx . . $ autoSpectrum# snx = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (nx.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true);
195 autoMatrixcxx . . $ autoMatrix# thee = Matrix_createcxx . . $ Matrix_create(494a3ae9ff7e3b60). (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., 1, 2, 2, 1, 1);
196
197 tbs.thlinccxx . . $ tribolet_struct#thlinc. = THLINCcxx . . $ `dwtools/Spectrum_extensions.cpp:42:9`!;
198 tbs.thlconcxx . . $ tribolet_struct#thlcon. = THLCONcxx . . $ `dwtools/Spectrum_extensions.cpp:41:9`!;
199 tbs.xcxx . . $ tribolet_struct#x. = & x ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).0];
200 tbs.nxcxx . . $ tribolet_struct#nx. = x ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.;
201 tbs.lcxx . . $ tribolet_struct#l. = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (powcxx . . $ pow(9454dda30a4b8335). (2, EXP2cxx . . $ `dwtools/Spectrum_extensions.cpp:43:9`!) + 0.1);
202 tbs.ddfcxx . . $ tribolet_struct#ddf. = NUM2picxx . . $ NUM2pi. / ( (tbs.lcxx . . $ tribolet_struct#l.) * nfft);
203 tbs.reverse_signcxx . . $ tribolet_struct#reverse_sign. = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] < 0;
204 tbs.countcxx . . $ tribolet_struct#count. = 0;
205 /*
206 Reuse snx : put phase derivative (d/df) in imaginary part.
207 */
208 tbs.dvtmn2cxx . . $ tribolet_struct#dvtmn2. = 0.0;
209 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) {
210 const double xr = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i], xi = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
211 const double nxr = snx ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i], nxi = snx ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
212 const double xmsq = xr * xr + xi * xi;
213 const double pdvt = PHADVTcxx . . $ `dwtools/Spectrum_extensions.cpp:46:9`! (xr, xi, nxr, nxi, xmsq);
214 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = xmsq;
215 snx ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = pdvt;
216 tbs.dvtmn2cxx . . $ tribolet_struct#dvtmn2. += pdvt;
217 }
218
219 tbs.dvtmn2cxx . . $ tribolet_struct#dvtmn2. = (2.0 * tbs.dvtmn2cxx . . $ tribolet_struct#dvtmn2. - snx ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] - snx ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.]) / (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1);
220
221 autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Spectrum: Unwrap phase...");
222
223 double pphase = 0.0, phase = 0.0;
224 double ppdvt = snx ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1];
225 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = PPVPHAcxx . . $ `dwtools/Spectrum_extensions.cpp:45:9`! (mycxx . . $ `melder/melder.h:44:9`! z [1] [1], mycxx . . $ `melder/melder.h:44:9`! z [2] [1], tbs.reverse_sign);
226 for (integercxx . . $ integer# i = 2; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) {
227 const double pfreq = NUM2picxx . . $ NUM2pi. * (i - 1) / nfft;
228 const double pdvt = snx ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
229 const double ppv = PPVPHAcxx . . $ `dwtools/Spectrum_extensions.cpp:45:9`! (mycxx . . $ `melder/melder.h:44:9`! z [1] [i], mycxx . . $ `melder/melder.h:44:9`! z [2] [i], tbs.reverse_sign);
230 phase = phase_unwrapcxx . . $ phase_unwrap(c48a7cd06ea9d4cb). (&tbs, pfreq, ppv, pdvt, &pphase, &ppdvt);
231 ppdvt = pdvt;
232 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = pphase = phase;
233 Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). ( (double) i / mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., i,
234 U" unwrapped phases from ", mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., U".");
235 }
236
237 const integercxx . . $ integer# iphase = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (phase / NUMpicxx . . $ NUMpi. + 0.1);
238
239 if (remove_linear_part) {
240 phase /= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1;
241 for (integercxx . . $ integer# i = 2; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++)
242 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] -= phase * (i - 1);
243 }
244 Melder_informationcxx . . $ Melder_information(3eb398314feba58b). (U"Number of spectral values: ", tbs.countcxx . . $ tribolet_struct#count.);
245 Melder_informationcxx . . $ Melder_information(3eb398314feba58b). (U" iphase = ", iphase);
246 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
247 } catch (MelderErrorcxx . . $ MelderError#) {
248 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not unwrapped.");
249 }
250 }
251
252 void Spectrum_drawPhasescxx . . $ Spectrum_drawPhases(7fbaa77ad438d35d). (Spectrumcxx . . $ Spectrum# me, Graphicscxx . . $ Graphics# g, double fmin, double fmax, double phase_min, double phase_max, int unwrap, bool /* garnish */) {
253 autoMatrixcxx . . $ autoMatrix# theecxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).;
254 bool reverse_sign = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] < 0;
255
256 if (unwrap)
257 thee =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Spectrum_unwrapcxx . . $ Spectrum_unwrap(bce19a848610ec35). (me);
258 else {
259 thee =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Matrix_createcxx . . $ Matrix_create(494a3ae9ff7e3b60). (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., 1.0, 2.0, 2, 1.0, 1.0);
260 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) {
261 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = PPVPHAcxx . . $ `dwtools/Spectrum_extensions.cpp:45:9`! (mycxx . . $ `melder/melder.h:44:9`! z [1] [i], mycxx . . $ `melder/melder.h:44:9`! z [2] [i], reverse_sign);
262 }
263 }
264 Matrix_drawRowscxx . . $ Matrix_drawRows(13c691ba74eecb49). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), g, fmin, fmax, 1.9, 2.1, phase_min, phase_max);
265 }
266
267 autoSpectrumcxx . . $ autoSpectrum# Spectra_multiplycxx . . $ Spectra_multiply(206819a400b9a314). (Spectrumcxx . . $ Spectrum# me, Spectrumcxx . . $ Spectrum# thee) {
268 try {
269 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! nx == thycxx . . $ `melder/melder.h:45:9`! nx && mycxx . . $ `melder/melder.h:44:9`! x1 == thycxx . . $ `melder/melder.h:45:9`! x1 && mycxx . . $ `melder/melder.h:44:9`! xmax == thycxx . . $ `melder/melder.h:45:9`! xmax && mycxx . . $ `melder/melder.h:44:9`! dx == thycxx . . $ `melder/melder.h:45:9`! dx,
270 U"Dimensions of both spectra should be the same.");
271
272 autoSpectrumcxx . . $ autoSpectrum# him = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me);
273
274 for (integercxx . . $ integer# i = 1; i <= hiscxx . . $ `melder/melder.h:47:9`! nxcxx . . $ structSampled#nx.; i ++) {
275 hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] - mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
276 hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
277 }
278 return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
279 } catch (MelderErrorcxx . . $ MelderError#) {
280 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not multiplied.");
281 }
282 }
283
284 void Spectrum_shiftPhaseBy90Degreescxx . . $ Spectrum_shiftPhaseBy90Degrees(9136b6455db7f37). (Spectrumcxx . . $ Spectrum# me) {
285 // shifting +pi/2 by a multiplication with i
286 for (integercxx . . $ integer# i = 2; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1; i ++) {
287 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i], mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]);
288 mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = - mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
289 }
290 }
291
292 void Spectrum_unshiftPhaseBy90Degreescxx . . $ Spectrum_unshiftPhaseBy90Degrees(9136b6455db7f37). (Spectrumcxx . . $ Spectrum# me) {
293 // shifting -pi/2 by a multiplication with -i
294 for (integercxx . . $ integer# i = 2; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1; i ++) {
295 mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = - mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
296 stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i], mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]);
297 }
298 }
299
300 void Spectrum_conjugatecxx . . $ Spectrum_conjugate(9136b6455db7f37). (Spectrumcxx . . $ Spectrum# me) {
301 for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++)
302 mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = - mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
303 }
304
305 autoSpectrumcxx . . $ autoSpectrum# Spectrum_resamplecxx . . $ Spectrum_resample(3cd26c3041bcef8a). (Spectrumcxx . . $ Spectrum# me, integercxx . . $ integer# numberOfFrequencies) {
306 try {
307 const double newSamplingFrequency = (1 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.) * numberOfFrequencies / mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.;
308 // resample real and imaginary part !
309 autoSoundcxx . . $ autoSound# thee = Sound_resamplecxx . . $ Sound_resample(872961da5f5e69e5). ((Soundcxx . . $ Sound#) me, newSamplingFrequency, 50);
310 autoSpectrumcxx . . $ autoSpectrum# him = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfFrequencies);
311 hiscxx . . $ `melder/melder.h:47:9`! zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).();
312 return himcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
313 } catch (MelderErrorcxx . . $ MelderError#) {
314 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not resampled.");
315 }
316 }
317 /*
318 autoSpectrum Spectrum_resample2 (Spectrum me, integer numberOfFrequencies) {
319 try {
320 autoSound sound = Spectrum_to_Sound (me);
321 const double newSamplingFrequency = (1.0 / my dx) * numberOfFrequencies / my nx;
322 const double resampleFactor = (my nx - 1.0) / numberOfFrequencies;
323 autoSound resampled = Sound_resample (sound.get(), resampleFactor / sound -> dx, 50);
324 autoSpectrum extendedSpectrum = Sound_to_Spectrum_resampled (resampled.get(), 50);
325 autoSpectrum him = Spectrum_create (my xmax, numberOfFrequencies);
326 his z.all() <<= extendedSpectrum -> z.all();
327 return him;
328 } catch (MelderError) {
329 Melder_throw (me, U": not resampled.");
330 }
331 }
332 */
333 #if 0
334 static autoSpectrum Spectrum_shiftFrequencies2 (Spectrum me, double shiftBy, bool changeMaximumFrequency) {
335 try {
336 double xmax = my xmax;
337 integer numberOfFrequencies = my nx, interpolationDepth = 50;
338 if (changeMaximumFrequency) {
339 xmax += shiftBy;
340 numberOfFrequencies += (xmax - my xmax) / my dx;
341 }
342 autoSpectrum thee = Spectrum_create (xmax, numberOfFrequencies);
343 // shiftBy >= 0
344 for (integer i = 1; i <= thy nx; i ++) {
345 const double thyf = thy x1 + (i - 1) * thy dx;
346 const double myf = thyf - shiftBy;
347 if (myf >= my xmin && myf <= my xmax) {
348 const double index = Sampled_xToIndex (me, myf);
349 thy z [1] [i] = NUM_interpolate_sinc (my z.row (1), index, interpolationDepth);
350 thy z [2] [i] = NUM_interpolate_sinc (my z.row (2), index, interpolationDepth);
351 }
352 }
353 return thee;
354 } catch (MelderError) {
355 Melder_throw (me, U": not shifted.");
356 }
357 }
358 #endif
359
360 autoSpectrumcxx . . $ autoSpectrum# Spectrum_shiftFrequenciescxx . . $ Spectrum_shiftFrequencies(71e5228f2f84cdd2). (Spectrumcxx . . $ Spectrum# me, double shiftBy, double newMaximumFrequency, integercxx . . $ integer# interpolationDepth) {
361 try {
362 double xmax = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.;
363 integercxx . . $ integer# numberOfFrequencies = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.;
364 if (newMaximumFrequency != 0.0) {
365 numberOfFrequencies = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (newMaximumFrequency / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.) + 1;
366 xmax = newMaximumFrequency;
367 }
368 autoSpectrumcxx . . $ autoSpectrum# thee = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (xmax, numberOfFrequencies);
369 for (integercxx . . $ integer# i = 1; i <= thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.; i ++) {
370 const double thyf = thycxx . . $ `melder/melder.h:45:9`! x1cxx . . $ structSampled#x1. + (i - 1) * thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx.;
371 const double myf = thyf - shiftBy;
372 if (myf >= mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. && myf <= mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.) {
373 const double index = Sampled_xToIndexcxx . . $ Sampled_xToIndex(b395a896ba2eff9a). (me, myf);
374 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = NUM_interpolate_sinccxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1), index, interpolationDepth);
375 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = NUM_interpolate_sinccxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2), index, interpolationDepth);
376 }
377 }
378 /*
379 Make imaginary part of first and last sample zero
380 so Spectrum_to_Sound uses FFT if numberOfSamples was power of 2!
381 */
382 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = hypotcxx . . $ hypot(b71928d39fde4295). (thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1]);
383 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = 0.0;
384 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.] = hypotcxx . . $ hypot(b71928d39fde4295). (thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.], thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.]);
385 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.] = 0.0;
386 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
387 } catch (MelderErrorcxx . . $ MelderError#) {
388 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not shifted.");
389 }
390 }
391
392 autoSpectrumcxx . . $ autoSpectrum# Spectrum_compressFrequencyDomaincxx . . $ Spectrum_compressFrequencyDomain(4995bd7ea6e4260e). (Spectrumcxx . . $ Spectrum# me, double fmax, integercxx . . $ integer# interpolationDepth, int freqscale, int method) {
393 try {
394 const double fdomain = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., factor = fdomain / fmax ;
395 //integer numberOfFrequencies = 1.0 + fmax / my dx; // keep dx the same, otherwise the "duration" changes
396 const double xmax = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. / factor;
397 const integercxx . . $ integer# numberOfFrequencies = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. / factor); // keep dx the same, otherwise the "duration" changes
398 autoSpectrumcxx . . $ autoSpectrum# thee = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (xmax, numberOfFrequencies);
399 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1];
400 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1];
401 const double df = freqscale == 1 ? factor * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. : log10cxx . . $ log10(fee4f1d22bc8e617). (fdomain) / (numberOfFrequencies - 1);
402 for (integercxx . . $ integer# i = 2; i <= numberOfFrequencies; i ++) {
403 const double f = mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. + (freqscale == 1 ? (i - 1) * df : powcxx . . $ pow(b71928d39fde4295). (10.0, (i - 1) * df));
404 const double index = (f - mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1.) / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. + 1;
405 double x, y;
406 if (index > mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.)
407 break;
408 if (method == 1) {
409 x = NUM_interpolate_sinccxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1), index, interpolationDepth);
410 y = NUM_interpolate_sinccxx . . $ NUM_interpolate_sinc(3bff97c7d2c43d6f). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (2), index, interpolationDepth);
411 } else {
412 x = undefinedcxx . . $ undefined.; // ppgb: better than data from random memory
413 y = undefinedcxx . . $ undefined.;
414 }
415 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = x;
416 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = y;
417 }
418 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
419 } catch (MelderErrorcxx . . $ MelderError#) {
420 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not compressed.");
421 }
422 }
423
424 /* End of file Spectrum_extensions.cpp */