| 1 | /* Pitch_Intensity.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2007,2011,2014-2017,2019 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_Intensity.h"cxx . . $ `<file>/fon/Pitch_Intensity.h`/ |
| 20 | |
| 21 | static void Pitch_getExtremacxx . . $ Pitch_getExtrema(cddaf295d394a3ef). (Pitchcxx . . $ Pitch# me, double *minimum, double *maximum) { |
| 22 | MelderExtremaWithInitcxx . . $ MelderExtremaWithInit# extrema; |
| 23 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) { |
| 24 | const double frequency = mycxx . . $ `melder/melder.h:44:9`! framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. candidatescxx . . $ structPitch_Frame#candidates. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. frequencycxx . . $ structPitch_Candidate#frequency.; |
| 25 | if (frequency == 0.0) |
| 26 | continue; // voiceless |
| 27 | extrema.updatecxx . . $ MelderExtremaWithInit#update(ba8836b59fb42830). (frequency); |
| 28 | } |
| 29 | if (extrema.isValidcxx . . $ MelderExtremaWithInit#isValid(50ce9a9e25b4a850).()) { |
| 30 | *minimum = extrema.mincxx . . $ MelderExtremaWithInit#min.; |
| 31 | *maximum = extrema.maxcxx . . $ MelderExtremaWithInit#max.; |
| 32 | } else { |
| 33 | *minimum = *maximum = 0.0; |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void Pitch_Intensity_drawcxx . . $ Pitch_Intensity_draw(eb3c54911c2755ef). (Pitchcxx . . $ Pitch# pitchObject, Intensitycxx . . $ Intensity# intensityObject, Graphicscxx . . $ Graphics# g, |
| 38 | double f1, double f2, double s1, double s2, bool garnish, int connect) |
| 39 | { |
| 40 | if (f1 == f2) |
| 41 | Pitch_getExtremacxx . . $ Pitch_getExtrema(cddaf295d394a3ef). (pitchObject, & f1, & f2); // autowindow |
| 42 | if (f1 == 0.0) // all voiceless? |
| 43 | return; |
| 44 | if (f1 == f2) { |
| 45 | f1 -= 1.0; |
| 46 | f2 += 1.0; |
| 47 | } |
| 48 | if (s1 == s2) |
| 49 | Matrix_getWindowExtremacxx . . $ Matrix_getWindowExtrema(d50591dd897af896). (intensityObject, 0, 0, 1, 1, & s1, & s2); // autowindow |
| 50 | if (s1 == s2) { |
| 51 | s1 -= 1.0; |
| 52 | s2 += 1.0; |
| 53 | } |
| 54 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, f1, f2, s1, s2); |
| 55 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 56 | double previousPitchValue = undefinedcxx . . $ undefined.; |
| 57 | double previousIntensityValue = undefinedcxx . . $ undefined.; |
| 58 | integercxx . . $ integer# previousPitchFrameNumber = 0; |
| 59 | for (integercxx . . $ integer# ipitchFrame = 1; ipitchFrame <= pitchObject -> nxcxx . . $ structSampled#nx.; ipitchFrame ++) { |
| 60 | /* |
| 61 | Get pitch value. |
| 62 | */ |
| 63 | const bool pitchMeasurementWillBeValid = Pitch_isVoiced_icxx . . $ Pitch_isVoiced_i(fd5b671aa7b53262). (pitchObject, ipitchFrame); |
| 64 | if (! pitchMeasurementWillBeValid) |
| 65 | continue; // voiceless -> don't draw |
| 66 | const Pitch_Framecxx . . $ Pitch_Frame# pitchFrame = & pitchObject -> framescxx . . $ structPitch#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).ipitchFrame]; |
| 67 | constexpr integercxx . . $ integer# winningPitchCandidateNumber = 1; |
| 68 | const double pitchValue = pitchFrame -> candidatescxx . . $ structPitch_Frame#candidates. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).winningPitchCandidateNumber]. frequencycxx . . $ structPitch_Candidate#frequency.; |
| 69 | /* |
| 70 | Get the corresponding intensity value. |
| 71 | "Corresponding" means: measure the intensity at the same time as the pitch. |
| 72 | */ |
| 73 | const double time = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (pitchObject, ipitchFrame); |
| 74 | constexpr integercxx . . $ integer# onlyIntensityChannel = 1; |
| 75 | constexpr integercxx . . $ integer# defaultUnit = 0; |
| 76 | const double intensityValue = Sampled_getValueAtXcxx . . $ Sampled_getValueAtX(a51129e8ab7e98e9). (intensityObject, time, onlyIntensityChannel, defaultUnit, true); |
| 77 | const bool intensityMeasurementIsValid = isdefinedcxx . . $ isdefined(f8197d536af80c96). (intensityValue); |
| 78 | if (! intensityMeasurementIsValid) |
| 79 | continue; // no intensity measured, e.g. at the edges of the time domain -> don't draw |
| 80 | /* |
| 81 | Draw. |
| 82 | */ |
| 83 | constexpr integercxx . . $ integer# shouldSpeckle_mask = 1; |
| 84 | constexpr integercxx . . $ integer# shouldCurve_mask = 2; |
| 85 | if (connect & shouldSpeckle_mask) |
| 86 | Graphics_specklecxx . . $ Graphics_speckle(c84ba9158791aef6). (g, pitchValue, intensityValue); |
| 87 | if ((connect & shouldCurve_mask) && isdefinedcxx . . $ isdefined(f8197d536af80c96). (previousPitchValue)) { |
| 88 | /* |
| 89 | We draw a solid line if the previous point represented the previous frame, |
| 90 | but a dotted line if, instead, the previous frame was voiceless. |
| 91 | */ |
| 92 | if (previousPitchFrameNumber >= 1 && previousPitchFrameNumber < ipitchFrame - 1) |
| 93 | Graphics_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (g, Graphics_DOTTEDcxx . . $ `sys/Graphics.h:288:9`!); |
| 94 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (g, previousPitchValue, previousIntensityValue, pitchValue, intensityValue); |
| 95 | Graphics_setLineTypecxx . . $ Graphics_setLineType(9abe41586e87648d). (g, Graphics_DRAWNcxx . . $ `sys/Graphics.h:287:9`!); |
| 96 | } |
| 97 | /* |
| 98 | Cycle. |
| 99 | */ |
| 100 | previousPitchValue = pitchValue; |
| 101 | previousIntensityValue = intensityValue; |
| 102 | previousPitchFrameNumber = ipitchFrame; |
| 103 | } |
| 104 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 105 | if (garnish) { |
| 106 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 107 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (g, true, U"Fundamental frequency (Hz)"); |
| 108 | Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 109 | Graphics_textLeftcxx . . $ Graphics_textLeft(48cf1f690ec61203). (g, true, U"Intensity (dB)"); |
| 110 | Graphics_marksLeftcxx . . $ Graphics_marksLeft(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | double Pitch_Intensity_getMeancxx . . $ Pitch_Intensity_getMean(5b6240476113f8ac). (Pitchcxx . . $ Pitch# thee, Intensitycxx . . $ Intensity# me) { |
| 115 | integercxx . . $ integer# numberOfValidLocalMeasurements = 0; |
| 116 | longdoublecxx . . $ longdouble# sumOfLocalValues = 0.0; |
| 117 | for (integercxx . . $ integer# iframe = 1; iframe <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; iframe ++) { |
| 118 | const double time = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, iframe); |
| 119 | const bool localMeasurentIsValid = Pitch_isVoiced_tcxx . . $ Pitch_isVoiced_t(9b4a997843717fd9). (thee, time); |
| 120 | if (localMeasurentIsValid) { |
| 121 | double localValue = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 122 | sumOfLocalValues += localValue; |
| 123 | numberOfValidLocalMeasurements += 1; |
| 124 | } |
| 125 | } |
| 126 | return numberOfValidLocalMeasurements > 0 ? double (sumOfLocalValues) / numberOfValidLocalMeasurements : undefinedcxx . . $ undefined.; |
| 127 | } |
| 128 | |
| 129 | double Pitch_Intensity_getMeanAbsoluteSlopecxx . . $ Pitch_Intensity_getMeanAbsoluteSlope(5b6240476113f8ac). (Pitchcxx . . $ Pitch# thee, Intensitycxx . . $ Intensity# me) { |
| 130 | integercxx . . $ integer# numberOfValidLocalMeasurements = 0; |
| 131 | longdoublecxx . . $ longdouble# sumOfLocalAbsoluteSlopes = 0.0; |
| 132 | for (integercxx . . $ integer# iframe = 1; iframe < mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; iframe ++) { |
| 133 | const double t1 = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (me, iframe); |
| 134 | const double t2 = t1 + mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 135 | const bool localMeasurentIsValid = ( Pitch_isVoiced_tcxx . . $ Pitch_isVoiced_t(9b4a997843717fd9). (thee, t1) && Pitch_isVoiced_tcxx . . $ Pitch_isVoiced_t(9b4a997843717fd9). (thee, t2) ); |
| 136 | if (localMeasurentIsValid) { |
| 137 | double absoluteLocalSlope = fabscxx . . $ fabs(fee4f1d22bc8e617). (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe + 1] - mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).iframe]); |
| 138 | sumOfLocalAbsoluteSlopes += absoluteLocalSlope; |
| 139 | numberOfValidLocalMeasurements += 1; |
| 140 | } |
| 141 | } |
| 142 | sumOfLocalAbsoluteSlopes /= mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; // convert to dB per second |
| 143 | return numberOfValidLocalMeasurements > 0 ? double (sumOfLocalAbsoluteSlopes) / numberOfValidLocalMeasurements : undefinedcxx . . $ undefined.; |
| 144 | } |
| 145 | |
| 146 | /* End of file Pitch_Intensity.cpp */ |