Debian Codegraph @

dwtools/ComplexSpectrogram.cpp

raw symbols SCIP
1 /* ComplexSpectrogram.cpp
2 *
3 * Copyright (C) 2014-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 #include "ComplexSpectrogram.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram.h`/
20 #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/
21
22 #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/
23 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
24 #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/
25 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
26 #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/
27 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
28 #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/
29 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
30 #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/
31 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
32 #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/
33 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
34 #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/
35 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
36 #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/
37 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
38 #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/
39 #include "ComplexSpectrogram_def.h"cxx . . $ `<file>/dwtools/ComplexSpectrogram_def.h`/
40
41 Thing_implementcxx . . $ `sys/Thing.h:65:9`! (ComplexSpectrogram, Matrix, 2);
42
43
44 autoComplexSpectrogram ComplexSpectrogram_createcxx . . $ ComplexSpectrogram_create(a967888bec90c2f7). (double tmin, double tmax, integercxx . . $ integer# nt, double dt,
45 double t1, double fmin, double fmax, integercxx . . $ integer# nf, double df, double f1)
46 {
47 try {
48 autoComplexSpectrogram me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (ComplexSpectrogram);
49 Matrix_initcxx . . $ Matrix_init(9dd6c49f9830a091). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1);
50 mycxx . . $ `melder/melder.h:44:9`! phase =cxx . . $ automatrix#`operator=`(a0f46581f260c488). zero_MATcxx . . $ zero_MAT(3d09731056d2b97d). (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny., mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
51 return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
52 } catch (MelderErrorcxx . . $ MelderError#) {
53 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"ComplexSpectrogram not created.");
54 }
55 }
56
57 static autoVECcxx . . $ autoVEC# newVEChanningcxx . . $ newVEChanning(e4395f764b859b1e). (integercxx . . $ integer# size) {
58 autoVECcxx . . $ autoVEC# window = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (size);
59 for (integercxx . . $ integer# i = 1; i <= size; i ++)
60 window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = 0.5 * (1.0 - coscxx . . $ cos(fee4f1d22bc8e617). (NUM2picxx . . $ NUM2pi. * (i - 0.5) / size));
61 return windowcxx . . $ autovector#autovector(d33c649a3aceae2f).;
62 }
63
64 autoComplexSpectrogram Sound_to_ComplexSpectrogramcxx . . $ Sound_to_ComplexSpectrogram(f9539aeec396eca0). (Soundcxx . . $ Sound# me, double windowLength, double maximumFrequency ) {
65 try {
66 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! ny == 1,
67 U"The sound should have onely one channel.");
68 const double samplingFrequency = 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., nyquistFrequency = 0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
69 const double myDuration = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.;
70 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (windowLength <= myDuration,
71 U"The sound is too short: it should be at least as long as one window length.");
72 if (maximumFrequency == 0.0)
73 maximumFrequency = nyquistFrequency;
74 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (maximumFrequency <= nyquistFrequency,
75 U"The maximum frequency should not exceed ", nyquistFrequency, U" Hz.");
76 integercxx . . $ integer# nsamp_window = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (windowLength / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.);
77 autoVECcxx . . $ autoVEC# window = newVEChanningcxx . . $ newVEChanning(e4395f764b859b1e). (nsamp_window);
78 const integercxx . . $ integer# halfnsamp_window = nsamp_window / 2 - 1;
79 nsamp_window = halfnsamp_window * 2;
80
81 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (nsamp_window > 1,
82 U"There should be at least two samples in the window.");
83
84 integercxx . . $ integer# numberOfFrames;
85 const double timeStep = 0.5 * windowLength;
86 double t1;
87 Sampled_shortTermAnalysiscxx . . $ Sampled_shortTermAnalysis(77f703705034c8a). (me, windowLength, timeStep, & numberOfFrames, & t1);
88
89 // Compute sampling of the spectrum
90
91 const integercxx . . $ integer# numberOfFrequencies = halfnsamp_window + 1;
92 const double df = samplingFrequency / (numberOfFrequencies - 1);
93
94 autoComplexSpectrogram thee = ComplexSpectrogram_createcxx . . $ ComplexSpectrogram_create(a967888bec90c2f7). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfFrames, timeStep, t1, 0.0, maximumFrequency, numberOfFrequencies, df, 0.0);
95
96 autoSoundcxx . . $ autoSound# analysisWindow = Sound_createcxx . . $ Sound_create(39ab106d6b9a0135). (1, 0.0, nsamp_window * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., nsamp_window, mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., 0.5 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.);
97
98 for (integercxx . . $ integer# iframe = 1; iframe <= numberOfFrames; iframe ++) {
99 const double tmid = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe);
100 const integercxx . . $ integer# leftSample = Sampled_xToLowIndexcxx . . $ Sampled_xToLowIndex(405c2491bc3636e9). (me, tmid);
101 const double rightSample = leftSample + 1;
102 const integercxx . . $ integer# startSample = rightSample - halfnsamp_window;
103 const integercxx . . $ integer# endSample = startSample + nsamp_window - 1;
104 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (startSample >= 1);
105 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (endSample <= mycxx . . $ `melder/melder.h:44:9`! nx);
106
107 analysisWindowcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) <<=cxx . . $ `operator<<=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (startSample, endSample);
108
109 analysisWindowcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1) *=cxx . . $ `operator*=`(adea042513329390). windowcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, nsamp_window);
110 autoSpectrumcxx . . $ autoSpectrum# spec = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (analysisWindow.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), false);
111
112 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] * spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1];
113 thycxx . . $ `melder/melder.h:45:9`! phase [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = 0.0;
114 for (integercxx . . $ integer# ifreq = 2; ifreq <= numberOfFrequencies - 1; ifreq ++) {
115 const double x = spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq], y = spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq];
116 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = x * x + y * y; // power
117 thycxx . . $ `melder/melder.h:45:9`! phase [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = atan2cxx . . $ atan2(b71928d39fde4295). (y, x); // phase [-pi,+pi]
118 }
119 // even number of samples
120 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).numberOfFrequencies] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).numberOfFrequencies] * spec ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).numberOfFrequencies];
121 thycxx . . $ `melder/melder.h:45:9`! phase [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).numberOfFrequencies] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe] = 0.0;
122 }
123 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
124 } catch (MelderErrorcxx . . $ MelderError#) {
125 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no ComplexSpectrogram created.");
126 }
127 }
128
129 autoSoundcxx . . $ autoSound# ComplexSpectrogram_to_Soundcxx . . $ ComplexSpectrogram_to_Sound(f88059f8c526a107). (ComplexSpectrogram me, double stretchFactor) {
130 try {
131 /*
132 original number of samples is odd: imaginary part of last spectral value is zero ->
133 phase is either zero or +/-pi
134 */
135 const double samplingFrequency = 2.0 * mycxx . . $ `melder/melder.h:44:9`! ymaxcxx . . $ structSampledXY#ymax.;
136 const double lastFrequency = mycxx . . $ `melder/melder.h:44:9`! y1cxx . . $ structSampledXY#y1. + (mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. - 1) * mycxx . . $ `melder/melder.h:44:9`! dycxx . . $ structSampledXY#dy., lastPhase = mycxx . . $ `melder/melder.h:44:9`! phase [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1];
137 const bool originalNumberOfSamplesProbablyOdd = ( lastPhase != 0.0 && lastPhase != NUMpicxx . . $ NUMpi. && lastPhase != -NUMpicxx . . $ NUMpi. ||
138 mycxx . . $ `melder/melder.h:44:9`! ymaxcxx . . $ structSampledXY#ymax. - lastFrequency > 0.25 * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. );
139 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! y1 == 0.0,
140 U"A Fourier-transformable ComplexSpectrogram should have a first frequency of 0 Hz, not ", mycxx . . $ `melder/melder.h:44:9`! y1, U" Hz.");
141
142 const integercxx . . $ integer# nsamp_window = 2 * mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny. - ( originalNumberOfSamplesProbablyOdd ? 1 : 2 );
143 const integercxx . . $ integer# halfnsamp_window = nsamp_window / 2;
144 const double synthesisWindowDuration = nsamp_window / samplingFrequency;
145 autoSpectrumcxx . . $ autoSpectrum# spectrum = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (mycxx . . $ `melder/melder.h:44:9`! ymaxcxx . . $ structSampledXY#ymax., mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.);
146 autoSoundcxx . . $ autoSound# synthesisWindow = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, synthesisWindowDuration, samplingFrequency);
147 const double newDuration = (mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.) * stretchFactor;
148 autoSoundcxx . . $ autoSound# thee = Sound_createSimplecxx . . $ Sound_createSimple(fcb03a673f737ce). (1, newDuration, samplingFrequency); //TODO
149 //double thyStartTime;
150 for (integercxx . . $ integer# iframe = 1; iframe <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; iframe ++) {
151 // "original" sound :
152 const double tmid = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, iframe);
153 const integercxx . . $ integer# leftSample = Sampled_xToLowIndexcxx . . $ Sampled_xToLowIndex(405c2491bc3636e9). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), tmid);
154 const integercxx . . $ integer# rightSample = leftSample + 1;
155 const integercxx . . $ integer# startSample = rightSample - halfnsamp_window;
156 const integercxx . . $ integer# endSample = stdcxx . . $ std/::mincxx . . $ std/min(2a338fce5842aa6a). (startSample + nsamp_window - 1, thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx.);
157
158 //const double startTime = Sampled_indexToX (thee.get(), startSample);
159 //if (iframe == 1)
160 // thyStartTime = Sampled_indexToX (thee.get(), startSample);
161 //integer endSample = leftSample + halfnsamp_window;
162 // New Sound with stretch
163 //const integer thyStartSample = Sampled_xToLowIndex (thee.get(), thyStartTime);
164 //const double thyEndTime = thyStartTime + my dx * stretchFactor;
165 //const integer thyEndSample = Sampled_xToLowIndex (thee.get(), thyEndTime);
166 //double extraTime = (thyStartSample - startSample + 1) * thy dx;
167 //const double extraTime = thyStartTime - startTime;
168 spectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe]);
169 for (integercxx . . $ integer# ifreq = 2; ifreq <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; ifreq ++) {
170 //const double f = my y1 + (ifreq - 1) * my dy;
171 const double a = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe]);
172 //double dummy;
173 const double phi = mycxx . . $ `melder/melder.h:44:9`! phase [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe]; // + extraPhase;
174 spectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq] = a * coscxx . . $ cos(fee4f1d22bc8e617). (phi);
175 spectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq] = a * sincxx . . $ sin(fee4f1d22bc8e617). (phi);
176 }
177 autoSoundcxx . . $ autoSound# synthesis = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (spectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
178
179 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (startSample, endSample) +=cxx . . $ `operator+=`(d166bd5b5c96d95c). 0.5 *cxx . . $ `operator*`(7f4bd74ab5a11911). synthesiscxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1).partcxx . . $ vector#part(ae267db9e2c04be7). (1, nsamp_window);
180 }
181 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
182 } catch (MelderErrorcxx . . $ MelderError#) {
183 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Sound created.");
184 }
185 }
186
187 #if 0
188 static autoSound ComplexSpectrogram_to_Sound2 (ComplexSpectrogram me, double stretchFactor) {
189 try {
190 /* original number of samples is odd: imaginary part of last spectral value is zero ->
191 * phase is either zero or pi
192 */
193 const double pi = atan2 (0.0, - 0.5);
194 const double samplingFrequency = 2.0 * my ymax;
195 const double lastFrequency = my y1 + (my ny - 1) * my dy;
196 int originalNumberOfSamplesProbablyOdd = (my phase [my ny] [1] != 0.0 && my phase [my ny] [1] != pi) || my ymax - lastFrequency > 0.25 * my dx;
197 if (my y1 != 0.0) {
198 Melder_throw (U"A Fourier-transformable Spectrum must have a first frequency of 0 Hz, not ", my y1, U" Hz.");
199 }
200 const integer numberOfSamples = 2 * my ny - (originalNumberOfSamplesProbablyOdd ? 1 : 2 );
201 const double synthesisWindowDuration = numberOfSamples / samplingFrequency;
202 autoSpectrum spectrum = Spectrum_create (my ymax, my ny);
203 autoSound synthesisWindow = Sound_createSimple (1, synthesisWindowDuration, samplingFrequency);
204 const integer stepSizeSamples = my dx * samplingFrequency * stretchFactor;
205 const double newDuration = (my xmax - my xmin) * stretchFactor + 0.05;
206 autoSound thee = Sound_createSimple (1, newDuration, samplingFrequency); //TODO
207 integer istart = 1, iend = istart + stepSizeSamples - 1;
208 for (integer iframe = 1; iframe <= my nx; iframe++) {
209 spectrum -> z [1] [1] = sqrt (my z [1] [iframe]);
210 for (integer ifreq = 2; ifreq <= my ny; ifreq ++) {
211 const double f = my y1 + (ifreq - 1) * my dy;
212 const double a = sqrt (my z [ifreq] [iframe]);
213 const double extraPhase = 2.0 * pi * (stretchFactor - 1.0) * my dx * f;
214 const double phi = my phase [ifreq] [iframe] + extraPhase;
215 spectrum -> z [1] [ifreq] = a * cos (phi);
216 spectrum -> z [2] [ifreq] = a * sin (phi);
217 }
218 autoSound synthesis = Spectrum_to_Sound (spectrum.get());
219 for (integer j = istart; j <= iend; j ++)
220 thy z [1] [j] = synthesis -> z [1] [j - istart + 1];
221 istart = iend + 1;
222 iend = istart + stepSizeSamples - 1;
223 }
224 return thee;
225 } catch (MelderError) {
226 Melder_throw (me, U": no Sound created.");
227 }
228 }
229 #endif
230
231 autoSpectrogramcxx . . $ autoSpectrogram# ComplexSpectrogram_to_Spectrogramcxx . . $ ComplexSpectrogram_to_Spectrogram(daf24da66482b30f). (ComplexSpectrogram me) {
232 try {
233 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., 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., mycxx . . $ `melder/melder.h:44:9`! ymincxx . . $ structSampledXY#ymin., mycxx . . $ `melder/melder.h:44:9`! ymaxcxx . . $ structSampledXY#ymax., mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny., mycxx . . $ `melder/melder.h:44:9`! dycxx . . $ structSampledXY#dy., mycxx . . $ `melder/melder.h:44:9`! y1cxx . . $ structSampledXY#y1.);
234 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).();
235 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
236 } catch (MelderErrorcxx . . $ MelderError#) {
237 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Spectrogram.");
238 }
239 }
240
241 void ComplexSpectrogram_Spectrogram_replaceAmplitudescxx . . $ ComplexSpectrogram_Spectrogram_replaceAmplitudes(613c9492ffcef6a3). (ComplexSpectrogram me, Spectrogramcxx . . $ Spectrogram# thee) {
242 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`! ny == thycxx . . $ `melder/melder.h:45:9`! ny,
243 U"The numbers of cells in the ComplexSpectrogram and Spectrogram should be equal.");
244 mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).();
245 }
246
247 autoSpectrumcxx . . $ autoSpectrum# ComplexSpectrogram_to_Spectrumcxx . . $ ComplexSpectrogram_to_Spectrum(1201fc852de79093). (ComplexSpectrogram me, double time) {
248 try {
249 integercxx . . $ integer# iframe = Sampled_xToLowIndexcxx . . $ Sampled_xToLowIndex(405c2491bc3636e9). (me, time);
250 if (iframe < 1)
251 iframe = 1;
252 if (iframe > mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.)
253 iframe = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.;
254 autoSpectrumcxx . . $ autoSpectrum# thee = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (mycxx . . $ `melder/melder.h:44:9`! ymaxcxx . . $ structSampledXY#ymax., mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.);
255 for (integercxx . . $ integer# ifreq = 1; ifreq <= mycxx . . $ `melder/melder.h:44:9`! nycxx . . $ structSampledXY#ny.; ifreq ++) {
256 const double a = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe]);
257 const double phi = mycxx . . $ `melder/melder.h:44:9`! phase [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).ifreq] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe];
258 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq] = a * coscxx . . $ cos(fee4f1d22bc8e617). (phi);
259 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq] = a * sincxx . . $ sin(fee4f1d22bc8e617). (phi);
260 }
261 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
262 } catch (MelderErrorcxx . . $ MelderError#) {
263 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Spectrum created.");
264 }
265 }
266
267 /* End of file ComplexSpectrogram.cpp */