| 1 |
/* Sound_and_LPC.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 "SoundFrames.h" |
| 20 |
#include "Sound_and_LPC.h" |
| 21 |
#include "Sound_extensions.h" |
| 22 |
#include "Spectrum.h" |
| 23 |
#include "NUM2.h" |
| 24 |
|
| 25 |
// TODO Remove the Sound_into_LPC variants. Only preemplasis here |
| 26 |
|
| 27 |
void checkLPCAnalysisParameters_e (double sound_dx, integer sound_nx, double physicalAnalysisWidth, integer predictionOrder) { |
| 28 |
volatile const double physicalDuration = sound_dx * sound_nx; |
| 29 |
Melder_require (physicalAnalysisWidth <= physicalDuration, |
| 30 |
U"Your sound is shorter than two window lengths. " |
| 31 |
"Either your sound is too short or your window is too long." |
| 32 |
); |
| 33 |
// we round the minimum duration to be able to use asserterror in testing scripts. |
| 34 |
conststring32 minimumDurationRounded = Melder_fixed (predictionOrder * sound_dx , 5); |
| 35 |
const integer approximateNumberOfSamplesPerWindow = Melder_iroundDown (physicalAnalysisWidth / sound_dx); |
| 36 |
Melder_require (approximateNumberOfSamplesPerWindow > predictionOrder, |
| 37 |
U"Analysis window duration too short. For a prediction order of ", predictionOrder, |
| 38 |
U", the analysis window duration should be greater than ", minimumDurationRounded, |
| 39 |
U" s. Please increase the analysis window duration or lower the prediction order." |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
static void Sound_to_LPC_common_e (constSound inputSound, int predictionOrder, double effectiveAnalysisWidth, double dt, |
| 44 |
double preEmphasisFrequency, autoSound& outputSound, autoLPC& outputLPC) |
| 45 |
{ |
| 46 |
try { |
| 47 |
const double physicalAnalysisWidth = getPhysicalAnalysisWidth2 (effectiveAnalysisWidth, kSound_windowShape::GAUSSIAN_2); |
| 48 |
checkLPCAnalysisParameters_e (inputSound -> dx, inputSound -> nx, physicalAnalysisWidth, predictionOrder); |
| 49 |
integer numberOfFrames; |
| 50 |
double t1; |
| 51 |
autoSound sound = Data_copy (inputSound); |
| 52 |
if (preEmphasisFrequency > 0) // TODO check |
| 53 |
Sound_preEmphasize_inplace (sound.get(), preEmphasisFrequency); |
| 54 |
outputSound = sound.move(); |
| 55 |
Sampled_shortTermAnalysis (outputSound.get(), physicalAnalysisWidth, dt, & numberOfFrames, & t1); |
| 56 |
autoLPC lpc = LPC_createCompletelyInitialized (outputSound -> xmin, outputSound -> xmax, numberOfFrames, dt, t1, predictionOrder, outputSound -> dx); |
| 57 |
outputLPC = lpc.move(); |
| 58 |
} catch (MelderError) { |
| 59 |
Melder_throw (inputSound, U": Sound and/or LPC not created from specification."); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
/*********************** Autocorrelation method *************************************************************/ |
| 64 |
|
| 65 |
void soundFrameIntoLPCFrame_auto (VEC soundFrame, LPC_Frame lpcFrame, VEC a, VEC r, VEC rc, integer& info) { |
| 66 |
integer order = lpcFrame -> nCoefficients, orderp1 = order + 1; |
| 67 |
Melder_assert (a.size >= orderp1); |
| 68 |
Melder_assert (r.size >= orderp1); |
| 69 |
Melder_assert (rc.size >= orderp1); |
| 70 |
info = 0; |
| 71 |
/* |
| 72 |
Compute the autocorrelations |
| 73 |
*/ |
| 74 |
VEC x = soundFrame; |
| 75 |
for (integer i = 1; i <= orderp1; i ++) |
| 76 |
r [i] = NUMinner (x.part (1, x.size - i + 1), x.part (i, x.size)); |
| 77 |
if (r [1] == 0.0) { |
| 78 |
/* |
| 79 |
The sound frame contains only zero's |
| 80 |
*/ |
| 81 |
lpcFrame -> gain = 0.0; |
| 82 |
lpcFrame -> resize (0_integer); // maintain invariant |
| 83 |
info = 1; |
| 84 |
return; |
| 85 |
} |
| 86 |
a [1] = 1.0; |
| 87 |
a [2] = rc [1] = - r [2] / r [1]; |
| 88 |
double gain = r [1] + r [2] * rc [1]; |
| 89 |
lpcFrame -> gain = gain; |
| 90 |
integer i = 1; |
| 91 |
for (i = 2; i <= order; i ++) { |
| 92 |
longdouble s = 0.0; |
| 93 |
for (integer j = 1; j <= i; j ++) |
| 94 |
s += r [i - j + 2] * a [j]; |
| 95 |
rc [i] = - s / gain; |
| 96 |
for (integer j = 2; j <= i / 2 + 1; j ++) { |
| 97 |
const double at = a [j] + rc [i] * a [i - j + 2]; |
| 98 |
a [i - j + 2] += rc [i] * a [j]; |
| 99 |
a [j] = at; |
| 100 |
} |
| 101 |
a [i + 1] = rc [i]; |
| 102 |
gain += rc [i] * s; |
| 103 |
if (gain <= 0.0) { |
| 104 |
info = 2; |
| 105 |
break; |
| 106 |
} |
| 107 |
lpcFrame -> gain = gain; |
| 108 |
} |
| 109 |
const integer numberOfCoefficients = i - 1; |
| 110 |
lpcFrame -> resize (numberOfCoefficients); |
| 111 |
lpcFrame -> a.part (1, numberOfCoefficients) <<= a.part (2, numberOfCoefficients + 1); |
| 112 |
} |
| 113 |
|
| 114 |
void Sound_into_LPC_auto (constSound me, mutableLPC outputLPC, double effectiveAnalysisWidth) { |
| 115 |
Sound_and_LPC_require_equalDomainsAndSamplingPeriods (me, outputLPC); |
| 116 |
const integer thresholdNumberOfFramesPerThread = 40, order = outputLPC -> maxnCoefficients; |
| 117 |
|
| 118 |
MelderThread_PARALLEL (outputLPC -> nx, thresholdNumberOfFramesPerThread) { |
| 119 |
integer info; |
| 120 |
autoVEC a = raw_VEC (order + 1), r = raw_VEC (order + 1), rc = raw_VEC (order + 1); |
| 121 |
autoSoundFrames soundFrames = SoundFrames_createForIntoSampled (me, outputLPC, effectiveAnalysisWidth, |
| 122 |
kSound_windowShape::GAUSSIAN_2, true); |
| 123 |
MelderThread_FOR (iframe) { |
| 124 |
const LPC_Frame lpcFrame = & outputLPC -> d_frames [iframe]; |
| 125 |
VEC soundFrame = soundFrames -> getMonoFrame (iframe); |
| 126 |
soundFrameIntoLPCFrame_auto (soundFrame, lpcFrame, a.get(), r.get(), rc.get(), info); |
| 127 |
} |
| 128 |
} MelderThread_ENDPARALLEL |
| 129 |
} |
| 130 |
|
| 131 |
autoLPC Sound_to_LPC_auto (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, double preEmphasisFrequency) { |
| 132 |
try { |
| 133 |
autoSound inputSound; |
| 134 |
autoLPC outputLPC; |
| 135 |
Sound_to_LPC_common_e (me, predictionOrder, effectiveAnalysisWidth, dt, preEmphasisFrequency, inputSound, outputLPC); |
| 136 |
Sound_into_LPC_auto (inputSound.get(), outputLPC.get(), effectiveAnalysisWidth); |
| 137 |
return outputLPC; |
| 138 |
} catch (MelderError) { |
| 139 |
Melder_throw (me, U": no LPC (auto) created."); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
/*********************** Covariance method *************************************************************/ |
| 144 |
|
| 145 |
void soundFrameIntoLPCFrame_covar (VEC soundFrame, LPC_Frame lpcFrame, VEC a, VEC b, VEC grc, VEC beta, VEC cc, integer & info) { |
| 146 |
const integer n = soundFrame.size, order = lpcFrame -> nCoefficients; |
| 147 |
|
| 148 |
if (lpcFrame -> nCoefficients == 0) { |
| 149 |
info = 6; |
| 150 |
return; |
| 151 |
} |
| 152 |
constVEC x = soundFrame; |
| 153 |
/* |
| 154 |
Compute the covariances |
| 155 |
*/ |
| 156 |
constVECVU xi = x.part (order + 1, n), xim1 = x.part (order, n - 1); |
| 157 |
double gain = NUMinner (xi, xi); |
| 158 |
cc [1] = NUMinner (xi, xim1); |
| 159 |
cc [2] = NUMinner (xim1, xim1); |
| 160 |
|
| 161 |
if (gain == 0.0) { |
| 162 |
info = 1; |
| 163 |
lpcFrame -> gain = gain; |
| 164 |
lpcFrame -> resize (0_integer); //maintain invariant |
| 165 |
return; |
| 166 |
} |
| 167 |
b <<= 0.0; |
| 168 |
b [1] = 1.0; |
| 169 |
beta [1] = cc [2]; |
| 170 |
a [1] = 1.0; |
| 171 |
a [2] = grc [1] = -cc [1] / cc [2]; |
| 172 |
lpcFrame -> gain = gain += grc [1] * cc [1]; |
| 173 |
integer i = 2; |
| 174 |
for (i = 2; i <= order; i ++) { // 130 |
| 175 |
for (integer j = 1; j <= i; j ++) |
| 176 |
cc [i - j + 2] = cc [i - j + 1] + x [order - i + 1] * x [order - i + j] - x [n - i + 1] * x [n - i + j]; |
| 177 |
|
| 178 |
// cc[1]=0.0; for (integer j = m + 1; j <= n; j ++) cc [1] += x [j - i] * x [j]; |
| 179 |
cc [1] = NUMinner (x.part (order + 1 - i, n - i), x.part (order + 1, n)); //30 |
| 180 |
|
| 181 |
b [i * (i + 1) / 2] = 1.0; |
| 182 |
for (integer j = 1; j <= i - 1; j ++) { // 70 |
| 183 |
if (beta [j] < 0.0) { |
| 184 |
info = 2; |
| 185 |
goto end; |
| 186 |
} else if (beta [j] == 0.0) |
| 187 |
continue; |
| 188 |
|
| 189 |
double gam = 0.0; |
| 190 |
for (integer k = 1; k <= j; k ++) |
| 191 |
gam += cc [k + 1] * b [j * (j - 1) / 2 + k]; // 50 |
| 192 |
gam /= beta [j]; |
| 193 |
for (integer k = 1; k <= j; k ++) |
| 194 |
b [i * (i - 1) / 2 + k] -= gam * b [j * (j - 1) / 2 + k]; // 60 |
| 195 |
} |
| 196 |
|
| 197 |
beta [i] = 0.0; |
| 198 |
for (integer j = 1; j <= i; j ++) |
| 199 |
beta [i] += cc [j + 1] * b [i * (i - 1) / 2 + j]; // 80 |
| 200 |
if (beta [i] <= 0.0) { |
| 201 |
info = 3; |
| 202 |
break; |
| 203 |
} |
| 204 |
double s = 0.0; |
| 205 |
for (integer j = 1; j <= i; j ++) |
| 206 |
s += cc [j] * a [j]; // 100 |
| 207 |
grc [i] = -s / beta [i]; |
| 208 |
|
| 209 |
for (integer j = 2; j <= i; j ++) |
| 210 |
a [j] += grc [i] * b [i * (i - 1) / 2 + j - 1]; // 110 |
| 211 |
a [i + 1] = grc [i]; |
| 212 |
s = grc [i] * grc [i] * beta [i]; |
| 213 |
gain -= s; |
| 214 |
if (gain <= 0.0) { |
| 215 |
info = 4; |
| 216 |
break; |
| 217 |
} |
| 218 |
lpcFrame -> gain = gain; |
| 219 |
} |
| 220 |
end: |
| 221 |
const integer numberOfCoefficients = i - 1; |
| 222 |
lpcFrame -> resize (numberOfCoefficients); |
| 223 |
lpcFrame -> a.part (1, numberOfCoefficients) <<= a.part (2, numberOfCoefficients + 1); |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
void Sound_into_LPC_covar (constSound me, mutableLPC outputLPC, double effectiveAnalysisWidth) { |
| 228 |
Sound_and_LPC_require_equalDomainsAndSamplingPeriods (me, outputLPC); |
| 229 |
const integer thresholdNumberOfFramesPerThread = 40, order = outputLPC -> maxnCoefficients; |
| 230 |
|
| 231 |
MelderThread_PARALLEL (outputLPC -> nx, thresholdNumberOfFramesPerThread) { |
| 232 |
autoSoundFrames soundFrames = Thing_new (SoundFrames); |
| 233 |
soundFrames -> initForIntoSampled (me, outputLPC, effectiveAnalysisWidth, kSound_windowShape::GAUSSIAN_2, true); |
| 234 |
integer info; |
| 235 |
autoVEC a = raw_VEC (order + 1); |
| 236 |
autoVEC b = raw_VEC (order * (order + 1) / 2); |
| 237 |
autoVEC grc = raw_VEC (order); |
| 238 |
autoVEC beta = raw_VEC (order); |
| 239 |
autoVEC cc = raw_VEC (order + 1); |
| 240 |
MelderThread_FOR (iframe) { |
| 241 |
const LPC_Frame lpcFrame = & outputLPC -> d_frames [iframe]; |
| 242 |
VEC soundFrame = soundFrames -> getMonoFrame (iframe); |
| 243 |
soundFrameIntoLPCFrame_covar (soundFrame, lpcFrame, a.get(), b.get(), grc.get(), |
| 244 |
beta.get(), cc.get(), info); |
| 245 |
} |
| 246 |
} MelderThread_ENDPARALLEL |
| 247 |
} |
| 248 |
|
| 249 |
autoLPC Sound_to_LPC_covar (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, double preEmphasisFrequency) { |
| 250 |
try { |
| 251 |
autoSound inputSound; |
| 252 |
autoLPC outputLPC; |
| 253 |
Sound_to_LPC_common_e (me, predictionOrder, effectiveAnalysisWidth, dt, preEmphasisFrequency, inputSound, outputLPC); |
| 254 |
Sound_into_LPC_covar (inputSound.get(), outputLPC.get(), effectiveAnalysisWidth); |
| 255 |
return outputLPC; |
| 256 |
} catch (MelderError) { |
| 257 |
Melder_throw (me, U": no LPC (covar) created."); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
/*********************** Burg method *************************************************************/ |
| 262 |
|
| 263 |
void soundFrameIntoLPCFrame_burg (VEC soundFrame, LPC_Frame lpcFrame, VEC b1, VEC b2, |
| 264 |
VEC aa, integer &info) |
| 265 |
{ |
| 266 |
const integer n = soundFrame.size, order = lpcFrame ->nCoefficients; |
| 267 |
VEC x = soundFrame; |
| 268 |
VEC a = lpcFrame -> a.get(); // always safe |
| 269 |
aa <<= 0.0; |
| 270 |
if (n <= 2) { |
| 271 |
a [1] = -1.0; |
| 272 |
lpcFrame -> gain = ( n == 2 ? 0.5 * (x [1] * x [1] + x [2] * x [2]) : x [1] * x [1] ); |
| 273 |
lpcFrame -> resize (1_integer); |
| 274 |
} |
| 275 |
|
| 276 |
// (3) |
| 277 |
|
| 278 |
double gain = NUMinner (x, x); |
| 279 |
|
| 280 |
if (gain == 0.0) { |
| 281 |
info = 1; |
| 282 |
lpcFrame -> gain = 0.0; |
| 283 |
lpcFrame -> resize (0_integer); |
| 284 |
return; |
| 285 |
} |
| 286 |
// (9) |
| 287 |
|
| 288 |
b1 [1] = x [1]; |
| 289 |
for (integer j = 2; j <= n - 1; j ++) |
| 290 |
b1 [j] = b2 [j - 1] = x [j]; |
| 291 |
b2 [n - 1] = x [n]; |
| 292 |
|
| 293 |
for (integer i = 1; i <= order; i ++) { |
| 294 |
// (7) |
| 295 |
|
| 296 |
/* |
| 297 |
longdouble num = 0.0, denum = 0.0; |
| 298 |
for (integer j = 1; j <= n - i; j ++) { |
| 299 |
num += b1 [j] * b2 [j]; |
| 300 |
denum += b1 [j] * b1 [j] + b2 [j] * b2 [j]; |
| 301 |
} |
| 302 |
*/ |
| 303 |
VEC b1part = b1.part (1, n - i), b2part = b2.part (1, n - i); |
| 304 |
const double num = NUMinner (b1part, b2part); |
| 305 |
const double denum = NUMinner (b1part, b1part) + NUMinner (b2part, b2part); |
| 306 |
|
| 307 |
if (denum <= 0.0) { // part of sound has zero amplitude |
| 308 |
info = 1; |
| 309 |
lpcFrame -> gain = 0.0; |
| 310 |
lpcFrame -> resize (0_integer); |
| 311 |
return; // warning ill-conditioned |
| 312 |
} |
| 313 |
a [i] = 2.0 * num / denum; |
| 314 |
|
| 315 |
// (10) |
| 316 |
|
| 317 |
gain *= 1.0 - a [i] * a [i]; |
| 318 |
|
| 319 |
// (5) |
| 320 |
|
| 321 |
for (integer j = 1; j <= i - 1; j ++) |
| 322 |
a [j] = aa [j] - a [i] * aa [i - j]; |
| 323 |
|
| 324 |
if (i < order) { |
| 325 |
|
| 326 |
// (8) Watch out: i -> i+1 |
| 327 |
|
| 328 |
for (integer j = 1; j <= i; j ++) |
| 329 |
aa [j] = a [j]; |
| 330 |
for (integer j = 1; j <= n - i - 1; j ++) { |
| 331 |
b1 [j] -= aa [i] * b2 [j]; |
| 332 |
b2 [j] = b2 [j + 1] - aa [i] * b1 [j + 1]; |
| 333 |
} |
| 334 |
} |
| 335 |
} |
| 336 |
lpcFrame -> gain = gain; // djmw 20251021 no need to multiply by n! |
| 337 |
for (integer i = 1; i <= lpcFrame -> nCoefficients; i ++) |
| 338 |
a [i] = - a [i]; |
| 339 |
} |
| 340 |
|
| 341 |
void Sound_into_LPC_burg (constSound me, mutableLPC outputLPC, double effectiveAnalysisWidth) { |
| 342 |
Sound_and_LPC_require_equalDomainsAndSamplingPeriods (me, outputLPC); |
| 343 |
const integer thresholdNumberOfFramesPerThread = 40, order = outputLPC -> maxnCoefficients; |
| 344 |
|
| 345 |
MelderThread_PARALLEL (outputLPC -> nx, thresholdNumberOfFramesPerThread) { |
| 346 |
autoSoundFrames soundFrames = SoundFrames_createForIntoSampled (me, outputLPC, effectiveAnalysisWidth, |
| 347 |
kSound_windowShape::GAUSSIAN_2, true); |
| 348 |
integer info; |
| 349 |
autoVEC aa = raw_VEC (order); |
| 350 |
autoVEC b1 = raw_VEC (soundFrames -> soundFrameSize); |
| 351 |
autoVEC b2 = raw_VEC (soundFrames -> soundFrameSize); |
| 352 |
MelderThread_FOR (iframe) { |
| 353 |
const LPC_Frame lpcFrame = & outputLPC -> d_frames [iframe]; |
| 354 |
VEC soundFrame = soundFrames -> getMonoFrame (iframe); |
| 355 |
soundFrameIntoLPCFrame_burg (soundFrame, lpcFrame, b1.get(), b2.get(), aa.get(), info); |
| 356 |
} |
| 357 |
} MelderThread_ENDPARALLEL |
| 358 |
} |
| 359 |
|
| 360 |
autoLPC Sound_to_LPC_burg (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, double preEmphasisFrequency) { |
| 361 |
try { |
| 362 |
autoSound inputSound; |
| 363 |
autoLPC outputLPC; |
| 364 |
Sound_to_LPC_common_e (me, predictionOrder, effectiveAnalysisWidth, dt, preEmphasisFrequency, inputSound, outputLPC); |
| 365 |
Sound_into_LPC_burg (inputSound.get(), outputLPC.get(), effectiveAnalysisWidth); |
| 366 |
return outputLPC; |
| 367 |
} catch (MelderError) { |
| 368 |
Melder_throw (me, U": no LPC (burg) created."); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
/*********************** Marple method *************************************************************/ |
| 373 |
|
| 374 |
void soundFrameIntoLPCFrame_marple (VEC soundFrame, LPC_Frame lpcFrame, VEC c, VEC d, VEC r, |
| 375 |
double tol1, double tol2, integer& info) |
| 376 |
{ |
| 377 |
const integer mmax = lpcFrame -> nCoefficients, n = soundFrame.size; |
| 378 |
VEC x = soundFrame; |
| 379 |
|
| 380 |
info = 0; |
| 381 |
VEC a = lpcFrame -> a.get(); |
| 382 |
|
| 383 |
double e0 = 2.0 * NUMsum2 (x); |
| 384 |
lpcFrame -> gain = 0.0; |
| 385 |
integer m = 1; |
| 386 |
if (e0 == 0.0) { |
| 387 |
info = 1; |
| 388 |
lpcFrame -> resize (0_integer); |
| 389 |
return; |
| 390 |
} |
| 391 |
double q1 = 1.0 / e0; |
| 392 |
double q2 = q1 * x [1], q = q1 * x [1] * x [1], w = q1 * x [n] * x [n]; |
| 393 |
double v = q, u = w; |
| 394 |
double den = 1.0 - q - w; |
| 395 |
double q4 = 1.0 / den, q5 = 1.0 - q, q6 = 1.0 - w; |
| 396 |
double h = q2 * x [n], s = h; |
| 397 |
double gain = e0 * den; |
| 398 |
q1 = 1.0 / gain; |
| 399 |
c [1] = q1 * x [1]; |
| 400 |
d [1] = q1 * x [n]; |
| 401 |
double s1 = 0.0; |
| 402 |
for (integer k = 1; k <= n - 1; k ++) |
| 403 |
s1 += x [k + 1] * x [k]; |
| 404 |
r [1] = 2.0 * s1; |
| 405 |
a [1] = - q1 * r [1]; |
| 406 |
gain *= (1.0 - a [1] * a [1]); |
| 407 |
while (m < mmax) { |
| 408 |
const double eOld = gain; |
| 409 |
double f = x [m + 1], b = x [n - m]; // n-1 ->n-m |
| 410 |
for (integer k = 1; k <= m; k ++) { |
| 411 |
// n-1 -> n-m |
| 412 |
f += x [m + 1 - k] * a [k]; |
| 413 |
b += x [n - m + k] * a [k]; |
| 414 |
} |
| 415 |
q1 = 1.0 / gain; |
| 416 |
q2 = q1 * f; |
| 417 |
const double q3 = q1 * b; |
| 418 |
for (integer k = m; k >= 1; k--) { |
| 419 |
c [k + 1] = c [k] + q2 * a [k]; |
| 420 |
d [k + 1] = d [k] * q3 * a [k]; |
| 421 |
} |
| 422 |
c [1] = q2; |
| 423 |
d [1] = q3; |
| 424 |
const double q7 = s * s; |
| 425 |
double y1 = f * f; |
| 426 |
const double y2 = v * v; |
| 427 |
const double y3 = b * b; |
| 428 |
const double y4 = u * u; |
| 429 |
double y5 = 2.0 * h * s; |
| 430 |
q += y1 * q1 + q4 * (y2 * q6 + q7 * q5 + v * y5); |
| 431 |
w += y3 * q1 + q4 * (y4 * q5 + q7 * q6 + u * y5); |
| 432 |
h = s = u = v = 0.0; |
| 433 |
for (integer k = 0; k <= m; k ++) { |
| 434 |
h += x [n - m + k] * c [k + 1]; |
| 435 |
s += x [n - k] * c [k + 1]; |
| 436 |
u += x [n - k] * d [k + 1]; |
| 437 |
v += x [k + 1] * c [k + 1]; |
| 438 |
} |
| 439 |
q5 = 1.0 - q; |
| 440 |
q6 = 1.0 - w; |
| 441 |
den = q5 * q6 - h * h; |
| 442 |
if (den <= 0.0) { |
| 443 |
info = 2; |
| 444 |
goto end; // 2: ill-conditioning |
| 445 |
} |
| 446 |
q4 = 1.0 / den; |
| 447 |
q1 *= q4; |
| 448 |
const double alf = 1.0 / (1.0 + q1 * (y1 * q6 + y3 * q5 + 2.0 * h * f * b)); |
| 449 |
gain *= alf; |
| 450 |
y5 = h * s; |
| 451 |
double c1 = q4 * (f * q6 + b * h); |
| 452 |
double c2 = q4 * (b * q5 + h * f); |
| 453 |
const double c3 = q4 * (v * q6 + y5); |
| 454 |
const double c4 = q4 * (s * q5 + v * h); |
| 455 |
const double c5 = q4 * (s * q6 + h * u); |
| 456 |
const double c6 = q4 * (u * q5 + y5); |
| 457 |
for (integer k = 1; k <= m; k ++) |
| 458 |
a [k] = alf * (a [k] + c1 * c [k + 1] + c2 * d [k + 1]); |
| 459 |
for (integer k = 1; k <= m / 2 + 1; k ++) { |
| 460 |
s1 = c [k]; |
| 461 |
const double s2 = d [k], s3 = c [m + 2 - k], s4 = d [m + 2 - k]; |
| 462 |
|
| 463 |
c [k] += c3 * s3 + c4 * s4; |
| 464 |
d [k] += c5 * s3 + c6 * s4; |
| 465 |
if (m + 2 - k == k) |
| 466 |
continue; |
| 467 |
c [m + 2 - k] += c3 * s1 + c4 * s2; |
| 468 |
d [m + 2 - k] += c5 * s1 + c6 * s2; |
| 469 |
} |
| 470 |
m ++; |
| 471 |
c1 = x [n + 1 - m]; |
| 472 |
c2 = x [m]; |
| 473 |
double delta = 0.0; |
| 474 |
for (integer k = m - 1; k >= 1; k--) { |
| 475 |
r [k + 1] = r [k] - x [n + 1 - k] * c1 - x [k] * c2; |
| 476 |
delta += r [k + 1] * a [k]; |
| 477 |
} |
| 478 |
s1 = 0.0; |
| 479 |
for (integer k = 1; k <= n - m; k ++) |
| 480 |
s1 += x [k + m] * x [k]; |
| 481 |
r [1] = 2.0 * s1; |
| 482 |
delta += r [1]; |
| 483 |
q2 = - delta / gain; |
| 484 |
a [m] = q2; |
| 485 |
for (integer k = 1; k <= m / 2; k ++) { |
| 486 |
s1 = a [k]; |
| 487 |
a [k] += q2 * a [m - k]; |
| 488 |
if (k == m - k) |
| 489 |
continue; |
| 490 |
a [m - k] += q2 * s1; |
| 491 |
} |
| 492 |
y1 = q2 * q2; |
| 493 |
gain *= 1.0 - y1; |
| 494 |
if (y1 >= 1.0) { |
| 495 |
info = 3; |
| 496 |
break; // |a [m]| > 1 |
| 497 |
} |
| 498 |
if (gain < e0 * tol1) { |
| 499 |
info = 4; |
| 500 |
break; |
| 501 |
} |
| 502 |
if (eOld - gain < eOld * tol2) { |
| 503 |
info = 5; |
| 504 |
break; |
| 505 |
} |
| 506 |
} |
| 507 |
lpcFrame -> resize (m); |
| 508 |
end: |
| 509 |
lpcFrame -> gain = gain * 0.5; // because e0 is twice the energy |
| 510 |
if (!(info == 0 || info == 4 || info == 5)) |
| 511 |
lpcFrame -> resize (m - 1); |
| 512 |
} |
| 513 |
|
| 514 |
void Sound_into_LPC_marple (constSound me, mutableLPC outputLPC, double effectiveAnalysisWidth, |
| 515 |
double tol1, double tol2) |
| 516 |
{ |
| 517 |
Sound_and_LPC_require_equalDomainsAndSamplingPeriods (me, outputLPC); |
| 518 |
const integer thresholdNumberOfFramesPerThread = 40, order = outputLPC -> maxnCoefficients; |
| 519 |
|
| 520 |
MelderThread_PARALLEL (outputLPC -> nx, thresholdNumberOfFramesPerThread) { |
| 521 |
integer info; |
| 522 |
autoSoundFrames soundFrames = SoundFrames_createForIntoSampled (me, outputLPC, effectiveAnalysisWidth, |
| 523 |
kSound_windowShape::GAUSSIAN_2, true); |
| 524 |
autoVEC c = raw_VEC (order + 1); |
| 525 |
autoVEC d = raw_VEC (order + 1); |
| 526 |
autoVEC r = raw_VEC (order + 1); |
| 527 |
MelderThread_FOR (iframe) { |
| 528 |
const LPC_Frame lpcFrame = & outputLPC -> d_frames [iframe]; |
| 529 |
VEC soundFrame = soundFrames -> getMonoFrame (iframe); |
| 530 |
soundFrameIntoLPCFrame_marple (soundFrame, lpcFrame, c.get(), d.get(), r.get(), tol1, tol2, info); |
| 531 |
} |
| 532 |
} MelderThread_ENDPARALLEL |
| 533 |
} |
| 534 |
|
| 535 |
autoLPC Sound_to_LPC_marple (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, |
| 536 |
double preEmphasisFrequency, double tol1, double tol2) |
| 537 |
{ |
| 538 |
try { |
| 539 |
autoSound inputSound; |
| 540 |
autoLPC outputLPC; |
| 541 |
Sound_to_LPC_common_e (me, predictionOrder, effectiveAnalysisWidth, dt, preEmphasisFrequency, inputSound, outputLPC); |
| 542 |
Sound_into_LPC_marple (inputSound.get(), outputLPC.get(), effectiveAnalysisWidth, tol1, tol2); |
| 543 |
return outputLPC; |
| 544 |
} catch (MelderError) { |
| 545 |
Melder_throw (me, U": no LPC (marple) created."); |
| 546 |
} |
| 547 |
} |
| 548 |
|
| 549 |
/*********************** PLP (Hermansky) method *************************************************************/ |
| 550 |
|
| 551 |
/* |
| 552 |
void structSoundFrameIntoLPCFramePLP :: getFilterCharacteristics () { |
| 553 |
struct structCriticalBandFilter { |
| 554 |
double fb, fm, fe; |
| 555 |
integer i1, i2; |
| 556 |
integer iv1, iv2; |
| 557 |
}; |
| 558 |
const double nyquistFrequency = 0.5 / frameAsSound -> dx; |
| 559 |
const double df = 1.0 / (frameAsSound -> dx * numberOfFourierSamples); |
| 560 |
const double fmax_bark = hertzToBark (nyquistFrequency); |
| 561 |
numberOfCriticalBandFilters = Melder_ifloor (fmax_bark) + 2; |
| 562 |
const double df_bark = fmax_bark / (numberOfCriticalBandFilters - 1); |
| 563 |
autovector<struct structCriticalBandFilter> filters = newvectorzero<struct structCriticalBandFilter> (numberOfCriticalBandFilters); |
| 564 |
for (integer ifilter = 1; ifilter <= numberOfCriticalBandFilters; ifilter ++) { |
| 565 |
struct structCriticalBandFilter *fstruct = & filters [ifilter]; |
| 566 |
const double fm_bark = ifilter * df_bark; |
| 567 |
const double fb_bark = Melder_clippedLeft (0.0, fm_bark - 2.5); |
| 568 |
const double fe_bark = Melder_clippedRight (fm_bark + 1.3, fmax_bark); |
| 569 |
fstruct -> fb = barkToHertz (fb_bark); |
| 570 |
fstruct -> fe = barkToHertz (fe_bark); |
| 571 |
fstruct -> fm = barkToHertz (fm_bark); |
| 572 |
|
| 573 |
} |
| 574 |
|
| 575 |
}*/ |
| 576 |
|
| 577 |
/*********************** Robust method (LPC & Sound) **********************************************/ |
| 578 |
|
| 579 |
Thing_implement (RobustLPCWorkspace, Thing, 0); |
| 580 |
|
| 581 |
void structRobustLPCWorkspace :: init (constLPC inputLPC, constSound inputSound, mutableLPC outputLPC, |
| 582 |
double effectiveAnalysisWidth, kSound_windowShape windowShape, |
| 583 |
double k_stdev, integer itermax, double tol, bool wantLocation) |
| 584 |
{ |
| 585 |
const double physicalAnalysisWidth = getPhysicalAnalysisWidth (effectiveAnalysisWidth, windowShape); |
| 586 |
const integer soundFrameSize = getSoundFrameSize (physicalAnalysisWidth, inputSound -> dx); |
| 587 |
our order = inputLPC -> maxnCoefficients; |
| 588 |
our inputLPC = inputLPC; |
| 589 |
our outputLPC = outputLPC; |
| 590 |
our k_stdev = k_stdev; |
| 591 |
our itermax = itermax; |
| 592 |
our tol1 = tol; |
| 593 |
our wantLocation = wantLocation; |
| 594 |
our wantScale = true; |
| 595 |
our location = 0.0; |
| 596 |
our huber_iterations = 5; |
| 597 |
our error = raw_VEC (soundFrameSize); |
| 598 |
our sampleWeights = raw_VEC (soundFrameSize); |
| 599 |
coefficients = raw_VEC (order); |
| 600 |
covariancesw = raw_VEC (order); |
| 601 |
covarmatrixw = raw_MAT (order, order); |
| 602 |
svd = SVD_create (order, order); |
| 603 |
SVD_setTolerance (svd.get(), tolSVD); |
| 604 |
computedSVDworksize = SVD_getWorkspaceSize (svd.get()); |
| 605 |
svdwork1 = raw_VEC (computedSVDworksize); |
| 606 |
svdwork2 = raw_VEC (order); |
| 607 |
filterMemory = raw_VEC (order); |
| 608 |
huberwork = raw_VEC (soundFrameSize); |
| 609 |
} |
| 610 |
|
| 611 |
void structRobustLPCWorkspace :: resize () { |
| 612 |
if (currentPredictionOrder == svd -> numberOfColumns) |
| 613 |
return; |
| 614 |
Melder_assert (currentPredictionOrder <= order); |
| 615 |
coefficients.resize (currentPredictionOrder); |
| 616 |
covariancesw.resize (currentPredictionOrder); |
| 617 |
covarmatrixw.resize (currentPredictionOrder, currentPredictionOrder); |
| 618 |
SVD_resizeWithinOldBounds (svd.get(), order, order, currentPredictionOrder, currentPredictionOrder); |
| 619 |
} |
| 620 |
|
| 621 |
void structRobustLPCWorkspace :: setSampleWeights () { |
| 622 |
for (integer isamp = 1 ; isamp <= error.size; isamp ++) { |
| 623 |
const double absDiff = fabs (error [isamp] - location); |
| 624 |
sampleWeights [isamp] = std::max (1.0, k_stdev * scale / absDiff); |
| 625 |
} |
| 626 |
} |
| 627 |
|
| 628 |
void structRobustLPCWorkspace :: setCovariances () { |
| 629 |
MATVU covar = MATVU (covarmatrixw.part (1, currentPredictionOrder, 1, currentPredictionOrder)); |
| 630 |
for (integer i = 1; i <= currentPredictionOrder; i ++) { |
| 631 |
for (integer j = i; j <= currentPredictionOrder; j ++) { |
| 632 |
/* |
| 633 |
The following inner loop will take the most CPU time of all the robust calculations |
| 634 |
*/ |
| 635 |
longdouble cv1 = 0.0; |
| 636 |
for (integer k = currentPredictionOrder + 1; k <= soundFrame.size; k ++) |
| 637 |
cv1 += soundFrame [k - j] * soundFrame [k - i] * sampleWeights [k]; |
| 638 |
covar [i] [j] = covar [j] [i] = cv1; |
| 639 |
} |
| 640 |
longdouble cv2 = 0.0; |
| 641 |
for (integer k = currentPredictionOrder + 1; k <= soundFrame.size; k ++) |
| 642 |
cv2 += soundFrame [k - i] * soundFrame [k] * sampleWeights [k]; |
| 643 |
covariancesw [i] = - cv2; |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
void structRobustLPCWorkspace :: solvelpc () { |
| 648 |
svd -> u.all() <<= covarmatrixw.all(); |
| 649 |
svdwork2.resize (currentPredictionOrder); |
| 650 |
SVD_compute (svd.get(), svdwork1.get()); |
| 651 |
SVD_solve_preallocated (svd.get(), covariancesw.get(), coefficients.get(), svdwork2.get()); |
| 652 |
coefficients.resize (currentPredictionOrder); // maintain invariant |
| 653 |
} |
| 654 |
|
| 655 |
void structRobustLPCWorkspace :: inputFrameIntoOutputFrame (LPC_Frame inputLPCFrame, LPC_Frame outputLPCFrame, VEC soundFrame, integer& info) { |
| 656 |
our soundFrame = soundFrame; |
| 657 |
currentPredictionOrder = inputLPCFrame -> nCoefficients; |
| 658 |
outputLPCFrame -> resize (currentPredictionOrder); |
| 659 |
outputLPCFrame -> gain = inputLPCFrame -> gain; |
| 660 |
info = 0; |
| 661 |
if (currentPredictionOrder == 0) // is empty frame ? |
| 662 |
return; |
| 663 |
for (integer i = 1; i <= currentPredictionOrder; i ++) |
| 664 |
outputLPCFrame -> a [i] = inputLPCFrame -> a [i]; |
| 665 |
|
| 666 |
VEC inout_a = outputLPCFrame -> a.get(); |
| 667 |
iter = 0; |
| 668 |
scale = 1e308; |
| 669 |
bool farFromScale = true; |
| 670 |
resize (); |
| 671 |
filterMemory.resize (currentPredictionOrder); |
| 672 |
do { |
| 673 |
const double previousScale = scale; |
| 674 |
error.all() <<= soundFrame; |
| 675 |
VECfilterInverse_inplace (error.get(), inout_a, filterMemory.get()); |
| 676 |
NUMstatistics_huber (error.get(), & location, wantLocation, & scale, wantScale, k_stdev, |
| 677 |
tol1, huber_iterations, huberwork.get()); |
| 678 |
setSampleWeights (); |
| 679 |
|
| 680 |
setCovariances (); |
| 681 |
/* |
| 682 |
Solve C a = [-] c |
| 683 |
*/ |
| 684 |
try { |
| 685 |
solvelpc (); |
| 686 |
inout_a <<= coefficients.all(); |
| 687 |
farFromScale = ( fabs (scale - previousScale) > std::max (tol1 * fabs (scale), NUMeps) ); |
| 688 |
} catch (MelderError) { |
| 689 |
Melder_clearError (); // No change could be made |
| 690 |
info = 2; // solvelpc in error |
| 691 |
inputLPCFrame -> copy (outputLPCFrame); |
| 692 |
return; |
| 693 |
} |
| 694 |
} while (++ iter < itermax && farFromScale); |
| 695 |
info = 3; // maximum number of iterations |
| 696 |
} |
| 697 |
|
| 698 |
autoRobustLPCWorkspace RobustLPCWorkspace_create (constLPC inputLPC, constSound inputSound, |
| 699 |
mutableLPC outputLPC, double effectiveAnalysisWidth, kSound_windowShape windowShape, |
| 700 |
double k_stdev, integer itermax, double tol, bool wantlocation) |
| 701 |
{ |
| 702 |
try { |
| 703 |
autoRobustLPCWorkspace me = Thing_new (RobustLPCWorkspace); |
| 704 |
my init (inputLPC, inputSound, outputLPC, effectiveAnalysisWidth, windowShape, k_stdev, itermax, tol, wantlocation); |
| 705 |
my wantScale = true; // explicit |
| 706 |
return me; |
| 707 |
} catch (MelderError) { |
| 708 |
Melder_throw (U"Cannot create RobustLPCWorkspace."); |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
void soundFrameAndLPCFrameIntoLPCFrame (VEC soundFrame, LPC_Frame inputLPCFrame, LPC_Frame outputLPCFrame, |
| 713 |
RobustLPCWorkspace ws, integer& info) |
| 714 |
{ |
| 715 |
const integer currentPredictionOrder = inputLPCFrame -> nCoefficients; |
| 716 |
ws -> currentPredictionOrder = currentPredictionOrder; |
| 717 |
ws -> inputFrameIntoOutputFrame (inputLPCFrame, outputLPCFrame, soundFrame, info); |
| 718 |
} |
| 719 |
|
| 720 |
void LPC_and_Sound_into_LPC_robust (constLPC inputLPC, constSound inputSound, mutableLPC outputLPC, |
| 721 |
double effectiveAnalysisWidth, double k_stdev, integer itermax, double tol, bool wantlocation) |
| 722 |
{ |
| 723 |
Sound_and_LPC_require_equalDomainsAndSamplingPeriods (inputSound, outputLPC); |
| 724 |
Sampleds_requireEqualDomainsAndSampling (inputLPC, outputLPC); |
| 725 |
const integer thresholdNumberOfFramesPerThread = 40, order = outputLPC -> maxnCoefficients; |
| 726 |
|
| 727 |
MelderThread_PARALLEL (outputLPC -> nx, thresholdNumberOfFramesPerThread) { |
| 728 |
autoSoundFrames soundFrames = SoundFrames_createForIntoSampled (inputSound, outputLPC, effectiveAnalysisWidth, |
| 729 |
kSound_windowShape::GAUSSIAN_2, true); |
| 730 |
integer info; |
| 731 |
autoRobustLPCWorkspace ws = RobustLPCWorkspace_create (inputLPC, inputSound, outputLPC, |
| 732 |
effectiveAnalysisWidth, kSound_windowShape::GAUSSIAN_2, k_stdev, itermax, tol, wantlocation); |
| 733 |
MelderThread_FOR (iframe) { |
| 734 |
const LPC_Frame outputLPCFrame = & outputLPC -> d_frames [iframe]; |
| 735 |
const LPC_Frame inputLPCFrame = & inputLPC -> d_frames [iframe]; |
| 736 |
VEC soundFrame = soundFrames -> getMonoFrame (iframe); |
| 737 |
soundFrameAndLPCFrameIntoLPCFrame (soundFrame, inputLPCFrame, outputLPCFrame, ws.get(), info); |
| 738 |
} |
| 739 |
} MelderThread_ENDPARALLEL |
| 740 |
} |
| 741 |
|
| 742 |
autoLPC LPC_and_Sound_to_LPC_robust (constLPC inputLPC, constSound inputSound, double effectiveAnalysisWidth, |
| 743 |
double preEmphasisFrequency, double k_stdev, integer itermax, double tol, bool wantlocation) |
| 744 |
{ |
| 745 |
try { |
| 746 |
Sound_and_LPC_require_equalDomainsAndSamplingPeriods (inputSound, inputLPC); |
| 747 |
autoSound sound = Data_copy (inputSound); |
| 748 |
if (preEmphasisFrequency >= 0.0) |
| 749 |
Sound_preEmphasize_inplace (sound.get(), preEmphasisFrequency); |
| 750 |
autoLPC outputLPC = Data_copy (inputLPC); |
| 751 |
LPC_and_Sound_into_LPC_robust (inputLPC, sound.get(), outputLPC.get(), effectiveAnalysisWidth, |
| 752 |
k_stdev, itermax, tol, wantlocation); |
| 753 |
return outputLPC; |
| 754 |
} catch (MelderError) { |
| 755 |
Melder_throw (inputLPC, U" and ", inputSound, U": no LPC (robust) created."); |
| 756 |
} |
| 757 |
} |
| 758 |
|
| 759 |
/*********************** Robust method (Sound) ******************************************************/ |
| 760 |
|
| 761 |
|
| 762 |
void Sound_into_LPC_robust (constSound inputSound, mutableLPC outputLPC, double effectiveAnalysisWidth, double k_stdev, integer itermax, double tol, bool wantlocation) |
| 763 |
{ |
| 764 |
Sound_and_LPC_require_equalDomainsAndSamplingPeriods (inputSound, outputLPC); |
| 765 |
autoLPC intermediateLPC = Data_copy (outputLPC); |
| 766 |
Sound_into_LPC_auto (inputSound, intermediateLPC.get(), effectiveAnalysisWidth); |
| 767 |
LPC_and_Sound_into_LPC_robust (intermediateLPC.get(), inputSound, outputLPC, effectiveAnalysisWidth, |
| 768 |
k_stdev, itermax, tol, wantlocation); |
| 769 |
} |
| 770 |
|
| 771 |
autoLPC Sound_to_LPC_robust (constSound me, int predictionOrder, double effectiveAnalysisWidth, double dt, |
| 772 |
double preEmphasisFrequency, double k_stdev, integer itermax, double tol, bool wantlocation) |
| 773 |
{ |
| 774 |
try { |
| 775 |
autoSound emphasizedSound; |
| 776 |
autoLPC outputLPC; |
| 777 |
Sound_to_LPC_common_e (me, predictionOrder, effectiveAnalysisWidth, dt, preEmphasisFrequency, emphasizedSound, outputLPC); |
| 778 |
Sound_into_LPC_robust (emphasizedSound.get(), outputLPC.get(), effectiveAnalysisWidth, k_stdev, |
| 779 |
itermax, tol, wantlocation); |
| 780 |
return outputLPC; |
| 781 |
} catch (MelderError) { |
| 782 |
Melder_throw (me, U": no LPC (robust) created."); |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
/*********************** (inverse) filtering ******************************/ |
| 787 |
|
| 788 |
static void LPC_Frame_Sound_filter (constLPC_Frame me, mutableSound thee, integer channel) { |
| 789 |
const VEC y = thy z.row (channel); |
| 790 |
for (integer i = 1; i <= thy nx; i ++) { |
| 791 |
const integer m = ( i > my nCoefficients ? my nCoefficients : i - 1 ); // ppgb: what is m? |
| 792 |
for (integer j = 1; j <= m; j ++) |
| 793 |
y [i] -= my a [j] * y [i - j]; |
| 794 |
} |
| 795 |
} |
| 796 |
|
| 797 |
autoSound LPC_Sound_filterInverse (constLPC me, constSound thee) { |
| 798 |
try { |
| 799 |
Melder_require (my samplingPeriod == thy dx, |
| 800 |
U"The sampling frequencies should be equal."); |
| 801 |
Melder_require (my xmin == thy xmin && thy xmax == my xmax, |
| 802 |
U"The domains of LPC and Sound should be equal."); |
| 803 |
|
| 804 |
autoSound him = Data_copy (thee); |
| 805 |
VEC source = his z.row (1); |
| 806 |
VEC sound = thy z.row (1); |
| 807 |
for (integer isamp = 1; isamp <= his nx; isamp ++) { |
| 808 |
const double sampleTime = Sampled_indexToX (him.get(), isamp); |
| 809 |
const integer frameNumber = Sampled_xToNearestIndex (me, sampleTime); |
| 810 |
if (frameNumber < 1 || frameNumber > my nx) { |
| 811 |
source [isamp] = 0.0; |
| 812 |
continue; |
| 813 |
} |
| 814 |
const LPC_Frame frame = & my d_frames [frameNumber]; |
| 815 |
const integer maximumFilterDepth = frame -> nCoefficients; |
| 816 |
const integer maximumSoundDepth = isamp - 1; |
| 817 |
const integer usableDepth = std::min (maximumFilterDepth, maximumSoundDepth); |
| 818 |
for (integer icoef = 1; icoef <= usableDepth; icoef ++) |
| 819 |
source [isamp] += frame -> a [icoef] * sound [isamp - icoef]; |
| 820 |
} |
| 821 |
return him; |
| 822 |
} catch (MelderError) { |
| 823 |
Melder_throw (thee, U": not inverse filtered."); |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
/* |
| 828 |
Gain used as a constant amplitude multiplier within a frame of duration my dx. |
| 829 |
future alternative: convolve gain with a smoother. |
| 830 |
*/ |
| 831 |
autoSound LPC_Sound_filter (constLPC me, constSound thee, bool useGain) { |
| 832 |
try { |
| 833 |
const double xmin = std::max (my xmin, thy xmin); |
| 834 |
const double xmax = std::min (my xmax, thy xmax); |
| 835 |
Melder_require (xmin < xmax, |
| 836 |
U"Domains of Sound [", thy xmin, U",", thy xmax, U"] and LPC [", |
| 837 |
my xmin, U",", my xmax, U"] should overlap." |
| 838 |
); |
| 839 |
/* |
| 840 |
Resample the sound if the sampling frequencies do not match. |
| 841 |
*/ |
| 842 |
autoSound source; |
| 843 |
if (my samplingPeriod != thy dx) { |
| 844 |
source = Sound_resample (thee, 1.0 / my samplingPeriod, 50); |
| 845 |
thee = source.get(); // reference copy; remove at end |
| 846 |
} |
| 847 |
|
| 848 |
autoSound him = Data_copy (thee); |
| 849 |
|
| 850 |
const integer ifirst = std::max (1_integer, Sampled_xToHighIndex (thee, xmin)); |
| 851 |
const integer ilast = std::min (Sampled_xToLowIndex (thee, xmax), thy nx); |
| 852 |
for (integer isamp = ifirst; isamp <= ilast; isamp ++) { |
| 853 |
const double sampleTime = Sampled_indexToX (him.get(), isamp); |
| 854 |
const integer frameNumber = Sampled_xToNearestIndex (me, sampleTime); |
| 855 |
if (frameNumber < 1 || frameNumber > my nx) { |
| 856 |
his z [1] [isamp] = 0.0; |
| 857 |
continue; |
| 858 |
} |
| 859 |
const LPC_Frame frame = & my d_frames [frameNumber]; |
| 860 |
const integer maximumFilterDepth = frame -> nCoefficients; |
| 861 |
const integer maximumSourceDepth = isamp - 1; |
| 862 |
const integer usableDepth = std::min (maximumFilterDepth, maximumSourceDepth); |
| 863 |
for (integer icoef = 1; icoef <= usableDepth; icoef ++) |
| 864 |
his z [1] [isamp] -= frame -> a [icoef] * his z [1] [isamp - icoef]; |
| 865 |
} |
| 866 |
/* |
| 867 |
Make samples before first frame and after last frame zero. |
| 868 |
*/ |
| 869 |
for (integer isamp = 1; isamp < ifirst; isamp ++) |
| 870 |
his z [1] [isamp] = 0.0; |
| 871 |
for (integer isamp = ilast + 1; isamp <= his nx; isamp ++) |
| 872 |
his z [1] [isamp] = 0.0; |
| 873 |
|
| 874 |
if (useGain) { |
| 875 |
for (integer isamp = ifirst; isamp <= ilast; isamp ++) { |
| 876 |
const double sampleTime = Sampled_indexToX (him.get(), isamp); |
| 877 |
const double realFrameNumber = Sampled_xToIndex (me, sampleTime); |
| 878 |
const integer leftFrameNumber = Melder_ifloor (realFrameNumber); |
| 879 |
const integer rightFrameNumber = leftFrameNumber + 1; |
| 880 |
const double phase = realFrameNumber - leftFrameNumber; |
| 881 |
if (rightFrameNumber < 1 || leftFrameNumber > my nx) |
| 882 |
his z [1] [isamp] = 0.0; |
| 883 |
else if (rightFrameNumber == 1) |
| 884 |
his z [1] [isamp] *= sqrt (my d_frames [1]. gain) * phase; |
| 885 |
else if (leftFrameNumber == my nx) |
| 886 |
his z [1] [isamp] *= sqrt (my d_frames [my nx]. gain) * (1.0 - phase); |
| 887 |
else |
| 888 |
his z [1] [isamp] *= |
| 889 |
phase * sqrt (my d_frames [rightFrameNumber]. gain) + |
| 890 |
(1.0 - phase) * sqrt (my d_frames [leftFrameNumber]. gain); |
| 891 |
} |
| 892 |
} |
| 893 |
return him; |
| 894 |
} catch (MelderError) { |
| 895 |
Melder_throw (thee, U": not filtered."); |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
void LPC_Sound_filterWithFilterAtTime_inplace (constLPC me, mutableSound thee, integer channel, double time) { |
| 900 |
integer frameIndex = Sampled_xToNearestIndex (me, time); |
| 901 |
Melder_clip (1_integer, & frameIndex, my nx); // constant extrapolation |
| 902 |
if (channel > thy ny) |
| 903 |
channel = 1; |
| 904 |
Melder_require (frameIndex > 0 && frameIndex <= my nx, |
| 905 |
U"Frame should be in the range [1, ", my nx, U"]."); |
| 906 |
|
| 907 |
if (channel > 0) |
| 908 |
LPC_Frame_Sound_filter (& my d_frames [frameIndex], thee, channel); |
| 909 |
else |
| 910 |
for (integer ichan = 1; ichan <= thy ny; ichan ++) |
| 911 |
LPC_Frame_Sound_filter (& my d_frames [frameIndex], thee, ichan); |
| 912 |
} |
| 913 |
|
| 914 |
autoSound LPC_Sound_filterWithFilterAtTime (constLPC me, constSound thee, integer channel, double time) { |
| 915 |
try { |
| 916 |
autoSound him = Data_copy (thee); |
| 917 |
LPC_Sound_filterWithFilterAtTime_inplace (me, him.get(), channel, time); |
| 918 |
return him; |
| 919 |
} catch (MelderError) { |
| 920 |
Melder_throw (thee, U": not filtered."); |
| 921 |
} |
| 922 |
} |
| 923 |
|
| 924 |
void LPC_Sound_filterInverseWithFilterAtTime_inplace (constLPC me, mutableSound thee, integer channel, double time) { |
| 925 |
try { |
| 926 |
integer frameIndex = Sampled_xToNearestIndex (me, time); |
| 927 |
Melder_clip (1_integer, & frameIndex, my nx); // constant extrapolation |
| 928 |
if (channel > thy ny) |
| 929 |
channel = 1; |
| 930 |
LPC_Frame lpc = & my d_frames [frameIndex]; |
| 931 |
autoVEC workspace = raw_VEC (lpc -> nCoefficients); |
| 932 |
if (channel > 0) |
| 933 |
VECfilterInverse_inplace (thy z.row (channel), lpc -> a.get(), workspace.get()); |
| 934 |
else |
| 935 |
for (integer ichan = 1; ichan <= thy ny; ichan ++) |
| 936 |
VECfilterInverse_inplace (thy z.row (ichan), lpc -> a.get(), workspace.get()); |
| 937 |
} catch (MelderError) { |
| 938 |
Melder_throw (thee, U": not inverse filtered."); |
| 939 |
} |
| 940 |
} |
| 941 |
|
| 942 |
autoSound LPC_Sound_filterInverseWithFilterAtTime (constLPC me, constSound thee, integer channel, double time) { |
| 943 |
try { |
| 944 |
autoSound him = Data_copy (thee); |
| 945 |
LPC_Sound_filterInverseWithFilterAtTime_inplace (me, him.get(), channel, time); |
| 946 |
return him; |
| 947 |
} catch (MelderError) { |
| 948 |
Melder_throw (thee, U": not inverse filtered."); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
/* End of file Sound_and_LPC.cpp */ |