| 1 | /* PowerCepstrogram.cpp |
| 2 | * |
| 3 | * Copyright (C) 2013-2025 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "PowerCepstrogram.h" |
| 20 | #include "Cepstrum_and_Spectrum.h" |
| 21 | #include "Matrix_extensions.h" |
| 22 | #include "NUM2.h" |
| 23 | #include "Sound_and_Spectrum.h" |
| 24 | #include "Sound_extensions.h" |
| 25 | |
| 26 | |
| 27 | #define TO10LOG(x) ((10 / NUMln10) * log ((x) + 1e-30)) |
| 28 | |
| 29 | integer a = sizeof(struct structMatrix); |
| 30 | |
| 31 | Thing_implement (PowerCepstrogram, Matrix, 2); // derives from Matrix -> also version 2 |
| 32 | |
| 33 | double structPowerCepstrogram :: v_getValueAtSample (integer sampleNumber, integer row, int unit) const { |
| 34 | double result = undefined; |
| 35 | if (row >= 1 && row <= ny) { |
| 36 | if (unit == 0) |
| 37 | result = z [row] [sampleNumber]; |
| 38 | else |
| 39 | result = 10.0 * log10 (z [row] [sampleNumber] + 1e-30); // always positive |
| 40 | } |
| 41 | return result; |
| 42 | } |
| 43 | |
| 44 | autoPowerCepstrogram PowerCepstrogram_create (double tmin, double tmax, integer nt, double dt, double t1, |
| 45 | double qmin, double qmax, integer nq, double dq, double q1) { |
| 46 | try { |
| 47 | autoPowerCepstrogram me = Thing_new (PowerCepstrogram); |
| 48 | Matrix_init (me.get(), tmin, tmax, nt, dt, t1, qmin, qmax, nq, dq, q1); |
| 49 | return me; |
| 50 | } catch (MelderError) { |
| 51 | Melder_throw (U"PowerCepstrogram not created."); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | void PowerCepstrogram_paint (PowerCepstrogram me, Graphics g, double tmin, double tmax, double qmin, double qmax, double dBmaximum, int autoscaling, double dynamicRangedB, double dynamicCompression, bool garnish) { |
| 56 | Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 57 | SampledXY_unidirectionalAutowindowY (me, & qmin, & qmax); |
| 58 | integer itmin, itmax, ifmin, ifmax; |
| 59 | if (Matrix_getWindowSamplesX (me, tmin - 0.49999 * my dx, tmax + 0.49999 * my dx, & itmin, & itmax) == 0 || |
| 60 | Matrix_getWindowSamplesY (me, qmin - 0.49999 * my dy, qmax + 0.49999 * my dy, & ifmin, & ifmax) == 0) |
| 61 | return; |
| 62 | autoMatrix thee = Data_copy (me); |
| 63 | MelderExtremaWithInit extrema; |
| 64 | for (integer irow = 1; irow <= my ny; irow ++) { |
| 65 | for (integer icol = 1; icol <= my nx; icol ++) { |
| 66 | const double val = TO10LOG (my z [irow] [icol]); |
| 67 | extrema.update (val); |
| 68 | thy z [irow] [icol] = val; |
| 69 | } |
| 70 | } |
| 71 | double dBminimum = dBmaximum - dynamicRangedB; |
| 72 | if (autoscaling) { |
| 73 | dBminimum = extrema.min; |
| 74 | dBmaximum = extrema.max; |
| 75 | } |
| 76 | |
| 77 | for (integer icol = 1; icol <= my nx; icol ++) { |
| 78 | const double lmax = NUMmax_u (thy z.column (icol)); |
| 79 | if (isundef (lmax)) |
| 80 | return; |
| 81 | const double factor = dynamicCompression * (extrema.max - lmax); |
| 82 | thy z.column (icol) += factor; |
| 83 | } |
| 84 | |
| 85 | Graphics_setInner (g); |
| 86 | Graphics_setWindow (g, tmin, tmax, qmin, qmax); |
| 87 | Graphics_image (g, thy z.part (ifmin, ifmax, itmin, itmax), |
| 88 | Matrix_columnToX (thee.get(), itmin - 0.5), |
| 89 | Matrix_columnToX (thee.get(), itmax + 0.5), |
| 90 | Matrix_rowToY (thee.get(), ifmin - 0.5), |
| 91 | Matrix_rowToY (thee.get(), ifmax + 0.5), |
| 92 | dBminimum, dBmaximum); |
| 93 | |
| 94 | Graphics_unsetInner (g); |
| 95 | if (garnish) { |
| 96 | Graphics_drawInnerBox (g); |
| 97 | Graphics_textBottom (g, true, U"Time (s)"); |
| 98 | Graphics_marksBottom (g, 2, true, true, false); |
| 99 | Graphics_marksLeft (g, 2, true, true, false); |
| 100 | Graphics_textLeft (g, true, U"Quefrency (s)"); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void PowerCepstrogram_subtractTrend_inplace (mutablePowerCepstrogram me, double qminFit, double qmaxFit, |
| 105 | kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 106 | { |
| 107 | autoPowerCepstrogram thee = PowerCepstrogram_subtractTrend (me, qminFit, qmaxFit, lineType, fitMethod); |
| 108 | my z = copy_MAT (thy z.get()); |
| 109 | } |
| 110 | |
| 111 | autoPowerCepstrogram PowerCepstrogram_subtractTrend (constPowerCepstrogram me, double qminFit, double qmaxFit, kCepstrum_trendType trendLineType, kCepstrum_trendFit fitMethod) { |
| 112 | try { |
| 113 | autoPowerCepstrogram thee = PowerCepstrogram_create (my xmin, my xmax, my nx, my dx, my x1, |
| 114 | my ymin, my ymax, my ny, my dy, my y1); |
| 115 | const integer thresholdNumberOfFramesPerThread = 10; |
| 116 | |
| 117 | MelderThread_PARALLEL (my nx, thresholdNumberOfFramesPerThread) { |
| 118 | autoPowerCepstrum powerCepstrum = PowerCepstrum_create (my ymax, my ny); |
| 119 | autoPowerCepstrumWorkspace ws = PowerCepstrumWorkspace_create (powerCepstrum.get(), qminFit, qmaxFit, trendLineType, fitMethod); |
| 120 | MelderThread_FOR (iframe) { |
| 121 | powerCepstrum -> z.row (1) <<= my z.column (iframe); |
| 122 | ws -> newData (powerCepstrum.get()); |
| 123 | ws -> getSlopeAndIntercept (); |
| 124 | ws -> subtractTrend (); |
| 125 | thy z.column (iframe) <<= powerCepstrum -> z.row (1); |
| 126 | } |
| 127 | } MelderThread_ENDPARALLEL |
| 128 | return thee; |
| 129 | } catch (MelderError) { |
| 130 | Melder_throw (me, U": no trend subtracted."); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | autoTable PowerCepstrogram_to_Table_hillenbrand (PowerCepstrogram me, double pitchFloor, double pitchCeiling) { |
| 135 | try { |
| 136 | const conststring32 columnNames [] = { U"time(s)", U"quefrency(s)", U"CPP(dB)", U"f0(Hz)" }; |
| 137 | autoTable thee = Table_createWithColumnNames (my nx, ARRAY_TO_STRVEC (columnNames)); |
| 138 | autoPowerCepstrum him = PowerCepstrum_create (my ymax, my ny); |
| 139 | for (integer icol = 1; icol <= my nx; icol ++) { |
| 140 | his z.row (1) <<= my z.column (icol); |
| 141 | double qpeak; |
| 142 | const double cpp = PowerCepstrum_getPeakProminence_hillenbrand (him.get(), pitchFloor, pitchCeiling, qpeak); |
| 143 | const double time = Sampled_indexToX (me, icol); |
| 144 | Table_setNumericValue (thee.get(), icol, 1, time); |
| 145 | Table_setNumericValue (thee.get(), icol, 2, qpeak); |
| 146 | Table_setNumericValue (thee.get(), icol, 3, cpp); // Cepstrogram_getCPPS depends on this index 3!! |
| 147 | Table_setNumericValue (thee.get(), icol, 4, 1.0 / qpeak); |
| 148 | } |
| 149 | return thee; |
| 150 | } catch (MelderError) { |
| 151 | Melder_throw (me, U": no Table with cepstral peak prominence values created."); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | static void PowerCepstrogram_into_Matrix_CPP (PowerCepstrogram me, mutableMatrix thee, bool trendSubtracted, double pitchFloor, |
| 156 | double pitchCeiling, double deltaF0, kVector_peakInterpolation peakInterpolationType, double qminFit, double qmaxFit, |
| 157 | kCepstrum_trendType trendLineType, kCepstrum_trendFit fitMethod) |
| 158 | { |
| 159 | Melder_assert (thy ny == 6); |
| 160 | const integer numberOfFrames = thy nx, thresholdNumberOfFramesPerThread = 10; |
| 161 | const double qminPeakSearch = 1.0 / pitchCeiling, qmaxPeakSearch = 1.0 / pitchFloor; |
| 162 | MelderThread_PARALLEL (numberOfFrames, thresholdNumberOfFramesPerThread) { |
| 163 | autoPowerCepstrum powerCepstrum = PowerCepstrum_create (my ymax, my ny); |
| 164 | autoPowerCepstrumWorkspace ws = PowerCepstrumWorkspace_create (powerCepstrum.get(), qminFit, qmaxFit, trendLineType, fitMethod); |
| 165 | ws -> initPeakSearchPart (qminPeakSearch, qmaxPeakSearch, peakInterpolationType); |
| 166 | MelderThread_FOR (iframe) { |
| 167 | powerCepstrum -> z.row (1) <<= my z.column (iframe); |
| 168 | ws -> newData (powerCepstrum.get()); |
| 169 | ws -> trendSubtracted = trendSubtracted; |
| 170 | ws -> getSlopeAndIntercept (); |
| 171 | ws -> slopeKnown = true; |
| 172 | ws -> getPeakAndPosition (); |
| 173 | ws -> getCPP (); |
| 174 | thy z [1] [iframe] = Sampled_indexToX (thee, iframe); |
| 175 | thy z [2] [iframe] = ws -> slope; |
| 176 | thy z [3] [iframe] = ws -> intercept; |
| 177 | thy z [4] [iframe] = ws -> peakdB; |
| 178 | thy z [5] [iframe] = ws -> peakQuefrency; |
| 179 | thy z [6] [iframe] = ws -> cpp; |
| 180 | } |
| 181 | } MelderThread_ENDPARALLEL |
| 182 | } |
| 183 | |
| 184 | static autoMatrix PowerCepstrogram_to_Matrix_CPP (PowerCepstrogram me, bool trendSubtracted, double pitchFloor, double pitchCeiling, |
| 185 | double deltaF0, kVector_peakInterpolation peakInterpolationType, double qminFit, double qmaxFit, |
| 186 | kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 187 | { |
| 188 | try { |
| 189 | /* Matrix rows: time, cppRaw, slope, intercept, cppCorrected, peakQuefrency */ |
| 190 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, 6.5, 6, 1.0, 1.0); |
| 191 | PowerCepstrogram_into_Matrix_CPP (me, thee.get(), trendSubtracted, pitchFloor, pitchCeiling, deltaF0, peakInterpolationType, |
| 192 | qminFit, qmaxFit, lineType, fitMethod); |
| 193 | return thee; |
| 194 | } catch (MelderError) { |
| 195 | Melder_throw (me, U": could mot create Matrix with CPP values. "); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | autoTable PowerCepstrogram_to_Table_CPP (PowerCepstrogram me, bool includeFrameNumber, bool includeTime, |
| 200 | integer numberOfTimeDecimals, integer numberOfCPPdecimals, bool includePeakQuefrency, integer numberOfQuefrencyDecimals, |
| 201 | double pitchFloor, double pitchCeiling, double deltaF0, kVector_peakInterpolation peakInterpolationType, double qminFit, double qmaxFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) { |
| 202 | try { |
| 203 | autoTable thee = Table_createWithoutColumnNames (my nx, includeFrameNumber + includeTime + includePeakQuefrency + 1); |
| 204 | integer icol = 0; |
| 205 | if (includeFrameNumber) |
| 206 | Table_renameColumn_e (thee.get(), ++ icol, U"frame"); |
| 207 | if (includeTime) |
| 208 | Table_renameColumn_e (thee.get(), ++ icol, U"time(s)"); |
| 209 | if (includePeakQuefrency) |
| 210 | Table_renameColumn_e (thee.get(), ++ icol, U"quefrency(s)"); |
| 211 | Table_renameColumn_e (thee.get(), ++ icol, U"CPP(dB)"); |
| 212 | autoPowerCepstrum him = PowerCepstrum_create (my ymax, my ny); |
| 213 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 214 | icol = 0; |
| 215 | if (includeFrameNumber) |
| 216 | Table_setNumericValue (thee.get(), iframe, ++ icol, iframe); |
| 217 | if (includeTime) |
| 218 | Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (Sampled_indexToX (me, iframe), numberOfTimeDecimals)); |
| 219 | his z.row (1) <<= my z.column (iframe); |
| 220 | double peakQuefrency; |
| 221 | const double cpp = PowerCepstrum_getPeakProminence (him.get(), pitchFloor, pitchCeiling, peakInterpolationType, |
| 222 | qminFit, qmaxFit, lineType, fitMethod, peakQuefrency); |
| 223 | if (includePeakQuefrency) |
| 224 | Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (peakQuefrency, numberOfQuefrencyDecimals)); |
| 225 | Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (cpp, numberOfCPPdecimals)); |
| 226 | } |
| 227 | return thee; |
| 228 | } catch (MelderError) { |
| 229 | Melder_throw (me, U": no Table with cepstral peak prominence values created."); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | autoTable PowerCepstrogram_to_Table_CPPvalues (PowerCepstrogram me, double pitchFloor, double pitchCeiling, |
| 234 | double deltaF0, kVector_peakInterpolation peakInterpolationType, double qminFit, double qmaxFit, |
| 235 | kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 236 | { |
| 237 | try { |
| 238 | static const conststring32 colNames [] = { U"time(s)", U"dB/s", U"intercept(dB)", U"peak(dB)", U"quefrency(s)", U"cpp(dB)" }; |
| 239 | autoTable thee = Table_createWithColumnNames (my nx, ARRAY_TO_STRVEC (colNames)); |
| 240 | if (lineType == kCepstrum_trendType::EXPONENTIAL_DECAY) |
| 241 | Table_renameColumn_e (thee.get(), 2, U"dB/ln(s)"); |
| 242 | autoMatrix m = PowerCepstrogram_to_Matrix_CPP (me, false, pitchFloor, pitchCeiling, |
| 243 | deltaF0, peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 244 | Melder_assert (m -> nx == my nx && m -> ny == 6); |
| 245 | for (integer irow = 1; irow <= my nx; irow ++) { |
| 246 | for (integer icol = 1; icol <= m -> ny; icol ++) |
| 247 | Table_setNumericValue (thee.get(), irow, icol, m -> z [icol] [irow]); |
| 248 | } |
| 249 | return thee; |
| 250 | } catch (MelderError) { |
| 251 | Melder_throw (U"Could not create Table from PowerCepstrogram."); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | void PowerCepstrogram_listCPP (PowerCepstrogram me, bool includeFrameNumber, bool includeTime, |
| 256 | integer numberOfTimeDecimals, integer numberOfCPPdecimals, bool includePeakQuefrency, integer numberOfQuefrencyDecimals, |
| 257 | double pitchFloor, double pitchCeiling, double deltaF0, kVector_peakInterpolation peakInterpolationType, |
| 258 | double qminFit, double qmaxFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 259 | { |
| 260 | try { |
| 261 | autoTable table = PowerCepstrogram_to_Table_CPP (me, includeFrameNumber, includeTime, |
| 262 | numberOfTimeDecimals, numberOfCPPdecimals, includePeakQuefrency, numberOfQuefrencyDecimals, |
| 263 | pitchFloor, pitchCeiling, deltaF0, peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 264 | Table_list (table.get(), false); |
| 265 | } catch (MelderError) { |
| 266 | Melder_throw (me, U": CPP not listed."); |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | static autoPowerCepstrogram PowerCepstrogram_smoothRectangular (PowerCepstrogram me, double timeAveragingWindow, double quefrencyAveragingWindow) { |
| 271 | try { |
| 272 | autoPowerCepstrogram thee = Data_copy (me); |
| 273 | /* |
| 274 | 1. average across time |
| 275 | */ |
| 276 | const integer numberOfFrames = Melder_ifloor (timeAveragingWindow / my dx); |
| 277 | if (numberOfFrames > 1) { |
| 278 | const double halfWindow = 0.5 * timeAveragingWindow; |
| 279 | autoVEC qout = raw_VEC (my nx); |
| 280 | for (integer iq = 1; iq <= my ny; iq ++) { |
| 281 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 282 | const double xmid = Sampled_indexToX (me, iframe); |
| 283 | qout [iframe] = Sampled_getMean (me, xmid - halfWindow, xmid + halfWindow, iq, 0, true); |
| 284 | } |
| 285 | thy z.row (iq) <<= qout.all(); |
| 286 | } |
| 287 | } |
| 288 | /* |
| 289 | 2. average across quefrencies |
| 290 | */ |
| 291 | const integer numberOfQuefrencyBins = Melder_ifloor (quefrencyAveragingWindow / my dy); |
| 292 | if (numberOfQuefrencyBins > 1) { |
| 293 | autoPowerCepstrum smooth = PowerCepstrum_create (thy ymax, thy ny); |
| 294 | for (integer iframe = 1; iframe <= thy nx; iframe ++) { |
| 295 | smooth -> z.row (1) <<= thy z.column (iframe); |
| 296 | PowerCepstrum_smooth_inplace (smooth.get(), quefrencyAveragingWindow, 1); |
| 297 | thy z.column (iframe) <<= smooth -> z.row (1); |
| 298 | } |
| 299 | } |
| 300 | return thee; |
| 301 | } catch (MelderError) { |
| 302 | Melder_throw (me, U": not smoothed."); |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | static autoPowerCepstrogram PowerCepstrogram_smoothRectangular_old (PowerCepstrogram me, double timeAveragingWindow, double quefrencyAveragingWindow) { |
| 307 | try { |
| 308 | autoPowerCepstrogram thee = Data_copy (me); |
| 309 | /* |
| 310 | 1. average across time |
| 311 | */ |
| 312 | const integer numberOfFrames = Melder_ifloor (timeAveragingWindow / my dx); |
| 313 | if (numberOfFrames > 1) |
| 314 | for (integer iq = 1; iq <= my ny; iq ++) |
| 315 | VECsmoothByMovingAverage_preallocated (thy z.row (iq), my z.row (iq), numberOfFrames); |
| 316 | /* |
| 317 | 2. average across quefrencies |
| 318 | */ |
| 319 | const integer numberOfQuefrencyBins = Melder_ifloor (quefrencyAveragingWindow / my dy); |
| 320 | if (numberOfQuefrencyBins > 1) { |
| 321 | autoVEC qin = raw_VEC (thy ny); |
| 322 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 323 | qin.all() <<= thy z.column (iframe); |
| 324 | VECsmoothByMovingAverage_preallocated (thy z.column (iframe), qin.all(), numberOfQuefrencyBins); |
| 325 | } |
| 326 | } |
| 327 | return thee; |
| 328 | } catch (MelderError) { |
| 329 | Melder_throw (me, U": not smoothed."); |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | static autoPowerCepstrogram PowerCepstrogram_smoothGaussian (PowerCepstrogram me, double timeAveragingWindow, double quefrencyAveragingWindow) { |
| 334 | try { |
| 335 | autoPowerCepstrogram thee = Data_copy (me); |
| 336 | /* |
| 337 | 1. average across time |
| 338 | */ |
| 339 | const double numberOfSigmasInWindow = 4.0; |
| 340 | const double numberOfFrames = timeAveragingWindow / my dx; |
| 341 | if (numberOfFrames > 1.0) { |
| 342 | const double sigma = numberOfFrames / numberOfSigmasInWindow; // 2sigma -> 95.4%, 3sigma -> 99.7 % of the data |
| 343 | const integer nfft = Melder_clippedLeft (2_integer, Melder_iroundUpToPowerOfTwo (my nx)); // TODO: explain edge case |
| 344 | autoNUMFourierTable fourierTable = NUMFourierTable_create (nfft); |
| 345 | for (integer iq = 1; iq <= my ny; iq ++) { |
| 346 | VECsmooth_gaussian (thy z.row (iq), my z.row (iq), sigma, fourierTable.get()); |
| 347 | abs_VEC_inout (thy z.row (iq)); |
| 348 | } |
| 349 | } |
| 350 | /* |
| 351 | 2. average across quefrencies |
| 352 | */ |
| 353 | const double numberOfQuefrencyBins = quefrencyAveragingWindow / my dy; |
| 354 | if (numberOfQuefrencyBins > 1.0) { |
| 355 | const integer nfft = Melder_clippedLeft (2_integer, Melder_iroundUpToPowerOfTwo (my ny)); // TODO: explain edge case |
| 356 | autoNUMFourierTable fourierTable = NUMFourierTable_create (nfft); |
| 357 | const double sigma = numberOfQuefrencyBins / numberOfSigmasInWindow; // 2sigma -> 95.4%, 3sigma -> 99.7 % of the data |
| 358 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 359 | VECsmooth_gaussian_inplace (thy z.column (iframe), sigma, fourierTable.get()); |
| 360 | abs_VEC_inout (thy z.column (iframe)); |
| 361 | } |
| 362 | } |
| 363 | return thee; |
| 364 | } catch (MelderError) { |
| 365 | Melder_throw (me, U": not smoothed."); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | autoPowerCepstrogram PowerCepstrogram_smooth (PowerCepstrogram me, double timeAveragingWindow, double quefrencyAveragingWindow) { |
| 370 | if (Melder_debug == -4) |
| 371 | return PowerCepstrogram_smoothRectangular_old (me, timeAveragingWindow, quefrencyAveragingWindow); |
| 372 | else if (Melder_debug == -5) |
| 373 | return PowerCepstrogram_smoothGaussian (me, timeAveragingWindow, quefrencyAveragingWindow); |
| 374 | else |
| 375 | return PowerCepstrogram_smoothRectangular (me, timeAveragingWindow, quefrencyAveragingWindow); |
| 376 | } |
| 377 | |
| 378 | autoMatrix PowerCepstrogram_to_Matrix (PowerCepstrogram me) { |
| 379 | try { |
| 380 | autoMatrix thee = Thing_new (Matrix); |
| 381 | my structMatrix :: v1_copy (thee.get()); |
| 382 | return thee; |
| 383 | } catch (MelderError) { |
| 384 | Melder_throw (me, U": not converted to Matrix."); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | autoPowerCepstrum PowerCepstrogram_to_PowerCepstrum_slice (PowerCepstrogram me, double time) { |
| 389 | try { |
| 390 | integer iframe = Sampled_xToNearestIndex (me, time); |
| 391 | iframe = iframe < 1 ? 1 : iframe > my nx ? my nx : iframe; |
| 392 | autoPowerCepstrum thee = PowerCepstrum_create (my ymax, my ny); |
| 393 | thy z.row (1) <<= my z.column (iframe); |
| 394 | return thee; |
| 395 | } catch (MelderError) { |
| 396 | Melder_throw (me, U": Cepstrum not extracted."); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | autoPowerCepstrogram Matrix_to_PowerCepstrogram (Matrix me) { |
| 401 | try { |
| 402 | autoPowerCepstrogram thee = Thing_new (PowerCepstrogram); |
| 403 | my structMatrix :: v1_copy (thee.get()); |
| 404 | return thee; |
| 405 | } catch (MelderError) { |
| 406 | Melder_throw (me, U": not converted to PowerCepstrogram."); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | double PowerCepstrogram_getCPPS (PowerCepstrogram me, bool subtractTrendBeforeSmoothing, double timeAveragingWindow, double quefrencyAveragingWindow, double pitchFloor, double pitchCeiling, double deltaF0, kVector_peakInterpolation peakInterpolationType, double qminFit, double qmaxFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) { |
| 414 | try { |
| 415 | autoPowerCepstrogram flattened; |
| 416 | bool trendSubtracted = subtractTrendBeforeSmoothing; |
| 417 | if (subtractTrendBeforeSmoothing) |
| 418 | flattened = PowerCepstrogram_subtractTrend (me, qminFit, qmaxFit, lineType, fitMethod); |
| 419 | |
| 420 | autoPowerCepstrogram smooth = PowerCepstrogram_smooth (subtractTrendBeforeSmoothing ? flattened.get() : me, timeAveragingWindow, quefrencyAveragingWindow); |
| 421 | if (Melder_debug == -6) { // old algorithm |
| 422 | autoTable table = PowerCepstrogram_to_Table_CPP (smooth.get(), false, false, 6, 16, false, 6, pitchFloor, pitchCeiling, |
| 423 | deltaF0, peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 424 | const double cpps = Table_getMean (table.get(), 1); // no frame number, no time, quefrency |
| 425 | return cpps; |
| 426 | } else { |
| 427 | autoMatrix cpp = PowerCepstrogram_to_Matrix_CPP (smooth.get(), trendSubtracted, pitchFloor, pitchCeiling, deltaF0, |
| 428 | peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 429 | const double cpps = Matrix_getMean (cpp.get(), cpp -> xmin, cpp -> xmax, 5.5, 6.5); // TODO Sampled_getMean?? |
| 430 | return cpps; |
| 431 | } |
| 432 | } catch (MelderError) { |
| 433 | Melder_throw (me, U": no CPPS value calculated."); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | double PowerCepstrogram_getCPPS_hillenbrand (PowerCepstrogram me, bool subtractTrendBeforeSmoothing, double timeAveragingWindow, double quefrencyAveragingWindow, double pitchFloor, double pitchCeiling) { |
| 438 | try { |
| 439 | autoPowerCepstrogram him; |
| 440 | if (subtractTrendBeforeSmoothing) |
| 441 | him = PowerCepstrogram_subtractTrend (me, 0.001, 0, kCepstrum_trendType::LINEAR, kCepstrum_trendFit::LEAST_SQUARES); |
| 442 | |
| 443 | autoPowerCepstrogram smooth = PowerCepstrogram_smooth (subtractTrendBeforeSmoothing ? him.get() : me, timeAveragingWindow, quefrencyAveragingWindow); |
| 444 | autoTable table = PowerCepstrogram_to_Table_hillenbrand (smooth.get(), pitchFloor, pitchCeiling); |
| 445 | const double cpps = Table_getMean (table.get(), 3); |
| 446 | return cpps; |
| 447 | } catch (MelderError) { |
| 448 | Melder_throw (me, U": no CPPS value calculated."); |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | /* End of file PowerCepstrogram.cpp */ |