Debian Codegraph @

LPC/Sound_to_PowerCepstrogram.cpp

raw
1 /* Sound_to_PowerCepstrogram.cpp
2 *
3 * Copyright (C) 2012-2025 David Weenink, 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. 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 "NUM2.h"
20 #include "Cepstrum_and_Spectrum.h"
21 #include "Sound_and_Spectrum.h"
22 #include "Sound_extensions.h"
23 #include "SoundFrames.h"
24 #include "Sound_to_PowerCepstrogram.h"
25
26 autoPowerCepstrogram Sound_to_PowerCepstrogram (constSound me, double pitchFloor, double dt, double maximumFrequency, double preEmphasisFrequency) {
27 try {
28 const kSound_windowShape windowShape = kSound_windowShape::GAUSSIAN_2;
29 const double effectiveAnalysisWidth = 3.0 / pitchFloor; // minimum analysis window has 3 periods of lowest pitch
30 const double physicalAnalysisWidth = getPhysicalAnalysisWidth (effectiveAnalysisWidth, windowShape);
31 const double physicalSoundDuration = my dx * my nx;
32 volatile const double windowDuration = Melder_clippedRight (physicalAnalysisWidth, physicalSoundDuration);
33 Melder_require (physicalSoundDuration >= physicalAnalysisWidth,
34 U"Your sound is too short:\n"
35 U"it should be longer than ", physicalAnalysisWidth, U" s."
36 );
37 const double samplingFrequency = 2.0 * maximumFrequency;
38 autoSound input = Sound_resampleAndOrPreemphasize (me, maximumFrequency, 50_integer, preEmphasisFrequency);
39 double t1;
40 integer nFrames;
41 Sampled_shortTermAnalysis (input.get(), windowDuration, dt, & nFrames, & t1);
42 const integer soundFrameSize = getSoundFrameSize (physicalAnalysisWidth, input -> dx);
43 const integer numberOfFourierSamples = Melder_clippedLeft (2_integer, Melder_iroundUpToPowerOfTwo (soundFrameSize));
44 const integer halfNumberOfFourierSamples = numberOfFourierSamples / 2;
45 const integer numberOfFrequencies = halfNumberOfFourierSamples + 1;
46 const integer numberOfChannels = my ny;
47 const double qmax = 0.5 * numberOfFourierSamples / samplingFrequency, dq = 1.0 / samplingFrequency;
48 autoPowerCepstrogram output = PowerCepstrogram_create (my xmin, my xmax, nFrames, dt, t1, 0, qmax, numberOfFrequencies, dq, 0);
49 bool subtractFrameMean = true;
50 const double powerScaling = input -> dx * input -> dx; // =amplitude_scaling^2
51
52 MelderThread_PARALLEL (nFrames, 10) {
53 autoSoundFrames soundFrames = SoundFrames_createForIntoSampled (input.get(), output.get(), effectiveAnalysisWidth, windowShape, subtractFrameMean);
54 autoVEC fourierSamples = raw_VEC (numberOfFourierSamples);
55 autoVEC onesidedPowerSpectrum = raw_VEC (numberOfFrequencies);
56 autoNUMFourierTable fourierTable = NUMFourierTable_create (numberOfFourierSamples); // of dimension numberOfFourierSamples;
57 MelderThread_FOR (iframe) {
58 /*
59 Get average power spectrum of channels
60 Let X(f) be the Fourier Transform, defined on the domain [-F,+F], of the real signal x(t).
61 The power spectrum is defined as the Fourier transform of the autocorrelation of x(t) which
62 equals |X(f)|^2
63 The one-sided power spectrum is then P(f)= |X(f)|^2+|X(-f)|^2 = 2|X(f)|^2 for 0 <= f <= F
64 The bin width of the first and last frequency in the onesidedPowerSpectrum is half the bin width at the other frequencies
65 Do scaling and averaging together
66 */
67 Sound sound = soundFrames -> getFrame (iframe);
68 onesidedPowerSpectrum.get() <<= 0.0;
69 for (integer ichannel = 1; ichannel <= numberOfChannels; ichannel ++) {
70 fourierSamples.part (1, soundFrameSize) <<= sound -> z.row (ichannel);
71 fourierSamples.part (soundFrameSize + 1, numberOfFourierSamples) <<= 0.0;
72 NUMfft_forward (fourierTable.get(), fourierSamples.get());
73 onesidedPowerSpectrum [1] += fourierSamples [1] * fourierSamples [1];
74 for (integer i = 2; i < numberOfFrequencies; i ++) {
75 double re = fourierSamples [2 * i - 2], im = fourierSamples [2 * i - 1];
76 onesidedPowerSpectrum [i] += re * re + im * im;
77 }
78 onesidedPowerSpectrum [numberOfFrequencies] += fourierSamples [numberOfFourierSamples] * fourierSamples [numberOfFourierSamples];
79 }
80 onesidedPowerSpectrum.get() *= 2.0 * powerScaling / numberOfChannels; // scaling and averaging over channels
81 /*
82 Get log power.
83 */
84 fourierSamples [1] = log (onesidedPowerSpectrum [1] + 1e-300);
85 for (integer i = 2; i < numberOfFrequencies; i ++) {
86 fourierSamples [2 * i - 2] = log (onesidedPowerSpectrum [i] + 1e-300);
87 fourierSamples [2 * i - 1] = 0.0;
88 }
89 fourierSamples [numberOfFourierSamples] = log (onesidedPowerSpectrum [numberOfFrequencies]);
90 /*
91 Inverse transform
92 */
93 NUMfft_backward (fourierTable.get(), fourierSamples.get());
94 /*
95 scale first.
96 */
97 const double df = 1.0 / (sound -> dx * numberOfFourierSamples);
98 fourierSamples.get() *= df;
99 for (integer i = 1; i <= numberOfFrequencies; i ++)
100 output -> z [i] [iframe] = fourierSamples [i] * fourierSamples [i];
101 }
102 } MelderThread_ENDPARALLEL
103 return output;
104 } catch (MelderError) {
105 Melder_throw (me, U": no PowerCepstrogram created.");
106 }
107 }
108
109 // 1 2 nfftdiv2
110 // re im re im re im
111 // ((fft [1],0) (fft [2],fft [3]), (,), (,), (fft [nfft], 0)) nfft even
112 // ((fft [1],0) (fft [2],fft [3]), (,), (,), (fft [nfft-1], fft [nfft])) nfft odd
113
114 #define TOLOG(x) ((1 / NUMln10) * log ((x) + 1e-30))
115
116 static void complexfftoutput_to_power (constVEC fft, VEC dbs, bool to_db) {
117 double valsq = fft [1] * fft [1];
118 dbs [1] = ( to_db ? TOLOG (valsq) : valsq );
119 const integer nfftdiv2p1 = (fft.size + 2) / 2;
120 const integer nend = ( fft.size % 2 == 0 ? nfftdiv2p1 : nfftdiv2p1 + 1 );
121 for (integer i = 2; i < nend; i ++) {
122 const double re = fft [i + i - 2], im = fft [i + i - 1];
123 valsq = re * re + im * im;
124 dbs [i] = ( to_db ? TOLOG (valsq) : valsq );
125 }
126 if (fft.size % 2 == 0) {
127 valsq = fft [fft.size] * fft [fft.size];
128 dbs [nfftdiv2p1] = ( to_db ? TOLOG (valsq) : valsq );
129 }
130 }
131
132 autoPowerCepstrogram Sound_to_PowerCepstrogram_hillenbrand (constSound me, double pitchFloor, double dt) {
133 try {
134 // minimum analysis window has 3 periods of lowest pitch
135 const double physicalDuration = my dx * my nx;
136 const double analysisWidth = std::min (3.0 / pitchFloor, physicalDuration);
137
138 double samplingFrequency = 1.0 / my dx;
139 autoSound thee;
140 if (samplingFrequency > 30000.0) {
141 samplingFrequency = samplingFrequency / 2.0;
142 thee = Sound_resample (me, samplingFrequency, 1);
143 } else {
144 thee = Data_copy (me);
145 }
146 /*
147 Pre-emphasis with fixed coefficient 0.9
148 */
149 for (integer i = thy nx; i > 1; i --)
150 thy z [1] [i] -= 0.9 * thy z [1] [i - 1];
151
152 const integer nosInWindow = Melder_ifloor (analysisWidth * samplingFrequency);
153 Melder_require (nosInWindow >= 8,
154 U"Analysis window too short.");
155
156 double t1;
157 integer numberOfFrames;
158 Sampled_shortTermAnalysis (thee.get(), analysisWidth, dt, & numberOfFrames, & t1);
159 autoVEC hamming = raw_VEC (nosInWindow);
160 for (integer i = 1; i <= nosInWindow; i ++)
161 hamming [i] = 0.54 - 0.46 * cos (NUM2pi * (i - 1) / (nosInWindow - 1));
162
163 const integer nfft = Melder_clippedLeft (8_integer /* minimum possible */, Melder_iroundUpToPowerOfTwo (nosInWindow));
164 const integer nfftdiv2 = nfft / 2;
165 autoVEC fftbuf = zero_VEC (nfft); // "complex" array
166 autoVEC spectrum = zero_VEC (nfftdiv2 + 1); // +1 needed
167 autoNUMFourierTable fftTable = NUMFourierTable_create (nfft); // sound to spectrum
168
169 const double qmax = 0.5 * nfft / samplingFrequency, dq = qmax / (nfftdiv2 + 1);
170 autoPowerCepstrogram him = PowerCepstrogram_create (my xmin, my xmax, numberOfFrames, dt, t1, 0, qmax, nfftdiv2+1, dq, 0);
171
172 autoMelderProgress progress (U"Sound to PowerCepstrogram...");
173
174 for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) {
175 const double tbegin = std::max (thy xmin, t1 + (iframe - 1) * dt - analysisWidth / 2.0);
176 const integer istart = std::max (1_integer, Sampled_xToNearestIndex (thee.get(), tbegin));
177 integer iend = istart + nosInWindow - 1;
178 Melder_clipRight (& iend, thy nx);
179 fftbuf.part (1, nosInWindow) <<= thy z.row (1).part (istart, iend) * hamming.all();
180 fftbuf.part (nosInWindow + 1, nfft) <<= 0.0;
181
182 NUMfft_forward (fftTable.get(), fftbuf.get());
183 complexfftoutput_to_power (fftbuf.get(), spectrum.get(), true); // log10(|fft|^2)
184
185 centre_VEC_inout (spectrum.get()); // subtract average
186 /*
187 Here we diverge from Hillenbrand as he takes the fft of half of the spectral values.
188 H. forgets that the actual spectrum has nfft/2+1 values. Therefore, we take the inverse
189 transform because this keeps the number of samples a power of 2.
190 At the same time this results in twice as many numbers in the quefrency domain, i.e. we end up with nfft/2+1
191 numbers while H. has only nfft/4!
192 */
193 fftbuf [1] = spectrum [1];
194 for (integer i = 2; i < nfftdiv2 + 1; i ++) {
195 fftbuf [i+i-2] = spectrum [i];
196 fftbuf [i+i-1] = 0.0;
197 }
198 fftbuf [nfft] = spectrum [nfftdiv2 + 1];
199 NUMfft_backward (fftTable.get(), fftbuf.get());
200 for (integer i = 1; i <= nfftdiv2 + 1; i ++)
201 his z [i] [iframe] = fftbuf [i] * fftbuf [i];
202
203 if (iframe % 10 == 1)
204 Melder_progress ((double) iframe / numberOfFrames, U"Cepstrogram analysis of frame ",
205 iframe, U" out of ", numberOfFrames, U".");
206 }
207 return him;
208 } catch (MelderError) {
209 Melder_throw (me, U": no Cepstrogram created.");
210 }
211 }
212 /* End of file Sound_to_PowerCepstrogram.cpp */