| 1 |
/* Ltas.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2012,2015-2018,2022,2023,2026 Paul Boersma |
| 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. |
| 13 |
* See the GNU 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 |
/* |
| 20 |
* a selection of changes: |
| 21 |
* pb 2005/11/26 pitch-corrected Ltas |
| 22 |
*/ |
| 23 |
|
| 24 |
#include "Ltas.h" |
| 25 |
#include "Sound_and_Spectrum.h" |
| 26 |
#include "Sound_to_PointProcess.h" |
| 27 |
|
| 28 |
Thing_implement (Ltas, Vector, 2); |
| 29 |
|
| 30 |
void structLtas :: v1_info () { |
| 31 |
structDaata :: v1_info (); |
| 32 |
MelderInfo_writeLine (U"Frequency domain:"); |
| 33 |
MelderInfo_writeLine (U" Lowest frequency: ", xmin, U" Hz"); |
| 34 |
MelderInfo_writeLine (U" Highest frequency: ", xmax, U" Hz"); |
| 35 |
MelderInfo_writeLine (U" Total frequency domain: ", xmax - xmin, U" Hz"); |
| 36 |
MelderInfo_writeLine (U"Frequency sampling:"); |
| 37 |
MelderInfo_writeLine (U" Number of frequency bands: ", nx); |
| 38 |
MelderInfo_writeLine (U" Width of each band: ", dx, U" Hz"); |
| 39 |
MelderInfo_writeLine (U" First band centred at: ", x1, U" Hz"); |
| 40 |
const double meanPowerDensity = Sampled_getMean (this, xmin, xmax, 0, 1, false); |
| 41 |
MelderInfo_writeLine (U"Total SPL: ", Melder_single (10.0 * log10 (meanPowerDensity * (xmax - xmin))), U" dB"); |
| 42 |
} |
| 43 |
|
| 44 |
double structLtas :: v_convertStandardToSpecialUnit (double value, integer /* level */, int unit) const { |
| 45 |
if (unit == 1) { |
| 46 |
return pow (10.0, 0.1 * value); // energy |
| 47 |
} else if (unit == 2) { |
| 48 |
return pow (2.0, 0.1 * value); // sones |
| 49 |
} |
| 50 |
return value; |
| 51 |
} |
| 52 |
|
| 53 |
double structLtas :: v_convertSpecialToStandardUnit (double value, integer /* level */, int unit) const { |
| 54 |
return |
| 55 |
unit == 1 ? |
| 56 |
10.0 * log10 (value) : // value = energy |
| 57 |
unit == 2 ? |
| 58 |
10.0 * NUMlog2 (value) : // value = sones |
| 59 |
value; // value = dB |
| 60 |
} |
| 61 |
|
| 62 |
autoLtas Ltas_create (integer nx, double dx) { |
| 63 |
try { |
| 64 |
autoLtas me = Thing_new (Ltas); |
| 65 |
Matrix_init (me.get(), 0.0, nx * dx, nx, dx, 0.5 * dx, 1.0, 1.0, 1, 1.0, 1.0); |
| 66 |
return me; |
| 67 |
} catch (MelderError) { |
| 68 |
Melder_throw (U"Ltas not created."); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void Ltas_draw (Ltas me, Graphics g, double fmin, double fmax, double minimum, double maximum, bool garnish, conststring32 method) { |
| 73 |
Vector_draw (me, g, & fmin, & fmax, & minimum, & maximum, 1.0, method); |
| 74 |
if (garnish) { |
| 75 |
Graphics_drawInnerBox (g); |
| 76 |
Graphics_textBottom (g, true, U"Frequency (Hz)"); |
| 77 |
Graphics_marksBottom (g, 2, true, true, false); |
| 78 |
Graphics_textLeft (g, true, U"Sound pressure level (dB/Hz)"); |
| 79 |
Graphics_marksLeft (g, 2, true, true, false); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
double Ltas_getSlope (Ltas me, double f1min, double f1max, double f2min, double f2max, int averagingUnits) { |
| 84 |
double low = Sampled_getMean (me, f1min, f1max, 0, averagingUnits, false); |
| 85 |
double high = Sampled_getMean (me, f2min, f2max, 0, averagingUnits, false); |
| 86 |
if (isundef (low) || isundef (high)) return undefined; |
| 87 |
return averagingUnits == 3 ? high - low : Function_convertSpecialToStandardUnit (me, high / low, 0, averagingUnits); |
| 88 |
} |
| 89 |
|
| 90 |
double Ltas_getLocalPeakHeight (Ltas me, double environmentMin, double environmentMax, double peakMin, double peakMax, int averagingUnits) { |
| 91 |
double environmentLow = Sampled_getMean (me, environmentMin, peakMin, 0, averagingUnits, false); |
| 92 |
double environmentHigh = Sampled_getMean (me, peakMax, environmentMax, 0, averagingUnits, false); |
| 93 |
double peak = Sampled_getMean (me, peakMin, peakMax, 0, averagingUnits, false); |
| 94 |
if (isundef (environmentLow) || isundef (environmentHigh) || isundef (peak)) return undefined; |
| 95 |
return averagingUnits == 3 ? peak - 0.5 * (environmentLow + environmentHigh) : |
| 96 |
Function_convertSpecialToStandardUnit (me, peak / (0.5 * (environmentLow + environmentHigh)), 0, averagingUnits); |
| 97 |
} |
| 98 |
|
| 99 |
autoMatrix Ltas_to_Matrix (Ltas me) { |
| 100 |
try { |
| 101 |
autoMatrix thee = Thing_new (Matrix); |
| 102 |
my structMatrix :: v1_copy (thee.get()); |
| 103 |
return thee; |
| 104 |
} catch (MelderError) { |
| 105 |
Melder_throw (me, U": not converted to Matrix."); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
autoLtas Matrix_to_Ltas (Matrix me) { |
| 110 |
try { |
| 111 |
autoLtas thee = Thing_new (Ltas); |
| 112 |
my structMatrix :: v1_copy (thee.get()); // because copying to a descendant of Matrix with additional members should not crash |
| 113 |
return thee; |
| 114 |
} catch (MelderError) { |
| 115 |
Melder_throw (me, U": not converted to Ltas."); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
autoLtas Ltases_merge (LtasBag ltases) { |
| 120 |
try { |
| 121 |
if (ltases->size < 1) |
| 122 |
Melder_throw (U"Cannot merge zero Ltas objects."); |
| 123 |
Ltas me = ltases->at [1]; |
| 124 |
autoLtas thee = Data_copy (me); |
| 125 |
/* |
| 126 |
* Convert to energy. |
| 127 |
*/ |
| 128 |
for (integer iband = 1; iband <= thy nx; iband ++) { |
| 129 |
thy z [1] [iband] = pow (10.0, thy z [1] [iband] / 10.0); |
| 130 |
} |
| 131 |
for (integer ispec = 2; ispec <= ltases->size; ispec ++) { |
| 132 |
Ltas him = ltases->at [ispec]; |
| 133 |
if (his xmin != thy xmin || his xmax != thy xmax) |
| 134 |
Melder_throw (U"Frequency domains do not match."); |
| 135 |
if (his dx != thy dx) |
| 136 |
Melder_throw (U"Bandwidths do not match."); |
| 137 |
if (his nx != thy nx || his x1 != thy x1) |
| 138 |
Melder_throw (U"Frequency bands do not match."); |
| 139 |
/* |
| 140 |
* Add band energies. |
| 141 |
*/ |
| 142 |
for (integer iband = 1; iband <= thy nx; iband ++) { |
| 143 |
thy z [1] [iband] += pow (10.0, his z [1] [iband] / 10.0); |
| 144 |
} |
| 145 |
} |
| 146 |
/* |
| 147 |
* Convert back to dB. |
| 148 |
*/ |
| 149 |
for (integer iband = 1; iband <= thy nx; iband ++) { |
| 150 |
thy z [1] [iband] = 10.0 * log10 (thy z [1] [iband]); |
| 151 |
} |
| 152 |
return thee; |
| 153 |
} catch (MelderError) { |
| 154 |
Melder_throw (U"Ltas objects not merged."); |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
Thing_implement (LtasBag, Collection, 0); |
| 159 |
|
| 160 |
autoLtas Ltases_average (LtasBag ltases) { |
| 161 |
try { |
| 162 |
double factor = -10.0 * log10 (ltases->size); |
| 163 |
autoLtas thee = Ltases_merge (ltases); |
| 164 |
for (integer iband = 1; iband <= thy nx; iband ++) { |
| 165 |
thy z [1] [iband] += factor; |
| 166 |
} |
| 167 |
return thee; |
| 168 |
} catch (MelderError) { |
| 169 |
Melder_throw (U"Ltas objects not averaged."); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
autoLtas Ltas_computeTrendLine (Ltas me, double fmin, double fmax) { |
| 174 |
try { |
| 175 |
/* |
| 176 |
* Find the first and last bin. |
| 177 |
*/ |
| 178 |
integer imin, imax, n; |
| 179 |
if ((n = Sampled_getWindowSamples (me, fmin, fmax, & imin, & imax)) < 2) |
| 180 |
Melder_throw (U"Number of bins too low (", n, U"). Should be at least 2."); |
| 181 |
autoLtas thee = Data_copy (me); |
| 182 |
/* |
| 183 |
* Compute average amplitude and frequency. |
| 184 |
*/ |
| 185 |
longdouble sum = 0.0, numerator = 0.0, denominator = 0.0; |
| 186 |
for (integer i = imin; i <= imax; i ++) { |
| 187 |
sum += thy z [1] [i]; |
| 188 |
} |
| 189 |
double amean = double (sum / n); |
| 190 |
double fmean = thy x1 + (0.5 * (imin + imax) - 1) * thy dx; |
| 191 |
/* |
| 192 |
* Compute slope. |
| 193 |
*/ |
| 194 |
for (integer i = imin; i <= imax; i ++) { |
| 195 |
double da = thy z [1] [i] - amean, df = thy x1 + (i - 1) * thy dx - fmean; |
| 196 |
numerator += da * df; |
| 197 |
denominator += df * df; |
| 198 |
} |
| 199 |
double slope = double (numerator / denominator); |
| 200 |
/* |
| 201 |
* Modify bins. |
| 202 |
*/ |
| 203 |
for (integer i = 1; i <= thy nx; i ++) { |
| 204 |
double df = thy x1 + (i - 1) * thy dx - fmean; |
| 205 |
thy z [1] [i] = amean + slope * df; |
| 206 |
} |
| 207 |
return thee; |
| 208 |
} catch (MelderError) { |
| 209 |
Melder_throw (me, U": trend line not computed."); |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
autoLtas Ltas_subtractTrendLine (Ltas me, double fmin, double fmax) { |
| 214 |
try { |
| 215 |
/* |
| 216 |
* Find the first and last bin. |
| 217 |
*/ |
| 218 |
integer imin, imax, n; |
| 219 |
if ((n = Sampled_getWindowSamples (me, fmin, fmax, & imin, & imax)) < 2) |
| 220 |
Melder_throw (U"Number of bins too low (", n, U"). Should be at least 2."); |
| 221 |
autoLtas thee = Data_copy (me); |
| 222 |
/* |
| 223 |
* Compute average amplitude and frequency. |
| 224 |
*/ |
| 225 |
longdouble sum = 0.0; |
| 226 |
for (integer i = imin; i <= imax; i ++) { |
| 227 |
sum += thy z [1] [i]; |
| 228 |
} |
| 229 |
double amean = (double) sum / n; |
| 230 |
double fmean = thy x1 + (0.5 * (imin + imax) - 1) * thy dx; |
| 231 |
/* |
| 232 |
* Compute slope. |
| 233 |
*/ |
| 234 |
longdouble numerator = 0.0, denominator = 0.0; |
| 235 |
for (integer i = imin; i <= imax; i ++) { |
| 236 |
double da = thy z [1] [i] - amean, df = thy x1 + (i - 1) * thy dx - fmean; |
| 237 |
numerator += da * df; |
| 238 |
denominator += df * df; |
| 239 |
} |
| 240 |
double slope = (double) (numerator / denominator); |
| 241 |
/* |
| 242 |
* Modify bins. |
| 243 |
*/ |
| 244 |
for (integer i = 1; i < imin; i ++) { |
| 245 |
thy z [1] [i] = 0.0; |
| 246 |
} |
| 247 |
for (integer i = imin; i <= imax; i ++) { |
| 248 |
double df = thy x1 + (i - 1) * thy dx - fmean; |
| 249 |
thy z [1] [i] -= amean + slope * df; |
| 250 |
} |
| 251 |
for (integer i = imax + 1; i <= thy nx; i ++) { |
| 252 |
thy z [1] [i] = 0.0; |
| 253 |
} |
| 254 |
return thee; |
| 255 |
} catch (MelderError) { |
| 256 |
Melder_throw (me, U": trend line not subtracted."); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
autoLtas Spectrum_to_Ltas (Spectrum me, double bandWidth) { |
| 261 |
try { |
| 262 |
integer numberOfBands = Melder_iceiling ((my xmax - my xmin) / bandWidth); |
| 263 |
if (bandWidth <= my dx) |
| 264 |
Melder_throw (U"Bandwidth must be greater than ", my dx, U"."); |
| 265 |
autoLtas thee = Thing_new (Ltas); |
| 266 |
Matrix_init (thee.get(), my xmin, my xmax, numberOfBands, bandWidth, my xmin + 0.5 * bandWidth, 1.0, 1.0, 1, 1.0, 1.0); |
| 267 |
for (integer iband = 1; iband <= numberOfBands; iband ++) { |
| 268 |
double fmin = thy xmin + (iband - 1) * bandWidth; |
| 269 |
double meanEnergyDensity = Sampled_getMean (me, fmin, fmin + bandWidth, 0, 1, false); |
| 270 |
double meanPowerDensity = meanEnergyDensity * my dx; // as an approximation for a division by the original duration |
| 271 |
thy z [1] [iband] = meanPowerDensity == 0.0 ? -300.0 : 10.0 * log10 (meanPowerDensity / 4.0e-10); |
| 272 |
} |
| 273 |
return thee; |
| 274 |
} catch (MelderError) { |
| 275 |
Melder_throw (me, U": not converted to Ltas."); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
autoLtas Spectrum_to_Ltas_1to1 (Spectrum me) { |
| 280 |
try { |
| 281 |
autoLtas thee = Thing_new (Ltas); |
| 282 |
Matrix_init (thee.get(), my xmin, my xmax, my nx, my dx, my x1, 1.0, 1.0, 1, 1.0, 1.0); |
| 283 |
for (integer iband = 1; iband <= my nx; iband ++) { |
| 284 |
thy z [1] [iband] = Sampled_getValueAtSample (me, iband, 0, 2); |
| 285 |
} |
| 286 |
return thee; |
| 287 |
} catch (MelderError) { |
| 288 |
Melder_throw (me, U": not converted to Ltas."); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
autoLtas Sound_to_Ltas (Sound me, double bandwidth) { |
| 293 |
try { |
| 294 |
autoSpectrum thee = Sound_to_Spectrum (me, true); |
| 295 |
autoLtas him = Spectrum_to_Ltas (thee.get(), bandwidth); |
| 296 |
double correction = -10.0 * log10 (thy dx * my nx * my dx); |
| 297 |
for (integer iband = 1; iband <= his nx; iband ++) { |
| 298 |
his z [1] [iband] += correction; |
| 299 |
} |
| 300 |
return him; |
| 301 |
} catch (MelderError) { |
| 302 |
Melder_throw (me, U": LTAS analysis not performed."); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
autoLtas PointProcess_Sound_to_Ltas (PointProcess pulses, Sound sound, |
| 307 |
double maximumFrequency, double bandWidth, |
| 308 |
double shortestPeriod, double longestPeriod, double maximumPeriodFactor) |
| 309 |
{ |
| 310 |
try { |
| 311 |
integer numberOfPeriods = pulses -> nt - 2, totalNumberOfEnergies = 0; |
| 312 |
autoLtas ltas = Ltas_create (Melder_ifloor (maximumFrequency / bandWidth), bandWidth); |
| 313 |
ltas -> xmax = maximumFrequency; |
| 314 |
autoLtas numbers = Data_copy (ltas.get()); |
| 315 |
if (numberOfPeriods < 1) |
| 316 |
Melder_throw (U"Cannot compute an Ltas if there are no periods in the point process."); |
| 317 |
autoMelderProgress progress (U"PointProcess & Sound: To Ltas..."); |
| 318 |
for (integer ipulse = 2; ipulse < pulses -> nt; ipulse ++) { |
| 319 |
double leftInterval = pulses -> t [ipulse] - pulses -> t [ipulse - 1]; |
| 320 |
double rightInterval = pulses -> t [ipulse + 1] - pulses -> t [ipulse]; |
| 321 |
double intervalFactor = leftInterval > rightInterval ? leftInterval / rightInterval : rightInterval / leftInterval; |
| 322 |
Melder_progress ((double) ipulse / pulses -> nt, U"Sound & PointProcess: To Ltas: pulse ", ipulse, U" out of ", pulses -> nt); |
| 323 |
if (leftInterval >= shortestPeriod && leftInterval <= longestPeriod && |
| 324 |
rightInterval >= shortestPeriod && rightInterval <= longestPeriod && |
| 325 |
intervalFactor <= maximumPeriodFactor) |
| 326 |
{ |
| 327 |
/* |
| 328 |
* We have a period! Compute the spectrum. |
| 329 |
*/ |
| 330 |
autoSound period = Sound_extractPart (sound, |
| 331 |
pulses -> t [ipulse] - 0.5 * leftInterval, pulses -> t [ipulse] + 0.5 * rightInterval, |
| 332 |
kSound_windowShape::RECTANGULAR, 1.0, false); |
| 333 |
autoSpectrum spectrum = Sound_to_Spectrum (period.get(), false); |
| 334 |
for (integer ifreq = 1; ifreq <= spectrum -> nx; ifreq ++) { |
| 335 |
double frequency = spectrum -> xmin + (ifreq - 1) * spectrum -> dx; |
| 336 |
double realPart = spectrum -> z [1] [ifreq]; |
| 337 |
double imaginaryPart = spectrum -> z [2] [ifreq]; |
| 338 |
double energy = (realPart * realPart + imaginaryPart * imaginaryPart) * 2.0 * spectrum -> dx /* OLD: * sound -> nx */; |
| 339 |
integer iband = Melder_iceiling (frequency / bandWidth); |
| 340 |
if (iband >= 1 && iband <= ltas -> nx) { |
| 341 |
ltas -> z [1] [iband] += energy; |
| 342 |
numbers -> z [1] [iband] += 1; |
| 343 |
totalNumberOfEnergies += 1; |
| 344 |
} |
| 345 |
} |
| 346 |
} else { |
| 347 |
numberOfPeriods -= 1; |
| 348 |
} |
| 349 |
} |
| 350 |
if (numberOfPeriods < 1) |
| 351 |
Melder_throw (U"There are no periods in the point process."); |
| 352 |
for (integer iband = 1; iband <= ltas -> nx; iband ++) { |
| 353 |
if (numbers -> z [1] [iband] == 0.0) { |
| 354 |
ltas -> z [1] [iband] = undefined; |
| 355 |
} else { |
| 356 |
/* |
| 357 |
Each bin now contains a total energy in Pa2 sec. |
| 358 |
We convert this to power density. |
| 359 |
*/ |
| 360 |
const double totalEnergyInThisBand = ltas -> z [1] [iband]; |
| 361 |
if (false /* i.e. if you just want to have a spectrum of the voiced parts... */) { |
| 362 |
const double energyDensityInThisBand = totalEnergyInThisBand / ltas -> dx; |
| 363 |
const double powerDensityInThisBand = energyDensityInThisBand / (sound -> xmax - sound -> xmin); |
| 364 |
ltas -> z [1] [iband] = 10.0 * log10 (powerDensityInThisBand / 4.0e-10); |
| 365 |
} else { |
| 366 |
/* |
| 367 |
And this is what we really want. The total energy has to be redistributed. |
| 368 |
*/ |
| 369 |
const double meanEnergyInThisBand = totalEnergyInThisBand / numbers -> z [1] [iband]; |
| 370 |
const double meanNumberOfEnergiesPerBand = (double) totalNumberOfEnergies / ltas -> nx; |
| 371 |
const double redistributedEnergyInThisBand = meanEnergyInThisBand * meanNumberOfEnergiesPerBand; |
| 372 |
const double redistributedEnergyDensityInThisBand = redistributedEnergyInThisBand / ltas -> dx; |
| 373 |
const double redistributedPowerDensityInThisBand = redistributedEnergyDensityInThisBand / (sound -> xmax - sound -> xmin); |
| 374 |
ltas -> z [1] [iband] = 10.0 * log10 (redistributedPowerDensityInThisBand / 4.0e-10); |
| 375 |
/* OLD: ltas -> z [1] [iband] = 10.0 * log10 (ltas -> z [1] [iband] / numbers -> z [1] [iband] * sound -> nx);*/ |
| 376 |
} |
| 377 |
} |
| 378 |
} |
| 379 |
for (integer iband = 1; iband <= ltas -> nx; iband ++) { |
| 380 |
if (isundef (ltas -> z [1] [iband])) { |
| 381 |
integer ibandleft = iband - 1, ibandright = iband + 1; |
| 382 |
while (ibandleft >= 1 && isundef (ltas -> z [1] [ibandleft])) |
| 383 |
ibandleft --; |
| 384 |
while (ibandright <= ltas -> nx && isundef (ltas -> z [1] [ibandright])) |
| 385 |
ibandright ++; |
| 386 |
if (ibandleft < 1 && ibandright > ltas -> nx) |
| 387 |
Melder_throw (U"Cannot create an Ltas without energy in any bins."); |
| 388 |
if (ibandleft < 1) { |
| 389 |
ltas -> z [1] [iband] = ltas -> z [1] [ibandright]; |
| 390 |
} else if (ibandright > ltas -> nx) { |
| 391 |
ltas -> z [1] [iband] = ltas -> z [1] [ibandleft]; |
| 392 |
} else { |
| 393 |
const double frequency = ltas -> x1 + (iband - 1) * ltas -> dx; |
| 394 |
const double fleft = ltas -> x1 + (ibandleft - 1) * ltas -> dx; |
| 395 |
const double fright = ltas -> x1 + (ibandright - 1) * ltas -> dx; |
| 396 |
ltas -> z [1] [iband] = ((fright - frequency) * ltas -> z [1] [ibandleft] |
| 397 |
+ (frequency - fleft) * ltas -> z [1] [ibandright]) / (fright - fleft); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
return ltas; |
| 402 |
} catch (MelderError) { |
| 403 |
Melder_throw (sound, U" & ", pulses, U": LTAS analysis not performed."); |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
autoLtas Sound_to_Ltas_pitchCorrected (Sound sound, double pitchFloor, double pitchCeiling, |
| 408 |
double maximumFrequency, double bandWidth, |
| 409 |
double shortestPeriod, double longestPeriod, double maximumPeriodFactor) |
| 410 |
{ |
| 411 |
try { |
| 412 |
autoPointProcess pulses = Sound_to_PointProcess_periodic_cc (sound, pitchFloor, pitchCeiling); |
| 413 |
autoLtas ltas = PointProcess_Sound_to_Ltas (pulses.get(), sound, maximumFrequency, bandWidth, |
| 414 |
shortestPeriod, longestPeriod, maximumPeriodFactor); |
| 415 |
return ltas; |
| 416 |
} catch (MelderError) { |
| 417 |
Melder_throw (sound, U": pitch-corrected LTAS analysis not performed."); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
autoLtas PointProcess_Sound_to_Ltas_harmonics (PointProcess pulses, Sound sound, |
| 422 |
integer maximumHarmonic, |
| 423 |
double shortestPeriod, double longestPeriod, double maximumPeriodFactor) |
| 424 |
{ |
| 425 |
try { |
| 426 |
integer numberOfPeriods = pulses -> nt - 2; |
| 427 |
autoLtas ltas = Ltas_create (maximumHarmonic, 1.0); |
| 428 |
ltas -> xmax = maximumHarmonic; |
| 429 |
if (numberOfPeriods < 1) |
| 430 |
Melder_throw (U"There are no periods in the point process."); |
| 431 |
autoMelderProgress progress (U"PointProcess & Sound: To LTAS (harmonics)..."); |
| 432 |
for (integer ipulse = 2; ipulse < pulses -> nt; ipulse ++) { |
| 433 |
const double leftInterval = pulses -> t [ipulse] - pulses -> t [ipulse - 1]; |
| 434 |
const double rightInterval = pulses -> t [ipulse + 1] - pulses -> t [ipulse]; |
| 435 |
const double intervalFactor = leftInterval > rightInterval ? leftInterval / rightInterval : rightInterval / leftInterval; |
| 436 |
Melder_progress ((double) ipulse / pulses -> nt, U"Sound & PointProcess: To Ltas: pulse ", ipulse, U" out of ", pulses -> nt); |
| 437 |
if (leftInterval >= shortestPeriod && leftInterval <= longestPeriod && |
| 438 |
rightInterval >= shortestPeriod && rightInterval <= longestPeriod && |
| 439 |
intervalFactor <= maximumPeriodFactor) |
| 440 |
{ |
| 441 |
/* |
| 442 |
* We have a period! Compute the spectrum. |
| 443 |
*/ |
| 444 |
integer localMaximumHarmonic; |
| 445 |
autoSound period = Sound_extractPart (sound, |
| 446 |
pulses -> t [ipulse] - 0.5 * leftInterval, pulses -> t [ipulse] + 0.5 * rightInterval, |
| 447 |
kSound_windowShape::RECTANGULAR, 1.0, false |
| 448 |
); |
| 449 |
autoSpectrum spectrum = Sound_to_Spectrum (period.get(), false); |
| 450 |
localMaximumHarmonic = maximumHarmonic < spectrum -> nx ? maximumHarmonic : spectrum -> nx; |
| 451 |
for (integer iharm = 1; iharm <= localMaximumHarmonic; iharm ++) { |
| 452 |
const double realPart = spectrum -> z [1] [iharm]; |
| 453 |
const double imaginaryPart = spectrum -> z [2] [iharm]; |
| 454 |
const double energy = (realPart * realPart + imaginaryPart * imaginaryPart) * 2.0 * spectrum -> dx; |
| 455 |
ltas -> z [1] [iharm] += energy; |
| 456 |
} |
| 457 |
} else { |
| 458 |
numberOfPeriods -= 1; |
| 459 |
} |
| 460 |
} |
| 461 |
if (numberOfPeriods < 1) |
| 462 |
Melder_throw (U"There are no periods in the point process."); |
| 463 |
for (integer iharm = 1; iharm <= ltas -> nx; iharm ++) { |
| 464 |
if (ltas -> z [1] [iharm] == 0.0) { |
| 465 |
ltas -> z [1] [iharm] = -300.0; |
| 466 |
} else { |
| 467 |
double energyInThisBand = ltas -> z [1] [iharm]; |
| 468 |
double powerInThisBand = energyInThisBand / (sound -> xmax - sound -> xmin); |
| 469 |
ltas -> z [1] [iharm] = 10.0 * log10 (powerInThisBand / 4.0e-10); |
| 470 |
} |
| 471 |
} |
| 472 |
return ltas; |
| 473 |
} catch (MelderError) { |
| 474 |
Melder_throw (sound, U" & ", pulses, U": LTAS analysis (harmonics) not performed."); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
/* End of file Ltas.cpp */ |