| 1 | /* Pitch.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2009,2011,2012,2014-2023 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 2 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 | #include "Pitch.h" |
| 20 | #include "Sound_and_Spectrum.h" |
| 21 | #include "Matrix_and_Pitch.h" |
| 22 | |
| 23 | #include "oo_DESTROY.h" |
| 24 | #include "Pitch_def.h" |
| 25 | #include "oo_COPY.h" |
| 26 | #include "Pitch_def.h" |
| 27 | #include "oo_EQUAL.h" |
| 28 | #include "Pitch_def.h" |
| 29 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 30 | #include "Pitch_def.h" |
| 31 | #include "oo_WRITE_TEXT.h" |
| 32 | #include "Pitch_def.h" |
| 33 | #include "oo_READ_TEXT.h" |
| 34 | #include "Pitch_def.h" |
| 35 | #include "oo_WRITE_BINARY.h" |
| 36 | #include "Pitch_def.h" |
| 37 | #include "oo_READ_BINARY.h" |
| 38 | #include "Pitch_def.h" |
| 39 | #include "oo_DESCRIPTION.h" |
| 40 | #include "Pitch_def.h" |
| 41 | |
| 42 | #include "enums_getText.h" |
| 43 | #include "Pitch_enums.h" |
| 44 | #include "enums_getValue.h" |
| 45 | #include "Pitch_enums.h" |
| 46 | |
| 47 | Thing_implement (Pitch, Sampled, 1); |
| 48 | |
| 49 | int structPitch :: v_getMinimumUnit (const integer level) const { |
| 50 | return level == Pitch_LEVEL_FREQUENCY ? (int) kPitch_unit::MIN : Pitch_STRENGTH_UNIT_min; |
| 51 | } |
| 52 | |
| 53 | int structPitch :: v_getMaximumUnit (const integer level) const { |
| 54 | return level == Pitch_LEVEL_FREQUENCY ? (int) kPitch_unit::MAX : Pitch_STRENGTH_UNIT_max; |
| 55 | } |
| 56 | |
| 57 | conststring32 structPitch :: v_getUnitText (const integer level, const int unit, const uint32 flags) const { |
| 58 | if (level == Pitch_LEVEL_FREQUENCY) { |
| 59 | return |
| 60 | unit == (int) kPitch_unit::HERTZ ? |
| 61 | flags & Function_UNIT_TEXT_MENU ? U"Hertz" : U"Hz" : |
| 62 | unit == (int) kPitch_unit::HERTZ_LOGARITHMIC ? |
| 63 | flags & Function_UNIT_TEXT_MENU ? U"Hertz (logarithmic)" : (flags & Function_UNIT_TEXT_SHORT) && (flags & Function_UNIT_TEXT_GRAPHICAL) ? U"%%Hz%" : U"Hz" : |
| 64 | unit == (int) kPitch_unit::MEL ? U"mel" : |
| 65 | unit == (int) kPitch_unit::LOG_HERTZ ? |
| 66 | flags & Function_UNIT_TEXT_MENU ? U"logHertz" : U"logHz" : |
| 67 | unit == (int) kPitch_unit::SEMITONES_1 ? |
| 68 | flags & Function_UNIT_TEXT_SHORT ? U"st__1_" : flags & Function_UNIT_TEXT_GRAPHICAL ? U"semitones %%re% 1 Hz" : U"semitones re 1 Hz" : |
| 69 | unit == (int) kPitch_unit::SEMITONES_100 ? |
| 70 | flags & Function_UNIT_TEXT_SHORT ? U"st__100_" : flags & Function_UNIT_TEXT_GRAPHICAL ? U"semitones %%re% 100 Hz" : U"semitones re 100 Hz" : |
| 71 | unit == (int) kPitch_unit::SEMITONES_200 ? |
| 72 | flags & Function_UNIT_TEXT_SHORT ? U"st__200_" : flags & Function_UNIT_TEXT_GRAPHICAL ? U"semitones %%re% 200 Hz" : U"semitones re 200 Hz" : |
| 73 | unit == (int) kPitch_unit::SEMITONES_440 ? |
| 74 | flags & Function_UNIT_TEXT_SHORT ? U"st__a_" : flags & Function_UNIT_TEXT_GRAPHICAL ? U"semitones %%re% 440 Hz" : U"semitones re 440 Hz" : |
| 75 | unit == (int) kPitch_unit::ERB ? |
| 76 | flags & Function_UNIT_TEXT_SHORT ? U"erb" : U"ERB" : |
| 77 | U""; |
| 78 | } else if (level == Pitch_LEVEL_STRENGTH) { |
| 79 | return |
| 80 | unit == Pitch_STRENGTH_UNIT_AUTOCORRELATION ? U"" : |
| 81 | unit == Pitch_STRENGTH_UNIT_NOISE_HARMONICS_RATIO ? U"" : |
| 82 | unit == Pitch_STRENGTH_UNIT_HARMONICS_NOISE_DB ? U"dB" : |
| 83 | U""; |
| 84 | } |
| 85 | return U"unknown"; |
| 86 | } |
| 87 | |
| 88 | bool structPitch :: v_isUnitLogarithmic (const integer level, const int unit) const { |
| 89 | return level == Pitch_LEVEL_FREQUENCY && unit == (int) kPitch_unit::HERTZ_LOGARITHMIC; |
| 90 | } |
| 91 | |
| 92 | double structPitch :: v_convertStandardToSpecialUnit (const double value, const integer level, const int unit) const { |
| 93 | if (level == Pitch_LEVEL_FREQUENCY) { |
| 94 | return |
| 95 | unit == (int) kPitch_unit::HERTZ ? value : |
| 96 | unit == (int) kPitch_unit::HERTZ_LOGARITHMIC ? value <= 0.0 ? undefined : log10 (value) : |
| 97 | unit == (int) kPitch_unit::MEL ? NUMhertzToMel (value) : |
| 98 | unit == (int) kPitch_unit::LOG_HERTZ ? value <= 0.0 ? undefined : log10 (value) : |
| 99 | unit == (int) kPitch_unit::SEMITONES_1 ? value <= 0.0 ? undefined : (12.0 / NUMln2) * log (value / 1.0) : |
| 100 | unit == (int) kPitch_unit::SEMITONES_100 ? value <= 0.0 ? undefined : (12.0 / NUMln2) * log (value / 100.0) : |
| 101 | unit == (int) kPitch_unit::SEMITONES_200 ? value <= 0.0 ? undefined : (12.0 / NUMln2) * log (value / 200.0) : |
| 102 | unit == (int) kPitch_unit::SEMITONES_440 ? value <= 0.0 ? undefined : (12.0 / NUMln2) * log (value / 440.0) : |
| 103 | unit == (int) kPitch_unit::ERB ? NUMhertzToErb (value) : |
| 104 | undefined; |
| 105 | } else { |
| 106 | return |
| 107 | unit == Pitch_STRENGTH_UNIT_AUTOCORRELATION ? value : |
| 108 | unit == Pitch_STRENGTH_UNIT_NOISE_HARMONICS_RATIO ? |
| 109 | value <= 1e-15 ? 1e15 : value > 1.0 - 1e-15 ? 1e-15 : (1.0 - value) / value : // before losing precision |
| 110 | unit == Pitch_STRENGTH_UNIT_HARMONICS_NOISE_DB ? |
| 111 | value <= 1e-15 ? -150.0 : value > 1.0 - 1e-15 ? 150.0 : 10.0 * log10 (value / (1.0 - value)) : // before losing precision |
| 112 | undefined; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | double structPitch :: v_convertSpecialToStandardUnit (const double value, const integer level, const int unit) const { |
| 117 | if (level == Pitch_LEVEL_FREQUENCY) { |
| 118 | return |
| 119 | unit == (int) kPitch_unit::HERTZ ? value : |
| 120 | unit == (int) kPitch_unit::HERTZ_LOGARITHMIC ? pow (10.0, value) : |
| 121 | unit == (int) kPitch_unit::MEL ? NUMmelToHertz (value) : |
| 122 | unit == (int) kPitch_unit::LOG_HERTZ ? pow (10.0, value) : |
| 123 | unit == (int) kPitch_unit::SEMITONES_1 ? 1.0 * exp (value * (NUMln2 / 12.0)): |
| 124 | unit == (int) kPitch_unit::SEMITONES_100 ? 100.0 * exp (value * (NUMln2 / 12.0)): |
| 125 | unit == (int) kPitch_unit::SEMITONES_200 ? 200.0 * exp (value * (NUMln2 / 12.0)): |
| 126 | unit == (int) kPitch_unit::SEMITONES_440 ? 440.0 * exp (value * (NUMln2 / 12.0)): |
| 127 | unit == (int) kPitch_unit::ERB ? NUMerbToHertz (value) : |
| 128 | undefined; |
| 129 | } else { |
| 130 | return undefined; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | #define doesUnitAllowNegativeValues(unit) \ |
| 135 | ( (unit) == kPitch_unit::HERTZ_LOGARITHMIC || (unit) == kPitch_unit::LOG_HERTZ || \ |
| 136 | (unit) == kPitch_unit::SEMITONES_1 || (unit) == kPitch_unit::SEMITONES_100 || \ |
| 137 | (unit) == kPitch_unit::SEMITONES_200 || (unit) == kPitch_unit::SEMITONES_440 ) |
| 138 | |
| 139 | double structPitch :: v_getValueAtSample (const integer iframe, const integer ilevel, const int unit) const { |
| 140 | const double f = our frames [iframe]. candidates [1]. frequency; |
| 141 | if (! Pitch_util_frequencyIsVoiced (f, our ceiling)) |
| 142 | return undefined; |
| 143 | return v_convertStandardToSpecialUnit (ilevel == Pitch_LEVEL_FREQUENCY ? f : |
| 144 | our frames [iframe]. candidates [1]. strength, ilevel, unit); |
| 145 | } |
| 146 | |
| 147 | bool Pitch_isVoiced_i (Pitch me, integer iframe) { |
| 148 | return isdefined (Sampled_getValueAtSample (me, iframe, Pitch_LEVEL_FREQUENCY, (int) kPitch_unit::HERTZ)); |
| 149 | } |
| 150 | |
| 151 | bool Pitch_isVoiced_t (Pitch me, double time) { |
| 152 | return isdefined (Sampled_getValueAtX (me, time, Pitch_LEVEL_FREQUENCY, (int) kPitch_unit::HERTZ, false)); |
| 153 | } |
| 154 | |
| 155 | double Pitch_getValueAtTime (Pitch me, double time, kPitch_unit unit, bool interpolate) { |
| 156 | return Sampled_getValueAtX (me, time, Pitch_LEVEL_FREQUENCY, (int) unit, interpolate); |
| 157 | } |
| 158 | |
| 159 | double Pitch_getStrengthAtTime (Pitch me, double time, kPitch_unit unit, bool interpolate) { |
| 160 | return Sampled_getValueAtX (me, time, Pitch_LEVEL_STRENGTH, (int) unit, interpolate); |
| 161 | } |
| 162 | |
| 163 | integer Pitch_countVoicedFrames (Pitch me) { |
| 164 | return Sampled_countDefinedSamples (me, 0.0, 0.0, Pitch_LEVEL_FREQUENCY, (int) kPitch_unit::HERTZ); |
| 165 | } |
| 166 | |
| 167 | double Pitch_getMean (Pitch me, double tmin, double tmax, kPitch_unit unit) { |
| 168 | return Sampled_getMean (me, tmin, tmax, Pitch_LEVEL_FREQUENCY, (int) unit, true); |
| 169 | } |
| 170 | |
| 171 | double Pitch_getMeanStrength (Pitch me, double tmin, double tmax, int strengthUnit) { |
| 172 | return Sampled_getMean (me, tmin, tmax, Pitch_LEVEL_STRENGTH, strengthUnit, true); |
| 173 | } |
| 174 | |
| 175 | double Pitch_getQuantile (Pitch me, double tmin, double tmax, double quantile, kPitch_unit unit) { |
| 176 | double value = Sampled_getQuantile (me, tmin, tmax, quantile, Pitch_LEVEL_FREQUENCY, (int) unit); |
| 177 | if (value <= 0.0 && ! doesUnitAllowNegativeValues (unit)) |
| 178 | value = undefined; |
| 179 | return value; |
| 180 | } |
| 181 | |
| 182 | double Pitch_getStandardDeviation (Pitch me, double tmin, double tmax, kPitch_unit unit) { |
| 183 | return Sampled_getStandardDeviation (me, tmin, tmax, Pitch_LEVEL_FREQUENCY, (int) unit, true); |
| 184 | } |
| 185 | |
| 186 | void Pitch_getMaximumAndTime (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate, |
| 187 | double *return_maximum, double *return_timeOfMaximum) |
| 188 | { |
| 189 | Sampled_getMaximumAndX (me, tmin, tmax, Pitch_LEVEL_FREQUENCY, (int) unit, interpolate, return_maximum, return_timeOfMaximum); |
| 190 | if (! doesUnitAllowNegativeValues (unit) && return_maximum && *return_maximum <= 0.0) |
| 191 | { |
| 192 | *return_maximum = undefined; // unlikely |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | double Pitch_getMaximum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate) { |
| 197 | double maximum; |
| 198 | Pitch_getMaximumAndTime (me, tmin, tmax, unit, interpolate, & maximum, nullptr); |
| 199 | return maximum; |
| 200 | } |
| 201 | |
| 202 | double Pitch_getTimeOfMaximum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate) { |
| 203 | double time; |
| 204 | Pitch_getMaximumAndTime (me, tmin, tmax, unit, interpolate, nullptr, & time); |
| 205 | return time; |
| 206 | } |
| 207 | |
| 208 | void Pitch_getMinimumAndTime (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate, |
| 209 | double *return_minimum, double *return_timeOfMinimum) |
| 210 | { |
| 211 | Sampled_getMinimumAndX (me, tmin, tmax, Pitch_LEVEL_FREQUENCY, (int) unit, interpolate, return_minimum, return_timeOfMinimum); |
| 212 | if (! doesUnitAllowNegativeValues (unit) && return_minimum && *return_minimum <= 0.0) |
| 213 | { |
| 214 | *return_minimum = undefined; // not so unlikely |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | double Pitch_getMinimum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate) { |
| 219 | double minimum; |
| 220 | Pitch_getMinimumAndTime (me, tmin, tmax, unit, interpolate, & minimum, nullptr); |
| 221 | return minimum; |
| 222 | } |
| 223 | |
| 224 | double Pitch_getTimeOfMinimum (Pitch me, double tmin, double tmax, kPitch_unit unit, bool interpolate) { |
| 225 | double time; |
| 226 | Pitch_getMinimumAndTime (me, tmin, tmax, unit, interpolate, nullptr, & time); |
| 227 | return time; |
| 228 | } |
| 229 | |
| 230 | static integer Pitch_getMeanAbsoluteSlope (Pitch me, |
| 231 | double *out_hertz, double *out_mel, double *out_semitones, double *out_erb, double *out_withoutOctaveJumps) |
| 232 | { |
| 233 | integer firstVoicedFrame = 0, lastVoicedFrame = 0, numberOfVoicedFrames = 0; |
| 234 | autoVEC frequencies = raw_VEC (my nx); |
| 235 | for (integer i = 1; i <= my nx; i ++) { |
| 236 | const double frequency = my frames [i]. candidates [1]. frequency; |
| 237 | if (Pitch_util_frequencyIsVoiced (frequency, my ceiling)) { |
| 238 | frequencies [i] = frequency; |
| 239 | numberOfVoicedFrames ++; |
| 240 | } else { |
| 241 | frequencies [i] = 0.0; |
| 242 | } |
| 243 | } |
| 244 | for (integer i = 1; i <= my nx; i ++) // look for first voiced frame |
| 245 | if (frequencies [i] != 0.0) { |
| 246 | firstVoicedFrame = i; |
| 247 | break; |
| 248 | } |
| 249 | for (integer i = my nx; i >= 1; i --) // look for last voiced frame |
| 250 | if (frequencies [i] != 0.0) { |
| 251 | lastVoicedFrame = i; |
| 252 | break; |
| 253 | } |
| 254 | if (numberOfVoicedFrames > 1) { |
| 255 | integer ilast = firstVoicedFrame; |
| 256 | double flast = frequencies [ilast]; |
| 257 | const double span = (lastVoicedFrame - firstVoicedFrame) * my dx; |
| 258 | longdouble slopeHz = 0.0, slopeMel = 0.0, slopeSemitones = 0.0, slopeErb = 0.0, slopeRobust = 0.0; |
| 259 | for (integer i = firstVoicedFrame + 1; i <= lastVoicedFrame; i ++) if (frequencies [i] != 0.0) { |
| 260 | double localStepSemitones = fabs (NUMhertzToSemitones (frequencies [i]) - NUMhertzToSemitones (flast)); |
| 261 | slopeHz += fabs (frequencies [i] - flast); |
| 262 | slopeMel += fabs (NUMhertzToMel (frequencies [i]) - NUMhertzToMel (flast)); |
| 263 | slopeSemitones += localStepSemitones; |
| 264 | slopeErb += fabs (NUMhertzToErb (frequencies [i]) - NUMhertzToErb (flast)); |
| 265 | while (localStepSemitones >= 12.0) |
| 266 | localStepSemitones -= 12.0; // kill octave jumps |
| 267 | if (localStepSemitones > 6.0) |
| 268 | localStepSemitones = 12.0 - localStepSemitones; |
| 269 | slopeRobust += localStepSemitones; |
| 270 | ilast = i; |
| 271 | flast = frequencies [ilast]; |
| 272 | } |
| 273 | if (out_hertz) |
| 274 | *out_hertz = double (slopeHz / span); |
| 275 | if (out_mel) |
| 276 | *out_mel = double (slopeMel / span); |
| 277 | if (out_semitones) |
| 278 | *out_semitones = double (slopeSemitones / span); |
| 279 | if (out_erb) |
| 280 | *out_erb = double (slopeErb / span); |
| 281 | if (out_withoutOctaveJumps) |
| 282 | *out_withoutOctaveJumps = double (slopeRobust / span); |
| 283 | } else { |
| 284 | if (out_hertz) |
| 285 | *out_hertz = undefined; |
| 286 | if (out_mel) |
| 287 | *out_mel = undefined; |
| 288 | if (out_semitones) |
| 289 | *out_semitones = undefined; |
| 290 | if (out_erb) |
| 291 | *out_erb = undefined; |
| 292 | if (out_withoutOctaveJumps) |
| 293 | *out_withoutOctaveJumps = undefined; |
| 294 | } |
| 295 | return numberOfVoicedFrames; |
| 296 | } |
| 297 | |
| 298 | integer Pitch_getMeanAbsSlope_hertz (Pitch me, double *slope) { |
| 299 | return Pitch_getMeanAbsoluteSlope (me, slope, nullptr, nullptr, nullptr, nullptr); |
| 300 | } |
| 301 | |
| 302 | integer Pitch_getMeanAbsSlope_mel (Pitch me, double *slope) { |
| 303 | return Pitch_getMeanAbsoluteSlope (me, nullptr, slope, nullptr, nullptr, nullptr); |
| 304 | } |
| 305 | |
| 306 | integer Pitch_getMeanAbsSlope_semitones (Pitch me, double *slope) { |
| 307 | return Pitch_getMeanAbsoluteSlope (me, nullptr, nullptr, slope, nullptr, nullptr); |
| 308 | } |
| 309 | |
| 310 | integer Pitch_getMeanAbsSlope_erb (Pitch me, double *slope) { |
| 311 | return Pitch_getMeanAbsoluteSlope (me, nullptr, nullptr, nullptr, slope, nullptr); |
| 312 | } |
| 313 | |
| 314 | integer Pitch_getMeanAbsSlope_noOctave (Pitch me, double *slope) { |
| 315 | return Pitch_getMeanAbsoluteSlope (me, nullptr, nullptr, nullptr, nullptr, slope); |
| 316 | } |
| 317 | |
| 318 | MelderFraction Pitch_getFractionOfLocallyVoicedFrames ( |
| 319 | Pitch me, double tmin, double tmax, double ceiling, double silenceThreshold, double voicingThreshold) |
| 320 | { |
| 321 | MelderFraction result; |
| 322 | integer imin, imax; |
| 323 | result.denominator = Sampled_getWindowSamples (me, tmin, tmax, & imin, & imax); |
| 324 | for (integer i = imin; i <= imax; i ++) { |
| 325 | const Pitch_Frame frame = & my frames [i]; |
| 326 | if (frame -> intensity >= silenceThreshold) { |
| 327 | for (integer icand = 1; icand <= frame -> nCandidates; icand ++) { |
| 328 | const Pitch_Candidate cand = & frame -> candidates [icand]; |
| 329 | if (cand -> frequency > 0.0 && cand -> frequency < ceiling && cand -> strength >= voicingThreshold) { |
| 330 | result.numerator += 1.0; |
| 331 | break; // next frame |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | } |
| 336 | return result; |
| 337 | } |
| 338 | |
| 339 | MelderFraction Pitch_getFractionOfLocallyUnvoicedFrames ( |
| 340 | Pitch me, double tmin, double tmax, double ceiling, double silenceThreshold, double voicingThreshold) |
| 341 | { |
| 342 | MelderFraction fraction = Pitch_getFractionOfLocallyVoicedFrames (me, tmin, tmax, ceiling, silenceThreshold, voicingThreshold); |
| 343 | fraction.numerator = fraction.denominator - fraction.numerator; |
| 344 | return fraction; |
| 345 | } |
| 346 | |
| 347 | void structPitch :: v1_info () { |
| 348 | structDaata :: v1_info (); |
| 349 | autoVEC frequencies = Sampled_getSortedValues (this, 0.0, 0.0, Pitch_LEVEL_FREQUENCY, (int) kPitch_unit::HERTZ); |
| 350 | MelderInfo_writeLine (U"Time domain:"); |
| 351 | MelderInfo_writeLine (U" Start time: ", our xmin, U" seconds"); |
| 352 | MelderInfo_writeLine (U" End time: ", our xmax, U" seconds"); |
| 353 | MelderInfo_writeLine (U" Total duration: ", our xmax - our xmin, U" seconds"); |
| 354 | MelderInfo_writeLine (U"Time sampling:"); |
| 355 | MelderInfo_writeLine (U" Number of frames: ", our nx, U" (", frequencies.size, U" voiced)"); |
| 356 | MelderInfo_writeLine (U" Time step: ", our dx, U" seconds"); |
| 357 | MelderInfo_writeLine (U" First frame centred at: ", our x1, U" seconds"); |
| 358 | MelderInfo_writeLine (U"Ceiling at: ", our ceiling, U" Hz"); |
| 359 | |
| 360 | if (frequencies.size > 0) { // quantiles |
| 361 | const double quantile10 = NUMquantile (frequencies.get(), 0.10); |
| 362 | const double quantile16 = NUMquantile (frequencies.get(), 0.16); |
| 363 | const double quantile50 = NUMquantile (frequencies.get(), 0.50); // median |
| 364 | const double quantile84 = NUMquantile (frequencies.get(), 0.84); |
| 365 | const double quantile90 = NUMquantile (frequencies.get(), 0.90); |
| 366 | MelderInfo_writeLine (U"\nEstimated quantiles:"); |
| 367 | MelderInfo_writeLine (U" 10% = ", |
| 368 | Melder_single (quantile10), U" Hz = ", |
| 369 | Melder_single (NUMhertzToMel (quantile10)), U" Mel = ", |
| 370 | Melder_single (NUMhertzToSemitones (quantile10)), U" semitones above 100 Hz = ", |
| 371 | Melder_single (NUMhertzToErb (quantile10)), U" ERB"); |
| 372 | MelderInfo_writeLine (U" 16% = ", |
| 373 | Melder_single (quantile16), U" Hz = ", |
| 374 | Melder_single (NUMhertzToMel (quantile16)), U" Mel = ", |
| 375 | Melder_single (NUMhertzToSemitones (quantile16)), U" semitones above 100 Hz = ", |
| 376 | Melder_single (NUMhertzToErb (quantile16)), U" ERB"); |
| 377 | MelderInfo_writeLine (U" 50% = ", |
| 378 | Melder_single (quantile50), U" Hz = ", |
| 379 | Melder_single (NUMhertzToMel (quantile50)), U" Mel = ", |
| 380 | Melder_single (NUMhertzToSemitones (quantile50)), U" semitones above 100 Hz = ", |
| 381 | Melder_single (NUMhertzToErb (quantile50)), U" ERB"); |
| 382 | MelderInfo_writeLine (U" 84% = ", |
| 383 | Melder_single (quantile84), U" Hz = ", |
| 384 | Melder_single (NUMhertzToMel (quantile84)), U" Mel = ", |
| 385 | Melder_single (NUMhertzToSemitones (quantile84)), U" semitones above 100 Hz = ", |
| 386 | Melder_single (NUMhertzToErb (quantile84)), U" ERB"); |
| 387 | MelderInfo_write (U" 90% = ", |
| 388 | Melder_single (quantile90), U" Hz = ", |
| 389 | Melder_single (NUMhertzToMel (quantile90)), U" Mel = ", |
| 390 | Melder_single (NUMhertzToSemitones (quantile90)), U" semitones above 100 Hz = ", |
| 391 | Melder_single (NUMhertzToErb (quantile90)), U" ERB"); |
| 392 | if (frequencies.size > 1) { |
| 393 | const double correction = sqrt (frequencies.size / (frequencies.size - 1.0)); |
| 394 | MelderInfo_writeLine (U"\nEstimated spreading:"); |
| 395 | MelderInfo_writeLine (U" 84%-median = ", |
| 396 | Melder_half ((quantile84 - quantile50) * correction), U" Hz = ", |
| 397 | Melder_half ((NUMhertzToMel (quantile84) - NUMhertzToMel (quantile50)) * correction), U" Mel = ", |
| 398 | Melder_half ((NUMhertzToSemitones (quantile84) - NUMhertzToSemitones (quantile50)) * correction), U" semitones = ", |
| 399 | Melder_half ((NUMhertzToErb (quantile84) - NUMhertzToErb (quantile50)) * correction), U" ERB"); |
| 400 | MelderInfo_writeLine (U" median-16% = ", |
| 401 | Melder_half ((quantile50 - quantile16) * correction), U" Hz = ", |
| 402 | Melder_half ((NUMhertzToMel (quantile50) - NUMhertzToMel (quantile16)) * correction), U" Mel = ", |
| 403 | Melder_half ((NUMhertzToSemitones (quantile50) - NUMhertzToSemitones (quantile16)) * correction), U" semitones = ", |
| 404 | Melder_half ((NUMhertzToErb (quantile50) - NUMhertzToErb (quantile16)) * correction), U" ERB"); |
| 405 | MelderInfo_writeLine (U" 90%-10% = ", |
| 406 | Melder_half ((quantile90 - quantile10) * correction), U" Hz = ", |
| 407 | Melder_half ((NUMhertzToMel (quantile90) - NUMhertzToMel (quantile10)) * correction), U" Mel = ", |
| 408 | Melder_half ((NUMhertzToSemitones (quantile90) - NUMhertzToSemitones (quantile10)) * correction), U" semitones = ", |
| 409 | Melder_half ((NUMhertzToErb (quantile90) - NUMhertzToErb (quantile10)) * correction), U" ERB"); |
| 410 | } |
| 411 | } |
| 412 | if (frequencies.size > 0) { // extrema, range, mean and standard deviation |
| 413 | const double minimum = Pitch_getMinimum (this, xmin, xmax, kPitch_unit::HERTZ, false); |
| 414 | const double maximum = Pitch_getMaximum (this, xmin, xmax, kPitch_unit::HERTZ, false); |
| 415 | MelderInfo_writeLine (U"\nMinimum ", |
| 416 | Melder_single (minimum), U" Hz = ", |
| 417 | Melder_single (NUMhertzToMel (minimum)), U" Mel = ", |
| 418 | Melder_single (NUMhertzToSemitones (minimum)), U" semitones above 100 Hz = ", |
| 419 | Melder_single (NUMhertzToErb (minimum)), U" ERB"); |
| 420 | MelderInfo_writeLine (U"Maximum ", |
| 421 | Melder_single (maximum), U" Hz = ", |
| 422 | Melder_single (NUMhertzToMel (maximum)), U" Mel = ", |
| 423 | Melder_single (NUMhertzToSemitones (maximum)), U" semitones above 100 Hz = ", |
| 424 | Melder_single (NUMhertzToErb (maximum)), U" ERB"); |
| 425 | MelderInfo_writeLine (U"Range ", |
| 426 | Melder_half (maximum - minimum), U" Hz = ", |
| 427 | Melder_single (NUMhertzToMel (maximum) - NUMhertzToMel (minimum)), U" Mel = ", |
| 428 | Melder_half (NUMhertzToSemitones (maximum) - NUMhertzToSemitones (minimum)), U" semitones = ", |
| 429 | Melder_half (NUMhertzToErb (maximum) - NUMhertzToErb (minimum)), U" ERB"); |
| 430 | const double meanHertz = Pitch_getMean (this, 0, 0, kPitch_unit::HERTZ); |
| 431 | const double meanMel = Pitch_getMean (this, 0, 0, kPitch_unit::MEL); |
| 432 | const double meanSemitones = Pitch_getMean (this, 0, 0, kPitch_unit::SEMITONES_100); |
| 433 | const double meanErb = Pitch_getMean (this, 0, 0, kPitch_unit::ERB); |
| 434 | MelderInfo_writeLine (U"Average: ", |
| 435 | Melder_single (meanHertz), U" Hz = ", |
| 436 | Melder_single (meanMel), U" Mel = ", |
| 437 | Melder_single (meanSemitones), U" semitones above 100 Hz = ", |
| 438 | Melder_single (meanErb), U" ERB"); |
| 439 | if (frequencies.size > 1) { |
| 440 | const double stdevHertz = Pitch_getStandardDeviation (this, 0, 0, kPitch_unit::HERTZ); |
| 441 | const double stdevMel = Pitch_getStandardDeviation (this, 0, 0, kPitch_unit::MEL); |
| 442 | const double stdevSemitones = Pitch_getStandardDeviation (this, 0, 0, kPitch_unit::SEMITONES_100); |
| 443 | const double stdevErb = Pitch_getStandardDeviation (this, 0, 0, kPitch_unit::ERB); |
| 444 | MelderInfo_writeLine (U"Standard deviation: ", |
| 445 | Melder_half (stdevHertz), U" Hz = ", |
| 446 | Melder_half (stdevMel), U" Mel = ", |
| 447 | Melder_half (stdevSemitones), U" semitones = ", |
| 448 | Melder_half (stdevErb), U" ERB"); |
| 449 | } |
| 450 | } |
| 451 | if (frequencies.size > 0) { // variability: mean absolute slope |
| 452 | double slopeHertz, slopeMel, slopeSemitones, slopeErb, slopeWithoutOctaveJumps; |
| 453 | Pitch_getMeanAbsoluteSlope (this, & slopeHertz, & slopeMel, & slopeSemitones, & slopeErb, & slopeWithoutOctaveJumps); |
| 454 | MelderInfo_writeLine (U"\nMean absolute slope: ", |
| 455 | Melder_half (slopeHertz), U" Hz/s = ", |
| 456 | Melder_half (slopeMel), U" Mel/s = ", |
| 457 | Melder_half (slopeSemitones), U" semitones/s = ", |
| 458 | Melder_half (slopeErb), U" ERB/s"); |
| 459 | MelderInfo_writeLine (U"Mean absolute slope without octave jumps: ", Melder_half (slopeWithoutOctaveJumps), U" semitones/s"); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | void Pitch_Frame_init (Pitch_Frame me, integer numberOfCandidates) { |
| 464 | my candidates = newvectorzero <structPitch_Candidate> (numberOfCandidates); |
| 465 | my nCandidates = numberOfCandidates; // maintain invariant |
| 466 | } |
| 467 | |
| 468 | autoPitch Pitch_create (double tmin, double tmax, integer nt, double dt, double t1, |
| 469 | double ceiling, integer maxnCandidates) |
| 470 | { |
| 471 | try { |
| 472 | autoPitch me = Thing_new (Pitch); |
| 473 | Sampled_init (me.get(), tmin, tmax, nt, dt, t1); |
| 474 | my ceiling = ceiling; |
| 475 | my maxnCandidates = maxnCandidates; |
| 476 | my frames = newvectorzero <structPitch_Frame> (nt); |
| 477 | |
| 478 | /* |
| 479 | Put one candidate in every frame (unvoiced, silent). |
| 480 | */ |
| 481 | for (integer it = 1; it <= nt; it ++) |
| 482 | Pitch_Frame_init (& my frames [it], 1); |
| 483 | |
| 484 | return me; |
| 485 | } catch (MelderError) { |
| 486 | Melder_throw (U"Pitch not created."); |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | void Pitch_setCeiling (Pitch me, double ceiling) { |
| 491 | my ceiling = ceiling; |
| 492 | } |
| 493 | |
| 494 | integer Pitch_getMaxnCandidates (Pitch me) { |
| 495 | integer result = 0; |
| 496 | for (integer i = 1; i <= my nx; i ++) { |
| 497 | integer numberOfCandidates = my frames [i]. nCandidates; |
| 498 | if (numberOfCandidates > result) |
| 499 | result = numberOfCandidates; |
| 500 | } |
| 501 | return result; |
| 502 | } |
| 503 | |
| 504 | autoMAT Pitch_Frame_getAllCandidates (Pitch_Frame me) { |
| 505 | integer numberOfCandidates = my nCandidates; |
| 506 | autoMAT candidates = raw_MAT (2, numberOfCandidates); |
| 507 | for (integer icand = 1; icand <= numberOfCandidates; icand ++) { |
| 508 | candidates [1] [icand] = my candidates [icand]. frequency; |
| 509 | candidates [2] [icand] = my candidates [icand]. strength; |
| 510 | } |
| 511 | return candidates; |
| 512 | } |
| 513 | |
| 514 | autoMAT Pitch_getAllCandidatesInFrame (Pitch me, integer frameNumber) { |
| 515 | try { |
| 516 | my checkIndex (frameNumber); |
| 517 | const Pitch_Frame frame = & my frames [frameNumber]; |
| 518 | return Pitch_Frame_getAllCandidates (frame); |
| 519 | } catch (MelderError) { |
| 520 | Melder_throw (U"Pitch candidate matrix not created."); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | void Pitch_pathFinder (Pitch me, double silenceThreshold, double voicingThreshold, |
| 525 | double octaveCost, double octaveJumpCost, double voicedUnvoicedCost, |
| 526 | double ceiling, int pullFormants) |
| 527 | { |
| 528 | if (Melder_debug == 33) |
| 529 | Melder_casual (U"Pitch path finder:" |
| 530 | U"\nSilence threshold = ", silenceThreshold, |
| 531 | U"\nVoicing threshold = ", voicingThreshold, |
| 532 | U"\nOctave cost = ", octaveCost, |
| 533 | U"\nOctave jump cost = ", octaveJumpCost, |
| 534 | U"\nVoiced/unvoiced cost = ", voicedUnvoicedCost, |
| 535 | U"\nCeiling = ", ceiling, |
| 536 | U"\nPull formants = ", pullFormants); |
| 537 | try { |
| 538 | const integer maxnCandidates = Pitch_getMaxnCandidates (me); |
| 539 | integer place; |
| 540 | volatile double maximum, value; |
| 541 | const double ceiling2 = ( pullFormants ? 2.0 * ceiling : ceiling ); |
| 542 | /* Next three lines 20011015 */ |
| 543 | const double timeStepCorrection = 0.01 / my dx; |
| 544 | octaveJumpCost *= timeStepCorrection; |
| 545 | voicedUnvoicedCost *= timeStepCorrection; |
| 546 | |
| 547 | my ceiling = ceiling; |
| 548 | autoMAT delta = zero_MAT (my nx, maxnCandidates); |
| 549 | autoINTMAT psi = zero_INTMAT (my nx, maxnCandidates); |
| 550 | |
| 551 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 552 | const Pitch_Frame frame = & my frames [iframe]; |
| 553 | double unvoicedStrength = ( silenceThreshold <= 0 ? 0.0 : |
| 554 | 2.0 - frame -> intensity / (silenceThreshold / (1.0 + voicingThreshold)) ); |
| 555 | unvoicedStrength = voicingThreshold + std::max (0.0, unvoicedStrength); |
| 556 | for (integer icand = 1; icand <= frame -> nCandidates; icand ++) { |
| 557 | const Pitch_Candidate candidate = & frame -> candidates [icand]; |
| 558 | const bool voiceless = ! Pitch_util_frequencyIsVoiced (candidate -> frequency, ceiling2); |
| 559 | delta [iframe] [icand] = ( voiceless ? unvoicedStrength : |
| 560 | candidate -> strength - octaveCost * NUMlog2 (ceiling / candidate -> frequency) ); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | /* Look for the most probable path through the maxima. */ |
| 565 | /* There is a cost for the voiced/unvoiced transition, */ |
| 566 | /* and a cost for a frequency jump. */ |
| 567 | |
| 568 | for (integer iframe = 2; iframe <= my nx; iframe ++) { |
| 569 | const Pitch_Frame prevFrame = & my frames [iframe - 1], curFrame = & my frames [iframe]; |
| 570 | const constVEC prevDelta = delta [iframe - 1]; |
| 571 | const VEC curDelta = delta [iframe]; |
| 572 | const INTVEC curPsi = psi [iframe]; |
| 573 | for (integer icand2 = 1; icand2 <= curFrame -> nCandidates; icand2 ++) { |
| 574 | const double f2 = curFrame -> candidates [icand2]. frequency; |
| 575 | maximum = -1e30; |
| 576 | place = 0; |
| 577 | for (integer icand1 = 1; icand1 <= prevFrame -> nCandidates; icand1 ++) { |
| 578 | double f1 = prevFrame -> candidates [icand1]. frequency; |
| 579 | double transitionCost; |
| 580 | const bool previousVoiceless = ! Pitch_util_frequencyIsVoiced (f1, ceiling2); |
| 581 | const bool currentVoiceless = ! Pitch_util_frequencyIsVoiced (f2, ceiling2); |
| 582 | if (currentVoiceless) { |
| 583 | if (previousVoiceless) { |
| 584 | transitionCost = 0.0; // both voiceless |
| 585 | } else { |
| 586 | transitionCost = voicedUnvoicedCost; // voiced-to-unvoiced transition |
| 587 | } |
| 588 | } else { |
| 589 | if (previousVoiceless) { |
| 590 | transitionCost = voicedUnvoicedCost; // unvoiced-to-voiced transition |
| 591 | if (Melder_debug == 30) { |
| 592 | /* |
| 593 | Try to take into account a frequency jump across a voiceless stretch. |
| 594 | */ |
| 595 | integer place1 = icand1; |
| 596 | for (integer jframe = iframe - 2; jframe >= 1; jframe --) { |
| 597 | place1 = psi [jframe + 1] [place1]; |
| 598 | f1 = my frames [jframe]. candidates [place1]. frequency; |
| 599 | if (Pitch_util_frequencyIsVoiced (f1, ceiling)) { |
| 600 | transitionCost += octaveJumpCost * fabs (NUMlog2 (f1 / f2)) / (iframe - jframe); |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } else { |
| 606 | transitionCost = octaveJumpCost * fabs (NUMlog2 (f1 / f2)); // both voiced |
| 607 | } |
| 608 | } |
| 609 | value = prevDelta [icand1] - transitionCost + curDelta [icand2]; |
| 610 | //if (Melder_debug == 33) Melder_casual (U"Frame ", iframe, U", current candidate ", icand2, |
| 611 | // U" (delta ", curDelta [icand2], U"), previous candidate ", icand1, U" (delta ", prevDelta [icand1], U"), ", |
| 612 | // U"transition cost ", transitionCost, U", value ", value, U", maximum ", maximum); |
| 613 | if (value > maximum) { |
| 614 | maximum = value; |
| 615 | place = icand1; |
| 616 | } else if (value == maximum) { |
| 617 | if (Melder_debug == 33) |
| 618 | Melder_casual ( |
| 619 | U"A tie in frame ", iframe, |
| 620 | U", current candidate ", icand2, |
| 621 | U", previous candidate ", icand1 |
| 622 | ); |
| 623 | } |
| 624 | } |
| 625 | curDelta [icand2] = maximum; |
| 626 | curPsi [icand2] = place; |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | /* Find the end of the most probable path. */ |
| 631 | |
| 632 | place = 1; |
| 633 | maximum = delta [my nx] [place]; |
| 634 | for (integer icand = 2; icand <= my frames [my nx]. nCandidates; icand ++) { |
| 635 | if (delta [my nx] [icand] > maximum) { |
| 636 | place = icand; |
| 637 | maximum = delta [my nx] [place]; |
| 638 | } |
| 639 | } |
| 640 | |
| 641 | /* Backtracking: follow the path backwards. */ |
| 642 | |
| 643 | for (integer iframe = my nx; iframe >= 1; iframe --) { |
| 644 | if (Melder_debug == 33) |
| 645 | Melder_casual ( |
| 646 | U"Frame ", iframe, U":", |
| 647 | U" swapping candidates 1 and ", place |
| 648 | ); |
| 649 | const Pitch_Frame frame = & my frames [iframe]; |
| 650 | std::swap (frame -> candidates [1], frame -> candidates [place]); |
| 651 | place = psi [iframe] [place]; |
| 652 | } |
| 653 | |
| 654 | /* Pull formants: devoice frames with frequencies between ceiling and ceiling2. */ |
| 655 | |
| 656 | if (ceiling2 > ceiling) { |
| 657 | if (Melder_debug == 33) |
| 658 | Melder_casual (U"Pulling formants..."); |
| 659 | for (integer iframe = my nx; iframe >= 1; iframe --) { |
| 660 | const Pitch_Frame frame = & my frames [iframe]; |
| 661 | const Pitch_Candidate winner = & frame -> candidates [1]; |
| 662 | const double f = winner -> frequency; |
| 663 | if (f > ceiling && f < ceiling2) { |
| 664 | for (integer icand = 2; icand <= frame -> nCandidates; icand ++) { |
| 665 | const Pitch_Candidate loser = & frame -> candidates [icand]; |
| 666 | if (loser -> frequency == 0.0) { |
| 667 | std::swap (* winner, * loser); |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | } |
| 673 | } |
| 674 | } catch (MelderError) { |
| 675 | Melder_throw (me, U": path not found."); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | void Pitch_drawInside (Pitch me, Graphics g, double xmin, double xmax, double fmin, double fmax, bool speckle, kPitch_unit unit) { |
| 680 | Sampled_drawInside (me, g, xmin, xmax, fmin, fmax, speckle, Pitch_LEVEL_FREQUENCY, (int) unit); |
| 681 | } |
| 682 | |
| 683 | void Pitch_draw (Pitch me, Graphics g, double tmin, double tmax, double fmin, double fmax, bool garnish, bool speckle, kPitch_unit unit) { |
| 684 | Graphics_setInner (g); |
| 685 | Pitch_drawInside (me, g, tmin, tmax, fmin, fmax, speckle, unit); |
| 686 | Graphics_unsetInner (g); |
| 687 | if (garnish) { |
| 688 | Graphics_drawInnerBox (g); |
| 689 | Graphics_textBottom (g, true, U"Time (s)"); |
| 690 | Graphics_marksBottom (g, 2, true, true, false); |
| 691 | Graphics_textLeft (g, true, Melder_cat (U"Pitch (", Function_getUnitText (me, Pitch_LEVEL_FREQUENCY, (int) unit, Function_UNIT_TEXT_GRAPHICAL), U")")); |
| 692 | if (Function_isUnitLogarithmic (me, Pitch_LEVEL_FREQUENCY, (int) unit)) { |
| 693 | Graphics_marksLeftLogarithmic (g, 6, true, true, false); |
| 694 | } else { |
| 695 | Graphics_marksLeft (g, 2, true, true, false); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | |
| 700 | void Pitch_difference (Pitch me, Pitch thee) { |
| 701 | integer nuvtov = 0, nvtouv = 0, ndfdown = 0, ndfup = 0; |
| 702 | if (my nx != thy nx || my dx != thy dx || my x1 != thy x1) { |
| 703 | Melder_flushError (U"Pitch_difference: these Pitches are not aligned."); |
| 704 | return; |
| 705 | } |
| 706 | for (integer i = 1; i <= my nx; i ++) { |
| 707 | const double myf = my frames [i]. candidates [1]. frequency, thyf = thy frames [i]. candidates [1]. frequency; |
| 708 | const bool myUnvoiced = ! Pitch_util_frequencyIsVoiced (myf, my ceiling); |
| 709 | const bool thyUnvoiced = ! Pitch_util_frequencyIsVoiced (thyf, thy ceiling); |
| 710 | const double t = Sampled_indexToX (me, i); |
| 711 | if (myUnvoiced && ! thyUnvoiced) { |
| 712 | Melder_casual ( |
| 713 | U"Frame ", i, |
| 714 | U" time ", t, |
| 715 | U": unvoiced to voiced." |
| 716 | ); |
| 717 | nuvtov ++; |
| 718 | } else if (! myUnvoiced && thyUnvoiced) { |
| 719 | Melder_casual ( |
| 720 | U"Frame ", i, |
| 721 | U" time ", t, |
| 722 | U": voiced to unvoiced." |
| 723 | ); |
| 724 | nvtouv ++; |
| 725 | } else if (! myUnvoiced && ! thyUnvoiced) { |
| 726 | if (myf > thyf) { |
| 727 | //Melder_casual ("Frame %ld time %f: downward frequency jump from %.5g Hz to %.5g Hz.", i, t, myf, thyf); |
| 728 | ndfdown ++; |
| 729 | } else if (myf < thyf) { |
| 730 | //Melder_casual ("Frame %ld time %f: upward frequency jump from %.5g Hz to %.5g Hz.", i, t, myf, thyf); |
| 731 | ndfup ++; |
| 732 | } |
| 733 | } |
| 734 | } |
| 735 | MelderInfo_open (); |
| 736 | MelderInfo_writeLine (U"Difference between two Pitches:"); |
| 737 | MelderInfo_writeLine (U"Unvoiced to voiced: ", nuvtov, U" frames."); |
| 738 | MelderInfo_writeLine (U"Voiced to unvoiced: ", nvtouv, U" frames."); |
| 739 | MelderInfo_writeLine (U"Downward frequency jump: ", ndfdown, U" frames."); |
| 740 | MelderInfo_writeLine (U"Upward frequency jump: ", ndfup, U" frames."); |
| 741 | MelderInfo_close (); |
| 742 | } |
| 743 | |
| 744 | autoPitch Pitch_killOctaveJumps (Pitch me) { |
| 745 | try { |
| 746 | autoPitch thee = Pitch_create (my xmin, my xmax, my nx, my dx, my x1, my ceiling, 2); |
| 747 | integer nVoiced = 0, nUp = 0; |
| 748 | double lastFrequency = 0.0; |
| 749 | for (integer i = 1; i <= my nx; i ++) { |
| 750 | double f = my frames [i]. candidates [1]. frequency; |
| 751 | thy frames [i]. candidates [1]. strength = my frames [i]. candidates [1]. strength; |
| 752 | if (Pitch_util_frequencyIsVoiced (f, my ceiling)) { |
| 753 | nVoiced ++; |
| 754 | if (lastFrequency != 0.0) { |
| 755 | const double fmin = lastFrequency * 0.7071, fmax = 2.0 * fmin; |
| 756 | while (f < fmin) { |
| 757 | f *= 2.0; |
| 758 | nUp ++; |
| 759 | } |
| 760 | while (f > fmax) { |
| 761 | f *= 0.5; |
| 762 | nUp --; |
| 763 | } |
| 764 | } |
| 765 | lastFrequency = thy frames [i]. candidates [1]. frequency = f; |
| 766 | } |
| 767 | } |
| 768 | thy ceiling *= 2.0; // make room for some octave jumps |
| 769 | while (nUp > nVoiced / 2) { |
| 770 | for (integer i = 1; i <= thy nx; i ++) |
| 771 | thy frames [i]. candidates [1]. frequency *= 0.5; |
| 772 | nUp -= nVoiced; |
| 773 | } |
| 774 | while (nUp < - nVoiced / 2) { |
| 775 | for (integer i = 1; i <= thy nx; i ++) |
| 776 | thy frames [i]. candidates [1]. frequency *= 2.0; |
| 777 | nUp += nVoiced; |
| 778 | } |
| 779 | return thee; |
| 780 | } catch (MelderError) { |
| 781 | Melder_throw (me, U": octave jumps not killed."); |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | autoPitch Pitch_interpolate (Pitch me) { |
| 786 | try { |
| 787 | autoPitch thee = Pitch_create (my xmin, my xmax, my nx, my dx, my x1, my ceiling, 2); |
| 788 | for (integer i = 1; i <= my nx; i ++) { |
| 789 | const double f = my frames [i]. candidates [1]. frequency; |
| 790 | thy frames [i]. candidates [1]. strength = 0.9; |
| 791 | if (Pitch_util_frequencyIsVoiced (f, my ceiling)) { |
| 792 | thy frames [i]. candidates [1]. frequency = f; |
| 793 | } else { |
| 794 | integer left, right; |
| 795 | double fleft = 0.0, fright = 0.0; |
| 796 | for (left = i - 1; left >= 1 && fleft == 0.0; left --) { |
| 797 | fleft = my frames [left]. candidates [1]. frequency; |
| 798 | if (fleft >= my ceiling) |
| 799 | fleft = 0.0; |
| 800 | } |
| 801 | for (right = i + 1; right <= my nx && fright == 0.0; right ++) { |
| 802 | fright = my frames [right]. candidates [1]. frequency; |
| 803 | if (fright >= my ceiling) |
| 804 | fright = 0.0; |
| 805 | } |
| 806 | if (fleft != 0.0 && fright != 0.0) |
| 807 | thy frames [i]. candidates [1]. frequency = |
| 808 | ((i - left) * fright + (right - i) * fleft) / (right - left); |
| 809 | } |
| 810 | } |
| 811 | return thee; |
| 812 | } catch (MelderError) { |
| 813 | Melder_throw (me, U": not interpolated."); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | autoPitch Pitch_subtractLinearFit (Pitch me, kPitch_unit unit) { |
| 818 | try { |
| 819 | autoPitch thee = Pitch_interpolate (me); |
| 820 | /* |
| 821 | Find the first and last voiced frame. |
| 822 | */ |
| 823 | integer imin = thy nx + 1, imax = 0; |
| 824 | for (integer i = 1; i <= my nx; i ++) |
| 825 | if (Pitch_isVoiced_i (thee.get(), i)) { |
| 826 | imin = i; |
| 827 | break; |
| 828 | } |
| 829 | for (integer i = imin + 1; i <= my nx; i ++) |
| 830 | if (! Pitch_isVoiced_i (thee.get(), i)) { |
| 831 | imax = i - 1; |
| 832 | break; |
| 833 | } |
| 834 | integer n = imax - imin + 1; |
| 835 | if (n <= 1) { |
| 836 | /* |
| 837 | If there is only one point, there is no slope to subtract. |
| 838 | The result is the original frequency if the original was voiced, else 0.0 Hz. |
| 839 | The interpolated Pitch already has these properties set correctly. |
| 840 | */ |
| 841 | return thee; |
| 842 | } |
| 843 | /* |
| 844 | Compute average pitch and time. |
| 845 | */ |
| 846 | longdouble sum = 0.0; |
| 847 | for (integer i = imin; i <= imax; i ++) |
| 848 | sum += Sampled_getValueAtSample (thee.get(), i, Pitch_LEVEL_FREQUENCY, (int) unit); |
| 849 | double fmean = double (sum) / n; |
| 850 | double tmean = thy x1 + (0.5 * (imin + imax) - 1) * thy dx; |
| 851 | /* |
| 852 | Compute slope. |
| 853 | */ |
| 854 | double numerator = 0.0, denominator = 0.0; |
| 855 | for (integer i = imin; i <= imax; i ++) { |
| 856 | const double t = thy x1 + (i - 1) * thy dx - tmean; |
| 857 | const double f = Sampled_getValueAtSample (thee.get(), i, Pitch_LEVEL_FREQUENCY, (int) unit) |
| 858 | - fmean; |
| 859 | numerator += f * t; |
| 860 | denominator += t * t; |
| 861 | } |
| 862 | double slope = numerator / denominator; |
| 863 | /* |
| 864 | Modify frequencies. |
| 865 | */ |
| 866 | for (integer i = imin; i <= imax; i ++) { |
| 867 | const Pitch_Frame myFrame = & my frames [i], thyFrame = & thy frames [i]; |
| 868 | const Pitch_Candidate myCandidate = & myFrame -> candidates [1], thyCandidate = & thyFrame -> candidates [1]; |
| 869 | const double t = thy x1 + (i - 1) * thy dx - tmean, myFreq = myCandidate -> frequency; |
| 870 | const double f = Sampled_getValueAtSample (thee.get(), i, Pitch_LEVEL_FREQUENCY, (int) unit) |
| 871 | - slope * t; |
| 872 | if (Pitch_util_frequencyIsVoiced (myFreq, my ceiling)) |
| 873 | thyCandidate -> frequency = Function_convertSpecialToStandardUnit (me, f, Pitch_LEVEL_FREQUENCY, (int) unit); |
| 874 | else |
| 875 | thyCandidate -> frequency = 0.0; |
| 876 | } |
| 877 | return thee; |
| 878 | } catch (MelderError) { |
| 879 | Melder_throw (me, U": linear fit not subtracted."); |
| 880 | } |
| 881 | } |
| 882 | |
| 883 | autoPitch Pitch_smooth (Pitch me, double bandWidth) { |
| 884 | try { |
| 885 | autoPitch interp = Pitch_interpolate (me); |
| 886 | autoMatrix matrix1 = Pitch_to_Matrix (interp.get()); |
| 887 | const double logicalDuration = matrix1 -> xmax - matrix1 -> xmin; |
| 888 | const double physicalDuration = matrix1 -> nx * matrix1 -> dx; |
| 889 | autoSound sound1 = Sound_create ( |
| 890 | 1, // one channel |
| 891 | matrix1 -> xmin - logicalDuration, // triply wide logical time domain (ignored) |
| 892 | matrix1 -> xmax + logicalDuration, // triply wide logical time domain (ignored) |
| 893 | 3 * matrix1 -> nx, // triply wide physical time domain |
| 894 | matrix1 -> dx, |
| 895 | matrix1 -> x1 - physicalDuration // (ignored) |
| 896 | ); |
| 897 | |
| 898 | integer firstVoiced = 0, lastVoiced = 0; |
| 899 | for (integer i = 1; i <= matrix1 -> nx; i ++) { |
| 900 | const double f = matrix1 -> z [1] [i]; |
| 901 | if (f != 0.0) { |
| 902 | if (! firstVoiced) |
| 903 | firstVoiced = i; |
| 904 | lastVoiced = i; |
| 905 | sound1 -> z [1] [i + matrix1 -> nx] = f; |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /* Extrapolate. */ |
| 910 | double fextrap = matrix1 -> z [1] [firstVoiced]; |
| 911 | firstVoiced += matrix1 -> nx; |
| 912 | for (integer i = 1; i < firstVoiced; i ++) |
| 913 | sound1 -> z [1] [i] = fextrap; |
| 914 | fextrap = matrix1 -> z [1] [lastVoiced]; |
| 915 | lastVoiced += matrix1 -> nx; |
| 916 | for (integer i = lastVoiced + 1; i <= sound1 -> nx; i ++) |
| 917 | sound1 -> z [1] [i] = fextrap; |
| 918 | |
| 919 | /* Smooth. */ |
| 920 | autoSpectrum spectrum = Sound_to_Spectrum (sound1.get(), true); |
| 921 | for (integer i = 1; i <= spectrum -> nx; i ++) { |
| 922 | double f = (i - 1) * spectrum -> dx, fT = f / bandWidth, factor = exp (- fT * fT); |
| 923 | spectrum -> z [1] [i] *= factor; |
| 924 | spectrum -> z [2] [i] *= factor; |
| 925 | } |
| 926 | autoSound sound2 = Spectrum_to_Sound (spectrum.get()); |
| 927 | |
| 928 | autoMatrix matrix2 = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 1.0, 1.0, 1, 1.0, 1.0); |
| 929 | for (integer i = 1; i <= my nx; i ++) { |
| 930 | const double originalF0 = my frames [i]. candidates [1]. frequency; |
| 931 | matrix2 -> z [1] [i] = ( Pitch_util_frequencyIsVoiced (originalF0, my ceiling) ? |
| 932 | sound2 -> z [1] [i + matrix2 -> nx] : 0.0 ); |
| 933 | } |
| 934 | autoPitch thee = Matrix_to_Pitch (matrix2.get()); |
| 935 | thy ceiling = my ceiling; |
| 936 | return thee; |
| 937 | } catch (MelderError) { |
| 938 | Melder_throw (me, U": not smoothed."); |
| 939 | } |
| 940 | } |
| 941 | |
| 942 | void Pitch_step (Pitch me, double step, double precision, double tmin, double tmax) { |
| 943 | Melder_assert (precision >= 0.0 && precision < 1.0); |
| 944 | integer imin, imax; |
| 945 | if (! Sampled_getWindowSamples (me, tmin, tmax, & imin, & imax)) |
| 946 | return; |
| 947 | for (integer i = imin; i <= imax; i ++) { |
| 948 | const Pitch_Frame frame = & my frames [i]; |
| 949 | const double currentFrequency = frame -> candidates [1]. frequency; |
| 950 | if (Pitch_util_frequencyIsVoiced (currentFrequency, my ceiling)) { |
| 951 | const double targetFrequency = currentFrequency * step; |
| 952 | const double fmin = (1.0 - precision) * targetFrequency; |
| 953 | const double fmax = std::min ((1.0 + precision) * targetFrequency, my ceiling); |
| 954 | int nearestCandidate = 0; |
| 955 | double nearestDistance = my ceiling; |
| 956 | for (int icand = 2; icand <= frame -> nCandidates; icand ++) { |
| 957 | const double f = frame -> candidates [icand]. frequency; |
| 958 | if (f > fmin && f < fmax) { |
| 959 | double localDistance = fabs (f - targetFrequency); |
| 960 | if (localDistance < nearestDistance) { |
| 961 | nearestCandidate = icand; |
| 962 | nearestDistance = localDistance; |
| 963 | } |
| 964 | } |
| 965 | } |
| 966 | if (nearestCandidate) |
| 967 | std::swap (frame -> candidates [nearestCandidate], frame -> candidates [1]); |
| 968 | } |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | static autoTable Pitch_Frame_tabulateCandidates (Pitch_Frame me) { |
| 973 | const conststring32 columnNames [] = { U"frequency", U"strength" }; |
| 974 | autoTable you = Table_createWithColumnNames (my nCandidates, ARRAY_TO_STRVEC (columnNames)); |
| 975 | for (integer icand = 1; icand <= my nCandidates; icand ++) { |
| 976 | const Pitch_Candidate candidate = & my candidates [icand]; |
| 977 | Table_setNumericValue (you.get(), icand, 1, candidate -> frequency); |
| 978 | Table_setNumericValue (you.get(), icand, 2, candidate -> strength); |
| 979 | } |
| 980 | return you; |
| 981 | } |
| 982 | |
| 983 | autoTable Pitch_tabulateCandidatesInFrame (Pitch me, integer frameNumber) { |
| 984 | my checkIndex (frameNumber); |
| 985 | const Pitch_Frame frame = & my frames [frameNumber]; |
| 986 | return Pitch_Frame_tabulateCandidates (frame); |
| 987 | } |
| 988 | |
| 989 | autoTable Pitch_tabulateCandidates (Pitch me) { |
| 990 | integer totalNumberOfCandidates = 0; |
| 991 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 992 | const Pitch_Frame frame = & my frames [iframe]; |
| 993 | totalNumberOfCandidates += frame -> nCandidates; |
| 994 | } |
| 995 | const conststring32 columnNames [] = { U"frame", U"frequency", U"strength" }; |
| 996 | autoTable result = Table_createWithColumnNames (totalNumberOfCandidates, ARRAY_TO_STRVEC (columnNames)); |
| 997 | integer rowNumber = 0; |
| 998 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 999 | const Pitch_Frame frame = & my frames [iframe]; |
| 1000 | for (integer icand = 1; icand <= frame -> nCandidates; icand ++) { |
| 1001 | const Pitch_Candidate candidate = & frame -> candidates [icand]; |
| 1002 | Table_setNumericValue (result.get(), ++ rowNumber, 1, double (iframe)); |
| 1003 | Table_setNumericValue (result.get(), rowNumber, 2, candidate -> frequency); |
| 1004 | Table_setNumericValue (result.get(), rowNumber, 3, candidate -> strength); |
| 1005 | } |
| 1006 | } |
| 1007 | return result; |
| 1008 | } |
| 1009 | |
| 1010 | /* End of file Pitch.cpp */ |