Debian Codegraph @

LPC/LPC_to_Spectrum.cpp

raw symbols SCIP
1 /* LPC_to_Spectrum.cpp
2 *
3 * Copyright (C) 1994-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 /*
20 djmw 20020529 GPL header
21 djmw 20020529 Changed NUMrealft to NUMforwardRealFastFourierTransform
22 djmw 20030708 Added NUM2.h
23 djmw 20080122 float -> double
24 */
25
26 #include "LPC_to_Spectrum.h"cxx . . $ `<file>/LPC/LPC_to_Spectrum.h`/
27 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
28
29 /*
30 PSD(f) = (sigma^2 T) /|1 + Sum (k=1..p, a [k] exp(-2 pi i f k T))|^2,
31 where sigma^2 == gain, T is samplinginterval
32
33 LPC-spectrum is approximately 20 dB too high (w.r.t. 25 ms spectrum from Sound)
34 */
35
36 void LPC_Frame_into_Spectrumcxx . . $ LPC_Frame_into_Spectrum(a6f57fd19bde75a1). (LPC_Framecxx . . $ LPC_Frame# me, Spectrumcxx . . $ Spectrum# thee, double bandwidthReduction, double deEmphasisFrequency) {
37 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! nCoefficients == mycxx . . $ `melder/melder.h:44:9`! a.size); // check invariant
38 if (mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients. == 0) {
39 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..allcxx . . $ automatrix#all(2114117ba8ed4224).() <<=cxx . . $ `operator<<=`(50b469ae42a42f64). 0.0;
40 return;
41 }
42 /*
43 When deEmphasisFrequency is effective we need 1 extra position in the fftbuffer.
44 */
45 const integercxx . . $ integer# nfft = 2 * (thycxx . . $ `melder/melder.h:45:9`! nxcxx . . $ structSampled#nx. - 1);
46 integercxx . . $ integer# ndata = mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients. + 1;
47 double scale = 1.0 / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (2.0 * thycxx . . $ `melder/melder.h:45:9`! xmaxcxx . . $ structFunction#xmax. * thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx.);
48 if (ndata >= nfft - 1 && (deEmphasisFrequency < thycxx . . $ `melder/melder.h:45:9`! xmaxcxx . . $ structFunction#xmax. || ndata > nfft))
49 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Spectrum size not large enough.");
50
51 autoVECcxx . . $ autoVEC# fftbuffer = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (nfft);
52 /*
53 Copy 1.0, a [1], ... a [p] into fftbuffer
54 */
55 fftbuffer [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = 1.0;
56 fftbuffercxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd)..partcxx . . $ autovector#part(2d8d48654ca64e70). (2, ndata) <<=cxx . . $ `operator<<=`(adea042513329390). mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structLPC_Frame#a..getcxx . . $ autovector#get(e8d1da7fe68a6585).();
57
58 if (deEmphasisFrequency < thycxx . . $ `melder/melder.h:45:9`! xmaxcxx . . $ structFunction#xmax.) {
59 /*
60 Multiply (1, a [1] z^-1, ... a [p] z^-p) by (1 - b z^-1)
61 */
62 const double b = expcxx . . $ exp(fee4f1d22bc8e617). (- NUM2picxx . . $ NUM2pi. * deEmphasisFrequency / thycxx . . $ `melder/melder.h:45:9`! xmaxcxx . . $ structFunction#xmax.);
63 ndata ++;
64 for (integercxx . . $ integer# i = ndata; i > 1; i--)
65 fftbuffer [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] -= b * fftbuffer [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - 1];
66 }
67 /*
68 Calculate sum (k=0..ndata; a [k] (z)^-k) along a contour with radius r:
69 sum (k=0..ndata; a [k] (rz)^-k) = sum (k=0..ndata; (a [k]r^-k) z^-k)
70 */
71 const double g = expcxx . . $ exp(fee4f1d22bc8e617). (NUMpicxx . . $ NUMpi. * bandwidthReduction / (thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx. * nfft)); /* r = 1/g */
72 for (integercxx . . $ integer# i = 2; i <= ndata; i ++)
73 fftbuffer [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] *= powcxx . . $ pow(9454dda30a4b8335). (g, i - 1);
74 /*
75 Perform the fft.
76 The LPC spectrum is obtained by inverting this spectrum.
77 The imaginary parts of the frequencies 0 and Nyquist are 0.
78 */
79 NUMforwardRealFastFourierTransformcxx . . $ NUMforwardRealFastFourierTransform(33ae648fa537298c). (fftbuffer.getcxx . . $ autovector#get(e8d1da7fe68a6585).());
80 if (mycxx . . $ `melder/melder.h:44:9`! gaincxx . . $ structLPC_Frame#gain. > 0.0)
81 scale *= sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! gaincxx . . $ structLPC_Frame#gain.);
82 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = scale / fftbuffer [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1];
83 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = 0.0;
84 for (integercxx . . $ integer# i = 2; i <= nfft / 2; i ++) {
85 /*
86 We use: 1 / (a + ib) = (a - ib) / (a^2 + b^2)
87 */
88 const double re = fftbuffer [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + i - 1], im = fftbuffer [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + i];
89 const double invSquared = scale / (re * re + im * im);
90 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = re * invSquared;
91 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = -im * invSquared;
92 }
93 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.] = scale / fftbuffer [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2];
94 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;
95 }
96
97 autoSpectrumcxx . . $ autoSpectrum# LPC_to_Spectrumcxx . . $ LPC_to_Spectrum(48d866ab98aaf9e7). (LPCcxx . . $ LPC# me, double t, double dfMin, double bandwidthReduction, double deEmphasisFrequency) {
98 try {
99 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! samplingPeriod > 0.0);
100 const double samplingFrequency = 1.0 / mycxx . . $ `melder/melder.h:44:9`! samplingPeriodcxx . . $ structLPC#samplingPeriod.;
101 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! nx >= 1); // preparing for the assertion in Melder_clipped
102 const integercxx . . $ integer# index = Melder_clippedcxx . . $ Melder_clipped(5eb09607cedc971b). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., Sampled_xToNearestIndexcxx . . $ Sampled_xToNearestIndex(405c2491bc3636e9). (me, t), mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
103 integercxx . . $ integer# nfft = 2;
104 if (dfMin <= 0.0) {
105 nfft = 512;
106 dfMin = samplingFrequency / nfft;
107 }
108 while (samplingFrequency / nfft > dfMin || nfft <= mycxx . . $ `melder/melder.h:44:9`! d_framescxx . . $ structLPC#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index].nCoefficientscxx . . $ structLPC_Frame#nCoefficients.)
109 nfft *= 2;
110
111 autoSpectrumcxx . . $ autoSpectrum# thee = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (samplingFrequency / 2.0, nfft / 2 + 1);
112 LPC_Frame_into_Spectrumcxx . . $ LPC_Frame_into_Spectrum(a6f57fd19bde75a1). (& mycxx . . $ `melder/melder.h:44:9`! d_framescxx . . $ structLPC#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index], thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), bandwidthReduction, deEmphasisFrequency);
113 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
114 } catch (MelderErrorcxx . . $ MelderError#) {
115 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Spectrum created.");
116 }
117 }
118
119 /* End of file LPC_to_Spectrum.cpp */