| 1 | /* LPC_and_LineSpectralFrequencies.cpp |
| 2 | * |
| 3 | * Copyright (C) 2016-2021,2024,2025 David Weenink |
| 4 | * |
| 5 | * This program 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 program 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 program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 18 | */ |
| 19 | |
| 20 | /* |
| 21 | djmw 20160421 Initial version |
| 22 | */ |
| 23 | |
| 24 | #include "LPC_and_LineSpectralFrequencies.h"cxx . . $ `<file>/LPC/LPC_and_LineSpectralFrequencies.h`/ |
| 25 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 26 | |
| 27 | /* |
| 28 | Conversion from Y(w) to a polynomial in x (= 2 cos (w)) |
| 29 | From: Joseph Rothweiler (1999), "On Polynomial Reduction in the Computation of LSP Frequencies." |
| 30 | IEEE Trans. on ASSP 7, 592--594. |
| 31 | */ |
| 32 | static void cos2xcxx . . $ cos2x(4452523f51a9471a). (VECVUcxx . . $ VECVU# const& g) { |
| 33 | for (integercxx . . $ integer# i = 3; i <= g.sizecxx . . $ vectorview#size.; i ++) { |
| 34 | for (integercxx . . $ integer# j = g.sizecxx . . $ vectorview#size.; j > i; j --) |
| 35 | g [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).j - 2] -= g [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).j]; |
| 36 | g [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).i - 2] -= 2.0 * g [cxx . . $ vectorview#`operator[]`(8a4599b8835d1ce1).i]; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | static void LPC_Frame_into_Polynomial_sumcxx . . $ LPC_Frame_into_Polynomial_sum(97441d2da5522b25). (LPC_Framecxx . . $ LPC_Frame# me, Polynomialcxx . . $ Polynomial# psum) { |
| 41 | /* |
| 42 | Fs (z) = A(z) + z^-(p+1) A(1/z) |
| 43 | */ |
| 44 | const integercxx . . $ integer# order = mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients., g_order = (order + 1) / 2; // orders |
| 45 | psum -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).order + 2] = 1.0; |
| 46 | for (integercxx . . $ integer# i = 1; i <= order; i ++) |
| 47 | psum -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).order + 2 - i] = mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structLPC_Frame#a. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] + mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structLPC_Frame#a. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).order + 1 - i]; |
| 48 | |
| 49 | psum -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = 1.0; |
| 50 | psum -> numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. = order + 2; |
| 51 | |
| 52 | if (order % 2 == 0) // order even |
| 53 | Polynomial_divide_firstOrderFactorcxx . . $ Polynomial_divide_firstOrderFactor(7b62d278d518bf28). (psum, -1.0, nullptr); |
| 54 | /* |
| 55 | Transform to cos(w) terms |
| 56 | */ |
| 57 | for (integercxx . . $ integer# i = 1; i <= g_order + 1; i ++) |
| 58 | psum -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = psum -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).g_order + i]; |
| 59 | |
| 60 | psum -> numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. = g_order + 1; |
| 61 | /* |
| 62 | To Chebychev |
| 63 | */ |
| 64 | cos2xcxx . . $ cos2x(4452523f51a9471a). (psumcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). -> coefficientscxx . . $ structFunctionSeries#coefficients..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, psum -> numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.)); |
| 65 | } |
| 66 | |
| 67 | static void LPC_Frame_into_Polynomial_difcxx . . $ LPC_Frame_into_Polynomial_dif(97441d2da5522b25). (LPC_Framecxx . . $ LPC_Frame# me, Polynomialcxx . . $ Polynomial# pdif) { |
| 68 | /* |
| 69 | Fa (z) = A(z) - z^-(p+1)A(1/z) |
| 70 | */ |
| 71 | const integercxx . . $ integer# order = mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients.; |
| 72 | pdif -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).order + 2] = -1.0; |
| 73 | for (integercxx . . $ integer# i = 1; i <= order; i ++) |
| 74 | pdif -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).order + 2 - i] = - mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structLPC_Frame#a. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] + mycxx . . $ `melder/melder.h:44:9`! acxx . . $ structLPC_Frame#a. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).order + 1 - i]; |
| 75 | |
| 76 | pdif -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = 1.0; |
| 77 | pdif -> numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. = order + 2; |
| 78 | |
| 79 | if (order % 2 == 0) { |
| 80 | /* |
| 81 | Fa(z)/(z-1) |
| 82 | */ |
| 83 | Polynomial_divide_firstOrderFactorcxx . . $ Polynomial_divide_firstOrderFactor(7b62d278d518bf28). (pdif, 1.0, nullptr); |
| 84 | } else { |
| 85 | /* |
| 86 | Fa(z) / (z^2 - 1) |
| 87 | */ |
| 88 | Polynomial_divide_secondOrderFactorcxx . . $ Polynomial_divide_secondOrderFactor(126344cf3e2bb2ad). (pdif, 1.0); |
| 89 | } |
| 90 | /* |
| 91 | Transform to cos(w) terms |
| 92 | */ |
| 93 | integercxx . . $ integer# g_order = pdif -> numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. / 2; |
| 94 | for (integercxx . . $ integer# i = 1; i <= g_order + 1; i ++) |
| 95 | pdif -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = pdif -> coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).g_order + i]; |
| 96 | |
| 97 | pdif -> numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. = g_order + 1; |
| 98 | /* |
| 99 | To Chebychev |
| 100 | */ |
| 101 | cos2xcxx . . $ cos2x(4452523f51a9471a). (pdifcxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). -> coefficientscxx . . $ structFunctionSeries#coefficients..partcxx . . $ autovector#part(2d8d48654ca64e70). (1, pdif -> numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.)); |
| 102 | } |
| 103 | |
| 104 | static integercxx . . $ integer# Polynomial_into_Roots_searchOnGridcxx . . $ Polynomial_into_Roots_searchOnGrid(1c0384dac4f34a14). (Polynomialcxx . . $ Polynomial# me, Rootscxx . . $ Roots# thee, double gridSize) { |
| 105 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (thycxx . . $ `melder/melder.h:45:9`! numberOfRoots >= mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients - 1); |
| 106 | double xmin = mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.; |
| 107 | integercxx . . $ integer# numberOfRootsFound = 0; |
| 108 | while (xmin < mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. && numberOfRootsFound < mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1) { |
| 109 | double xmax = xmin + gridSize; |
| 110 | xmax = xmax > mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. ? mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. : xmax; |
| 111 | const double root = Polynomial_findOneSimpleRealRoot_ridderscxx . . $ Polynomial_findOneSimpleRealRoot_ridders(2ebb020f5d743d85). (me, xmin, xmax); |
| 112 | if (isdefinedcxx . . $ isdefined(f8197d536af80c96). (root) && (numberOfRootsFound == 0 || thycxx . . $ `melder/melder.h:45:9`! rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).numberOfRootsFound].realcxx . . $ std/complex#real(21b6c25e127938e2).() != root)) { |
| 113 | thycxx . . $ `melder/melder.h:45:9`! rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).++ numberOfRootsFound]. realcxx . . $ std/complex#real(ba8836b59fb42830). (root); // root not at border of interval |
| 114 | thycxx . . $ `melder/melder.h:45:9`! rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).numberOfRootsFound]. imagcxx . . $ std/complex#imag(ba8836b59fb42830). (0.0); |
| 115 | } |
| 116 | xmin = xmax; |
| 117 | } |
| 118 | return numberOfRootsFound; |
| 119 | } |
| 120 | |
| 121 | void LPC_into_LineSpectralFrequenciescxx . . $ LPC_into_LineSpectralFrequencies(291bfda6ea7ba733). (constLPCcxx . . $ constLPC# inputLPC, mutableLineSpectralFrequenciescxx . . $ mutableLineSpectralFrequencies# outputLSF, double gridSize) { |
| 122 | Sampleds_requireEqualDomainsAndSamplingcxx . . $ Sampleds_requireEqualDomainsAndSampling(8783de53eef05e7). (inputLPC, outputLSF); |
| 123 | if (gridSize <= 0.0) |
| 124 | gridSize = 0.02; |
| 125 | const integercxx . . $ integer# numberOfFrames = inputLPC -> nxcxx . . $ structSampled#nx., thresholdNumberOfFramesPerThread = 40; |
| 126 | const integercxx . . $ integer# numberOfCoefficients = inputLPC -> maxnCoefficientscxx . . $ structLPC#maxnCoefficients. + 1; |
| 127 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"LPC to LineSpectralFrequencies..."); |
| 128 | |
| 129 | MelderThread_PARALLELcxx . . $ `melder/MelderThread.h:173:9`! (numberOfFrames, thresholdNumberOfFramesPerThread) { |
| 130 | |
| 131 | autoPolynomialcxx . . $ autoPolynomial# gsum = Polynomial_createcxx . . $ Polynomial_create(7da2dc008a3770b9). (-2.0, 2.0, numberOfCoefficients); // large enough |
| 132 | autoPolynomialcxx . . $ autoPolynomial# gdif = Polynomial_createcxx . . $ Polynomial_create(7da2dc008a3770b9). (-2.0, 2.0, numberOfCoefficients); |
| 133 | autoRootscxx . . $ autoRoots# roots = Roots_createcxx . . $ Roots_create(27f7be40982d89fc). ((numberOfCoefficients + 1) / 2); |
| 134 | |
| 135 | MelderThread_FORcxx . . $ `melder/MelderThread.h:183:9`! (iframe) { |
| 136 | |
| 137 | if (MelderThread_IS_MASTERcxx . . $ `melder/MelderThread.h:199:9`!) { |
| 138 | const double estimatedProgress = MelderThread_ESTIMATED_PROGRESScxx . . $ `melder/MelderThread.h:202:9`!; |
| 139 | Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). (0.98 * estimatedProgress, |
| 140 | U"Analysed approximately ", Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (numberOfFrames * estimatedProgress), |
| 141 | U" out of ", numberOfFrames, U" frames" |
| 142 | ); |
| 143 | } |
| 144 | LPC_Framecxx . . $ LPC_Frame# inputFrame = & inputLPC -> d_framescxx . . $ structLPC#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 145 | LineSpectralFrequencies_Framecxx . . $ LineSpectralFrequencies_Frame# outputFrame = & outputLSF -> d_framescxx . . $ structLineSpectralFrequencies#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 146 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (inputFrame -> nCoefficients == inputFrame -> a.size); // check invariant |
| 147 | const double maximumFrequency = outputLSF -> maximumFrequencycxx . . $ structLineSpectralFrequencies#maximumFrequency.; |
| 148 | /* |
| 149 | Construct Fs and Fa |
| 150 | divide out the zeros |
| 151 | transform to polynomial equations gsum and gdif of half the order |
| 152 | */ |
| 153 | LPC_Frame_into_Polynomial_sumcxx . . $ LPC_Frame_into_Polynomial_sum(97441d2da5522b25). (inputFrame, gsum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 154 | const integercxx . . $ integer# half_order_gsum = gsum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1; |
| 155 | LPC_Frame_into_Polynomial_difcxx . . $ LPC_Frame_into_Polynomial_dif(97441d2da5522b25). (inputFrame, gdif.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 156 | const integercxx . . $ integer# half_order_gdif = gdif ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1; |
| 157 | double currentGridSize = gridSize; |
| 158 | integercxx . . $ integer# numberOfBisections = 0, numberOfRootsFound = 0; |
| 159 | while (numberOfRootsFound < half_order_gsum && numberOfBisections < 10) { |
| 160 | numberOfRootsFound = Polynomial_into_Roots_searchOnGridcxx . . $ Polynomial_into_Roots_searchOnGrid(1c0384dac4f34a14). (gsum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), roots.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), currentGridSize); |
| 161 | currentGridSize *= 0.5; |
| 162 | numberOfBisections++; |
| 163 | } |
| 164 | integercxx . . $ integer# frameAnalysisInfo = 0; |
| 165 | if (numberOfBisections >= 10) |
| 166 | frameAnalysisInfo = 1; // too many bisections |
| 167 | /* |
| 168 | [gsum-> xmin, gsum -> xmax] <==> [nyquistFrequency, 0], |
| 169 | i.e. highest root corresponds to lowest frequency |
| 170 | */ |
| 171 | for (integercxx . . $ integer# i = 1; i <= half_order_gsum; i ++) |
| 172 | outputFrame -> frequenciescxx . . $ structLineSpectralFrequencies_Frame#frequencies. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2 * i - 1] = acoscxx . . $ acos(fee4f1d22bc8e617). (roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).half_order_gsum + 1 - i].realcxx . . $ std/complex#real(21b6c25e127938e2).() / 2.0) / NUMpicxx . . $ NUMpi. * maximumFrequency; |
| 173 | /* |
| 174 | The roots of gdif lie inbetween the roots of gsum |
| 175 | */ |
| 176 | for (integercxx . . $ integer# i = 1; i <= half_order_gdif; i ++) { |
| 177 | const double xmax = roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).half_order_gsum + 1 - i].realcxx . . $ std/complex#real(21b6c25e127938e2).(); |
| 178 | const double xmin = ( i == half_order_gsum ? gsum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). xmincxx . . $ structFunction#xmin. : roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).half_order_gsum - i].realcxx . . $ std/complex#real(21b6c25e127938e2).() ); |
| 179 | const double root = Polynomial_findOneSimpleRealRoot_ridderscxx . . $ Polynomial_findOneSimpleRealRoot_ridders(2ebb020f5d743d85). (gdif.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), xmin, xmax); |
| 180 | if (isdefinedcxx . . $ isdefined(f8197d536af80c96). (root)) |
| 181 | outputFrame -> frequenciescxx . . $ structLineSpectralFrequencies_Frame#frequencies. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2 * i] = acoscxx . . $ acos(fee4f1d22bc8e617). (root / 2.0) / NUMpicxx . . $ NUMpi. * maximumFrequency; |
| 182 | else |
| 183 | outputFrame -> numberOfFrequenciescxx . . $ structLineSpectralFrequencies_Frame#numberOfFrequencies. --; |
| 184 | } |
| 185 | outputFrame -> frequenciescxx . . $ structLineSpectralFrequencies_Frame#frequencies..resizecxx . . $ autovector#resize(d91e89fbd57cd1). (outputFrame -> numberOfFrequenciescxx . . $ structLineSpectralFrequencies_Frame#numberOfFrequencies.); // maintain invariant |
| 186 | |
| 187 | } |
| 188 | |
| 189 | } MelderThread_ENDPARALLELcxx . . $ `melder/MelderThread.h:189:9`! |
| 190 | } |
| 191 | |
| 192 | autoLineSpectralFrequenciescxx . . $ autoLineSpectralFrequencies# LPC_to_LineSpectralFrequenciescxx . . $ LPC_to_LineSpectralFrequencies(b4e47ed5ea5d143). (constLPCcxx . . $ constLPC# me, double gridSize) { |
| 193 | try { |
| 194 | const double nyquistFrequency = 0.5 / mycxx . . $ `melder/melder.h:44:9`! samplingPeriodcxx . . $ structLPC#samplingPeriod.; |
| 195 | autoLineSpectralFrequenciescxx . . $ autoLineSpectralFrequencies# outputLSF = LineSpectralFrequencies_createcxx . . $ LineSpectralFrequencies_create(e0310cab48391c2c). (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`! maxnCoefficientscxx . . $ structLPC#maxnCoefficients., nyquistFrequency); |
| 196 | for (integercxx . . $ integer# iframe = 1; iframe <= outputLSF ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; iframe ++) |
| 197 | LineSpectralFrequencies_Frame_initcxx . . $ LineSpectralFrequencies_Frame_init(b16022a24ee3fbdc). (& outputLSF ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). d_framescxx . . $ structLineSpectralFrequencies#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe], outputLSF ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). maximumNumberOfFrequenciescxx . . $ structLineSpectralFrequencies#maximumNumberOfFrequencies.); |
| 198 | LPC_into_LineSpectralFrequenciescxx . . $ LPC_into_LineSpectralFrequencies(291bfda6ea7ba733). (me, outputLSF.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), gridSize); |
| 199 | return outputLSFcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 200 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 201 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no LineSpectralFrequencies created."); |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | /**************************** LineSpectralFrequencies to LPC **********************************/ |
| 206 | |
| 207 | void LineSpectralFrequencies_into_LPCcxx . . $ LineSpectralFrequencies_into_LPC(f5604394291c4f3e). (constLineSpectralFrequenciescxx . . $ constLineSpectralFrequencies# me, mutableLPCcxx . . $ mutableLPC# outputLPC) { |
| 208 | Sampleds_requireEqualDomainsAndSamplingcxx . . $ Sampleds_requireEqualDomainsAndSampling(8783de53eef05e7). (me, outputLPC); |
| 209 | const integercxx . . $ integer# numberOfFrames = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., thresholdNumberOfFramesPerThread = 40; |
| 210 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"LineSpectralFrequencies to LPC..."); |
| 211 | |
| 212 | MelderThread_PARALLELcxx . . $ `melder/MelderThread.h:173:9`! (numberOfFrames, thresholdNumberOfFramesPerThread) { |
| 213 | autoPolynomialcxx . . $ autoPolynomial# fs = Polynomial_createcxx . . $ Polynomial_create(7da2dc008a3770b9). (-1.0, 1.0, mycxx . . $ `melder/melder.h:44:9`! maximumNumberOfFrequenciescxx . . $ structLineSpectralFrequencies#maximumNumberOfFrequencies. + 2); |
| 214 | autoPolynomialcxx . . $ autoPolynomial# fa = Polynomial_createcxx . . $ Polynomial_create(7da2dc008a3770b9). (-1.0, 1.0, mycxx . . $ `melder/melder.h:44:9`! maximumNumberOfFrequenciescxx . . $ structLineSpectralFrequencies#maximumNumberOfFrequencies. + 2); |
| 215 | MelderThread_FORcxx . . $ `melder/MelderThread.h:183:9`! (iframe) { |
| 216 | if (MelderThread_IS_MASTERcxx . . $ `melder/MelderThread.h:199:9`!) { |
| 217 | const double estimatedProgress = MelderThread_ESTIMATED_PROGRESScxx . . $ `melder/MelderThread.h:202:9`!; |
| 218 | Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). (0.98 * estimatedProgress, |
| 219 | U"Analysed approximately ", Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (numberOfFrames * estimatedProgress), |
| 220 | U" out of ", numberOfFrames, U" frames" |
| 221 | ); |
| 222 | } |
| 223 | LPC_Framecxx . . $ LPC_Frame# lpcFrame = & outputLPC -> d_framescxx . . $ structLPC#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 224 | VECcxx . . $ VEC# a = lpcFrame -> acxx . . $ structLPC_Frame#a..getcxx . . $ autovector#get(e8d1da7fe68a6585).(); |
| 225 | LineSpectralFrequencies_Framecxx . . $ LineSpectralFrequencies_Frame# lsfFrame = & mycxx . . $ `melder/melder.h:44:9`! d_framescxx . . $ structLineSpectralFrequencies#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 226 | const double maximumFrequency = mycxx . . $ `melder/melder.h:44:9`! maximumFrequencycxx . . $ structLineSpectralFrequencies#maximumFrequency.; |
| 227 | const integercxx . . $ integer# numberOfFrequencies = lsfFrame -> numberOfFrequenciescxx . . $ structLineSpectralFrequencies_Frame#numberOfFrequencies.; |
| 228 | integercxx . . $ integer# numberOfOmegas = (numberOfFrequencies + 1) / 2; |
| 229 | /* |
| 230 | Reconstruct Fs (z) |
| 231 | Use lpcFrame -> a as a buffer whose size changes!!! |
| 232 | */ |
| 233 | for (integercxx . . $ integer# i = 1; i <= numberOfOmegas; i ++) { |
| 234 | const double omega = lsfFrame -> frequenciescxx . . $ structLineSpectralFrequencies_Frame#frequencies. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2 * i - 1] / maximumFrequency * NUMpicxx . . $ NUMpi.; |
| 235 | a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = -2.0 * coscxx . . $ cos(fee4f1d22bc8e617). (omega); |
| 236 | } |
| 237 | Polynomial_initFromProductOfSecondOrderTermscxx . . $ Polynomial_initFromProductOfSecondOrderTerms(9f3ac13450f26e09). (fs.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), acxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ vector#part(ae267db9e2c04be7). (1, numberOfOmegas)); |
| 238 | /* |
| 239 | Reconstruct Fa (z) |
| 240 | */ |
| 241 | numberOfOmegas = numberOfFrequencies / 2; |
| 242 | for (integercxx . . $ integer# i = 1; i <= numberOfOmegas; i ++) { |
| 243 | const double omega = lsfFrame -> frequenciescxx . . $ structLineSpectralFrequencies_Frame#frequencies. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2 * i] / maximumFrequency * NUMpicxx . . $ NUMpi.; |
| 244 | a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = -2.0 * coscxx . . $ cos(fee4f1d22bc8e617). (omega); |
| 245 | } |
| 246 | Polynomial_initFromProductOfSecondOrderTermscxx . . $ Polynomial_initFromProductOfSecondOrderTerms(9f3ac13450f26e09). (fa.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), acxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd)..partcxx . . $ vector#part(ae267db9e2c04be7). (1, numberOfOmegas)); |
| 247 | |
| 248 | if (numberOfFrequencies % 2 == 0) { |
| 249 | Polynomial_multiply_firstOrderFactorcxx . . $ Polynomial_multiply_firstOrderFactor(126344cf3e2bb2ad). (fs.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), -1.0); // * (z + 1) |
| 250 | Polynomial_multiply_firstOrderFactorcxx . . $ Polynomial_multiply_firstOrderFactor(126344cf3e2bb2ad). (fa.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1.0); // * (z - 1) |
| 251 | } else { |
| 252 | Polynomial_multiply_secondOrderFactorcxx . . $ Polynomial_multiply_secondOrderFactor(126344cf3e2bb2ad). (fa.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), 1.0); // * (z^2 - 1) |
| 253 | } |
| 254 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (fs -> numberOfCoefficients == fa -> numberOfCoefficients); |
| 255 | /* |
| 256 | A(z) = (Fs(z) + Fa(z) / 2 |
| 257 | */ |
| 258 | for (integercxx . . $ integer# i = 1; i <= fs ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 2; i ++) |
| 259 | a [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).lsfFrame -> numberOfFrequenciescxx . . $ structLineSpectralFrequencies_Frame#numberOfFrequencies. - i + 1] = 0.5 * (fs ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1] + fa ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1]); |
| 260 | } |
| 261 | } MelderThread_ENDPARALLELcxx . . $ `melder/MelderThread.h:189:9`! |
| 262 | } |
| 263 | |
| 264 | autoLPCcxx . . $ autoLPC# LineSpectralFrequencies_to_LPCcxx . . $ LineSpectralFrequencies_to_LPC(cf2aeb5bccf3b796). (constLineSpectralFrequenciescxx . . $ constLineSpectralFrequencies# me) { |
| 265 | try { |
| 266 | autoLPCcxx . . $ autoLPC# thee = LPC_createCompletelyInitializedcxx . . $ LPC_createCompletelyInitialized(dee197ba90f07c69). (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`! maximumNumberOfFrequenciescxx . . $ structLineSpectralFrequencies#maximumNumberOfFrequencies., |
| 267 | 0.5 / mycxx . . $ `melder/melder.h:44:9`! maximumFrequencycxx . . $ structLineSpectralFrequencies#maximumFrequency.); |
| 268 | LineSpectralFrequencies_into_LPCcxx . . $ LineSpectralFrequencies_into_LPC(f5604394291c4f3e). (me, thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 269 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 270 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 271 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no LPC created from LineSpectralFrequencies."); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | /* End of file LPC_and_LineSpectralFrequencies.cpp */ |