| 1 |
/* PowerCepstrum.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2012-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 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. 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 "PowerCepstrum.h" |
| 20 |
#include "PowerCepstrumWorkspace.h" |
| 21 |
#include "NUM2.h" |
| 22 |
|
| 23 |
Thing_implement (PowerCepstrum, Cepstrum, 2); // derives from Matrix; therefore also version 2 |
| 24 |
|
| 25 |
Thing_implement (PowerCepstrumWorkspace, Thing, 0); |
| 26 |
|
| 27 |
|
| 28 |
static inline bool greaterThanOrEqual (double x, double y) { |
| 29 |
// x >= y means !(x < y) |
| 30 |
return ( (x == 0.0 || y == 0.0) ? (x - y) : (x - y) / fabs (x) ) > -1e-12; |
| 31 |
} |
| 32 |
|
| 33 |
static inline bool greaterThan (double x, double y) { |
| 34 |
return ( (x == 0.0 || y == 0.0) ? (x - y) : (x - y) / fabs (x) ) > 1e-12; |
| 35 |
} |
| 36 |
|
| 37 |
static void Vector_getMaximumAndX_twoSideApproach (constVector me, double xmin, double xmax, integer channelNumber, |
| 38 |
kVector_peakInterpolation peakInterpolationType, bool startHigh, double *out_maximum, double *out_xOfMaximum) |
| 39 |
{ |
| 40 |
Melder_assert (channelNumber >= 1 && channelNumber <= my ny); |
| 41 |
constVEC y = my z.row (channelNumber); |
| 42 |
double maximum, x; |
| 43 |
Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 44 |
integer imin, imax; |
| 45 |
if (! Sampled_getWindowSamples (me, xmin, xmax, & imin, & imax)) { |
| 46 |
/* |
| 47 |
No samples between xmin and xmax. |
| 48 |
Try to return the greater of the values at these two points. |
| 49 |
*/ |
| 50 |
kVector_valueInterpolation valueInterpolationType = ( peakInterpolationType > kVector_peakInterpolation :: NONE ? |
| 51 |
kVector_valueInterpolation :: LINEAR : kVector_valueInterpolation :: NEAREST ); |
| 52 |
const double yleft = Vector_getValueAtX (me, xmin, channelNumber, valueInterpolationType); |
| 53 |
const double yright = Vector_getValueAtX (me, xmax, channelNumber, valueInterpolationType); |
| 54 |
maximum = std::max (yleft, yright); |
| 55 |
x = ( yleft == yright ? (xmin + xmax) / 2.0 : yleft > yright ? xmin : xmax ); |
| 56 |
} else { |
| 57 |
maximum = y [imin]; |
| 58 |
x = imin; |
| 59 |
if (y [imax] > maximum) { |
| 60 |
maximum = y [imax]; |
| 61 |
x = imax; |
| 62 |
} |
| 63 |
if (imin == 1) |
| 64 |
imin ++; |
| 65 |
if (imax == my nx) |
| 66 |
imax --; |
| 67 |
if (! startHigh) { // approach from the start |
| 68 |
for (integer i = imin; i <= imax; i ++) { |
| 69 |
if (greaterThan (y [i], y [i - 1]) && greaterThanOrEqual (y [i], y [i + 1])) { |
| 70 |
double i_real; |
| 71 |
const double localMaximum = NUMimproveMaximum (y, i, kVector_peakInterpolation_to_interpolationDepth (peakInterpolationType), & i_real); |
| 72 |
if (localMaximum > maximum) { |
| 73 |
maximum = localMaximum; |
| 74 |
x = i_real; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
} else { // approach from the end |
| 79 |
for (integer i = imax; i >= imin; i --) { |
| 80 |
if (greaterThan (y [i], y [i + 1]) && greaterThanOrEqual (y [i], y [i - 1])) { |
| 81 |
double i_real; |
| 82 |
const double localMaximum = NUMimproveMaximum (y, i, kVector_peakInterpolation_to_interpolationDepth (peakInterpolationType), & i_real); |
| 83 |
if (localMaximum > maximum) { |
| 84 |
maximum = localMaximum; |
| 85 |
x = i_real; |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
x = my x1 + (x - 1) * my dx; // convert sample to x |
| 91 |
Melder_clip (xmin, & x, xmax); |
| 92 |
|
| 93 |
} |
| 94 |
if (out_maximum) |
| 95 |
*out_maximum = maximum; |
| 96 |
if (out_xOfMaximum) |
| 97 |
*out_xOfMaximum = x; |
| 98 |
} |
| 99 |
|
| 100 |
void structPowerCepstrumWorkspace :: getSlopeAndIntercept () { |
| 101 |
SlopeSelector_getSlopeAndIntercept (slopeSelector.get(), slope, intercept, method); |
| 102 |
slopeKnown = true; |
| 103 |
} |
| 104 |
|
| 105 |
double structPowerCepstrumWorkspace :: getTrend (double quefrency) { |
| 106 |
if (! slopeKnown) |
| 107 |
getSlopeAndIntercept (); |
| 108 |
const double xq = ( trendLineType == kCepstrum_trendType::EXPONENTIAL_DECAY ? log (quefrency) : quefrency ); |
| 109 |
trenddB = slope * xq + intercept; |
| 110 |
return trenddB; |
| 111 |
} |
| 112 |
|
| 113 |
void structPowerCepstrumWorkspace :: subtractTrend () { |
| 114 |
if (! slopeKnown) |
| 115 |
getSlopeAndIntercept (); |
| 116 |
for (integer j = 1; j <= powerCepstrum -> nx; j ++) { |
| 117 |
/* |
| 118 |
For the exponential decay function, y = slope*log(quefrency)+intercept, the PowerCepstrum's first quefrency |
| 119 |
value (at x1) has quefrency == 0 and therefore value y(0) is not defined. As an approximation for y(0) |
| 120 |
we use y(0.5*dx). This is no problem because the value at quefrency == 0 is not relevant. |
| 121 |
*/ |
| 122 |
const double quefrency = ( j == 1 && trendLineType == kCepstrum_trendType::EXPONENTIAL_DECAY ? 0.5 * powerCepstrum -> dx : (j - 1) * powerCepstrum -> dx ); |
| 123 |
const double db_background = getTrend (quefrency); |
| 124 |
const double db_cepstrum = powerCepstrum -> v_getValueAtSample (j, 1, 1); |
| 125 |
const double diff = Melder_clippedLeft (0.0, db_cepstrum - db_background); |
| 126 |
powerCepstrum -> z [1] [j] = exp (diff * NUMln10 / 10.0); |
| 127 |
} |
| 128 |
trendSubtracted = true; |
| 129 |
} |
| 130 |
|
| 131 |
void structPowerCepstrumWorkspace :: newData (constPowerCepstrum thee) { |
| 132 |
Melder_assert (thy nx == powerCepstrum -> nx); |
| 133 |
for (integer ipoint = 1, i = imin; i <= imax; i ++, ipoint ++) { |
| 134 |
double xval = Sampled_indexToX (thee, i); |
| 135 |
if (trendLineType == kCepstrum_trendType::EXPONENTIAL_DECAY) |
| 136 |
xval = log (xval); |
| 137 |
x [ipoint] = xval; |
| 138 |
y [ipoint] = thy v_getValueAtSample (i, 1, 1); // dB's |
| 139 |
} |
| 140 |
slopeSelector -> newDataPoints (x.get(), y.get()); |
| 141 |
todBs (); |
| 142 |
peakKnown = slopeKnown = trendSubtracted = false; |
| 143 |
} |
| 144 |
|
| 145 |
void structPowerCepstrumWorkspace :: getPeakAndPosition () { |
| 146 |
Matrix thee = asdBs.get(); |
| 147 |
double peakdBR, peakQuefrencyR; |
| 148 |
Vector_getMaximumAndX_twoSideApproach ((Vector) thee, qminPeakSearch, qmaxPeakSearch, |
| 149 |
1, peakInterpolationType, false, & peakdB, & peakQuefrency); |
| 150 |
Vector_getMaximumAndX_twoSideApproach ((Vector) thee, qminPeakSearch, qmaxPeakSearch, |
| 151 |
1, peakInterpolationType, true, & peakdBR, & peakQuefrencyR); |
| 152 |
const integer index = Sampled_xToIndex (thee, peakQuefrency), indexR = Sampled_xToIndex(thee, peakQuefrencyR); |
| 153 |
if (index != indexR && (indexR - index) <= 5) { |
| 154 |
double indexm = 0.5 * (index + indexR); |
| 155 |
peakQuefrency = thy x1 + (indexm - 1) * thy dx; |
| 156 |
peakdB = thy z [1] [index]; // always with flat peak |
| 157 |
} |
| 158 |
peakKnown = true; |
| 159 |
} |
| 160 |
|
| 161 |
void structPowerCepstrumWorkspace :: getCPP () { |
| 162 |
if (! slopeKnown) |
| 163 |
getSlopeAndIntercept (); |
| 164 |
if (! peakKnown) |
| 165 |
getPeakAndPosition (); |
| 166 |
trenddB = getTrend (peakQuefrency); |
| 167 |
cpp = peakdB - trenddB; |
| 168 |
} |
| 169 |
|
| 170 |
void structPowerCepstrumWorkspace :: todBs () { |
| 171 |
for (integer i = 1; i <= powerCepstrum -> nx; i ++) |
| 172 |
asdBs -> z [1] [i] = powerCepstrum -> v_getValueAtSample (i, 1, 1); // 10 log val^2 |
| 173 |
} |
| 174 |
|
| 175 |
void structPowerCepstrumWorkspace :: fromdBs () { |
| 176 |
for (integer i = 1; i <= powerCepstrum -> nx; i ++) |
| 177 |
powerCepstrum -> z [1] [i] = pow (10.0, powerCepstrum -> z [1] [i] / 10.0) - 1e-30; |
| 178 |
} |
| 179 |
|
| 180 |
void structPowerCepstrumWorkspace :: setMaximumNumberOfRhamonics (integer maximumNumberOfRhamonics) { |
| 181 |
our maximumNumberOfRhamonics = maximumNumberOfRhamonics; |
| 182 |
// resize when we calculate values. |
| 183 |
} |
| 184 |
|
| 185 |
void structPowerCepstrumWorkspace :: getNumberOfRhamonics (double qmin, double qmax) { |
| 186 |
integer numberOfRhamonics = 2; |
| 187 |
if (! peakKnown) |
| 188 |
getPeakAndPosition (); |
| 189 |
while (peakQuefrency >= qmin && peakQuefrency * numberOfRhamonics <= qmax) |
| 190 |
numberOfRhamonics ++; |
| 191 |
numberOfRhamonics --; |
| 192 |
numberOfRhamonics = std:: min (numberOfRhamonics, maximumNumberOfRhamonics); |
| 193 |
} |
| 194 |
|
| 195 |
void structPowerCepstrumWorkspace :: getRhamonicsPower (double qmin, double qmax, double f0fractionalWidth) { |
| 196 |
getSlopeAndIntercept (); |
| 197 |
getPeakAndPosition (); |
| 198 |
getNumberOfRhamonics (qmin, qmax); |
| 199 |
rhamonics.resize (numberOfRhamonics, 6_integer); |
| 200 |
// q, peakdB, power, q1, q2, trenddb |
| 201 |
for (integer rhamonic = 1; rhamonic <= numberOfRhamonics; rhamonic ++) { |
| 202 |
const double quefrency = rhamonic * peakQuefrency; |
| 203 |
const double f = 1.0 / quefrency; |
| 204 |
const double flow = f * (1.0 - f0fractionalWidth); |
| 205 |
const double fhigh = f * (1.0 + f0fractionalWidth); |
| 206 |
const double qlow = 1.0 / fhigh; |
| 207 |
const double qhigh = ( f0fractionalWidth >= 1.0 ? powerCepstrum -> xmax : 1.0 / flow ); |
| 208 |
integer iqmin, iqmax; |
| 209 |
Matrix_getWindowSamplesX (powerCepstrum, qlow, qhigh, & iqmin, & iqmax); |
| 210 |
double power = 0.0; |
| 211 |
for (integer iq = iqmin; iq <= iqmax; iq ++) |
| 212 |
power += powerCepstrum -> v_getValueAtSample (iq, 1, 0); |
| 213 |
rhamonics [rhamonic] [1] = power; |
| 214 |
rhamonics [rhamonic] [2] = qlow; |
| 215 |
rhamonics [rhamonic] [3] = quefrency; |
| 216 |
rhamonics [rhamonic] [4] = qhigh; |
| 217 |
rhamonics [rhamonic] [5] = peakdB; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
void structPowerCepstrumWorkspace :: getRhamonicPeaks (double qmin, double qmax) { |
| 222 |
getPeakAndPosition (); |
| 223 |
getNumberOfRhamonics (qmin, qmax); |
| 224 |
rhamonics.resize (numberOfRhamonics, 6_integer); |
| 225 |
rhamonics [1] [2] = peakQuefrency; |
| 226 |
rhamonics [1] [1] = peakdB; |
| 227 |
kVector_valueInterpolation valueInterpolationType = ( peakInterpolationType > kVector_peakInterpolation :: NONE ? |
| 228 |
kVector_valueInterpolation :: LINEAR : kVector_valueInterpolation :: NEAREST ); |
| 229 |
for (integer rhamonic = 2; rhamonic <= numberOfRhamonics; rhamonic ++) { |
| 230 |
const double quefrency = rhamonic * peakQuefrency; |
| 231 |
const double amplitudedB = Vector_getValueAtX ((Vector) asdBs.get(), quefrency, 1, valueInterpolationType); |
| 232 |
rhamonics [rhamonic] [2] = quefrency; |
| 233 |
rhamonics [rhamonic] [1] = amplitudedB; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
double structPowerCepstrumWorkspace :: getRNR (double qmin, double qmax, double f0fractionalWidth) { |
| 238 |
getRhamonicsPower (qmin, qmax, f0fractionalWidth); |
| 239 |
const double sumOfRhamonics = NUMsum (rhamonics.column (3)); |
| 240 |
double power = 0.0; |
| 241 |
for (integer iq = imin; iq <= imax; iq ++) |
| 242 |
power += powerCepstrum -> v_getValueAtSample (iq, 1, 0); |
| 243 |
const double rnr = sumOfRhamonics / (power - sumOfRhamonics); |
| 244 |
return rnr; |
| 245 |
} |
| 246 |
|
| 247 |
double structPowerCepstrum :: v_getValueAtSample (integer isamp, integer row, int units) const { |
| 248 |
double result = undefined; |
| 249 |
if (row == 1) { |
| 250 |
if (units == 0) |
| 251 |
result = z [1] [isamp]; |
| 252 |
else |
| 253 |
result = 10.0 * log10 (z [1] [isamp] + 1e-30); // result >= -300 |
| 254 |
} |
| 255 |
return result; |
| 256 |
} |
| 257 |
|
| 258 |
void structPowerCepstrumWorkspace :: initWorkspace (constPowerCepstrum me, double qminFit, double qmaxFit, |
| 259 |
kCepstrum_trendType trendLineType, kCepstrum_trendFit method) |
| 260 |
{ |
| 261 |
powerCepstrum = me; |
| 262 |
Function_unidirectionalAutowindow (powerCepstrum, & qminFit, & qmaxFit); |
| 263 |
Melder_require (qminFit >= powerCepstrum ->xmin && qmaxFit <= powerCepstrum ->xmax, |
| 264 |
U"Your quefrency range is outside the domain."); |
| 265 |
(void) Matrix_getWindowSamplesX (powerCepstrum, qminFit, qmaxFit, & imin, & imax); |
| 266 |
Melder_clipLeft (2_integer, & imin); // never use q=0 in fitting |
| 267 |
numberOfPoints = imax - imin + 1; |
| 268 |
Melder_require (numberOfPoints > 2, |
| 269 |
U"Not enough points in the selection."); |
| 270 |
our trendLineType = trendLineType; |
| 271 |
our method = ( method == kCepstrum_trendFit::LEAST_SQUARES ? kSlopeSelector_method::LEAST_SQUARES : |
| 272 |
(method == kCepstrum_trendFit::ROBUST_SLOW ? kSlopeSelector_method::THEILSEN : kSlopeSelector_method::SIEGEL)); |
| 273 |
x = raw_VEC (numberOfPoints); |
| 274 |
y = raw_VEC (numberOfPoints); |
| 275 |
asdBs = Matrix_create (powerCepstrum -> xmin, powerCepstrum -> xmax, powerCepstrum -> nx, powerCepstrum -> dx, powerCepstrum -> x1, |
| 276 |
powerCepstrum -> ymin, powerCepstrum -> ymax, powerCepstrum -> ny, powerCepstrum -> dy, powerCepstrum ->y1); |
| 277 |
maximumNumberOfRhamonics = 15; |
| 278 |
rhamonics = raw_MAT (maximumNumberOfRhamonics, 5_integer); |
| 279 |
slopeSelector = SlopeSelector_create (x.get(), y.get()); // only reference to the x and y vectors |
| 280 |
newData (powerCepstrum); // new xp and yp reference |
| 281 |
} |
| 282 |
|
| 283 |
void structPowerCepstrumWorkspace :: initPeakSearchPart (double qminPeakSearch, double qmaxPeakSearch, |
| 284 |
kVector_peakInterpolation peakInterpolationType) |
| 285 |
{ |
| 286 |
our qminPeakSearch = qminPeakSearch; |
| 287 |
our qmaxPeakSearch = qmaxPeakSearch; |
| 288 |
our peakInterpolationType = peakInterpolationType; |
| 289 |
} |
| 290 |
|
| 291 |
autoPowerCepstrumWorkspace PowerCepstrumWorkspace_create (constPowerCepstrum powerCepstrum, double qminFit, double qmaxFit, |
| 292 |
kCepstrum_trendType trendLineType, kCepstrum_trendFit fitMethod) |
| 293 |
{ |
| 294 |
try { |
| 295 |
autoPowerCepstrumWorkspace me = Thing_new (PowerCepstrumWorkspace); |
| 296 |
my initWorkspace (powerCepstrum, qminFit, qmaxFit, trendLineType, fitMethod); |
| 297 |
return me; |
| 298 |
} catch (MelderError) { |
| 299 |
Melder_throw (U"Cannot create PowerCepstrumWorkspace."); |
| 300 |
} |
| 301 |
|
| 302 |
} |
| 303 |
|
| 304 |
autoPowerCepstrum PowerCepstrum_create (double qmax, integer nq) { |
| 305 |
try { |
| 306 |
autoPowerCepstrum me = Thing_new (PowerCepstrum); |
| 307 |
const double dq = qmax / (nq - 1); |
| 308 |
Matrix_init (me.get(), 0.0, qmax, nq, dq, 0.0, 1.0, 1.0, 1, 1, 1.0); |
| 309 |
return me; |
| 310 |
} catch (MelderError) { |
| 311 |
Melder_throw (U"PowerCepstrum not created."); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
void PowerCepstrum_draw (constPowerCepstrum me, Graphics g, double qmin, double qmax, double dBminimum, double dBmaximum, bool garnish) { |
| 317 |
Cepstrum_draw (me, g, qmin, qmax, dBminimum, dBmaximum, true, garnish); |
| 318 |
} |
| 319 |
|
| 320 |
void PowerCepstrum_fitTrendLine (PowerCepstrum me, double qstartFit, double qendFit, double *out_slope, double *out_intercept, |
| 321 |
kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 322 |
{ |
| 323 |
Function_intersectRangeWithDomain (me, & qstartFit, & qendFit); |
| 324 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qstartFit, qendFit, lineType, fitMethod); |
| 325 |
workspace -> getSlopeAndIntercept (); |
| 326 |
if (out_slope) |
| 327 |
*out_slope = workspace -> slope; |
| 328 |
if (out_intercept) |
| 329 |
*out_intercept = workspace -> intercept; |
| 330 |
} |
| 331 |
|
| 332 |
void PowerCepstrum_drawTrendLine (PowerCepstrum me, Graphics g, double qmin, double qmax, double dBminimum, double dBmaximum, |
| 333 |
double qstartFit, double qendFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 334 |
{ |
| 335 |
Function_unidirectionalAutowindow (me, & qmin, & qmax); |
| 336 |
Function_intersectRangeWithDomain (me, & qstartFit, & qendFit); |
| 337 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qstartFit, qendFit, lineType, fitMethod); |
| 338 |
workspace -> getSlopeAndIntercept (); |
| 339 |
|
| 340 |
if (dBminimum >= dBmaximum) { // autoscaling |
| 341 |
MelderExtremaWithInit extrema_db; |
| 342 |
for (integer i = workspace -> imin; i <= workspace -> imax; i ++) |
| 343 |
extrema_db.update (workspace -> y[i]); |
| 344 |
dBmaximum = extrema_db.max; |
| 345 |
dBminimum = extrema_db.min; |
| 346 |
} |
| 347 |
|
| 348 |
Graphics_setInner (g); |
| 349 |
Graphics_setWindow (g, qmin, qmax, dBminimum, dBmaximum); |
| 350 |
const double slope = workspace -> slope, intercept = workspace -> intercept; |
| 351 |
/* |
| 352 |
Don't draw part outside window |
| 353 |
*/ |
| 354 |
const double lineWidth = Graphics_inqLineWidth (g); |
| 355 |
Graphics_setLineWidth (g, 2); |
| 356 |
if (lineType == kCepstrum_trendType::EXPONENTIAL_DECAY ) { |
| 357 |
integer n = 500; |
| 358 |
const double dq = (qendFit - qstartFit) / (n + 1); |
| 359 |
const double q1 = qstartFit; |
| 360 |
if (qstartFit <= 0.0) { |
| 361 |
qstartFit = 0.1 * dq; // some small offset to avoid log(0) |
| 362 |
n --; |
| 363 |
} |
| 364 |
autoVEC y = raw_VEC (n); |
| 365 |
|
| 366 |
for (integer i = 1; i <= n; i ++) { |
| 367 |
const double q = q1 + (i - 1) * dq; |
| 368 |
y [i] = slope * log (q) + intercept; |
| 369 |
} |
| 370 |
Graphics_function (g, y.asArgumentToFunctionThatExpectsOneBasedArray(), 1, n, qstartFit, qendFit); |
| 371 |
} else { |
| 372 |
const double y1 = slope * qstartFit + intercept; |
| 373 |
const double y2 = slope * qendFit + intercept; |
| 374 |
if (y1 >= dBminimum && y2 >= dBminimum) { |
| 375 |
Graphics_line (g, qstartFit, y1, qendFit, y2); |
| 376 |
} else if (y1 < dBminimum) { |
| 377 |
qstartFit = (dBminimum - intercept) / slope; |
| 378 |
Graphics_line (g, qstartFit, dBminimum, qendFit, y2); |
| 379 |
} else if (y2 < dBminimum) { |
| 380 |
qendFit = (dBminimum - intercept) / slope; |
| 381 |
Graphics_line (g, qstartFit, y1, qendFit, dBminimum); |
| 382 |
} else { |
| 383 |
// don't draw anything below lower limit |
| 384 |
} |
| 385 |
} |
| 386 |
Graphics_setLineWidth (g, lineWidth); |
| 387 |
Graphics_unsetInner (g); |
| 388 |
} |
| 389 |
|
| 390 |
double PowerCepstrum_getTrendLineValue (PowerCepstrum me, double quefrency, double qstartFit, double qendFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 391 |
{ |
| 392 |
Function_intersectRangeWithDomain (me, & qstartFit, & qendFit); |
| 393 |
double trenddb = undefined; |
| 394 |
if (quefrency >= my xmin && quefrency <= my xmax) { |
| 395 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qstartFit, qendFit, lineType, fitMethod); |
| 396 |
trenddb = workspace -> getTrend (quefrency); |
| 397 |
} |
| 398 |
return trenddb; |
| 399 |
} |
| 400 |
|
| 401 |
void PowerCepstrum_subtractTrend_inplace (mutablePowerCepstrum me, double qstartFit, double qendFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) |
| 402 |
{ |
| 403 |
Function_intersectRangeWithDomain (me, & qstartFit, & qendFit); |
| 404 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qstartFit, qendFit, lineType, fitMethod); |
| 405 |
workspace -> subtractTrend (); |
| 406 |
} |
| 407 |
|
| 408 |
autoPowerCepstrum PowerCepstrum_subtractTrend (constPowerCepstrum me, double qstartFit, double qendFit, kCepstrum_trendType lineType, kCepstrum_trendFit fitMethod) { |
| 409 |
try { |
| 410 |
autoPowerCepstrum thee = Data_copy (me); |
| 411 |
PowerCepstrum_subtractTrend_inplace (thee.get(), qstartFit, qendFit, lineType, fitMethod); |
| 412 |
return thee; |
| 413 |
} catch (MelderError) { |
| 414 |
Melder_throw (me, U": couldn't subtract trend line."); |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
static void PowerCepstrum_smooth_inplaceRectangular (mutablePowerCepstrum me, double quefrencyAveragingWindow, integer numberOfIterations) { |
| 419 |
try { |
| 420 |
const double halfWindwow = 0.5 * quefrencyAveragingWindow; |
| 421 |
const double numberOfQuefrencyBins = quefrencyAveragingWindow / my dx; |
| 422 |
if (numberOfQuefrencyBins > 1.0) { |
| 423 |
autoVEC qout = raw_VEC (my nx); |
| 424 |
for (integer k = 1; k <= numberOfIterations; k ++) { |
| 425 |
for (integer isamp = 1; isamp <= my nx; isamp ++) { |
| 426 |
const double xmid = Sampled_indexToX (me, isamp); |
| 427 |
qout [isamp] = Sampled_getMean (me, xmid - halfWindwow, xmid + halfWindwow, 1, 0, true); |
| 428 |
} |
| 429 |
my z.row (1) <<= qout.all(); |
| 430 |
} |
| 431 |
} |
| 432 |
} catch (MelderError) { |
| 433 |
Melder_throw (me, U": not smoothed."); |
| 434 |
} |
| 435 |
} |
| 436 |
|
| 437 |
static void PowerCepstrum_smooth_inplaceRectangular_old (mutablePowerCepstrum me, double quefrencyAveragingWindow, integer numberOfIterations) { |
| 438 |
try { |
| 439 |
const integer numberOfQuefrencyBins = Melder_ifloor (quefrencyAveragingWindow / my dx); |
| 440 |
if (numberOfQuefrencyBins > 1) { |
| 441 |
autoVEC qin = copy_VEC (my z.row (1)); |
| 442 |
autoVEC qout = raw_VEC (my nx); |
| 443 |
for (integer k = 1; k <= numberOfIterations; k ++) |
| 444 |
if (k % 2 == 1) |
| 445 |
VECsmoothByMovingAverage_preallocated (qout.get(), qin.get(), numberOfQuefrencyBins); |
| 446 |
else |
| 447 |
VECsmoothByMovingAverage_preallocated (qin.get(), qout.get(), numberOfQuefrencyBins); |
| 448 |
my z.row (1) <<= ( numberOfIterations % 2 == 1 ? qout.get() : qin.get() ); |
| 449 |
} |
| 450 |
} catch (MelderError) { |
| 451 |
Melder_throw (me, U": not smoothed."); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
static void PowerCepstrum_smooth_inplaceGaussian (mutablePowerCepstrum me, double quefrencyAveragingWindow, integer numberOfIterations) { |
| 456 |
try { |
| 457 |
const double numberOfQuefrencyBins = quefrencyAveragingWindow / my dx; |
| 458 |
if (numberOfQuefrencyBins > 1.0) { |
| 459 |
/* |
| 460 |
Applying two Gaussians after another is associative: (G(s2)*(G(s1)*f) = (G(s2)*G(s1))*f. |
| 461 |
G(s2) * G(s1) = G(s), where s^2=s1^2+s2^2 |
| 462 |
*/ |
| 463 |
const double numberOfSigmasInWindow = 4.0; |
| 464 |
const double sigma = numberOfQuefrencyBins / numberOfSigmasInWindow; // 3sigma -> 99.7 % of the data (2sigma -> 95.4%)g |
| 465 |
const double sigma_n = sqrt (numberOfIterations) * sigma; |
| 466 |
VECsmooth_gaussian_inplace (my z.row (1), sigma_n); |
| 467 |
/* |
| 468 |
Due to imprecise arithmatic some values might turn out to be negative |
| 469 |
(but very small). Just make them positive. |
| 470 |
*/ |
| 471 |
abs_VEC_inout (my z.row (1)); |
| 472 |
} |
| 473 |
} catch (MelderError) { |
| 474 |
Melder_throw (me, U": not smoothed."); |
| 475 |
} |
| 476 |
} |
| 477 |
|
| 478 |
void PowerCepstrum_smooth_inplace (mutablePowerCepstrum me, double quefrencyAveragingWindow, integer numberOfIterations) { |
| 479 |
if (Melder_debug == -4) |
| 480 |
PowerCepstrum_smooth_inplaceRectangular_old (me, quefrencyAveragingWindow, numberOfIterations); |
| 481 |
else if (Melder_debug == -5) |
| 482 |
PowerCepstrum_smooth_inplaceGaussian (me, quefrencyAveragingWindow, numberOfIterations); |
| 483 |
else |
| 484 |
PowerCepstrum_smooth_inplaceRectangular (me, quefrencyAveragingWindow, numberOfIterations); |
| 485 |
} |
| 486 |
|
| 487 |
autoPowerCepstrum PowerCepstrum_smooth (constPowerCepstrum me, double quefrencyAveragingWindow, integer numberOfIterations) { |
| 488 |
autoPowerCepstrum thee = Data_copy (me); |
| 489 |
PowerCepstrum_smooth_inplace (thee.get(), quefrencyAveragingWindow, numberOfIterations); |
| 490 |
return thee; |
| 491 |
} |
| 492 |
|
| 493 |
|
| 494 |
void PowerCepstrum_getMaximumAndQuefrency_q (PowerCepstrum me, double qminPeakSearch, double qmaxPeakSearch, |
| 495 |
kCepstrum_peakInterpolation peakInterpolationType, double& peakdB, double& quefrency) |
| 496 |
{ |
| 497 |
kVector_peakInterpolation interpolation = ( peakInterpolationType == kCepstrum_peakInterpolation :: PARABOLIC ? |
| 498 |
kVector_peakInterpolation :: PARABOLIC : (peakInterpolationType == kCepstrum_peakInterpolation :: CUBIC ? |
| 499 |
kVector_peakInterpolation :: CUBIC : kVector_peakInterpolation :: NONE) |
| 500 |
); |
| 501 |
Function_intersectRangeWithDomain (me, & qminPeakSearch, & qmaxPeakSearch); |
| 502 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qminPeakSearch, qmaxPeakSearch, kCepstrum_trendType::LINEAR, kCepstrum_trendFit::ROBUST_FAST); |
| 503 |
workspace -> initPeakSearchPart (qminPeakSearch, qmaxPeakSearch, interpolation); |
| 504 |
workspace -> getPeakAndPosition (); |
| 505 |
peakdB = workspace -> peakdB; |
| 506 |
quefrency = workspace -> peakQuefrency; |
| 507 |
} |
| 508 |
|
| 509 |
//TODO |
| 510 |
void PowerCepstrum_getMaximumAndQuefrency_pitch (PowerCepstrum me, double pitchFloor, double pitchCeiling, |
| 511 |
kVector_peakInterpolation peakInterpolationType, double& peakdB, double& quefrency) |
| 512 |
{ |
| 513 |
const double qminPeakSearch = 1.0 / pitchCeiling; |
| 514 |
const double qmaxPeakSearch = std::min (my xmax, 1.0 / pitchFloor); |
| 515 |
|
| 516 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qminPeakSearch, qmaxPeakSearch, kCepstrum_trendType::LINEAR, kCepstrum_trendFit::ROBUST_FAST); |
| 517 |
workspace -> initPeakSearchPart (qminPeakSearch, qmaxPeakSearch, peakInterpolationType); |
| 518 |
workspace -> getPeakAndPosition (); |
| 519 |
peakdB = workspace -> peakdB; |
| 520 |
quefrency = workspace -> peakQuefrency; |
| 521 |
} |
| 522 |
|
| 523 |
autoTable PowerCepstrum_tabulateRhamonics (PowerCepstrum me, double pitchFloor, double pitchCeiling, kVector_peakInterpolation peakInterpolationType) { |
| 524 |
try { |
| 525 |
const double qminPeakSearch = 1.0 / pitchCeiling; |
| 526 |
const double qmaxPeakSearch = std::min (my xmax, 1.0 / pitchFloor); |
| 527 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qminPeakSearch, qmaxPeakSearch, |
| 528 |
kCepstrum_trendType::LINEAR, kCepstrum_trendFit::ROBUST_FAST); |
| 529 |
|
| 530 |
workspace -> initPeakSearchPart (qminPeakSearch, qmaxPeakSearch, peakInterpolationType); |
| 531 |
workspace -> getRhamonicPeaks (qminPeakSearch, qmaxPeakSearch); |
| 532 |
|
| 533 |
const conststring32 columnNames [] = { U"peak(dB)", U"quefrency(s)" }; |
| 534 |
autoTable thee = Table_createWithColumnNames (workspace -> numberOfRhamonics, ARRAY_TO_STRVEC (columnNames)); |
| 535 |
|
| 536 |
for (integer rhamonic = 1; rhamonic <= workspace -> numberOfRhamonics; rhamonic ++) { |
| 537 |
Table_setNumericValue (thee.get(), rhamonic, 1, workspace -> rhamonics [rhamonic][1]); |
| 538 |
Table_setNumericValue (thee.get(), rhamonic, 2, workspace -> rhamonics [rhamonic][2]); |
| 539 |
} |
| 540 |
return thee; |
| 541 |
} catch (MelderError) { |
| 542 |
Melder_throw (me, U": rhamonics could not be tabulated."); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
static autoMAT PowerCepstrum_getRhamonicsPower (PowerCepstrum me, double pitchFloor, double pitchCeiling, double f0fractionalWidth) { |
| 547 |
try { |
| 548 |
const double qminPeakSearch = 1.0 /pitchCeiling; |
| 549 |
const double qmaxPeakSearch = std::min (my xmax, 1.0 / pitchFloor); |
| 550 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qminPeakSearch, qmaxPeakSearch, |
| 551 |
kCepstrum_trendType::LINEAR, kCepstrum_trendFit::ROBUST_FAST); |
| 552 |
workspace -> initPeakSearchPart (qminPeakSearch, qmaxPeakSearch, kVector_peakInterpolation :: CUBIC); |
| 553 |
workspace -> getRhamonicsPower (qminPeakSearch, qmaxPeakSearch, f0fractionalWidth); |
| 554 |
autoMAT result = copy_MAT (workspace -> rhamonics.get()); |
| 555 |
return result; |
| 556 |
} catch (MelderError) { |
| 557 |
Melder_throw (me, U": rhamonics could not be calculated."); |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
double PowerCepstrum_getRNR (PowerCepstrum me, double pitchFloor, double pitchCeiling, double f0fractionalWidth) { |
| 562 |
const double qminPeakSearch = 1.0 /pitchCeiling; |
| 563 |
const double qmaxPeakSearch = std::min (my xmax, 1.0 / pitchFloor); |
| 564 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me,qminPeakSearch, qmaxPeakSearch, |
| 565 |
kCepstrum_trendType::LINEAR, kCepstrum_trendFit::ROBUST_FAST); |
| 566 |
workspace -> initPeakSearchPart (qminPeakSearch, qmaxPeakSearch, kVector_peakInterpolation :: CUBIC); |
| 567 |
const double rnr = workspace -> getRNR (qminPeakSearch, qmaxPeakSearch, f0fractionalWidth); |
| 568 |
return rnr; |
| 569 |
} |
| 570 |
|
| 571 |
double PowerCepstrum_getPeakProminence_hillenbrand (PowerCepstrum me, double pitchFloor, double pitchCeiling, double& qpeak) { |
| 572 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, 0.001, my xmax, kCepstrum_trendType::LINEAR, |
| 573 |
kCepstrum_trendFit::LEAST_SQUARES); |
| 574 |
const double qminPeakSearch = 1.0 /pitchCeiling; |
| 575 |
const double qmaxPeakSearch = std::min (my xmax, 1.0 / pitchFloor); |
| 576 |
workspace -> initPeakSearchPart (qminPeakSearch,qmaxPeakSearch, kVector_peakInterpolation :: NONE); |
| 577 |
workspace -> getCPP (); |
| 578 |
qpeak = workspace -> peakQuefrency; |
| 579 |
return workspace -> cpp; |
| 580 |
} |
| 581 |
|
| 582 |
double PowerCepstrum_getPeakProminence (PowerCepstrum me, double pitchFloor, double pitchCeiling, |
| 583 |
kVector_peakInterpolation peakInterpolationType, double qstartFit, double qendFit, |
| 584 |
kCepstrum_trendType trendLineType, kCepstrum_trendFit fitMethod, double& qpeak) |
| 585 |
{ |
| 586 |
Function_intersectRangeWithDomain (me, & qstartFit, & qendFit); |
| 587 |
autoPowerCepstrumWorkspace workspace = PowerCepstrumWorkspace_create (me, qstartFit, qendFit, trendLineType, fitMethod); |
| 588 |
const double qminPeakSearch = 1.0 /pitchCeiling; |
| 589 |
const double qmaxPeakSearch = std::min (my xmax, 1.0 / pitchFloor); |
| 590 |
workspace -> initPeakSearchPart (qminPeakSearch,qmaxPeakSearch, peakInterpolationType); |
| 591 |
workspace -> getCPP (); |
| 592 |
qpeak = workspace -> peakQuefrency; |
| 593 |
return workspace -> cpp; |
| 594 |
} |
| 595 |
|
| 596 |
autoPowerCepstrum Cepstrum_downto_PowerCepstrum (constCepstrum me) { |
| 597 |
try { |
| 598 |
autoPowerCepstrum thee = PowerCepstrum_create (my xmax, my nx); |
| 599 |
for (integer i = 1; i <= my nx; i ++) |
| 600 |
thy z [1] [i] = sqr (my z [1] [i]); |
| 601 |
return thee; |
| 602 |
} catch (MelderError) { |
| 603 |
Melder_throw (me, U" not converted."); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
autoMatrix PowerCepstrum_to_Matrix (constPowerCepstrum me) { |
| 608 |
try { |
| 609 |
autoMatrix thee = Thing_new (Matrix); |
| 610 |
my structMatrix :: v1_copy (thee.get()); |
| 611 |
return thee; |
| 612 |
} catch (MelderError) { |
| 613 |
Melder_throw (me, U": no Matrix created."); |
| 614 |
} |
| 615 |
} |
| 616 |
|
| 617 |
autoPowerCepstrum Matrix_to_PowerCepstrum (constMatrix me) { |
| 618 |
try { |
| 619 |
Melder_require (my ny == 1, |
| 620 |
U"Matrix should have exactly 1 row."); |
| 621 |
autoPowerCepstrum thee = Thing_new (PowerCepstrum); |
| 622 |
my structMatrix :: v1_copy (thee.get()); |
| 623 |
return thee; |
| 624 |
} catch (MelderError) { |
| 625 |
Melder_throw (me, U": not converted to PowerCepstrum."); |
| 626 |
} |
| 627 |
} |
| 628 |
|
| 629 |
autoPowerCepstrum Matrix_to_PowerCepstrum_row (constMatrix me, integer row) { |
| 630 |
try { |
| 631 |
autoPowerCepstrum thee = PowerCepstrum_create (my xmax, my nx); |
| 632 |
Melder_require (row > 0 && row <= my ny, |
| 633 |
U"Row number should be between 1 and ", my ny, U" inclusive."); |
| 634 |
thy z.row (1) <<= my z.row (row); |
| 635 |
return thee; |
| 636 |
} catch (MelderError) { |
| 637 |
Melder_throw (me, U": no PowerCepstrum created."); |
| 638 |
} |
| 639 |
} |
| 640 |
|
| 641 |
autoPowerCepstrum Matrix_to_PowerCepstrum_column (constMatrix me, integer col) { |
| 642 |
try { |
| 643 |
autoPowerCepstrum thee = PowerCepstrum_create (my ymax, my ny); |
| 644 |
Melder_require (col > 0 && col <= my nx, |
| 645 |
U"Column number should be between 1 and ", my nx, U" inclusive."); |
| 646 |
thy z.row (1) <<= my z.column (col); |
| 647 |
return thee; |
| 648 |
} catch (MelderError) { |
| 649 |
Melder_throw (me, U": no PowerCepstrum created."); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
/* End of file PowerCepstrum.cpp */ |