| 1 |
/* FormantPath.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2020-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 d 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 "FormantPath.h" |
| 20 |
#include "FormantModeler.h" |
| 21 |
#include "Formant_extensions.h" |
| 22 |
#include "Graphics_extensions.h" |
| 23 |
#include "LPC_and_Formant.h" |
| 24 |
#include "Matrix.h" |
| 25 |
#include "Sound_to_Formant.h" |
| 26 |
#include "Sound_and_LPC.h" |
| 27 |
//#include "Sound_and_LPC.h" |
| 28 |
#include "Sound_extensions.h" |
| 29 |
#include "TextGrid_extensions.h" |
| 30 |
|
| 31 |
#include "oo_DESTROY.h" |
| 32 |
#include "FormantPath_def.h" |
| 33 |
#include "oo_COPY.h" |
| 34 |
#include "FormantPath_def.h" |
| 35 |
#include "oo_EQUAL.h" |
| 36 |
#include "FormantPath_def.h" |
| 37 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 38 |
#include "FormantPath_def.h" |
| 39 |
#include "oo_WRITE_TEXT.h" |
| 40 |
#include "FormantPath_def.h" |
| 41 |
#include "oo_WRITE_BINARY.h" |
| 42 |
#include "FormantPath_def.h" |
| 43 |
#include "oo_READ_TEXT.h" |
| 44 |
#include "FormantPath_def.h" |
| 45 |
#include "oo_READ_BINARY.h" |
| 46 |
#include "FormantPath_def.h" |
| 47 |
#include "oo_DESCRIPTION.h" |
| 48 |
#include "FormantPath_def.h" |
| 49 |
|
| 50 |
void FormantPath_getCandidateAtTime (FormantPath me, double time, double *out_tmin, double *out_tmax, integer *out_candidate) { |
| 51 |
IntervalTier intervalTier = static_cast <IntervalTier> (my path -> tiers -> at [1]); |
| 52 |
const integer index = IntervalTier_timeToIndex (intervalTier, time); |
| 53 |
TextInterval textInterval = ( index > 0 ? intervalTier -> intervals.at [index] : nullptr ); |
| 54 |
if (out_tmin) |
| 55 |
*out_tmin = ( index > 0 ? textInterval -> xmin : undefined ); |
| 56 |
if (out_tmax) |
| 57 |
*out_tmax = ( index > 0 ? textInterval -> xmax : undefined ); |
| 58 |
if (out_candidate) |
| 59 |
*out_candidate = ( index > 0 ? (textInterval -> text.get() ? Melder_atoi (textInterval -> text.get()) : 0) : 0 ); |
| 60 |
} |
| 61 |
|
| 62 |
integer FormantPath_getCandidateInFrame (FormantPath me, integer iframe) { |
| 63 |
Melder_assert (iframe > 0 && iframe <= my nx); |
| 64 |
const double time = Sampled_indexToX (me, iframe); |
| 65 |
integer candidate; |
| 66 |
FormantPath_getCandidateAtTime (me, time, nullptr, nullptr, & candidate); |
| 67 |
return candidate; |
| 68 |
} |
| 69 |
|
| 70 |
integer FormantPath_getUniqueCandidateInInterval (FormantPath me, double tmin, double tmax) { |
| 71 |
Melder_assert (tmin <= tmax); |
| 72 |
IntervalTier intervalTier = static_cast <IntervalTier> (my path -> tiers -> at [1]); |
| 73 |
const integer index = IntervalTier_timeToLowIndex (intervalTier, tmin); |
| 74 |
integer candidate = 0; |
| 75 |
if (index > 0) { |
| 76 |
TextInterval textInterval = intervalTier -> intervals.at [index]; |
| 77 |
if (tmin >= textInterval -> xmin && tmax <= textInterval -> xmax) |
| 78 |
candidate = (textInterval -> text.get() ? Melder_atoi (textInterval -> text.get()) : 0); |
| 79 |
} |
| 80 |
return candidate; |
| 81 |
} |
| 82 |
|
| 83 |
void structFormantPath :: v1_info () { |
| 84 |
structDaata :: v1_info (); |
| 85 |
MelderInfo_writeLine (U"Number of Formant candidates: ", formantCandidates.size); |
| 86 |
for (integer ic = 1; ic <= ceilings.size; ic ++) |
| 87 |
MelderInfo_writeLine (U"Ceiling ", ic, U": ", ceilings [ic], U" Hz"); |
| 88 |
} |
| 89 |
|
| 90 |
double structFormantPath :: v_getValueAtSample (integer iframe, integer which, int units) const { |
| 91 |
const double time = x1 + (iframe - 1) * dx; |
| 92 |
IntervalTier intervalTier = static_cast <IntervalTier> (path -> tiers -> at [1]); |
| 93 |
const integer index = IntervalTier_timeToIndex (intervalTier, time); |
| 94 |
const Formant formant = static_cast <Formant> (our formantCandidates.at [index]); |
| 95 |
return formant -> v_getValueAtSample (iframe, which, units); |
| 96 |
} |
| 97 |
|
| 98 |
conststring32 structFormantPath :: v_getUnitText (integer /*level*/, int /*unit*/, uint32 /*flags*/) const { |
| 99 |
return U"Frequency (Hz)"; |
| 100 |
}; |
| 101 |
|
| 102 |
Thing_implement (FormantPath, Sampled, 1); // version 0: INTVEC path; version 1: TextGrid path |
| 103 |
|
| 104 |
|
| 105 |
static MelderIntegerRange FormantPath_getPathTierIndicesRange (FormantPath me, double tmin, double tmax) { |
| 106 |
Melder_assert (tmin < tmax); |
| 107 |
MelderIntegerRange range = {1, 1}; |
| 108 |
IntervalTier intervalTier = static_cast <IntervalTier> (my path -> tiers -> at [1]); |
| 109 |
range.first = IntervalTier_timeToLowIndex (intervalTier, tmin); |
| 110 |
range.last = IntervalTier_timeToHighIndex (intervalTier, tmax); |
| 111 |
return range; |
| 112 |
} |
| 113 |
|
| 114 |
autoTextGrid FormantPath_to_TextGrid_version0 (FormantPath me, INTVEC const& path) { |
| 115 |
autoTextGrid thee = TextGrid_create (my xmin, my xmax, U"path", U""); |
| 116 |
IntervalTier tier = static_cast <IntervalTier> (thy tiers -> at [1]); |
| 117 |
integer previousPathIndex = path [1]; |
| 118 |
TextInterval_setText (tier ->intervals.at [1], Melder_integer (previousPathIndex)); |
| 119 |
TextGrid_setIntervalText (thee.get(), 1, 1, Melder_integer (previousPathIndex)); |
| 120 |
for (integer ip = 2; ip <= path.size; ip ++) { |
| 121 |
if (path [ip] != previousPathIndex) { |
| 122 |
const double t = Sampled_indexToX (me, ip) - 0.5 * my dx; |
| 123 |
const integer currentIndex = IntervalTier_timeToLowIndex (tier, t); |
| 124 |
TextInterval currentInterval = tier -> intervals.at [currentIndex]; |
| 125 |
autoTextInterval newInterval = TextInterval_create (t, my xmax, Melder_integer (path [ip])); |
| 126 |
currentInterval -> xmax = t; |
| 127 |
tier -> intervals. addItem_move (newInterval.move()); |
| 128 |
previousPathIndex = path [ip]; |
| 129 |
} |
| 130 |
} |
| 131 |
return thee; |
| 132 |
} |
| 133 |
|
| 134 |
autoFormantPath FormantPath_create (double xmin, double xmax, integer nx, double dx, double x1, integer numberOfCandidates) { |
| 135 |
autoFormantPath me = Thing_new (FormantPath); |
| 136 |
Sampled_init (me.get (), xmin, xmax, nx, dx, x1); |
| 137 |
my ceilings = zero_VEC (numberOfCandidates); |
| 138 |
my path = TextGrid_create (xmin, xmax, U"path", U""); |
| 139 |
return me; |
| 140 |
} |
| 141 |
|
| 142 |
void FormantPath_pathFinder (FormantPath me, double qWeight, double frequencyChangeWeight, double stressWeight, double ceilingChangeWeight, |
| 143 |
double intensityModulationStepSize, double windowLength, constINTVEC const& parameters, double powerf) |
| 144 |
{ |
| 145 |
autoINTVEC path = FormantPath_getOptimumPath (me, qWeight, frequencyChangeWeight, stressWeight, ceilingChangeWeight, intensityModulationStepSize, windowLength, parameters, powerf, nullptr); |
| 146 |
my path = FormantPath_to_TextGrid_version0 (me, path.get()); |
| 147 |
} |
| 148 |
|
| 149 |
autoINTVEC FormantPath_getOptimumPath (FormantPath me, double qWeight, double frequencyChangeWeight, double stressWeight, double ceilingChangeWeight, double intensityModulationStepSize, double windowLength, constINTVEC const& parameters, double powerf, autoMatrix *out_delta) { |
| 150 |
constexpr double qCutoff = 20.0; |
| 151 |
constexpr double stressCutoff = 100.0; |
| 152 |
try { |
| 153 |
const integer transtionCostType = ( Melder_debug == -3 ? 2 : 1 ); |
| 154 |
const integer numberOfCandidates = my formantCandidates.size; |
| 155 |
const double transitionCostCuttoff = ( Melder_debug == -3 ? 0.3 : 100.0 ); |
| 156 |
autoMatrix stresses, qsums; |
| 157 |
MelderExtremaWithInit intensities; |
| 158 |
const integer midformant = (numberOfCandidates + 1) / 2; |
| 159 |
if (intensityModulationStepSize > 0.0) { |
| 160 |
for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 161 |
const Formant_Frame frame = & my formantCandidates.at [midformant] -> frames [iframe]; |
| 162 |
intensities.update (frame -> intensity); |
| 163 |
} |
| 164 |
} |
| 165 |
const bool hasIntensityDifference = ( intensities.max - intensities.min > 0.0 ); |
| 166 |
const double dbMid = 0.5 * 10.0 * log10 (intensities.max * intensities.min); |
| 167 |
const integer maxnFormants = my formantCandidates.at [1] -> maxnFormants; |
| 168 |
const integer numberOfTracks = std::min (maxnFormants, parameters.size); |
| 169 |
if (qWeight > 0.0) |
| 170 |
qsums = FormantPath_to_Matrix_qSums (me, numberOfTracks); |
| 171 |
if (stressWeight > 0.0) |
| 172 |
stresses = FormantPath_to_Matrix_stress (me, windowLength, parameters, powerf); |
| 173 |
|
| 174 |
autoINTMAT psi = zero_INTMAT (numberOfCandidates, my nx); |
| 175 |
autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, numberOfCandidates + 0.5, numberOfCandidates, 1.0, 1.0); |
| 176 |
/* |
| 177 |
delta [i] [j] = minimum cost to reach state i at time j |
| 178 |
*/ |
| 179 |
MAT delta (& thy z [1] [1], thy ny, thy nx); |
| 180 |
autoINTVEC path = zero_INTVEC (my nx); |
| 181 |
autoVEC intensity = raw_VEC (my nx); |
| 182 |
/* |
| 183 |
We have a trellis of size S x T, where S is the number of states, i.e. the number of formant objects, |
| 184 |
and T the number of frames (S= formantCandidates.size and T=nx). |
| 185 |
Evaluate the static costs for state s [i] at times t=1..T |
| 186 |
There are two components at each t: |
| 187 |
1. (+) the local stress at s [i] [t], which is evaluated from an interval around time t |
| 188 |
2. (-) the local qsums at s [i] [t], evaluated from the frequencies/bandwidths in s [i] [t] |
| 189 |
The sum of these components might be modulated by the local intensity |
| 190 |
*/ |
| 191 |
for (integer itime = 1; itime <= my nx; itime ++) { |
| 192 |
for (integer iformant = 1; iformant <= numberOfCandidates; iformant ++) { |
| 193 |
const Formant_Frame frame = & my formantCandidates.at [iformant] -> frames [itime]; |
| 194 |
double wIntensity = 1.0, costs = 0.0; |
| 195 |
if (hasIntensityDifference && intensityModulationStepSize > 0.0) { |
| 196 |
if (frame -> intensity > 0.0) { |
| 197 |
const double dbi = 10.0 * log10 (frame -> intensity / 2e-5); |
| 198 |
wIntensity = NUMsigmoid ((dbi - dbMid) / intensityModulationStepSize); |
| 199 |
} else |
| 200 |
wIntensity = 0.0; |
| 201 |
} |
| 202 |
if (stressWeight > 0.0 && isdefined (stresses -> z [iformant] [itime])) |
| 203 |
costs += stressWeight * std::min (stresses -> z [iformant] [itime] / stressCutoff, 1.0); |
| 204 |
if (qWeight > 0.0) |
| 205 |
costs -= qWeight * std::min (qsums -> z [iformant] [itime] / qCutoff, 1.0); |
| 206 |
delta [iformant] [itime] += wIntensity * costs; |
| 207 |
} |
| 208 |
} |
| 209 |
/* |
| 210 |
Evaluate the costs delta [j] [t] going from s [i] [t-1] to s [j] [t] |
| 211 |
delta [j] [t] = min_over_i { delta [i] [t-1] + transition_costs (s [i] [t-1], s [j] [t] }, |
| 212 |
where the transition_costs consists of two parts: |
| 213 |
3. (+) costs involving the sum of the distances between the formant frequencies in state s [i] [t-1] and s [j] [t] |
| 214 |
4. (+) costs involving the difference in ceiling [i] [t-1] and ceiling [j] [t] |
| 215 |
(or should we measure the costs w.r.t the middle frequency?) |
| 216 |
*/ |
| 217 |
const double ceilingsRange = my ceilings [numberOfCandidates] - my ceilings [1]; |
| 218 |
for (integer itime = 2; itime <= my nx; itime ++) { |
| 219 |
for (integer iformant = 1; iformant <= numberOfCandidates; iformant++) { |
| 220 |
const Formant_Frame ffi = & my formantCandidates.at [iformant] -> frames [itime]; |
| 221 |
const integer numberOfTracks_i = std::min (numberOfTracks, ffi -> numberOfFormants); |
| 222 |
double deltamin = 1e100; |
| 223 |
integer minPos = 0; |
| 224 |
for (integer jformant = 1; jformant <= numberOfCandidates; jformant++) { |
| 225 |
const Formant_Frame ffj = & my formantCandidates.at [jformant] -> frames [itime - 1]; |
| 226 |
const integer ntracks = std::min (ffj -> numberOfFormants, numberOfTracks_i); |
| 227 |
double transitionCosts = delta [jformant] [itime - 1]; |
| 228 |
if (frequencyChangeWeight > 0.0) { |
| 229 |
double fcost = 0.0; |
| 230 |
for (integer itrack = 1; itrack <= ntracks; itrack ++) { |
| 231 |
const double fi = ffi -> formant [itrack]. frequency, fj = ffj -> formant [itrack]. frequency; |
| 232 |
if (transtionCostType == 1) { |
| 233 |
const double dif = fabs (fi - fj); |
| 234 |
const double sum = fi + fj; |
| 235 |
const double bw = sqrt (ffi -> formant [itrack]. bandwidth * ffj -> formant [itrack]. bandwidth); |
| 236 |
fcost += bw * dif / sum; |
| 237 |
} else |
| 238 |
fcost += fabs (NUMlog2 (fi / fj)); |
| 239 |
} |
| 240 |
fcost /= ntracks; |
| 241 |
transitionCosts += frequencyChangeWeight * std::min (fcost / transitionCostCuttoff, 1.0); |
| 242 |
} |
| 243 |
if (ceilingChangeWeight > 0.0) { |
| 244 |
const double ceilingChangeCosts = fabs (my ceilings [iformant] - my ceilings [jformant]) / ceilingsRange; |
| 245 |
transitionCosts += ceilingChangeWeight * ceilingChangeCosts; |
| 246 |
} |
| 247 |
if (transitionCosts < deltamin) { |
| 248 |
deltamin = transitionCosts; |
| 249 |
minPos = jformant; |
| 250 |
} |
| 251 |
} |
| 252 |
if (stressWeight > 0.0 && isdefined (stresses -> z [iformant] [itime])) |
| 253 |
deltamin += stressWeight * std::min (stresses -> z [iformant] [itime] / stressCutoff, 1.0); |
| 254 |
if (qWeight > 0.0) |
| 255 |
deltamin -= qWeight * std::min (qsums -> z [iformant] [itime] / qCutoff, 1.0); |
| 256 |
delta [iformant] [itime] += deltamin; |
| 257 |
psi [iformant] [itime] = minPos; |
| 258 |
} |
| 259 |
} |
| 260 |
path [my nx] = NUMmaxPos (delta.column (my nx)); |
| 261 |
/* |
| 262 |
Backtrack |
| 263 |
*/ |
| 264 |
for (integer itime = my nx; itime > 1; itime --) |
| 265 |
path [itime - 1] = psi [path [itime]] [itime]; |
| 266 |
if (out_delta) |
| 267 |
*out_delta = thee.move(); |
| 268 |
return path; |
| 269 |
} catch (MelderError) { |
| 270 |
Melder_throw (me, U": cannot find path."); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
autoFormant FormantPath_extractFormant (FormantPath me) { |
| 275 |
Melder_assert (my formantCandidates.size > 0); |
| 276 |
Formant formant = my formantCandidates.at [1]; |
| 277 |
autoFormant thee = Formant_create (my xmin, my xmax, my nx, my dx, my x1, formant -> maxnFormants); |
| 278 |
for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 279 |
const integer candidate = FormantPath_getCandidateInFrame (me, iframe); |
| 280 |
Formant source = static_cast <Formant> (my formantCandidates.at [candidate]); |
| 281 |
Formant_Frame targetFrame = & thy frames [iframe]; |
| 282 |
Formant_Frame sourceFrame = & source -> frames [iframe]; |
| 283 |
sourceFrame -> copy (targetFrame); |
| 284 |
} |
| 285 |
return thee; |
| 286 |
} |
| 287 |
|
| 288 |
static autoVEC getCeilings (double middleCeiling, double stepSize, integer numberOfStepsUpDown) { |
| 289 |
const integer numberOfCeilings = 2 * numberOfStepsUpDown + 1, mid = numberOfStepsUpDown + 1; |
| 290 |
autoVEC ceilings = raw_VEC (numberOfCeilings); |
| 291 |
for (integer istep = 1; istep <= numberOfStepsUpDown; istep ++) { |
| 292 |
ceilings [mid + istep] = middleCeiling * exp ( stepSize * istep); |
| 293 |
ceilings [mid - istep] = middleCeiling * exp (- stepSize * istep); |
| 294 |
} |
| 295 |
ceilings [mid] = middleCeiling; |
| 296 |
return ceilings; |
| 297 |
} |
| 298 |
|
| 299 |
autoFormantPath Sound_to_FormantPath_any (Sound me, kLPC_Analysis lpcType, double timeStep, double maximumNumberOfFormants, |
| 300 |
double middleCeiling, double analysisWidth, double preemphasisFrequency, double ceilingStepSize, |
| 301 |
integer numberOfStepsUpDown, double marple_tol1, double marple_tol2, double huber_numberOfStdDev, double huber_tol, |
| 302 |
integer huber_maximumNumberOfIterations, autoSound *out_sourcesMultiChannel) |
| 303 |
{ |
| 304 |
try { |
| 305 |
Melder_require (timeStep > 0.0, |
| 306 |
U"The timeStep needs to be larger than zero seconds."); |
| 307 |
Melder_require (ceilingStepSize > 0.0, |
| 308 |
U"The ceiling step size should be larger than 0.0."); |
| 309 |
autoVEC ceilings = getCeilings (middleCeiling, ceilingStepSize, numberOfStepsUpDown); |
| 310 |
const integer numberOfCandidates = ceilings.size; |
| 311 |
const double maximumCeiling = ceilings [numberOfCandidates]; |
| 312 |
const double nyquistFrequency = 0.5 / my dx; |
| 313 |
Melder_require (maximumCeiling <= nyquistFrequency, |
| 314 |
U"The calculated maximum ceiling is higher than the Nyquist frequency of the sound (", nyquistFrequency, U" Hz). " |
| 315 |
"Decrease the 'ceiling step size' or the 'number of steps' or both."); |
| 316 |
volatile const double physicalAnalysisWidth = Melder_clippedRight (2.0 * analysisWidth, my dx * my nx); |
| 317 |
/* |
| 318 |
If the resampled sound with the lowest sampling frequency passes the minimal duration tests then all will. |
| 319 |
*/ |
| 320 |
const double samplingFrequencyLowest = 2.0 * ceilings [1], dxLowest = 1.0 / samplingFrequencyLowest; |
| 321 |
const integer nxLowest = Melder_iroundDown (my dx * my nx / dxLowest); |
| 322 |
const integer predictionOrder = Melder_iround (2.0 * maximumNumberOfFormants); |
| 323 |
|
| 324 |
checkLPCAnalysisParameters_e (dxLowest, nxLowest, physicalAnalysisWidth, predictionOrder); |
| 325 |
|
| 326 |
/* |
| 327 |
Get the data for the LPC from the resampled sound with 'middleCeiling' as maximum frequency |
| 328 |
to make the sampling exactly equal as if performed with a standard LPC analysis. |
| 329 |
*/ |
| 330 |
integer numberOfFrames; |
| 331 |
double t1; |
| 332 |
autoSound midCeiling = Sound_resampleAndOrPreemphasize (me, middleCeiling, 50, preemphasisFrequency); |
| 333 |
Sampled_shortTermAnalysis (midCeiling.get(), physicalAnalysisWidth, timeStep, & numberOfFrames, & t1); // Gaussian window |
| 334 |
autoFormantPath thee = FormantPath_create (my xmin, my xmax, numberOfFrames, timeStep, t1, numberOfCandidates); |
| 335 |
autoSound multiChannelSound; |
| 336 |
if (out_sourcesMultiChannel) |
| 337 |
multiChannelSound = Sound_create (numberOfCandidates, midCeiling -> xmin, midCeiling -> xmax, midCeiling -> nx, midCeiling -> dx, midCeiling -> x1); |
| 338 |
const double formantSafetyMargin = 50.0; |
| 339 |
thy ceilings = ceilings.move(); |
| 340 |
for (integer candidate = 1; candidate <= numberOfCandidates; candidate ++) { |
| 341 |
autoFormant formant; |
| 342 |
autoSound resampledAndPreemphasized; |
| 343 |
if (candidate != numberOfStepsUpDown + 1) |
| 344 |
resampledAndPreemphasized = Sound_resampleAndOrPreemphasize (me, thy ceilings [candidate], 50, preemphasisFrequency); |
| 345 |
else |
| 346 |
resampledAndPreemphasized = midCeiling.move(); |
| 347 |
autoLPC lpc = LPC_createCompletelyInitialized (my xmin, my xmax, numberOfFrames, timeStep, t1, predictionOrder, |
| 348 |
resampledAndPreemphasized -> dx); |
| 349 |
if (lpcType == kLPC_Analysis::BURG) { |
| 350 |
Sound_into_LPC_burg (resampledAndPreemphasized.get(), lpc.get(), analysisWidth); |
| 351 |
} else if (lpcType == kLPC_Analysis::AUTOCORRELATION) { |
| 352 |
Sound_into_LPC_auto (resampledAndPreemphasized.get(), lpc.get(), analysisWidth); |
| 353 |
} else if (lpcType == kLPC_Analysis::COVARIANCE) { |
| 354 |
Sound_into_LPC_covar (resampledAndPreemphasized.get(), lpc.get(), analysisWidth); |
| 355 |
} else if (lpcType == kLPC_Analysis::MARPLE) { |
| 356 |
Sound_into_LPC_marple (resampledAndPreemphasized.get(), lpc.get(), analysisWidth, marple_tol1, marple_tol2); |
| 357 |
} else if (lpcType == kLPC_Analysis::ROBUST) { |
| 358 |
Sound_into_LPC_auto (resampledAndPreemphasized.get(), lpc.get(), analysisWidth); |
| 359 |
lpc = LPC_and_Sound_to_LPC_robust (lpc.get(), resampledAndPreemphasized.get(), analysisWidth, preemphasisFrequency, |
| 360 |
huber_numberOfStdDev, huber_maximumNumberOfIterations, huber_tol, true); |
| 361 |
} |
| 362 |
formant = LPC_to_Formant (lpc.get(), formantSafetyMargin); |
| 363 |
thy formantCandidates. addItem_move (formant.move()); |
| 364 |
if (out_sourcesMultiChannel) { |
| 365 |
// TODO 20240625 is this still correct because we have already pre-emphasized the sound?? |
| 366 |
autoSound source = LPC_Sound_filterInverse (lpc.get(), resampledAndPreemphasized.get ()); |
| 367 |
autoSound source_resampled = Sound_resample (source.get(), 2.0 * middleCeiling, 50); |
| 368 |
const integer numberOfSamples = std::min (midCeiling -> nx, source_resampled -> nx); |
| 369 |
multiChannelSound -> z.row (candidate).part (1, numberOfSamples) <<= source_resampled -> z.row (1).part (1, numberOfSamples); |
| 370 |
} |
| 371 |
} |
| 372 |
/* |
| 373 |
Maintain invariants |
| 374 |
*/ |
| 375 |
Melder_assert (thy formantCandidates.size == numberOfCandidates); |
| 376 |
TextGrid_setIntervalText (thy path.get(), 1, 1, Melder_integer (numberOfStepsUpDown + 1)); |
| 377 |
if (out_sourcesMultiChannel) |
| 378 |
*out_sourcesMultiChannel = multiChannelSound.move(); |
| 379 |
return thee; |
| 380 |
} catch (MelderError) { |
| 381 |
Melder_throw (me, U": FormantPath not created."); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
integer FormantPath_getNumberOfFormantTracks (FormantPath me) { |
| 386 |
Melder_assert (my formantCandidates.size > 0); |
| 387 |
return my formantCandidates.at [1] -> maxnFormants; |
| 388 |
} |
| 389 |
|
| 390 |
autoMatrix FormantPath_to_Matrix_qSums (FormantPath me, integer numberOfTracks) { |
| 391 |
try { |
| 392 |
Melder_assert (numberOfTracks >= 0); |
| 393 |
const integer numberOfCandidates = my formantCandidates.size; |
| 394 |
autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, numberOfCandidates + 0.5, numberOfCandidates, 1.0, 1.0); |
| 395 |
const integer maxnFormants = FormantPath_getNumberOfFormantTracks (me); |
| 396 |
if (numberOfTracks == 0) |
| 397 |
numberOfTracks = maxnFormants; |
| 398 |
for (integer itime = 1; itime <= my nx; itime ++) { |
| 399 |
for (integer candidate = 1; candidate <= numberOfCandidates; candidate ++) { |
| 400 |
const Formant_Frame frame = & my formantCandidates.at [candidate] -> frames [itime]; |
| 401 |
const integer currentNumberOfFormants = std::min (numberOfTracks, frame -> numberOfFormants); |
| 402 |
longdouble qsum = 0.0; |
| 403 |
for (integer itrack = 1; itrack <= currentNumberOfFormants; itrack ++) |
| 404 |
qsum += frame -> formant [itrack]. frequency / frame -> formant [itrack]. bandwidth; |
| 405 |
thy z [candidate] [itime] = ( currentNumberOfFormants > 0 ? (double) (qsum / currentNumberOfFormants) : 0.0 ); |
| 406 |
} |
| 407 |
} |
| 408 |
return thee; |
| 409 |
} catch (MelderError) { |
| 410 |
Melder_throw (me, U": cannot calculate qsums."); |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
autoMatrix FormantPath_to_Matrix_transition (FormantPath me, integer numberOfTracks, bool maximumCosts) { |
| 415 |
try { |
| 416 |
Melder_assert (numberOfTracks >= 0); |
| 417 |
const integer numberOfCandidates = my formantCandidates.size; |
| 418 |
autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, numberOfCandidates + 0.5, numberOfCandidates, 1.0, 1.0); |
| 419 |
const integer maxnFormants = FormantPath_getNumberOfFormantTracks (me); |
| 420 |
if (numberOfTracks == 0) |
| 421 |
numberOfTracks = maxnFormants; |
| 422 |
for (integer itime = 2; itime <= my nx; itime ++) { |
| 423 |
for (integer candidate = 1; candidate <= numberOfCandidates; candidate ++) { |
| 424 |
const Formant_Frame ffi = & my formantCandidates.at [candidate] -> frames [itime]; |
| 425 |
const integer numberOfTracks_i = std::min (numberOfTracks, ffi -> numberOfFormants); |
| 426 |
MelderExtremaWithInit costs; |
| 427 |
for (integer jformant = 1; jformant <= my formantCandidates.size; jformant++) { |
| 428 |
const Formant_Frame ffj = & my formantCandidates.at [jformant] -> frames [itime - 1]; |
| 429 |
const integer ntracks = std::min (ffj -> numberOfFormants, numberOfTracks_i); |
| 430 |
longdouble transitionCosts = 0.0; |
| 431 |
for (integer itrack = 1; itrack <= ntracks; itrack ++) { |
| 432 |
const double fi = ffi -> formant [itrack]. frequency, fj = ffj -> formant [itrack]. frequency; |
| 433 |
const double dif = fabs (fi - fj); |
| 434 |
const double sum = fi + fj; |
| 435 |
const double bw = sqrt (ffi -> formant [itrack]. bandwidth * ffj -> formant [itrack]. bandwidth); |
| 436 |
double cost = bw * dif / sum; |
| 437 |
if (Melder_debug == -3) |
| 438 |
cost = fabs (NUMlog2 (fi / fj)); |
| 439 |
transitionCosts += cost; |
| 440 |
} |
| 441 |
transitionCosts /= ntracks; |
| 442 |
costs.update ((double) transitionCosts); |
| 443 |
} |
| 444 |
thy z [candidate] [itime] = ( maximumCosts ? costs.max : costs.min ); |
| 445 |
} |
| 446 |
} |
| 447 |
return thee; |
| 448 |
} catch (MelderError) { |
| 449 |
Melder_throw (me, U": cannot calculate transition costs."); |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
autoMatrix FormantPath_to_Matrix_stress (FormantPath me, double windowLength, constINTVEC const& parameters, double powerf) { |
| 454 |
try { |
| 455 |
const integer numberOfCandidates = my formantCandidates.size; |
| 456 |
const integer maxnFormants = FormantPath_getNumberOfFormantTracks (me); |
| 457 |
Melder_require (parameters.size > 0 && parameters.size <= maxnFormants, |
| 458 |
U"The number of parameters should be between 1 and ", maxnFormants, U"."); |
| 459 |
integer fromFormant = 1; |
| 460 |
const integer maximumNumberOfCoefficients = NUMmax_e (parameters); |
| 461 |
const integer numberOfDataPoints = (windowLength + 0.5 * my dx) / my dx; |
| 462 |
Melder_require (numberOfDataPoints >= maximumNumberOfCoefficients, |
| 463 |
U"The window length is too short for the number of coefficients you use in the stress determination (", |
| 464 |
maximumNumberOfCoefficients, U"). Either increase your window length or decrease the number of coefficients per track." |
| 465 |
); |
| 466 |
while (fromFormant <= parameters.size && parameters [fromFormant] <= 0) |
| 467 |
fromFormant ++; |
| 468 |
integer toFormant = parameters.size; |
| 469 |
while (toFormant > 0 && parameters [toFormant] <= 0) |
| 470 |
toFormant --; |
| 471 |
Melder_require (fromFormant <= toFormant, |
| 472 |
U"Not all parameter values should equal zero."); |
| 473 |
autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, numberOfCandidates + 0.5, numberOfCandidates, 1.0, 1.0); |
| 474 |
for (integer candidate = 1; candidate <= numberOfCandidates; candidate ++) { |
| 475 |
const Formant formanti = (Formant) my formantCandidates.at [candidate]; |
| 476 |
for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 477 |
const double time = my x1 + (iframe - 1) * my dx; |
| 478 |
const double startTime = time - 0.5 * windowLength; |
| 479 |
const double endTime = time + 0.5 * windowLength; |
| 480 |
autoFormantModeler fm = Formant_to_FormantModeler (formanti, startTime, endTime, parameters); |
| 481 |
thy z [candidate] [iframe] = FormantModeler_getStress (fm.get(), fromFormant, toFormant, 0, powerf); |
| 482 |
} |
| 483 |
} |
| 484 |
return thee; |
| 485 |
} catch (MelderError) { |
| 486 |
Melder_throw (me, U": cannot create stress Matrix"); |
| 487 |
} |
| 488 |
} |
| 489 |
|
| 490 |
|
| 491 |
double FormantPath_getStressOfCandidate (FormantPath me, double tmin, double tmax, integer fromFormant, integer toFormant, |
| 492 |
constINTVEC const& parameters, double powerf, integer candidate) |
| 493 |
{ |
| 494 |
Melder_assert (candidate > 0); |
| 495 |
Melder_require (candidate <= my formantCandidates.size, |
| 496 |
U"The candidate number should be between 1 and ", my formantCandidates.size, U"."); |
| 497 |
const Formant formant = (Formant) my formantCandidates.at [candidate]; |
| 498 |
autoFormantModeler fm = Formant_to_FormantModeler (formant, tmin, tmax, parameters); |
| 499 |
return FormantModeler_getStress (fm.get(), fromFormant, toFormant, 0, powerf); |
| 500 |
} |
| 501 |
|
| 502 |
autoVEC FormantPath_getStressOfCandidates (FormantPath me, double tmin, double tmax, |
| 503 |
integer fromFormant, integer toFormant, constINTVEC const& parameters, double powerf) |
| 504 |
{ |
| 505 |
autoVEC stresses = raw_VEC (my formantCandidates.size); |
| 506 |
for (integer candidate = 1; candidate <= my formantCandidates.size; candidate ++) |
| 507 |
stresses [candidate] = FormantPath_getStressOfCandidate (me, tmin, tmax, fromFormant, toFormant, parameters, powerf, candidate); |
| 508 |
return stresses; |
| 509 |
} |
| 510 |
|
| 511 |
double FormantPath_getOptimalCeiling (FormantPath me, double tmin, double tmax, constINTVEC const& parameters, double powerf) { |
| 512 |
autoVEC stresses = FormantPath_getStressOfCandidates (me, tmin, tmax, 0, 0, parameters, powerf); |
| 513 |
const integer minPos = NUMminPos (stresses.get()); |
| 514 |
return my ceilings [minPos]; |
| 515 |
} |
| 516 |
|
| 517 |
void FormantPath_setPath (FormantPath me, double tmin, double tmax, integer selectedCandidate) { |
| 518 |
Melder_assert (selectedCandidate > 0); |
| 519 |
Melder_require (selectedCandidate <= my formantCandidates.size, |
| 520 |
U"The candidate number should be between 1 and ", my formantCandidates.size, U"."); |
| 521 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 522 |
Function_intersectRangeWithDomain (me, & tmin, & tmax); |
| 523 |
const double ceilingFrequency = my ceilings [selectedCandidate]; |
| 524 |
TextGrid_addInterval_force (my path.get(), tmin, tmax, 1, Melder_double (ceilingFrequency)); |
| 525 |
} |
| 526 |
|
| 527 |
void FormantPath_setOptimalPath (FormantPath me, double tmin, double tmax, constINTVEC const& parameters, double powerf) { |
| 528 |
autoVEC stresses = FormantPath_getStressOfCandidates (me, tmin, tmax, 0, 0, parameters, powerf); |
| 529 |
const integer minPos = NUMminPos (stresses.get()); |
| 530 |
FormantPath_setPath (me, tmin, tmax, minPos); |
| 531 |
} |
| 532 |
|
| 533 |
autoTable FormantPath_downTo_Table_stresses (FormantPath me, double tmin, double tmax, constINTVEC const& parameters, |
| 534 |
double powerf, integer numberOfStressDecimals, bool includeIntervalTimes, integer numberOfTimeDecimals) |
| 535 |
{ |
| 536 |
try { |
| 537 |
autoVEC stresses = FormantPath_getStressOfCandidates (me, tmin, tmax, 0, 0, parameters, powerf); |
| 538 |
const integer numberOfCandidates = my formantCandidates.size; |
| 539 |
const integer numberOfFormantsInFit = parameters.size; |
| 540 |
autoTable thee = Table_createWithoutColumnNames (numberOfCandidates, includeIntervalTimes + 1 + includeIntervalTimes + numberOfFormantsInFit + numberOfFormantsInFit - 1); |
| 541 |
integer icol = 0; |
| 542 |
if (includeIntervalTimes) { |
| 543 |
Table_renameColumn_e (thee.get(), 1, U"Start(s)"); |
| 544 |
Table_renameColumn_e (thee.get(), 2, U"End(s)"); |
| 545 |
for (integer irow = 1; irow <= numberOfCandidates; irow ++) { |
| 546 |
Table_setStringValue (thee.get(), irow, 1, Melder_fixed (tmin, numberOfTimeDecimals)); |
| 547 |
Table_setStringValue (thee.get(), irow, 2, Melder_fixed (tmax, numberOfTimeDecimals)); |
| 548 |
} |
| 549 |
icol = 2; |
| 550 |
} |
| 551 |
Table_renameColumn_e (thee.get(), ++ icol, U"Ceiling(Hz)"); |
| 552 |
for (integer irow = 1; irow <= numberOfCandidates; irow ++) |
| 553 |
Table_setStringValue (thee.get(), irow, icol, Melder_fixed (my ceilings [irow], 1)); |
| 554 |
|
| 555 |
for (integer iformant = 1; iformant <= numberOfFormantsInFit; iformant ++) { |
| 556 |
Table_renameColumn_e (thee.get(), ++ icol, Melder_cat (U"Stress", iformant)); |
| 557 |
autoVEC stresses_fi = FormantPath_getStressOfCandidates (me, tmin, tmax, iformant, iformant, parameters, powerf); |
| 558 |
for (integer irow = 1; irow <= numberOfCandidates; irow ++) |
| 559 |
Table_setStringValue (thee.get(), irow, icol, Melder_fixed (stresses_fi [irow], numberOfStressDecimals)); |
| 560 |
} |
| 561 |
for (integer iformant = 2; iformant <= numberOfFormantsInFit; iformant ++) { |
| 562 |
Table_renameColumn_e (thee.get(), ++ icol, Melder_cat (U"Stress", 1, iformant)); |
| 563 |
autoVEC stresses_fij = FormantPath_getStressOfCandidates (me, tmin, tmax, 1, iformant, parameters, powerf); |
| 564 |
for (integer irow = 1; irow <= numberOfCandidates; irow ++) |
| 565 |
Table_setStringValue (thee.get(), irow, icol, Melder_fixed (stresses_fij [irow], numberOfStressDecimals)); |
| 566 |
} |
| 567 |
return thee; |
| 568 |
} catch (MelderError) { |
| 569 |
Melder_throw (me, U": cannot create Table with stresses of fit."); |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
autoTable FormantPath_downTo_Table_optimalInterval (FormantPath me, double tmin, double tmax, |
| 574 |
constINTVEC const& parameters, double powerf, bool includeFrameNumber, bool includeTime, integer numberOfTimeDecimals, |
| 575 |
bool includeIntensity, integer numberOfIntensityDecimals, bool includeNumberOfFormants, integer numberOfFrequencyDecimals, |
| 576 |
bool includeBandwidths, bool includeOptimumCeiling, bool includeMinimumStress) |
| 577 |
{ |
| 578 |
try { |
| 579 |
Melder_assert (numberOfTimeDecimals >= 0 && numberOfIntensityDecimals >= 0 && numberOfFrequencyDecimals >= 0); |
| 580 |
autoVEC stresses = FormantPath_getStressOfCandidates (me, tmin, tmax, 0, 0, parameters, powerf); |
| 581 |
const integer minPos = NUMminPos (stresses.get()); |
| 582 |
const integer minPosFallBack = ( minPos != 0 ? minPos : stresses.size / 2 ); |
| 583 |
const Formant formant = (Formant) my formantCandidates.at [minPosFallBack]; |
| 584 |
integer ifmin, ifmax; |
| 585 |
Sampled_getWindowSamples (formant, tmin, tmax, & ifmin, & ifmax); |
| 586 |
autoFormant thee = Formant_extractPart (formant, tmin, tmax); |
| 587 |
autoTable him = Formant_downto_Table (thee.get(), includeFrameNumber, includeTime, numberOfTimeDecimals, |
| 588 |
includeIntensity, numberOfIntensityDecimals, includeNumberOfFormants, numberOfFrequencyDecimals, includeBandwidths); |
| 589 |
if (includeFrameNumber) { |
| 590 |
/* |
| 591 |
Correct frame number, it has to start at ifmin instead of 1 |
| 592 |
*/ |
| 593 |
for (integer irow = 1; irow <= his rows.size; irow ++) |
| 594 |
Table_setNumericValue (him.get(), irow, 1, ifmin + irow - 1); |
| 595 |
} |
| 596 |
if (includeOptimumCeiling) { |
| 597 |
Table_appendColumn (him.get(), U"Ceiling(Hz)"); |
| 598 |
for (integer irow = 1; irow <= his rows.size; irow ++) |
| 599 |
Table_setStringValue (him.get(), irow, his numberOfColumns, Melder_fixed (my ceilings [minPosFallBack], 1)); |
| 600 |
} |
| 601 |
if (includeMinimumStress) { |
| 602 |
Table_appendColumn (him.get(), U"Stress"); |
| 603 |
for (integer irow = 1; irow <= his rows.size; irow ++) |
| 604 |
Table_setStringValue (him.get(), irow, his numberOfColumns, Melder_fixed (stresses [minPosFallBack], 2)); |
| 605 |
} |
| 606 |
return him; |
| 607 |
} catch (MelderError) { |
| 608 |
Melder_throw (me, U"could not list as Table."); |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
static void Formant_speckles_inside (Formant me, Graphics g, double tmin, double tmax, double fmin, double fmax, |
| 613 |
integer fromFormant, integer toFormant, double suppress_dB, bool drawBandWidths, MelderColour oddNumberedFormants, MelderColour evenNumberedFormants) |
| 614 |
{ |
| 615 |
double maximumIntensity = 0.0, minimumIntensity; |
| 616 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 617 |
integer ifmin, ifmax; |
| 618 |
if (! Sampled_getWindowSamples (me, tmin, tmax, & ifmin, & ifmax)) |
| 619 |
return; |
| 620 |
if (fromFormant == 0 && toFormant == 0) { |
| 621 |
fromFormant = 1; |
| 622 |
toFormant = my maxnFormants; |
| 623 |
} |
| 624 |
Graphics_setWindow (g, tmin, tmax, fmin, fmax); |
| 625 |
|
| 626 |
for (integer iframe = ifmin; iframe <= ifmax; iframe ++) { |
| 627 |
const Formant_Frame frame = & my frames [iframe]; |
| 628 |
if (frame -> intensity > maximumIntensity) |
| 629 |
maximumIntensity = frame -> intensity; |
| 630 |
} |
| 631 |
if (maximumIntensity == 0.0 || suppress_dB <= 0.0) |
| 632 |
minimumIntensity = 0.0; // ignore |
| 633 |
else |
| 634 |
minimumIntensity = maximumIntensity / pow (10.0, suppress_dB / 10.0); |
| 635 |
|
| 636 |
for (integer iframe = ifmin; iframe <= ifmax; iframe ++) { |
| 637 |
const Formant_Frame frame = & my frames [iframe]; |
| 638 |
const double x = Sampled_indexToX (me, iframe); |
| 639 |
if (frame -> intensity < minimumIntensity) |
| 640 |
continue; |
| 641 |
/* |
| 642 |
Higher formants in general have larger bandwidths. Draw them first to show lower formants clearer. |
| 643 |
*/ |
| 644 |
for (integer iformant = std::min (frame -> numberOfFormants, toFormant); iformant >= fromFormant; iformant --) { |
| 645 |
const double frequency = frame -> formant [iformant]. frequency; |
| 646 |
Graphics_setColour (g, iformant % 2 == 1 ? oddNumberedFormants : evenNumberedFormants ); |
| 647 |
if (frequency >= fmin && frequency <= fmax) { |
| 648 |
Graphics_speckle (g, x, frequency); |
| 649 |
if (drawBandWidths) { |
| 650 |
const double bandwidth = frame -> formant [iformant]. bandwidth; |
| 651 |
const double upper = std::min (frequency + 0.5 * bandwidth, fmax); |
| 652 |
const double lower = std::max (frequency - 0.5 * bandwidth, fmin); |
| 653 |
Graphics_line (g, x, upper, x, lower); |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
void FormantPath_drawAsGrid_inside (FormantPath me, Graphics g, double tmin, double tmax, double fmax, |
| 661 |
integer fromFormant, integer toFormant, bool showBandwidths, MelderColour oddNumberedFormants, MelderColour evenNumberedFormants, |
| 662 |
integer nrow, integer ncol, double spaceBetweenFraction_x, double spaceBetweenFraction_y, double yGridLineEvery_Hz, |
| 663 |
double xCursor, double yCursor, MelderColour selectedCeilingsColour, constINTVEC const& parameters, |
| 664 |
bool markCandidatesWithinPath, bool showStress, double powerf, bool showEstimatedModels, bool garnish) |
| 665 |
{ |
| 666 |
Melder_assert (nrow >= 0 && ncol >= 0); |
| 667 |
constexpr double fmin = 0.0; |
| 668 |
const integer numberOfCandidates = my formantCandidates.size; |
| 669 |
if (nrow <= 0 || ncol <= 0) |
| 670 |
getGridLayout (numberOfCandidates, & nrow, & ncol); |
| 671 |
double x1NDC, x2NDC, y1NDC, y2NDC; |
| 672 |
Graphics_inqViewport (g, & x1NDC, & x2NDC, & y1NDC, & y2NDC); |
| 673 |
const double fontSize_old = Graphics_inqFontSize (g); |
| 674 |
double newFontSize = fontSize_old; |
| 675 |
|
| 676 |
const double vp_width = x2NDC - x1NDC, vp_height = y2NDC - y1NDC; |
| 677 |
const double vpi_width = vp_width / (ncol + (ncol - 1) * spaceBetweenFraction_x); |
| 678 |
const double vpi_height = vp_height / (nrow + (nrow - 1) * spaceBetweenFraction_y); |
| 679 |
MelderIntegerRange intervalRange = FormantPath_getPathTierIndicesRange (me, tmin, tmax); |
| 680 |
IntervalTier intervalTier = static_cast<IntervalTier> (my path -> tiers-> at[1]); |
| 681 |
integer ifmin, ifmax; |
| 682 |
const integer numberOfSamples = Sampled_getWindowSamples (me, tmin, tmax, & ifmin, & ifmax); |
| 683 |
|
| 684 |
for (integer candidate = 1; candidate <= numberOfCandidates; candidate ++) { |
| 685 |
const integer irow = 1 + (candidate - 1) / ncol; // left-to-right + top-to-bottom |
| 686 |
const integer icol = 1 + (candidate - 1) % ncol; |
| 687 |
const double vpi_x1 = x1NDC + (icol - 1) * vpi_width * (1.0 + spaceBetweenFraction_x); |
| 688 |
const double vpi_x2 = vpi_x1 + vpi_width; |
| 689 |
const double vpi_y2 = y2NDC - (irow - 1) * vpi_height * (1.0 + spaceBetweenFraction_y); |
| 690 |
const double vpi_y1 = vpi_y2 - vpi_height; |
| 691 |
const Formant formant = my formantCandidates.at [candidate]; |
| 692 |
autoFormantModeler fm; |
| 693 |
if (numberOfSamples > 0) |
| 694 |
fm = Formant_to_FormantModeler (formant, tmin, tmax, parameters); |
| 695 |
Graphics_setViewport (g, vpi_x1, vpi_x2, vpi_y1, vpi_y2); |
| 696 |
Graphics_setWindow (g, tmin, tmax, fmin, fmax); |
| 697 |
if (candidate == 1) { |
| 698 |
newFontSize = Graphics_getFontSizeInsideBox (g, tmax - tmin, spaceBetweenFraction_y * (fmax - fmin), 18.0, 3.0); |
| 699 |
Graphics_setFontSize (g, newFontSize); |
| 700 |
} |
| 701 |
if (garnish && markCandidatesWithinPath) { |
| 702 |
for (integer interval = intervalRange.first; interval <= intervalRange.last; interval ++) { |
| 703 |
TextInterval textInterval = intervalTier -> intervals.at [interval]; |
| 704 |
const integer icandidate = ( textInterval -> text.get() ? Melder_atoi (textInterval -> text.get()) : 0); |
| 705 |
if (icandidate == candidate) { |
| 706 |
MelderColour colourCopy = Graphics_inqColour (g); |
| 707 |
Graphics_setColour (g, selectedCeilingsColour); |
| 708 |
double x1 = textInterval -> xmin, x2 = textInterval -> xmax; |
| 709 |
Melder_clipLeft (tmin, & x1); |
| 710 |
Melder_clipRight (& x2, tmax); |
| 711 |
Graphics_fillRectangle (g, x1, x2, 0, fmax); |
| 712 |
Graphics_setColour (g, colourCopy); |
| 713 |
} |
| 714 |
} |
| 715 |
} |
| 716 |
Formant_speckles_inside (formant, g, tmin, tmax, fmin, fmax, fromFormant, toFormant, 100.0, showBandwidths, oddNumberedFormants, evenNumberedFormants); |
| 717 |
if (showEstimatedModels && numberOfSamples > 0) |
| 718 |
FormantModeler_drawModel_inside (fm.get(), g, tmin, tmax, fmax, fromFormant, toFormant, oddNumberedFormants, evenNumberedFormants, 100_integer); |
| 719 |
Graphics_setColour (g, Melder_BLACK); |
| 720 |
if (garnish) |
| 721 |
Graphics_rectangle (g, tmin, tmax, fmin, fmax); |
| 722 |
|
| 723 |
Graphics_setLineType (g, Graphics_DRAWN); |
| 724 |
Graphics_setLineWidth (g, 1.0); |
| 725 |
/* |
| 726 |
Mark ceiling & stress |
| 727 |
*/ |
| 728 |
autoMelderString info; |
| 729 |
if (garnish) { |
| 730 |
const double tLeftPos = tmin - 0.01 * (tmax - tmin); |
| 731 |
MelderString_append (& info, U"Ceiling = ", Melder_fixed (my ceilings [candidate], 0), U" Hz"); |
| 732 |
if (showStress && numberOfSamples > 0) { |
| 733 |
const double stress = FormantModeler_getStress (fm.get(), fromFormant, toFormant, 0, powerf); |
| 734 |
MelderString_append (& info, U"\nStress = ", Melder_fixed (stress, 2)); |
| 735 |
Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::LEFT, Graphics_TOP); |
| 736 |
} |
| 737 |
Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::LEFT, Graphics_BOTTOM); |
| 738 |
Graphics_text (g, tLeftPos, fmax, info.string); |
| 739 |
} |
| 740 |
Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF); |
| 741 |
if (garnish) { |
| 742 |
auto getXtick = [] (Graphics gg, double fontSize) { |
| 743 |
const double margin = 2.8 * fontSize * gg -> resolution / 72.0; |
| 744 |
const double wDC = (gg -> d_x2DC - gg -> d_x1DC) / (gg -> d_x2wNDC - gg -> d_x1wNDC) * (gg -> d_x2NDC - gg -> d_x1NDC); |
| 745 |
double dx = 1.5 * margin / wDC; |
| 746 |
const double xTick = 0.06 * dx; |
| 747 |
Melder_clipRight (& dx, 0.4); |
| 748 |
return xTick / (1.0 - 2.0 * dx); |
| 749 |
}; |
| 750 |
auto getYtick = [] (Graphics gg, double fontSize) { |
| 751 |
const double margin = 2.8 * fontSize * gg -> resolution / 72.0; |
| 752 |
const double hDC = integer_abs (gg->d_y2DC - gg->d_y1DC) / (gg->d_y2wNDC - gg->d_y1wNDC) * (gg->d_y2NDC - gg-> d_y1NDC); |
| 753 |
double dy = margin / hDC; |
| 754 |
const double yTick = 0.09 * dy; |
| 755 |
Melder_clipRight (& dy, 0.4); |
| 756 |
return yTick / (1.0 - 2.0 * dy); |
| 757 |
}; |
| 758 |
const double xTick = (double) getXtick (g, newFontSize) * (tmax - tmin); |
| 759 |
const double yTick = (double) getYtick (g, newFontSize) * (fmax - fmin); |
| 760 |
if (irow == nrow) { |
| 761 |
MelderString_empty (& info); |
| 762 |
MelderString_append (& info, Melder_fixed (tmin, 3), U" s"); |
| 763 |
Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::LEFT, Graphics_TOP); |
| 764 |
Graphics_line (g, tmin, fmin, tmin, fmin - yTick); |
| 765 |
Graphics_text (g, tmin , fmin - yTick, info.string); |
| 766 |
MelderString_empty (& info); |
| 767 |
MelderString_append (& info, Melder_fixed (tmax, 3), U" s"); |
| 768 |
Graphics_line (g, tmax, fmin, tmax, fmin - yTick); |
| 769 |
Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::RIGHT, Graphics_TOP); |
| 770 |
Graphics_text (g, tmax, fmin - yTick, info.string); |
| 771 |
} |
| 772 |
if (icol == 1) { |
| 773 |
MelderString_empty (& info); |
| 774 |
MelderString_append (& info, Melder_iround (fmin), U" Hz"); |
| 775 |
Graphics_line (g, tmin - xTick, fmin, tmin, fmin); |
| 776 |
Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::RIGHT, Graphics_HALF); |
| 777 |
Graphics_text (g, tmin - xTick, fmin, info.string); |
| 778 |
MelderString_empty (& info); |
| 779 |
MelderString_append (& info, Melder_iround (fmax), U" Hz"); |
| 780 |
Graphics_text (g, tmin - xTick, fmax, info.string); |
| 781 |
|
| 782 |
} |
| 783 |
double yGridLine_Hz = yGridLineEvery_Hz; |
| 784 |
Graphics_setLineType (g, Graphics_DOTTED); |
| 785 |
while (yGridLine_Hz < 0.95 * fmax) { |
| 786 |
Graphics_line (g, tmin, yGridLine_Hz, tmax, yGridLine_Hz); |
| 787 |
yGridLine_Hz += yGridLineEvery_Hz; |
| 788 |
} |
| 789 |
/* |
| 790 |
Cursors |
| 791 |
*/ |
| 792 |
Graphics_setColour (g, Melder_RED); |
| 793 |
Graphics_setLineType (g, Graphics_DASHED); |
| 794 |
if (xCursor > tmin && xCursor <= tmax) |
| 795 |
Graphics_line (g, xCursor, 0.0, xCursor, fmax); |
| 796 |
if (yCursor > fmin && yCursor < fmax) |
| 797 |
Graphics_line (g, tmin, yCursor, tmax, yCursor); |
| 798 |
Graphics_setColour (g, Melder_BLACK); |
| 799 |
Graphics_setLineType (g, Graphics_DRAWN); |
| 800 |
} |
| 801 |
} |
| 802 |
Graphics_setFontSize (g, fontSize_old); |
| 803 |
Graphics_setViewport (g, x1NDC, x2NDC, y1NDC, y2NDC); |
| 804 |
|
| 805 |
} |
| 806 |
|
| 807 |
void FormantPath_drawAsGrid (FormantPath me, Graphics g, double tmin, double tmax, double fmax, |
| 808 |
integer fromFormant, integer toFormant, bool showBandwidths, MelderColour oddNumberedFormants, MelderColour evenNumberedFormants, |
| 809 |
integer nrow, integer ncol, double spaceBetweenFraction_x, double spaceBetweenFraction_y, double yGridLineEvery_Hz, |
| 810 |
double xCursor, double yCursor, MelderColour selected, constINTVEC const& parameters, |
| 811 |
bool markCandidatesWithinPath, bool showStress, double powerf, bool showEstimatedModels, bool garnish) |
| 812 |
{ |
| 813 |
Function_bidirectionalAutowindow (me, & tmin, & tmax); |
| 814 |
Graphics_setInner (g); |
| 815 |
FormantPath_drawAsGrid_inside (me, g, tmin, tmax, fmax, fromFormant, toFormant, showBandwidths, oddNumberedFormants, evenNumberedFormants, |
| 816 |
nrow, ncol, spaceBetweenFraction_x, spaceBetweenFraction_y, yGridLineEvery_Hz, xCursor, yCursor, selected, parameters, |
| 817 |
markCandidatesWithinPath, showStress, powerf, showEstimatedModels, garnish |
| 818 |
); |
| 819 |
Graphics_unsetInner (g); |
| 820 |
} |
| 821 |
|
| 822 |
/* End of file FormantPath.cpp */ |