| 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 |
autoCPP PowerCepstrogram_to_CPP (PowerCepstrogram me, double pitchFloor, double pitchCeiling, |
| 200 |
kVector_peakInterpolation peakInterpolationType, double qminFit, double qmaxFit, |
| 201 |
kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 202 |
{ |
| 203 |
try { |
| 204 |
autoMatrix mat = PowerCepstrogram_to_Matrix_CPP (me, false, pitchFloor, pitchCeiling, |
| 205 |
0.05, peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 206 |
autoCPP thee = CPP_create (my xmin, my xmax, my nx, my dx, my x1); |
| 207 |
for (integer iframe = 1; iframe <= my nx; iframe ++) |
| 208 |
thy z[1] [iframe] = mat -> z [6][iframe]; |
| 209 |
return thee; |
| 210 |
} catch (MelderError) { |
| 211 |
Melder_throw (me, U"no CPP created."); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
autoTable PowerCepstrogram_to_Table_CPP (PowerCepstrogram me, bool includeFrameNumber, bool includeTime, |
| 216 |
integer numberOfTimeDecimals, integer numberOfCPPdecimals, bool includePeakQuefrency, integer numberOfQuefrencyDecimals, |
| 217 |
double pitchFloor, double pitchCeiling, double deltaF0, kVector_peakInterpolation peakInterpolationType, double qminFit, double qmaxFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) { |
| 218 |
try { |
| 219 |
autoTable thee = Table_createWithoutColumnNames (my nx, includeFrameNumber + includeTime + includePeakQuefrency + 1); |
| 220 |
integer icol = 0; |
| 221 |
if (includeFrameNumber) |
| 222 |
Table_renameColumn_e (thee.get(), ++ icol, U"frame"); |
| 223 |
if (includeTime) |
| 224 |
Table_renameColumn_e (thee.get(), ++ icol, U"time(s)"); |
| 225 |
if (includePeakQuefrency) |
| 226 |
Table_renameColumn_e (thee.get(), ++ icol, U"quefrency(s)"); |
| 227 |
Table_renameColumn_e (thee.get(), ++ icol, U"CPP(dB)"); |
| 228 |
autoPowerCepstrum him = PowerCepstrum_create (my ymax, my ny); |
| 229 |
for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 230 |
icol = 0; |
| 231 |
if (includeFrameNumber) |
| 232 |
Table_setNumericValue (thee.get(), iframe, ++ icol, iframe); |
| 233 |
if (includeTime) |
| 234 |
Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (Sampled_indexToX (me, iframe), numberOfTimeDecimals)); |
| 235 |
his z.row (1) <<= my z.column (iframe); |
| 236 |
double peakQuefrency; |
| 237 |
const double cpp = PowerCepstrum_getPeakProminence (him.get(), pitchFloor, pitchCeiling, peakInterpolationType, |
| 238 |
qminFit, qmaxFit, lineType, fitMethod, peakQuefrency); |
| 239 |
if (includePeakQuefrency) |
| 240 |
Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (peakQuefrency, numberOfQuefrencyDecimals)); |
| 241 |
Table_setStringValue (thee.get(), iframe, ++ icol, Melder_fixed (cpp, numberOfCPPdecimals)); |
| 242 |
} |
| 243 |
return thee; |
| 244 |
} catch (MelderError) { |
| 245 |
Melder_throw (me, U": no Table with cepstral peak prominence values created."); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
autoTable PowerCepstrogram_to_Table_CPPvalues (PowerCepstrogram me, double pitchFloor, double pitchCeiling, |
| 250 |
double deltaF0, kVector_peakInterpolation peakInterpolationType, double qminFit, double qmaxFit, |
| 251 |
kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 252 |
{ |
| 253 |
try { |
| 254 |
static const conststring32 colNames [] = { U"time(s)", U"dB/s", U"intercept(dB)", U"peak(dB)", U"quefrency(s)", U"cpp(dB)" }; |
| 255 |
autoTable thee = Table_createWithColumnNames (my nx, ARRAY_TO_STRVEC (colNames)); |
| 256 |
if (lineType == kCepstrum_trendType::EXPONENTIAL_DECAY) |
| 257 |
Table_renameColumn_e (thee.get(), 2, U"dB/ln(s)"); |
| 258 |
autoMatrix m = PowerCepstrogram_to_Matrix_CPP (me, false, pitchFloor, pitchCeiling, |
| 259 |
deltaF0, peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 260 |
Melder_assert (m -> nx == my nx && m -> ny == 6); |
| 261 |
for (integer irow = 1; irow <= my nx; irow ++) { |
| 262 |
for (integer icol = 1; icol <= m -> ny; icol ++) |
| 263 |
Table_setNumericValue (thee.get(), irow, icol, m -> z [icol] [irow]); |
| 264 |
} |
| 265 |
return thee; |
| 266 |
} catch (MelderError) { |
| 267 |
Melder_throw (U"Could not create Table from PowerCepstrogram."); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
void PowerCepstrogram_listCPP (PowerCepstrogram me, bool includeFrameNumber, bool includeTime, |
| 272 |
integer numberOfTimeDecimals, integer numberOfCPPdecimals, bool includePeakQuefrency, integer numberOfQuefrencyDecimals, |
| 273 |
double pitchFloor, double pitchCeiling, double deltaF0, kVector_peakInterpolation peakInterpolationType, |
| 274 |
double qminFit, double qmaxFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 275 |
{ |
| 276 |
try { |
| 277 |
autoTable table = PowerCepstrogram_to_Table_CPP (me, includeFrameNumber, includeTime, |
| 278 |
numberOfTimeDecimals, numberOfCPPdecimals, includePeakQuefrency, numberOfQuefrencyDecimals, |
| 279 |
pitchFloor, pitchCeiling, deltaF0, peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 280 |
Table_list (table.get(), false); |
| 281 |
} catch (MelderError) { |
| 282 |
Melder_throw (me, U": CPP not listed."); |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
static autoPowerCepstrogram PowerCepstrogram_smoothRectangular (PowerCepstrogram me, double timeAveragingWindow, double quefrencyAveragingWindow) { |
| 287 |
try { |
| 288 |
autoPowerCepstrogram thee = Data_copy (me); |
| 289 |
/* |
| 290 |
1. average across time |
| 291 |
*/ |
| 292 |
const integer numberOfFrames = Melder_ifloor (timeAveragingWindow / my dx); |
| 293 |
if (numberOfFrames > 1) { |
| 294 |
const double halfWindow = 0.5 * timeAveragingWindow; |
| 295 |
autoVEC qout = raw_VEC (my nx); |
| 296 |
for (integer iq = 1; iq <= my ny; iq ++) { |
| 297 |
for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 298 |
const double xmid = Sampled_indexToX (me, iframe); |
| 299 |
qout [iframe] = Sampled_getMean (me, xmid - halfWindow, xmid + halfWindow, iq, 0, true); |
| 300 |
} |
| 301 |
thy z.row (iq) <<= qout.all(); |
| 302 |
} |
| 303 |
} |
| 304 |
/* |
| 305 |
2. average across quefrencies |
| 306 |
*/ |
| 307 |
const integer numberOfQuefrencyBins = Melder_ifloor (quefrencyAveragingWindow / my dy); |
| 308 |
if (numberOfQuefrencyBins > 1) { |
| 309 |
autoPowerCepstrum smooth = PowerCepstrum_create (thy ymax, thy ny); |
| 310 |
for (integer iframe = 1; iframe <= thy nx; iframe ++) { |
| 311 |
smooth -> z.row (1) <<= thy z.column (iframe); |
| 312 |
PowerCepstrum_smooth_inplace (smooth.get(), quefrencyAveragingWindow, 1); |
| 313 |
thy z.column (iframe) <<= smooth -> z.row (1); |
| 314 |
} |
| 315 |
} |
| 316 |
return thee; |
| 317 |
} catch (MelderError) { |
| 318 |
Melder_throw (me, U": not smoothed."); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
static autoPowerCepstrogram PowerCepstrogram_smoothRectangular_old (PowerCepstrogram me, double timeAveragingWindow, double quefrencyAveragingWindow) { |
| 323 |
try { |
| 324 |
autoPowerCepstrogram thee = Data_copy (me); |
| 325 |
/* |
| 326 |
1. average across time |
| 327 |
*/ |
| 328 |
const integer numberOfFrames = Melder_ifloor (timeAveragingWindow / my dx); |
| 329 |
if (numberOfFrames > 1) |
| 330 |
for (integer iq = 1; iq <= my ny; iq ++) |
| 331 |
VECsmoothByMovingAverage_preallocated (thy z.row (iq), my z.row (iq), numberOfFrames); |
| 332 |
/* |
| 333 |
2. average across quefrencies |
| 334 |
*/ |
| 335 |
const integer numberOfQuefrencyBins = Melder_ifloor (quefrencyAveragingWindow / my dy); |
| 336 |
if (numberOfQuefrencyBins > 1) { |
| 337 |
autoVEC qin = raw_VEC (thy ny); |
| 338 |
for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 339 |
qin.all() <<= thy z.column (iframe); |
| 340 |
VECsmoothByMovingAverage_preallocated (thy z.column (iframe), qin.all(), numberOfQuefrencyBins); |
| 341 |
} |
| 342 |
} |
| 343 |
return thee; |
| 344 |
} catch (MelderError) { |
| 345 |
Melder_throw (me, U": not smoothed."); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
static autoPowerCepstrogram PowerCepstrogram_smoothGaussian (PowerCepstrogram me, double timeAveragingWindow, double quefrencyAveragingWindow) { |
| 350 |
try { |
| 351 |
autoPowerCepstrogram thee = Data_copy (me); |
| 352 |
/* |
| 353 |
1. average across time |
| 354 |
*/ |
| 355 |
const double numberOfSigmasInWindow = 4.0; |
| 356 |
const double numberOfFrames = timeAveragingWindow / my dx; |
| 357 |
if (numberOfFrames > 1.0) { |
| 358 |
const double sigma = numberOfFrames / numberOfSigmasInWindow; // 2sigma -> 95.4%, 3sigma -> 99.7 % of the data |
| 359 |
const integer nfft = Melder_clippedLeft (2_integer, Melder_iroundUpToPowerOfTwo (my nx)); // TODO: explain edge case |
| 360 |
autoNUMFourierTable fourierTable = NUMFourierTable_create (nfft); |
| 361 |
for (integer iq = 1; iq <= my ny; iq ++) { |
| 362 |
VECsmooth_gaussian (thy z.row (iq), my z.row (iq), sigma, fourierTable.get()); |
| 363 |
abs_VEC_inout (thy z.row (iq)); |
| 364 |
} |
| 365 |
} |
| 366 |
/* |
| 367 |
2. average across quefrencies |
| 368 |
*/ |
| 369 |
const double numberOfQuefrencyBins = quefrencyAveragingWindow / my dy; |
| 370 |
if (numberOfQuefrencyBins > 1.0) { |
| 371 |
const integer nfft = Melder_clippedLeft (2_integer, Melder_iroundUpToPowerOfTwo (my ny)); // TODO: explain edge case |
| 372 |
autoNUMFourierTable fourierTable = NUMFourierTable_create (nfft); |
| 373 |
const double sigma = numberOfQuefrencyBins / numberOfSigmasInWindow; // 2sigma -> 95.4%, 3sigma -> 99.7 % of the data |
| 374 |
for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 375 |
VECsmooth_gaussian_inplace (thy z.column (iframe), sigma, fourierTable.get()); |
| 376 |
abs_VEC_inout (thy z.column (iframe)); |
| 377 |
} |
| 378 |
} |
| 379 |
return thee; |
| 380 |
} catch (MelderError) { |
| 381 |
Melder_throw (me, U": not smoothed."); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
autoPowerCepstrogram PowerCepstrogram_smooth (PowerCepstrogram me, double timeAveragingWindow, double quefrencyAveragingWindow) { |
| 386 |
if (Melder_debug == -4) |
| 387 |
return PowerCepstrogram_smoothRectangular_old (me, timeAveragingWindow, quefrencyAveragingWindow); |
| 388 |
else if (Melder_debug == -5) |
| 389 |
return PowerCepstrogram_smoothGaussian (me, timeAveragingWindow, quefrencyAveragingWindow); |
| 390 |
else |
| 391 |
return PowerCepstrogram_smoothRectangular (me, timeAveragingWindow, quefrencyAveragingWindow); |
| 392 |
} |
| 393 |
|
| 394 |
autoMatrix PowerCepstrogram_to_Matrix (PowerCepstrogram me) { |
| 395 |
try { |
| 396 |
autoMatrix thee = Thing_new (Matrix); |
| 397 |
my structMatrix :: v1_copy (thee.get()); |
| 398 |
return thee; |
| 399 |
} catch (MelderError) { |
| 400 |
Melder_throw (me, U": not converted to Matrix."); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
autoPowerCepstrum PowerCepstrogram_to_PowerCepstrum_slice (PowerCepstrogram me, double time) { |
| 405 |
try { |
| 406 |
integer iframe = Sampled_xToNearestIndex (me, time); |
| 407 |
iframe = iframe < 1 ? 1 : iframe > my nx ? my nx : iframe; |
| 408 |
autoPowerCepstrum thee = PowerCepstrum_create (my ymax, my ny); |
| 409 |
thy z.row (1) <<= my z.column (iframe); |
| 410 |
return thee; |
| 411 |
} catch (MelderError) { |
| 412 |
Melder_throw (me, U": Cepstrum not extracted."); |
| 413 |
} |
| 414 |
} |
| 415 |
|
| 416 |
autoPowerCepstrogram Matrix_to_PowerCepstrogram (Matrix me) { |
| 417 |
try { |
| 418 |
autoPowerCepstrogram thee = Thing_new (PowerCepstrogram); |
| 419 |
my structMatrix :: v1_copy (thee.get()); |
| 420 |
return thee; |
| 421 |
} catch (MelderError) { |
| 422 |
Melder_throw (me, U": not converted to PowerCepstrogram."); |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
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) { |
| 430 |
try { |
| 431 |
autoPowerCepstrogram flattened; |
| 432 |
bool trendSubtracted = subtractTrendBeforeSmoothing; |
| 433 |
if (subtractTrendBeforeSmoothing) |
| 434 |
flattened = PowerCepstrogram_subtractTrend (me, qminFit, qmaxFit, lineType, fitMethod); |
| 435 |
|
| 436 |
autoPowerCepstrogram smooth = PowerCepstrogram_smooth (subtractTrendBeforeSmoothing ? flattened.get() : me, timeAveragingWindow, quefrencyAveragingWindow); |
| 437 |
if (Melder_debug == -6) { // old algorithm |
| 438 |
autoTable table = PowerCepstrogram_to_Table_CPP (smooth.get(), false, false, 6, 16, false, 6, pitchFloor, pitchCeiling, |
| 439 |
deltaF0, peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 440 |
const double cpps = Table_getMean (table.get(), 1); // no frame number, no time, quefrency |
| 441 |
return cpps; |
| 442 |
} else { |
| 443 |
autoMatrix cpp = PowerCepstrogram_to_Matrix_CPP (smooth.get(), trendSubtracted, pitchFloor, pitchCeiling, deltaF0, |
| 444 |
peakInterpolationType, qminFit, qmaxFit, lineType, fitMethod); |
| 445 |
const double cpps = Matrix_getMean (cpp.get(), cpp -> xmin, cpp -> xmax, 5.5, 6.5); // TODO Sampled_getMean?? |
| 446 |
return cpps; |
| 447 |
} |
| 448 |
} catch (MelderError) { |
| 449 |
Melder_throw (me, U": no CPPS value calculated."); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
double PowerCepstrogram_getCPPS_hillenbrand (PowerCepstrogram me, bool subtractTrendBeforeSmoothing, double timeAveragingWindow, double quefrencyAveragingWindow, double pitchFloor, double pitchCeiling) { |
| 454 |
try { |
| 455 |
autoPowerCepstrogram him; |
| 456 |
if (subtractTrendBeforeSmoothing) |
| 457 |
him = PowerCepstrogram_subtractTrend (me, 0.001, 0, kCepstrum_trendType::LINEAR, kCepstrum_trendFit::LEAST_SQUARES); |
| 458 |
|
| 459 |
autoPowerCepstrogram smooth = PowerCepstrogram_smooth (subtractTrendBeforeSmoothing ? him.get() : me, timeAveragingWindow, quefrencyAveragingWindow); |
| 460 |
autoTable table = PowerCepstrogram_to_Table_hillenbrand (smooth.get(), pitchFloor, pitchCeiling); |
| 461 |
const double cpps = Table_getMean (table.get(), 3); |
| 462 |
return cpps; |
| 463 |
} catch (MelderError) { |
| 464 |
Melder_throw (me, U": no CPPS value calculated."); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
/* End of file PowerCepstrogram.cpp */ |