| 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" |
| 25 |
#include "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 cos2x (VECVU const& g) { |
| 33 |
for (integer i = 3; i <= g.size; i ++) { |
| 34 |
for (integer j = g.size; j > i; j --) |
| 35 |
g [j - 2] -= g [j]; |
| 36 |
g [i - 2] -= 2.0 * g [i]; |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static void LPC_Frame_into_Polynomial_sum (LPC_Frame me, Polynomial psum) { |
| 41 |
/* |
| 42 |
Fs (z) = A(z) + z^-(p+1) A(1/z) |
| 43 |
*/ |
| 44 |
const integer order = my nCoefficients, g_order = (order + 1) / 2; // orders |
| 45 |
psum -> coefficients [order + 2] = 1.0; |
| 46 |
for (integer i = 1; i <= order; i ++) |
| 47 |
psum -> coefficients [order + 2 - i] = my a [i] + my a [order + 1 - i]; |
| 48 |
|
| 49 |
psum -> coefficients [1] = 1.0; |
| 50 |
psum -> numberOfCoefficients = order + 2; |
| 51 |
|
| 52 |
if (order % 2 == 0) // order even |
| 53 |
Polynomial_divide_firstOrderFactor (psum, -1.0, nullptr); |
| 54 |
/* |
| 55 |
Transform to cos(w) terms |
| 56 |
*/ |
| 57 |
for (integer i = 1; i <= g_order + 1; i ++) |
| 58 |
psum -> coefficients [i] = psum -> coefficients [g_order + i]; |
| 59 |
|
| 60 |
psum -> numberOfCoefficients = g_order + 1; |
| 61 |
/* |
| 62 |
To Chebychev |
| 63 |
*/ |
| 64 |
cos2x (psum -> coefficients.part (1, psum -> numberOfCoefficients)); |
| 65 |
} |
| 66 |
|
| 67 |
static void LPC_Frame_into_Polynomial_dif (LPC_Frame me, Polynomial pdif) { |
| 68 |
/* |
| 69 |
Fa (z) = A(z) - z^-(p+1)A(1/z) |
| 70 |
*/ |
| 71 |
const integer order = my nCoefficients; |
| 72 |
pdif -> coefficients [order + 2] = -1.0; |
| 73 |
for (integer i = 1; i <= order; i ++) |
| 74 |
pdif -> coefficients [order + 2 - i] = - my a [i] + my a [order + 1 - i]; |
| 75 |
|
| 76 |
pdif -> coefficients [1] = 1.0; |
| 77 |
pdif -> numberOfCoefficients = order + 2; |
| 78 |
|
| 79 |
if (order % 2 == 0) { |
| 80 |
/* |
| 81 |
Fa(z)/(z-1) |
| 82 |
*/ |
| 83 |
Polynomial_divide_firstOrderFactor (pdif, 1.0, nullptr); |
| 84 |
} else { |
| 85 |
/* |
| 86 |
Fa(z) / (z^2 - 1) |
| 87 |
*/ |
| 88 |
Polynomial_divide_secondOrderFactor (pdif, 1.0); |
| 89 |
} |
| 90 |
/* |
| 91 |
Transform to cos(w) terms |
| 92 |
*/ |
| 93 |
integer g_order = pdif -> numberOfCoefficients / 2; |
| 94 |
for (integer i = 1; i <= g_order + 1; i ++) |
| 95 |
pdif -> coefficients [i] = pdif -> coefficients [g_order + i]; |
| 96 |
|
| 97 |
pdif -> numberOfCoefficients = g_order + 1; |
| 98 |
/* |
| 99 |
To Chebychev |
| 100 |
*/ |
| 101 |
cos2x (pdif -> coefficients.part (1, pdif -> numberOfCoefficients)); |
| 102 |
} |
| 103 |
|
| 104 |
static integer Polynomial_into_Roots_searchOnGrid (Polynomial me, Roots thee, double gridSize) { |
| 105 |
Melder_assert (thy numberOfRoots >= my numberOfCoefficients - 1); |
| 106 |
double xmin = my xmin; |
| 107 |
integer numberOfRootsFound = 0; |
| 108 |
while (xmin < my xmax && numberOfRootsFound < my numberOfCoefficients - 1) { |
| 109 |
double xmax = xmin + gridSize; |
| 110 |
xmax = xmax > my xmax ? my xmax : xmax; |
| 111 |
const double root = Polynomial_findOneSimpleRealRoot_ridders (me, xmin, xmax); |
| 112 |
if (isdefined (root) && (numberOfRootsFound == 0 || thy roots [numberOfRootsFound].real() != root)) { |
| 113 |
thy roots [++ numberOfRootsFound]. real (root); // root not at border of interval |
| 114 |
thy roots [numberOfRootsFound]. imag (0.0); |
| 115 |
} |
| 116 |
xmin = xmax; |
| 117 |
} |
| 118 |
return numberOfRootsFound; |
| 119 |
} |
| 120 |
|
| 121 |
void LPC_into_LineSpectralFrequencies (constLPC inputLPC, mutableLineSpectralFrequencies outputLSF, double gridSize) { |
| 122 |
Sampleds_requireEqualDomainsAndSampling (inputLPC, outputLSF); |
| 123 |
if (gridSize <= 0.0) |
| 124 |
gridSize = 0.02; |
| 125 |
const integer numberOfFrames = inputLPC -> nx, thresholdNumberOfFramesPerThread = 40; |
| 126 |
const integer numberOfCoefficients = inputLPC -> maxnCoefficients + 1; |
| 127 |
autoMelderProgress progress (U"LPC to LineSpectralFrequencies..."); |
| 128 |
|
| 129 |
MelderThread_PARALLEL (numberOfFrames, thresholdNumberOfFramesPerThread) { |
| 130 |
|
| 131 |
autoPolynomial gsum = Polynomial_create (-2.0, 2.0, numberOfCoefficients); // large enough |
| 132 |
autoPolynomial gdif = Polynomial_create (-2.0, 2.0, numberOfCoefficients); |
| 133 |
autoRoots roots = Roots_create ((numberOfCoefficients + 1) / 2); |
| 134 |
|
| 135 |
MelderThread_FOR (iframe) { |
| 136 |
|
| 137 |
if (MelderThread_IS_MASTER) { |
| 138 |
const double estimatedProgress = MelderThread_ESTIMATED_PROGRESS; |
| 139 |
Melder_progress (0.98 * estimatedProgress, |
| 140 |
U"Analysed approximately ", Melder_iround (numberOfFrames * estimatedProgress), |
| 141 |
U" out of ", numberOfFrames, U" frames" |
| 142 |
); |
| 143 |
} |
| 144 |
LPC_Frame inputFrame = & inputLPC -> d_frames [iframe]; |
| 145 |
LineSpectralFrequencies_Frame outputFrame = & outputLSF -> d_frames [iframe]; |
| 146 |
Melder_assert (inputFrame -> nCoefficients == inputFrame -> a.size); // check invariant |
| 147 |
const double maximumFrequency = outputLSF -> 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_sum (inputFrame, gsum.get()); |
| 154 |
const integer half_order_gsum = gsum -> numberOfCoefficients - 1; |
| 155 |
LPC_Frame_into_Polynomial_dif (inputFrame, gdif.get()); |
| 156 |
const integer half_order_gdif = gdif -> numberOfCoefficients - 1; |
| 157 |
double currentGridSize = gridSize; |
| 158 |
integer numberOfBisections = 0, numberOfRootsFound = 0; |
| 159 |
while (numberOfRootsFound < half_order_gsum && numberOfBisections < 10) { |
| 160 |
numberOfRootsFound = Polynomial_into_Roots_searchOnGrid (gsum.get(), roots.get(), currentGridSize); |
| 161 |
currentGridSize *= 0.5; |
| 162 |
numberOfBisections++; |
| 163 |
} |
| 164 |
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 (integer i = 1; i <= half_order_gsum; i ++) |
| 172 |
outputFrame -> frequencies [2 * i - 1] = acos (roots -> roots [half_order_gsum + 1 - i].real() / 2.0) / NUMpi * maximumFrequency; |
| 173 |
/* |
| 174 |
The roots of gdif lie inbetween the roots of gsum |
| 175 |
*/ |
| 176 |
for (integer i = 1; i <= half_order_gdif; i ++) { |
| 177 |
const double xmax = roots -> roots [half_order_gsum + 1 - i].real(); |
| 178 |
const double xmin = ( i == half_order_gsum ? gsum -> xmin : roots -> roots [half_order_gsum - i].real() ); |
| 179 |
const double root = Polynomial_findOneSimpleRealRoot_ridders (gdif.get(), xmin, xmax); |
| 180 |
if (isdefined (root)) |
| 181 |
outputFrame -> frequencies [2 * i] = acos (root / 2.0) / NUMpi * maximumFrequency; |
| 182 |
else |
| 183 |
outputFrame -> numberOfFrequencies --; |
| 184 |
} |
| 185 |
outputFrame -> frequencies.resize (outputFrame -> numberOfFrequencies); // maintain invariant |
| 186 |
|
| 187 |
} |
| 188 |
|
| 189 |
} MelderThread_ENDPARALLEL |
| 190 |
} |
| 191 |
|
| 192 |
autoLineSpectralFrequencies LPC_to_LineSpectralFrequencies (constLPC me, double gridSize) { |
| 193 |
try { |
| 194 |
const double nyquistFrequency = 0.5 / my samplingPeriod; |
| 195 |
autoLineSpectralFrequencies outputLSF = LineSpectralFrequencies_create (my xmin, my xmax, my nx, my dx, my x1, my maxnCoefficients, nyquistFrequency); |
| 196 |
for (integer iframe = 1; iframe <= outputLSF -> nx; iframe ++) |
| 197 |
LineSpectralFrequencies_Frame_init (& outputLSF -> d_frames [iframe], outputLSF -> maximumNumberOfFrequencies); |
| 198 |
LPC_into_LineSpectralFrequencies (me, outputLSF.get(), gridSize); |
| 199 |
return outputLSF; |
| 200 |
} catch (MelderError) { |
| 201 |
Melder_throw (me, U": no LineSpectralFrequencies created."); |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
/**************************** LineSpectralFrequencies to LPC **********************************/ |
| 206 |
|
| 207 |
void LineSpectralFrequencies_into_LPC (constLineSpectralFrequencies me, mutableLPC outputLPC) { |
| 208 |
Sampleds_requireEqualDomainsAndSampling (me, outputLPC); |
| 209 |
const integer numberOfFrames = my nx, thresholdNumberOfFramesPerThread = 40; |
| 210 |
autoMelderProgress progress (U"LineSpectralFrequencies to LPC..."); |
| 211 |
|
| 212 |
MelderThread_PARALLEL (numberOfFrames, thresholdNumberOfFramesPerThread) { |
| 213 |
autoPolynomial fs = Polynomial_create (-1.0, 1.0, my maximumNumberOfFrequencies + 2); |
| 214 |
autoPolynomial fa = Polynomial_create (-1.0, 1.0, my maximumNumberOfFrequencies + 2); |
| 215 |
MelderThread_FOR (iframe) { |
| 216 |
if (MelderThread_IS_MASTER) { |
| 217 |
const double estimatedProgress = MelderThread_ESTIMATED_PROGRESS; |
| 218 |
Melder_progress (0.98 * estimatedProgress, |
| 219 |
U"Analysed approximately ", Melder_iround (numberOfFrames * estimatedProgress), |
| 220 |
U" out of ", numberOfFrames, U" frames" |
| 221 |
); |
| 222 |
} |
| 223 |
LPC_Frame lpcFrame = & outputLPC -> d_frames [iframe]; |
| 224 |
VEC a = lpcFrame -> a.get(); |
| 225 |
LineSpectralFrequencies_Frame lsfFrame = & my d_frames [iframe]; |
| 226 |
const double maximumFrequency = my maximumFrequency; |
| 227 |
const integer numberOfFrequencies = lsfFrame -> numberOfFrequencies; |
| 228 |
integer numberOfOmegas = (numberOfFrequencies + 1) / 2; |
| 229 |
/* |
| 230 |
Reconstruct Fs (z) |
| 231 |
Use lpcFrame -> a as a buffer whose size changes!!! |
| 232 |
*/ |
| 233 |
for (integer i = 1; i <= numberOfOmegas; i ++) { |
| 234 |
const double omega = lsfFrame -> frequencies [2 * i - 1] / maximumFrequency * NUMpi; |
| 235 |
a [i] = -2.0 * cos (omega); |
| 236 |
} |
| 237 |
Polynomial_initFromProductOfSecondOrderTerms (fs.get(), a.part (1, numberOfOmegas)); |
| 238 |
/* |
| 239 |
Reconstruct Fa (z) |
| 240 |
*/ |
| 241 |
numberOfOmegas = numberOfFrequencies / 2; |
| 242 |
for (integer i = 1; i <= numberOfOmegas; i ++) { |
| 243 |
const double omega = lsfFrame -> frequencies [2 * i] / maximumFrequency * NUMpi; |
| 244 |
a [i] = -2.0 * cos (omega); |
| 245 |
} |
| 246 |
Polynomial_initFromProductOfSecondOrderTerms (fa.get(), a.part (1, numberOfOmegas)); |
| 247 |
|
| 248 |
if (numberOfFrequencies % 2 == 0) { |
| 249 |
Polynomial_multiply_firstOrderFactor (fs.get(), -1.0); // * (z + 1) |
| 250 |
Polynomial_multiply_firstOrderFactor (fa.get(), 1.0); // * (z - 1) |
| 251 |
} else { |
| 252 |
Polynomial_multiply_secondOrderFactor (fa.get(), 1.0); // * (z^2 - 1) |
| 253 |
} |
| 254 |
Melder_assert (fs -> numberOfCoefficients == fa -> numberOfCoefficients); |
| 255 |
/* |
| 256 |
A(z) = (Fs(z) + Fa(z) / 2 |
| 257 |
*/ |
| 258 |
for (integer i = 1; i <= fs -> numberOfCoefficients - 2; i ++) |
| 259 |
a [lsfFrame -> numberOfFrequencies - i + 1] = 0.5 * (fs -> coefficients [i + 1] + fa -> coefficients [i + 1]); |
| 260 |
} |
| 261 |
} MelderThread_ENDPARALLEL |
| 262 |
} |
| 263 |
|
| 264 |
autoLPC LineSpectralFrequencies_to_LPC (constLineSpectralFrequencies me) { |
| 265 |
try { |
| 266 |
autoLPC thee = LPC_createCompletelyInitialized (my xmin, my xmax, my nx, my dx, my x1, my maximumNumberOfFrequencies, |
| 267 |
0.5 / my maximumFrequency); |
| 268 |
LineSpectralFrequencies_into_LPC (me, thee.get()); |
| 269 |
return thee; |
| 270 |
} catch (MelderError) { |
| 271 |
Melder_throw (me, U": no LPC created from LineSpectralFrequencies."); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
/* End of file LPC_and_LineSpectralFrequencies.cpp */ |