| 1 | /* Electroglottogram.cpp |
| 2 | * |
| 3 | * Copyright (C) 2019-2023 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 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 "AmplitudeTier.h" |
| 20 | #include "Electroglottogram.h" |
| 21 | #include "Sound_and_Spectrum.h" |
| 22 | #include "Sound_extensions.h" |
| 23 | #include "Sound_to_PointProcess.h" |
| 24 | #include "Vector_extensions.h" |
| 25 | |
| 26 | #include "enums_getText.h" |
| 27 | #include "Electroglottogram_enums.h" |
| 28 | #include "enums_getValue.h" |
| 29 | #include "Electroglottogram_enums.h" |
| 30 | |
| 31 | Thing_implement (Electroglottogram, Sound, 2); |
| 32 | |
| 33 | void Electroglottogram_drawStylized (Graphics g, bool marks, bool levels) { |
| 34 | Graphics_setFontSize (g, 10.0); |
| 35 | Graphics_setInner (g); |
| 36 | const double xmax = 3.9; |
| 37 | Graphics_setWindow (g, 0.0, xmax, 0, 2.0); |
| 38 | constexpr integer numberOfPoints = 6; |
| 39 | const double x [numberOfPoints] = { 0.55, 1.0, 1.3, 2.0, 2.75, 3.1 }; |
| 40 | const double y [numberOfPoints] = { 0.10, 0.3, 1.8, 1.8, 1.00, 0.1 }; |
| 41 | const double maximum = y [3], minimum = y [0]; |
| 42 | const double range = maximum - minimum; |
| 43 | conststring32 labels [numberOfPoints] = { U"a", U"b", U"c", U"d", U"e", U"f" }; |
| 44 | const double width = Graphics_textWidth (g, U"a"); |
| 45 | const double h = 2.0 * width; |
| 46 | const double dx [numberOfPoints] = { 0.0, -width, width, -0.5 * width, 0.5 * width, 0.5 * width}; |
| 47 | const double dy [numberOfPoints] = { h, h, -h, -h, h, h }; |
| 48 | double tx [numberOfPoints], ty [numberOfPoints]; |
| 49 | for (integer i = 0; i < numberOfPoints; i++) { |
| 50 | tx [i] = x [i] + dx [i]; |
| 51 | ty [i] = y [i] + dy [i]; |
| 52 | } |
| 53 | const double lineWidth = Graphics_inqLineWidth (g); |
| 54 | Graphics_setLineWidth (g, 3.0); |
| 55 | Graphics_line (g, 0.2, 0.1, x [0], y [0]); |
| 56 | for (integer i = 1; i < numberOfPoints; i ++) |
| 57 | Graphics_line (g, x [i - 1], y [i - 1], x [i], y [i]); |
| 58 | Graphics_line (g, x [5], y [5], 3.5, 0.1); |
| 59 | Graphics_setLineWidth (g, lineWidth); |
| 60 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 61 | if (marks) { |
| 62 | Graphics_setFontSize (g, 12.0); |
| 63 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 64 | for (integer i = 0; i < numberOfPoints; i++) |
| 65 | Graphics_text (g, tx [i], ty [i], labels [i]); |
| 66 | Graphics_setFontSize (g, 10.0); |
| 67 | } |
| 68 | const double closingThreshold = 0.3; |
| 69 | const double at03 = minimum + closingThreshold * range; |
| 70 | // Get the two x values of intersection at y = at03 with the EGG |
| 71 | double a = (y [1] - y [2]) / (x [1] - x [2]), b = y [1] - a * x [1]; |
| 72 | const double x1 = (at03 - b) / a; |
| 73 | a = (y [4] - y [5]) / (x [4] - x [5]); |
| 74 | b = y [4] - a * x [4]; |
| 75 | const double x2 = (at03 - b) / a; |
| 76 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::LEFT, Graphics_HALF); |
| 77 | if (levels) { |
| 78 | Graphics_setLineType (g, Graphics_DASHED); |
| 79 | Graphics_doubleArrow (g, x1, at03, x2, at03); |
| 80 | const double x3 = x [2] + 0.1; |
| 81 | Graphics_doubleArrow (g, x3, minimum, x3, at03); |
| 82 | Graphics_setLineType (g, Graphics_DRAWN); |
| 83 | Graphics_text (g, x3 + width, 0.5 * at03, U"0.3(%%Peak%\\--%%Valley%)"); |
| 84 | Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_BOTTOM); |
| 85 | Graphics_text (g, 0.5 * (x1 + x2), at03, U"Closed Glottis Interval"); |
| 86 | } |
| 87 | Graphics_unsetInner (g); |
| 88 | if (levels) { |
| 89 | Graphics_markRight (g, maximum, false, true, true, U"%Peak"); |
| 90 | Graphics_markRight (g, minimum, false, true, true, U"%Valley"); |
| 91 | Graphics_markRight (g, at03, false, true, false, U"%%closingThreshold% (0.3)"); |
| 92 | Graphics_markBottom (g, x1, false, true, true, U"%t__1_"); |
| 93 | Graphics_markBottom (g, x2, false, true, true, U"%t__2_"); |
| 94 | } |
| 95 | Graphics_textLeft (g, false, U"norm. VFCA"); |
| 96 | Graphics_textBottom (g, true, U"norm. cycle progress"); |
| 97 | Graphics_drawInnerBox (g); |
| 98 | } |
| 99 | |
| 100 | static void IntervalTier_insertBoundary (IntervalTier me, double t) { |
| 101 | try { |
| 102 | Melder_require (! IntervalTier_hasTime (me, t), |
| 103 | U"Cannot add a boundary at ", Melder_fixed (t, 6), U" seconds, because there is already a boundary there."); |
| 104 | const integer intervalNumber = IntervalTier_timeToIndex (me, t); |
| 105 | Melder_require (intervalNumber != 0, |
| 106 | U"Cannot add a boundary at ", Melder_fixed (t, 6), U" seconds, because this is outside the time domain of the intervals."); |
| 107 | const TextInterval interval = my intervals.at [intervalNumber]; |
| 108 | /* |
| 109 | Move the text to the left of the boundary. |
| 110 | */ |
| 111 | autoTextInterval newInterval = TextInterval_create (t, interval -> xmax, U""); |
| 112 | interval -> xmax = t; |
| 113 | my intervals. addItem_move (newInterval.move()); |
| 114 | } catch (MelderError) { |
| 115 | Melder_throw (me, U": boundary not inserted."); |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | void IntervalTier_setIntervalText (IntervalTier me, integer intervalNumber, conststring32 text) { |
| 120 | try { |
| 121 | Melder_require (intervalNumber >= 1 && intervalNumber <= my intervals.size, |
| 122 | U"Interval ", intervalNumber, U" does not exist."); |
| 123 | const TextInterval interval = my intervals.at [intervalNumber]; |
| 124 | TextInterval_setText (interval, text); |
| 125 | } catch (MelderError) { |
| 126 | Melder_throw (me, U": interval text not set."); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void structElectroglottogram :: v1_info () { |
| 131 | structDaata :: v1_info (); |
| 132 | MelderInfo_writeLine (U"Time domain:"); |
| 133 | MelderInfo_writeLine (U" Start time: ", our xmin, U" seconds"); |
| 134 | MelderInfo_writeLine (U" End time: ", our xmax, U" seconds"); |
| 135 | MelderInfo_writeLine (U" Total duration: ", our xmax - our xmin, U" seconds"); |
| 136 | MelderInfo_writeLine (U"Time sampling:"); |
| 137 | MelderInfo_writeLine (U" Number of samples: ", our nx); |
| 138 | MelderInfo_writeLine (U" Sampling period: ", our dx, U" seconds"); |
| 139 | MelderInfo_writeLine (U" Sampling frequency: ", Melder_single (1.0 / our dx), U" Hz"); |
| 140 | MelderInfo_writeLine (U" First sample centred at: ", our x1, U" seconds"); |
| 141 | } |
| 142 | |
| 143 | autoElectroglottogram Electroglottogram_create (double xmin, double xmax, integer nx, double dx, double x1) { |
| 144 | try { |
| 145 | autoElectroglottogram me = Thing_new (Electroglottogram); |
| 146 | Matrix_init (me.get(), xmin, xmax, nx, dx, x1, 1.0, 1.0, 1.0, 1, 1.0); |
| 147 | return me; |
| 148 | } catch (MelderError) { |
| 149 | Melder_throw (U"Electroglottogram not created."); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | autoElectroglottogram Sound_extractElectroglottogram (Sound me, integer channel, bool invert) { |
| 154 | try { |
| 155 | Melder_require (channel > 0 && channel <= my ny, |
| 156 | U"The channel number should be in the interval from 1 to ", my ny); |
| 157 | autoElectroglottogram thee = Electroglottogram_create (my xmin, my xmax, my nx, my dx, my x1); |
| 158 | thy z.all() <<= my z.row (channel); |
| 159 | if (invert) |
| 160 | thy z.all() *= -1.0; |
| 161 | return thee; |
| 162 | } catch (MelderError) { |
| 163 | Melder_throw (me, U": not converted to Electroglottogram."); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | autoAmplitudeTier Electroglottogram_to_AmplitudeTier_levels (Electroglottogram me, double pitchFloor, double pitchCeiling, double closingThreshold, autoAmplitudeTier *out_peaks, autoAmplitudeTier *out_valleys) { |
| 168 | try { |
| 169 | autoPointProcess peakPositions = Sound_to_PointProcess_periodic_peaks (me, pitchFloor, pitchCeiling, true, false); |
| 170 | autoPointProcess valleyPositions = Sound_to_PointProcess_periodic_peaks (me, pitchFloor, pitchCeiling, false, true); |
| 171 | /* |
| 172 | Get the values of the peaks and valleys |
| 173 | */ |
| 174 | autoAmplitudeTier peaks = PointProcess_Sound_to_AmplitudeTier_point (peakPositions.get(), me); |
| 175 | autoAmplitudeTier valleys = PointProcess_Sound_to_AmplitudeTier_point (valleyPositions.get(), me); |
| 176 | autoAmplitudeTier thee = Electroglottogram_and_AmplitudeTiers_getLevels (me, peaks.get(), valleys.get(), closingThreshold); |
| 177 | if (out_peaks) |
| 178 | *out_peaks = peaks.move(); |
| 179 | if (out_valleys) |
| 180 | *out_valleys = valleys.move(); |
| 181 | return thee; |
| 182 | } catch (MelderError) { |
| 183 | Melder_throw (me, U": AmplitudeTier not created."); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | autoAmplitudeTier Electroglottogram_and_AmplitudeTiers_getLevels (Electroglottogram me, AmplitudeTier peaks, AmplitudeTier valleys, double closingThreshold) { |
| 188 | try { |
| 189 | Melder_require (my xmin == peaks -> xmin && my xmax == peaks -> xmax, |
| 190 | U"The domains of the Electroglottogram and the peaks should be equal."); |
| 191 | Melder_require (my xmin == valleys -> xmin && my xmax == valleys -> xmax, |
| 192 | U"The domains of the Electroglottogram and the valleys should be equal."); |
| 193 | Melder_require (peaks -> points.size > 1 && valleys -> points.size > 1, |
| 194 | U"The AmplitudeTiers cannot be empty."); |
| 195 | Melder_require (closingThreshold > 0.0 && closingThreshold < 1.0, |
| 196 | U"The closing threshold should be a number between 0.0 and 1.0."); |
| 197 | autoAmplitudeTier thee = AmplitudeTier_create (my xmin, my xmax); |
| 198 | double peakAmplitudeLeft = RealTier_getValueAtIndex (peaks, 1); |
| 199 | double peakTimeLeft = peaks -> points.at [1] -> number; |
| 200 | for (integer ipoint = 2; ipoint <= peaks -> points.size - 1; ipoint ++) { |
| 201 | const double peakAmplitudeRight = RealTier_getValueAtIndex (peaks, ipoint); |
| 202 | const double peakTimeRight = peaks -> points.at [ipoint] -> number; |
| 203 | const integer indexValley = AnyTier_timeToNearestIndex (valleys->asAnyTier(), peakTimeRight); |
| 204 | const double timeValley = valleys -> points.at [indexValley] -> number; |
| 205 | if (timeValley > peakTimeLeft && timeValley < peakTimeRight) { |
| 206 | const double valleyAmplitude = RealTier_getValueAtIndex (valleys, indexValley); |
| 207 | const double amplitudeRange = peakAmplitudeLeft - valleyAmplitude; |
| 208 | const double level = valleyAmplitude + amplitudeRange * closingThreshold; |
| 209 | RealTier_addPoint (thee.get(), peakTimeLeft, level); |
| 210 | } |
| 211 | peakAmplitudeLeft = peakAmplitudeRight; |
| 212 | peakTimeLeft = peakTimeRight; |
| 213 | } |
| 214 | return thee; |
| 215 | } catch (MelderError) { |
| 216 | Melder_throw (me, U": Levels not determined."); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | autoTextGrid Electroglottogram_to_TextGrid_closedGlottis (Electroglottogram me, double pitchFloor, double pitchCeiling, double closingThreshold, double peakThresholdFraction) { |
| 221 | try { |
| 222 | autoAmplitudeTier peaks, valleys; |
| 223 | autoAmplitudeTier levels = Electroglottogram_to_AmplitudeTier_levels (me, pitchFloor, pitchCeiling, closingThreshold, & peaks, & valleys); |
| 224 | |
| 225 | const double maximum = RealTier_getMaximumValue (peaks.get()); |
| 226 | const double minimumPeakAmplitude = maximum * peakThresholdFraction; |
| 227 | |
| 228 | autoTextGrid thee = TextGrid_create (my xmin, my xmax, U"closedGlottis", U""); |
| 229 | IntervalTier intervalTier = static_cast <IntervalTier> (thy tiers -> at [1]); |
| 230 | double previousOpeningTime = my xmin; |
| 231 | for (integer ipoint = 1; ipoint <= peaks -> points.size; ipoint ++) { |
| 232 | const RealPoint peak = peaks -> points.at [ipoint]; |
| 233 | const double peakPosition = peak -> number; |
| 234 | const double peakAmplitude = peak -> value; |
| 235 | double closingTime = undefined, openingTime = undefined; |
| 236 | if (peakAmplitude > minimumPeakAmplitude) { |
| 237 | const double level = RealTier_getValueAtTime (levels.get(), peakPosition); |
| 238 | closingTime = Vector_getNearestLevelCrossing (me, 1, peakPosition, level, kVectorSearchDirection::LEFT); |
| 239 | openingTime = Vector_getNearestLevelCrossing (me, 1, peakPosition, level, kVectorSearchDirection::RIGHT); |
| 240 | if (isdefined (closingTime) && isdefined (openingTime) && closingTime != previousOpeningTime) { |
| 241 | IntervalTier_insertBoundary (intervalTier, closingTime); |
| 242 | IntervalTier_insertBoundary (intervalTier, openingTime); |
| 243 | const double midPoint = 0.5 * (closingTime + openingTime); |
| 244 | const integer intervalNumber = IntervalTier_timeToIndex (intervalTier, midPoint); |
| 245 | IntervalTier_setIntervalText (intervalTier, intervalNumber, U"c"); |
| 246 | previousOpeningTime = openingTime; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | return thee; |
| 251 | } catch (MelderError) { |
| 252 | Melder_throw (me, U": TextTier not created."); |
| 253 | } |
| 254 | |
| 255 | } |
| 256 | |
| 257 | autoSound Electroglottogram_firstCentralDifference (Electroglottogram me, double peakAmplitude) { |
| 258 | try { |
| 259 | autoSound thee = Sound_create (1, my xmin, my xmax, my nx, my dx, my x1); |
| 260 | thy z [1] [1] = 0.0; |
| 261 | for (integer i = 2; i < my nx; i ++) |
| 262 | thy z [1] [i] = (my z [1] [i + 1] - my z [1] [i - 1]) / (2.0 * my dx); |
| 263 | |
| 264 | thy z [1] [my nx] = 0.0; |
| 265 | if (peakAmplitude != 0.0) |
| 266 | Vector_scale (thee.get(), peakAmplitude); |
| 267 | return thee; |
| 268 | } catch (MelderError) { |
| 269 | Melder_throw (me, U": cannot create the simple derivative of the Electroglottogram."); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | Derivative of x(t) = integral (X(f)exp(2*pi*f*t) * df) |
| 275 | d(x(t)/dt = integral (X(f)*2*pi*i*exp(2*pi*f*t) * df) |
| 276 | = integral ((-2*pi*f*Im(X(f)), 2*pi*f*Re(X(f))) * exp (2*pi*f*t) * df) |
| 277 | */ |
| 278 | autoSound Electroglottogram_derivative (Electroglottogram me, double lowPassFrequency, double smoothing, double peakAmplitude) { |
| 279 | try { |
| 280 | autoSpectrum thee = Sound_to_Spectrum (me, false); |
| 281 | for (integer ifreq = 1; ifreq <= thy nx; ifreq ++) { |
| 282 | const double frequency = Sampled_indexToX (thee.get(), ifreq); |
| 283 | const double im = thy z [2] [ifreq]; |
| 284 | thy z [2] [ifreq] = NUM2pi * frequency * thy z [1] [ifreq]; |
| 285 | thy z [1] [ifreq] = - NUM2pi * frequency * im; |
| 286 | } |
| 287 | Spectrum_passHannBand (thee.get(), 0.0, lowPassFrequency, smoothing); |
| 288 | autoSound him = Spectrum_to_Sound (thee.get()); |
| 289 | if (peakAmplitude != 0.0) |
| 290 | Vector_scale (him.get(), peakAmplitude); |
| 291 | return him; |
| 292 | } catch (MelderError) { |
| 293 | Melder_throw (me, U": cannot create the derivative of the Electroglottogram."); |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | autoElectroglottogram Electroglottogram_highPassFilter (Electroglottogram me, double fromFrequency, double smoothing) { |
| 298 | try { |
| 299 | autoElectroglottogram thee = Data_copy (me); |
| 300 | autoSpectrum spec = Sound_to_Spectrum (me, true); |
| 301 | Spectrum_passHannBand (spec.get(), fromFrequency, spec -> xmax, smoothing); |
| 302 | autoSound him = Spectrum_to_Sound (spec.get()); |
| 303 | thy z.row (1) <<= his z.row (1).part (1, thy nx); |
| 304 | return thee; |
| 305 | } catch (MelderError) { |
| 306 | Melder_throw (me, U": not high-pass filered."); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | autoSound Electroglottogram_to_Sound (Electroglottogram me) { |
| 311 | try { |
| 312 | autoSound thee = Sound_create (1, my xmin, my xmax, my nx, my dx, my x1); |
| 313 | thy z.row (1) <<= my z.row (1); |
| 314 | return thee; |
| 315 | } catch (MelderError) { |
| 316 | Melder_throw (me, U": not converted to Sound."); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | /* End of file Electroglottogram.cpp */ |