| 1 |
/* CPP.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2026 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 "Graphics.h" |
| 20 |
#include "CPP.h" |
| 21 |
|
| 22 |
Thing_implement (CPP, Vector, 2); |
| 23 |
|
| 24 |
static autoVEC CPP_getSoundingValues (CPP me, double tmin, double tmax) { |
| 25 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 26 |
integer imin, imax; |
| 27 |
integer numberOfFrames = Sampled_getWindowSamples (me, tmin, tmax, & imin, & imax); |
| 28 |
if (numberOfFrames < 1) |
| 29 |
return autoVEC(); |
| 30 |
autoVEC soundingValues = raw_VEC (numberOfFrames); |
| 31 |
integer numberOfSoundingFrames = 0; |
| 32 |
for (integer iframe = imin; iframe <= imax; iframe ++) |
| 33 |
if (my z [1] [iframe] > 0.0) |
| 34 |
soundingValues [++ numberOfSoundingFrames] = my z [1] [iframe]; |
| 35 |
if (numberOfSoundingFrames < 1) |
| 36 |
return autoVEC(); |
| 37 |
soundingValues.size = numberOfSoundingFrames; // shrink (without reallocation) |
| 38 |
return soundingValues; |
| 39 |
} |
| 40 |
|
| 41 |
void structCPP :: v1_info () { |
| 42 |
structDaata :: v1_info (); |
| 43 |
MelderInfo_writeLine (U"Time domain:"); |
| 44 |
MelderInfo_writeLine (U" Start time: ", our xmin, U" seconds"); |
| 45 |
MelderInfo_writeLine (U" End time: ", our xmax, U" seconds"); |
| 46 |
MelderInfo_writeLine (U" Total duration: ", our xmax - our xmin, U" seconds"); |
| 47 |
MelderInfo_writeLine (U"Time sampling:"); |
| 48 |
MelderInfo_writeLine (U" Number of frames: ", our nx); |
| 49 |
MelderInfo_writeLine (U" Time step: ", our dx, U" seconds"); |
| 50 |
MelderInfo_writeLine (U" First frame centred at: ", our x1, U" seconds"); |
| 51 |
autoVEC soundingValues = CPP_getSoundingValues (this, 0.0, 0.0); |
| 52 |
if (soundingValues.size > 0) { |
| 53 |
MelderInfo_writeLine (U"Cepstral Peak Prominence values of sounding frames:"); |
| 54 |
MelderInfo_writeLine (U" Number of sounding frames: ", soundingValues.size); |
| 55 |
sort_e_VEC_inout (soundingValues.get()); |
| 56 |
MelderInfo_writeLine (U" Median ", Melder_single (NUMquantile (soundingValues.get(), 0.50)), U" dB"); |
| 57 |
MelderInfo_writeLine (U" 10 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.10)), U" dB 90 %% = ", |
| 58 |
Melder_single (NUMquantile (soundingValues.get(), 0.90)), U" dB"); |
| 59 |
MelderInfo_writeLine (U" 16 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.16)), U" dB 84 %% = ", |
| 60 |
Melder_single (NUMquantile (soundingValues.get(), 0.84)), U" dB"); |
| 61 |
MelderInfo_writeLine (U" 25 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.25)), U" dB 75 %% = ", |
| 62 |
Melder_single (NUMquantile (soundingValues.get(), 0.75)), U" dB"); |
| 63 |
MelderInfo_writeLine (U"Minimum: ", Melder_single (soundingValues [1]), U" dB"); |
| 64 |
MelderInfo_writeLine (U"Maximum: ", Melder_single (soundingValues [soundingValues.size]), U" dB"); |
| 65 |
MelderGaussianStats stats = NUMmeanStdev (soundingValues.all()); |
| 66 |
MelderInfo_writeLine (U"Average: ", Melder_single (stats.mean), U" dB"); |
| 67 |
if (soundingValues.size > 1) |
| 68 |
MelderInfo_writeLine (U"Standard deviation: ", Melder_single (stats.stdev), U" dB"); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
autoCPP CPP_create (double tmin, double tmax, integer nt, double dt, double t1) { |
| 73 |
try { |
| 74 |
autoCPP me = Thing_new (CPP); |
| 75 |
Matrix_init (me.get(), tmin, tmax, nt, dt, t1, 1.0, 1.0, 1, 1.0, 1.0); |
| 76 |
return me; |
| 77 |
} catch (MelderError) { |
| 78 |
Melder_throw (U"CPP not created."); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
void CPP_draw (CPP me, Graphics g, double tmin, double tmax, double min, double max, bool garnish) { |
| 83 |
Vector_draw (me, g, & tmin, & tmax, & min, & max, my dy, U"speckles"); |
| 84 |
if (garnish) { |
| 85 |
Graphics_drawInnerBox (g); |
| 86 |
Graphics_textBottom (g, true, U"Time (s)"); |
| 87 |
Graphics_marksBottom (g, 2, true, true, false); |
| 88 |
Graphics_marksLeftEvery (g, 1.0, 10.0, true, true, false); |
| 89 |
Graphics_textLeft (g, true, U"CPP"); |
| 90 |
} |
| 91 |
} |
| 92 |
/* |
| 93 |
draw a CPP contour into the current Graphics. |
| 94 |
If tmax <= tmin, draw whole time domain. |
| 95 |
If max <= min, scale to extrema. |
| 96 |
*/ |
| 97 |
|
| 98 |
double CPP_getMean (CPP me, double tmin, double tmax) { |
| 99 |
autoVEC soundingValues = CPP_getSoundingValues (me, tmin, tmax); |
| 100 |
return NUMmean (soundingValues.get()); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
double CPP_getStandardDeviation (CPP me, double tmin, double tmax) { |
| 105 |
autoVEC soundingValues = CPP_getSoundingValues (me, tmin, tmax); |
| 106 |
return NUMstdev (soundingValues.get()); |
| 107 |
|
| 108 |
} |
| 109 |
|
| 110 |
double CPP_getQuantile (CPP me, double quantile) { |
| 111 |
autoVEC soundingValues = CPP_getSoundingValues (me, 0.0, 0.0); |
| 112 |
sort_e_VEC_inout (soundingValues.get()); |
| 113 |
return NUMquantile (soundingValues.get(), quantile); |
| 114 |
} |
| 115 |
|
| 116 |
autoCPP CPP_and_Pitch_to_CPP_markUnvoiced (CPP me, Pitch thee) { |
| 117 |
Melder_require (my xmin == thy xmin && my xmax == thy xmax, |
| 118 |
U"The domains of the CPP and the Pitch should be equal."); |
| 119 |
try { |
| 120 |
autoCPP him = Data_copy (me); |
| 121 |
for (integer i = 1; i <= my nx; i ++) { |
| 122 |
const double time = my x1 + (i - 1) * my dx; |
| 123 |
if (! Pitch_isVoiced_t (thee, time)) |
| 124 |
his z [1] [i] = 0.0; |
| 125 |
} |
| 126 |
return him; |
| 127 |
} catch (MelderError) { |
| 128 |
Melder_throw (me, U"cannot convert to object with marked unvoiced frames."); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
autoMatrix CPP_to_Matrix (CPP me) { |
| 133 |
try { |
| 134 |
autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 135 |
thy z.all() <<= my z.all(); |
| 136 |
return thee; |
| 137 |
} catch (MelderError) { |
| 138 |
Melder_throw (me, U"not converted to Matrix."); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
autoCPP Matrix_to_CPP (Matrix me) { |
| 143 |
try { |
| 144 |
autoCPP thee = CPP_create (my xmin, my xmax, my nx, my dx, my x1); |
| 145 |
thy z.all() <<= my z.row (1); |
| 146 |
return thee; |
| 147 |
} catch (MelderError) { |
| 148 |
Melder_throw (me, U"not converted to CPP."); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
/* End of file CPP.cpp */ |