| 1 | /* LPC_and_Formant.cpp |
| 2 | * |
| 3 | * Copyright (C) 1994-2025 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 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 "LPC_and_Formant.h"cxx . . $ `<file>/LPC/LPC_and_Formant.h`/ |
| 20 | #include "LPC_and_Polynomial.h"cxx . . $ `<file>/LPC/LPC_and_Polynomial.h`/ |
| 21 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 22 | #include "Roots_and_Formant.h"cxx . . $ `<file>/LPC/Roots_and_Formant.h`/ |
| 23 | |
| 24 | void Formant_Frame_initcxx . . $ Formant_Frame_init(c15ad6279e61c40d). (Formant_Framecxx . . $ Formant_Frame# me, integercxx . . $ integer# numberOfFormants) { |
| 25 | if (numberOfFormants > 0) |
| 26 | mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <structFormant_Formantcxx . . $ structFormant_Formant#> (numberOfFormants); |
| 27 | mycxx . . $ `melder/melder.h:44:9`! numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. = mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant..sizecxx . . $ autovector#size.; // maintain invariant |
| 28 | } |
| 29 | |
| 30 | void LPC_into_Formantcxx . . $ LPC_into_Formant(93cc955d6b6c1ef9). (constLPCcxx . . $ constLPC# inputLPC, mutableFormantcxx . . $ mutableFormant# outputFormant, double margin) { |
| 31 | Sampleds_requireEqualDomainsAndSamplingcxx . . $ Sampleds_requireEqualDomainsAndSampling(8783de53eef05e7). (inputLPC, outputFormant); |
| 32 | const integercxx . . $ integer# numberOfFrames = inputLPC -> nxcxx . . $ structSampled#nx., thresholdNumberOfFramesPerThread = 40; |
| 33 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"LPC to LineSpectralFrequencies..."); |
| 34 | const integercxx . . $ integer# order = inputLPC -> maxnCoefficientscxx . . $ structLPC#maxnCoefficients.; |
| 35 | const integercxx . . $ integer# bufferSize = order * order + order + order + 11 * order; |
| 36 | const double samplingFrequency = 1.0 / inputLPC -> samplingPeriodcxx . . $ structLPC#samplingPeriod.; |
| 37 | |
| 38 | MelderThread_PARALLELcxx . . $ `melder/MelderThread.h:173:9`! (numberOfFrames, thresholdNumberOfFramesPerThread) { |
| 39 | autoVECcxx . . $ autoVEC# buffer = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (bufferSize); |
| 40 | autoPolynomialcxx . . $ autoPolynomial# p = Polynomial_createcxx . . $ Polynomial_create(7da2dc008a3770b9). (-1.0, 1.0, order); |
| 41 | autoRootscxx . . $ autoRoots# roots = Roots_createcxx . . $ Roots_create(27f7be40982d89fc). (order); |
| 42 | MelderThread_FORcxx . . $ `melder/MelderThread.h:183:9`! (iframe) { |
| 43 | Formant_Framecxx . . $ Formant_Frame# formantFrame = & outputFormant -> framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 44 | LPC_Framecxx . . $ LPC_Frame# inputLPCFrame = & inputLPC -> d_framescxx . . $ structLPC#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 45 | formantFrame -> intensitycxx . . $ structFormant_Frame#intensity. = inputLPCFrame -> gaincxx . . $ structLPC_Frame#gain.; |
| 46 | if (inputLPCFrame -> nCoefficientscxx . . $ structLPC_Frame#nCoefficients. == 0) { |
| 47 | formantFrame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. = 0; // TODO Formant_Frame -> resize (newNumberOfFormants) |
| 48 | formantFrame -> formantcxx . . $ structFormant_Frame#formant..resizecxx . . $ autovector#resize(d91e89fbd57cd1). (formantFrame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.); // maintain invariant |
| 49 | } else { |
| 50 | LPC_Frame_into_Polynomialcxx . . $ LPC_Frame_into_Polynomial(243bb52546919744). (inputLPCFrame, p.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 51 | Polynomial_into_Rootscxx . . $ Polynomial_into_Roots(ecf59616bf38ddc0). (p.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), roots.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), buffer.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 52 | Roots_fixIntoUnitCirclecxx . . $ Roots_fixIntoUnitCircle(ed161181b88d194d). (roots.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 53 | Roots_into_Formant_Framecxx . . $ Roots_into_Formant_Frame(5aa6edc9255cb8f9). (roots.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), formantFrame, samplingFrequency, margin); |
| 54 | } |
| 55 | } |
| 56 | } MelderThread_ENDPARALLELcxx . . $ `melder/MelderThread.h:189:9`! |
| 57 | |
| 58 | Formant_sortcxx . . $ Formant_sort(69449fdcf6481858). (outputFormant); |
| 59 | } |
| 60 | |
| 61 | autoFormantcxx . . $ autoFormant# LPC_to_Formantcxx . . $ LPC_to_Formant(6c989ebfc69f4efc). (constLPCcxx . . $ constLPC# me, double margin) { |
| 62 | try { |
| 63 | /* |
| 64 | In very extreme case all roots of the lpc polynomial might be real. |
| 65 | A real root gives either a frequency at 0 Hz or at the Nyquist frequency. |
| 66 | If margin > 0 these frequencies are filtered out and the number of formants can never exceed |
| 67 | (my maxnCoefficients+1) / 2. |
| 68 | */ |
| 69 | const integercxx . . $ integer# maximumNumberOfFormants = ( margin == 0.0 ? mycxx . . $ `melder/melder.h:44:9`! maxnCoefficientscxx . . $ structLPC#maxnCoefficients. : (mycxx . . $ `melder/melder.h:44:9`! maxnCoefficientscxx . . $ structLPC#maxnCoefficients. + 1) / 2 ); |
| 70 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! maxnCoefficients < 100, |
| 71 | U"We cannot find the roots of a polynomial of order > 99."); |
| 72 | autoFormantcxx . . $ autoFormant# outputFormant = Formant_createcxx . . $ Formant_create(c4f62f8fc805b821). (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., maximumNumberOfFormants); |
| 73 | for (integercxx . . $ integer# iframe = 1; iframe <= outputFormant ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). nxcxx . . $ structSampled#nx.; iframe ++) { |
| 74 | Formant_Frame_initcxx . . $ Formant_Frame_init(c15ad6279e61c40d). (& outputFormant ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe], maximumNumberOfFormants); |
| 75 | } |
| 76 | LPC_into_Formantcxx . . $ LPC_into_Formant(93cc955d6b6c1ef9). (me, outputFormant.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), margin); |
| 77 | return outputFormantcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 78 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 79 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Formant created."); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void Formant_Frame_scalecxx . . $ Formant_Frame_scale(71451262e0ce19ba). (Formant_Framecxx . . $ Formant_Frame# me, double scale) { |
| 84 | for (integercxx . . $ integer# iformant = 1; iformant <= mycxx . . $ `melder/melder.h:44:9`! numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.; iformant ++) { |
| 85 | mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]. frequencycxx . . $ structFormant_Formant#frequency. *= scale; |
| 86 | mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]. bandwidthcxx . . $ structFormant_Formant#bandwidth. *= scale; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void LPC_Frame_into_Formant_Framecxx . . $ LPC_Frame_into_Formant_Frame(dfd7bec558386286). (constLPC_Framecxx . . $ constLPC_Frame# me, Formant_Framecxx . . $ Formant_Frame# thee, double samplingPeriod, double margin) { |
| 91 | 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 |
| 92 | thycxx . . $ `melder/melder.h:45:9`! intensitycxx . . $ structFormant_Frame#intensity. = mycxx . . $ `melder/melder.h:44:9`! gaincxx . . $ structLPC_Frame#gain.; |
| 93 | if (mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients. == 0) { |
| 94 | thycxx . . $ `melder/melder.h:45:9`! formantcxx . . $ structFormant_Frame#formant.. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (0); |
| 95 | thycxx . . $ `melder/melder.h:45:9`! numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. = thycxx . . $ `melder/melder.h:45:9`! formantcxx . . $ structFormant_Frame#formant..sizecxx . . $ autovector#size.; // maintain invariant |
| 96 | return; |
| 97 | } |
| 98 | autoPolynomialcxx . . $ autoPolynomial# p = LPC_Frame_to_Polynomialcxx . . $ LPC_Frame_to_Polynomial(ad2d84d72e496d5e). (me); |
| 99 | autoRootscxx . . $ autoRoots# r = Polynomial_to_Rootscxx . . $ Polynomial_to_Roots(877cc1a78227ca1). (p.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 100 | Roots_fixIntoUnitCirclecxx . . $ Roots_fixIntoUnitCircle(ed161181b88d194d). (r.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 101 | Roots_into_Formant_Framecxx . . $ Roots_into_Formant_Frame(5aa6edc9255cb8f9). (r.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), thee, 1.0 / samplingPeriod, margin); |
| 102 | } |
| 103 | |
| 104 | void LPC_Frame_into_Formant_Framecxx . . $ LPC_Frame_into_Formant_Frame(924e0a11d6140f04). (constLPC_Framecxx . . $ constLPC_Frame# me, Formant_Framecxx . . $ Formant_Frame# thee, double samplingPeriod, |
| 105 | double margin, Polynomialcxx . . $ Polynomial# p, Rootscxx . . $ Roots# roots, VECcxx . . $ VEC# polynomialIntoRootsWorkspace) |
| 106 | { |
| 107 | thycxx . . $ `melder/melder.h:45:9`! intensitycxx . . $ structFormant_Frame#intensity. = mycxx . . $ `melder/melder.h:44:9`! gaincxx . . $ structLPC_Frame#gain.; |
| 108 | if (mycxx . . $ `melder/melder.h:44:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients. == 0) { |
| 109 | thycxx . . $ `melder/melder.h:45:9`! formantcxx . . $ structFormant_Frame#formant.. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (0); |
| 110 | return; |
| 111 | } |
| 112 | LPC_Frame_into_Polynomialcxx . . $ LPC_Frame_into_Polynomial(243bb52546919744). (me, p); |
| 113 | Polynomial_into_Rootscxx . . $ Polynomial_into_Roots(ecf59616bf38ddc0). (p, roots, polynomialIntoRootsWorkspace); |
| 114 | Roots_fixIntoUnitCirclecxx . . $ Roots_fixIntoUnitCircle(ed161181b88d194d). (roots); |
| 115 | Roots_into_Formant_Framecxx . . $ Roots_into_Formant_Frame(5aa6edc9255cb8f9). (roots, thee, 1.0 / samplingPeriod, margin); |
| 116 | } |
| 117 | |
| 118 | void Formant_Frame_into_LPC_Framecxx . . $ Formant_Frame_into_LPC_Frame(95fe1d925a21a5f7). (constFormant_Framecxx . . $ constFormant_Frame# me, LPC_Framecxx . . $ LPC_Frame# thee, double samplingPeriod) { |
| 119 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. < 1) |
| 120 | return; |
| 121 | const double nyquistFrequency = 0.5 / samplingPeriod; |
| 122 | integercxx . . $ integer# numberOfPoles = 2 * mycxx . . $ `melder/melder.h:44:9`! numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.; |
| 123 | autoVECcxx . . $ autoVEC# lpc = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (numberOfPoles + 2); // all odd coefficients have to be initialized to zero |
| 124 | lpc [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = 1.0; |
| 125 | integercxx . . $ integer# m = 2; |
| 126 | for (integercxx . . $ integer# iformant = 1; iformant <= mycxx . . $ `melder/melder.h:44:9`! numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.; iformant ++) { |
| 127 | const double formantFrequency = mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]. frequencycxx . . $ structFormant_Formant#frequency.; |
| 128 | if (formantFrequency > nyquistFrequency) |
| 129 | continue; |
| 130 | /* |
| 131 | D(z): 1 + p z^-1 + q z^-2 |
| 132 | */ |
| 133 | const double r = expcxx . . $ exp(fee4f1d22bc8e617). (- NUMpicxx . . $ NUMpi. * mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iformant]. bandwidthcxx . . $ structFormant_Formant#bandwidth. * samplingPeriod); |
| 134 | const double p = - 2.0 * r * coscxx . . $ cos(fee4f1d22bc8e617). (NUM2picxx . . $ NUM2pi. * formantFrequency * samplingPeriod); |
| 135 | const double q = r * r; |
| 136 | /* |
| 137 | By setting the two extra elements (0, 1) in the lpc vector we can avoid boundary testing; |
| 138 | lpc [3..n+2] come to contain the coefficients. |
| 139 | */ |
| 140 | for (integercxx . . $ integer# j = m + 2; j > 2; j --) |
| 141 | lpc [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] += p * lpc [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - 1] + q * lpc [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - 2]; |
| 142 | m += 2; |
| 143 | } |
| 144 | if (thycxx . . $ `melder/melder.h:45:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients. < numberOfPoles) |
| 145 | numberOfPoles = thycxx . . $ `melder/melder.h:45:9`! nCoefficientscxx . . $ structLPC_Frame#nCoefficients.; |
| 146 | for (integercxx . . $ integer# i = 1; i <= numberOfPoles; i ++) |
| 147 | thycxx . . $ `melder/melder.h:45:9`! acxx . . $ structLPC_Frame#a. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = lpc [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 2]; |
| 148 | thycxx . . $ `melder/melder.h:45:9`! gaincxx . . $ structLPC_Frame#gain. = mycxx . . $ `melder/melder.h:44:9`! intensitycxx . . $ structFormant_Frame#intensity.; |
| 149 | } |
| 150 | |
| 151 | autoLPCcxx . . $ autoLPC# Formant_to_LPCcxx . . $ Formant_to_LPC(d5e8f4892869150a). (constFormantcxx . . $ constFormant# me, double samplingPeriod) { |
| 152 | try { |
| 153 | autoLPCcxx . . $ autoLPC# outputLPC = LPC_createcxx . . $ LPC_create(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., 2 * mycxx . . $ `melder/melder.h:44:9`! maxnFormantscxx . . $ structFormant#maxnFormants., samplingPeriod); |
| 154 | const integercxx . . $ integer# thresholdNumberOfFramesPerThread = 80; |
| 155 | |
| 156 | MelderThread_PARALLELcxx . . $ `melder/MelderThread.h:173:9`! (mycxx . . $ `melder/melder.h:44:9`! nx, thresholdNumberOfFramesPerThread) { |
| 157 | MelderThread_FORcxx . . $ `melder/MelderThread.h:183:9`! (iframe) { |
| 158 | const Formant_Framecxx . . $ Formant_Frame# f = & mycxx . . $ `melder/melder.h:44:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 159 | const LPC_Framecxx . . $ LPC_Frame# lpcFrame = & outputLPC ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). d_framescxx . . $ structLPC#d_frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 160 | const integercxx . . $ integer# numberOfCoefficients = 2 * f -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.; |
| 161 | LPC_Frame_initcxx . . $ LPC_Frame_init(36b24d3ac8bf631d). (lpcFrame, numberOfCoefficients); |
| 162 | Formant_Frame_into_LPC_Framecxx . . $ Formant_Frame_into_LPC_Frame(95fe1d925a21a5f7). (f, lpcFrame, samplingPeriod); |
| 163 | } |
| 164 | } MelderThread_ENDPARALLELcxx . . $ `melder/MelderThread.h:189:9`! |
| 165 | |
| 166 | return outputLPCcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 167 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 168 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no LPC created."); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /* End of file LPC_and_Formant.cpp */ |