| 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" |
| 20 |
#include "LPC_and_Polynomial.h" |
| 21 |
#include "NUM2.h" |
| 22 |
#include "Roots_and_Formant.h" |
| 23 |
|
| 24 |
void Formant_Frame_init (Formant_Frame me, integer numberOfFormants) { |
| 25 |
if (numberOfFormants > 0) |
| 26 |
my formant = newvectorzero <structFormant_Formant> (numberOfFormants); |
| 27 |
my numberOfFormants = my formant.size; // maintain invariant |
| 28 |
} |
| 29 |
|
| 30 |
void LPC_into_Formant (constLPC inputLPC, mutableFormant outputFormant, double margin) { |
| 31 |
Sampleds_requireEqualDomainsAndSampling (inputLPC, outputFormant); |
| 32 |
const integer numberOfFrames = inputLPC -> nx, thresholdNumberOfFramesPerThread = 40; |
| 33 |
autoMelderProgress progress (U"LPC to LineSpectralFrequencies..."); |
| 34 |
const integer order = inputLPC -> maxnCoefficients; |
| 35 |
const integer bufferSize = order * order + order + order + 11 * order; |
| 36 |
const double samplingFrequency = 1.0 / inputLPC -> samplingPeriod; |
| 37 |
|
| 38 |
MelderThread_PARALLEL (numberOfFrames, thresholdNumberOfFramesPerThread) { |
| 39 |
autoVEC buffer = raw_VEC (bufferSize); |
| 40 |
autoPolynomial p = Polynomial_create (-1.0, 1.0, order); |
| 41 |
autoRoots roots = Roots_create (order); |
| 42 |
MelderThread_FOR (iframe) { |
| 43 |
Formant_Frame formantFrame = & outputFormant -> frames [iframe]; |
| 44 |
LPC_Frame inputLPCFrame = & inputLPC -> d_frames [iframe]; |
| 45 |
formantFrame -> intensity = inputLPCFrame -> gain; |
| 46 |
if (inputLPCFrame -> nCoefficients == 0) { |
| 47 |
formantFrame -> numberOfFormants = 0; // TODO Formant_Frame -> resize (newNumberOfFormants) |
| 48 |
formantFrame -> formant.resize (formantFrame -> numberOfFormants); // maintain invariant |
| 49 |
} else { |
| 50 |
LPC_Frame_into_Polynomial (inputLPCFrame, p.get()); |
| 51 |
Polynomial_into_Roots (p.get(), roots.get(), buffer.get()); |
| 52 |
Roots_fixIntoUnitCircle (roots.get()); |
| 53 |
Roots_into_Formant_Frame (roots.get(), formantFrame, samplingFrequency, margin); |
| 54 |
} |
| 55 |
} |
| 56 |
} MelderThread_ENDPARALLEL |
| 57 |
|
| 58 |
Formant_sort (outputFormant); |
| 59 |
} |
| 60 |
|
| 61 |
autoFormant LPC_to_Formant (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 integer maximumNumberOfFormants = ( margin == 0.0 ? my maxnCoefficients : (my maxnCoefficients + 1) / 2 ); |
| 70 |
Melder_require (my maxnCoefficients < 100, |
| 71 |
U"We cannot find the roots of a polynomial of order > 99."); |
| 72 |
autoFormant outputFormant = Formant_create (my xmin, my xmax, my nx, my dx, my x1, maximumNumberOfFormants); |
| 73 |
for (integer iframe = 1; iframe <= outputFormant -> nx; iframe ++) { |
| 74 |
Formant_Frame_init (& outputFormant -> frames [iframe], maximumNumberOfFormants); |
| 75 |
} |
| 76 |
LPC_into_Formant (me, outputFormant.get(), margin); |
| 77 |
return outputFormant; |
| 78 |
} catch (MelderError) { |
| 79 |
Melder_throw (me, U": no Formant created."); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void Formant_Frame_scale (Formant_Frame me, double scale) { |
| 84 |
for (integer iformant = 1; iformant <= my numberOfFormants; iformant ++) { |
| 85 |
my formant [iformant]. frequency *= scale; |
| 86 |
my formant [iformant]. bandwidth *= scale; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
void LPC_Frame_into_Formant_Frame (constLPC_Frame me, Formant_Frame thee, double samplingPeriod, double margin) { |
| 91 |
Melder_assert (my nCoefficients == my a.size); // check invariant |
| 92 |
thy intensity = my gain; |
| 93 |
if (my nCoefficients == 0) { |
| 94 |
thy formant. resize (0); |
| 95 |
thy numberOfFormants = thy formant.size; // maintain invariant |
| 96 |
return; |
| 97 |
} |
| 98 |
autoPolynomial p = LPC_Frame_to_Polynomial (me); |
| 99 |
autoRoots r = Polynomial_to_Roots (p.get()); |
| 100 |
Roots_fixIntoUnitCircle (r.get()); |
| 101 |
Roots_into_Formant_Frame (r.get(), thee, 1.0 / samplingPeriod, margin); |
| 102 |
} |
| 103 |
|
| 104 |
void LPC_Frame_into_Formant_Frame (constLPC_Frame me, Formant_Frame thee, double samplingPeriod, |
| 105 |
double margin, Polynomial p, Roots roots, VEC polynomialIntoRootsWorkspace) |
| 106 |
{ |
| 107 |
thy intensity = my gain; |
| 108 |
if (my nCoefficients == 0) { |
| 109 |
thy formant. resize (0); |
| 110 |
return; |
| 111 |
} |
| 112 |
LPC_Frame_into_Polynomial (me, p); |
| 113 |
Polynomial_into_Roots (p, roots, polynomialIntoRootsWorkspace); |
| 114 |
Roots_fixIntoUnitCircle (roots); |
| 115 |
Roots_into_Formant_Frame (roots, thee, 1.0 / samplingPeriod, margin); |
| 116 |
} |
| 117 |
|
| 118 |
void Formant_Frame_into_LPC_Frame (constFormant_Frame me, LPC_Frame thee, double samplingPeriod) { |
| 119 |
if (my numberOfFormants < 1) |
| 120 |
return; |
| 121 |
const double nyquistFrequency = 0.5 / samplingPeriod; |
| 122 |
integer numberOfPoles = 2 * my numberOfFormants; |
| 123 |
autoVEC lpc = zero_VEC (numberOfPoles + 2); // all odd coefficients have to be initialized to zero |
| 124 |
lpc [2] = 1.0; |
| 125 |
integer m = 2; |
| 126 |
for (integer iformant = 1; iformant <= my numberOfFormants; iformant ++) { |
| 127 |
const double formantFrequency = my formant [iformant]. frequency; |
| 128 |
if (formantFrequency > nyquistFrequency) |
| 129 |
continue; |
| 130 |
/* |
| 131 |
D(z): 1 + p z^-1 + q z^-2 |
| 132 |
*/ |
| 133 |
const double r = exp (- NUMpi * my formant [iformant]. bandwidth * samplingPeriod); |
| 134 |
const double p = - 2.0 * r * cos (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 (integer j = m + 2; j > 2; j --) |
| 141 |
lpc [j] += p * lpc [j - 1] + q * lpc [j - 2]; |
| 142 |
m += 2; |
| 143 |
} |
| 144 |
if (thy nCoefficients < numberOfPoles) |
| 145 |
numberOfPoles = thy nCoefficients; |
| 146 |
for (integer i = 1; i <= numberOfPoles; i ++) |
| 147 |
thy a [i] = lpc [i + 2]; |
| 148 |
thy gain = my intensity; |
| 149 |
} |
| 150 |
|
| 151 |
autoLPC Formant_to_LPC (constFormant me, double samplingPeriod) { |
| 152 |
try { |
| 153 |
autoLPC outputLPC = LPC_create (my xmin, my xmax, my nx, my dx, my x1, 2 * my maxnFormants, samplingPeriod); |
| 154 |
const integer thresholdNumberOfFramesPerThread = 80; |
| 155 |
|
| 156 |
MelderThread_PARALLEL (my nx, thresholdNumberOfFramesPerThread) { |
| 157 |
MelderThread_FOR (iframe) { |
| 158 |
const Formant_Frame f = & my frames [iframe]; |
| 159 |
const LPC_Frame lpcFrame = & outputLPC -> d_frames [iframe]; |
| 160 |
const integer numberOfCoefficients = 2 * f -> numberOfFormants; |
| 161 |
LPC_Frame_init (lpcFrame, numberOfCoefficients); |
| 162 |
Formant_Frame_into_LPC_Frame (f, lpcFrame, samplingPeriod); |
| 163 |
} |
| 164 |
} MelderThread_ENDPARALLEL |
| 165 |
|
| 166 |
return outputLPC; |
| 167 |
} catch (MelderError) { |
| 168 |
Melder_throw (me, U": no LPC created."); |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
/* End of file LPC_and_Formant.cpp */ |