| 1 |
/* H1minusH2Tier.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2025 Paul Boersma |
| 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. |
| 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 "H1minusH2Tier.h" |
| 20 |
|
| 21 |
Thing_implement (H1minusH2Tier, RealTier, 0); |
| 22 |
|
| 23 |
autoH1minusH2Tier H1minusH2Tier_create (double tmin, double tmax) { |
| 24 |
try { |
| 25 |
autoH1minusH2Tier me = Thing_new (H1minusH2Tier); |
| 26 |
RealTier_init (me.get(), tmin, tmax); |
| 27 |
return me; |
| 28 |
} catch (MelderError) { |
| 29 |
Melder_throw (U"H1minusH2Tier not created."); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
void H1minusH2Tier_draw (H1minusH2Tier me, Graphics g, double tmin, double tmax, |
| 34 |
double ymin, double ymax, conststring32 method, bool garnish) |
| 35 |
{ |
| 36 |
RealTier_draw (me, g, tmin, tmax, ymin, ymax, garnish, method, U"H1-H2 (dB)"); |
| 37 |
} |
| 38 |
|
| 39 |
autoH1minusH2Tier RealTier_to_H1minusH2Tier (RealTier me) { |
| 40 |
try { |
| 41 |
autoH1minusH2Tier thee = Thing_new (H1minusH2Tier); |
| 42 |
my structRealTier :: v1_copy (thee.get()); |
| 43 |
return thee; |
| 44 |
} catch (MelderError) { |
| 45 |
Melder_throw (me, U": not converted to H1minusH2Tier."); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
static double Sound_getHarmonicStrength (Sound me, double tmin, double tmax, integer harmonic) { |
| 50 |
integer imin, imax; |
| 51 |
const integer n = Sampled_getWindowSamples (me, tmin, tmax, & imin, & imax); |
| 52 |
if (n < 3) |
| 53 |
return undefined; |
| 54 |
autoVEC cosine = raw_VEC (n), sine = raw_VEC (n); |
| 55 |
for (integer i = 1; i <= n; i ++) { |
| 56 |
const double phase = harmonic * NUM2pi * (i - 0.5) / n; |
| 57 |
cosine [i] = cos (phase); |
| 58 |
sine [i] = sin (phase); |
| 59 |
} |
| 60 |
longdouble re = 0.0, im = 0.0; |
| 61 |
for (integer ichan = 1; ichan <= my ny; ichan ++) { |
| 62 |
re += NUMinner (my z.row (ichan).part (imin, imax), cosine.all()); |
| 63 |
im += NUMinner (my z.row (ichan).part (imin, imax), sine.all()); |
| 64 |
} |
| 65 |
return sqr (double (re)) + sqr (double (im)); |
| 66 |
} |
| 67 |
autoH1minusH2Tier PointProcess_Sound_to_H1minusH2Tier_u (PointProcess me, Sound you, double tmin, double tmax, |
| 68 |
double pmin, double pmax, double maximumPeriodFactor) |
| 69 |
{ |
| 70 |
try { |
| 71 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 72 |
const MelderIntegerRange peaks = PointProcess_getWindowPoints (me, tmin, tmax); |
| 73 |
//if (peaks.size() < 3) |
| 74 |
// Melder_throw (U"Too few pulses between ", tmin, U" and ", tmax, U" seconds."); |
| 75 |
autoH1minusH2Tier him = H1minusH2Tier_create (tmin, tmax); |
| 76 |
for (integer i = peaks.first + 1; i <= peaks.last; i ++) { |
| 77 |
const double period = my t [i] - my t [i - 1]; |
| 78 |
if (pmin == pmax || (period >= pmin && period <= pmax)) { |
| 79 |
const double h1_Pa2 = Sound_getHarmonicStrength (you, my t [i - 1], my t [i], 1); |
| 80 |
const double h2_Pa2 = Sound_getHarmonicStrength (you, my t [i - 1], my t [i], 2); |
| 81 |
const double h1minusH2 = 10.0 * log10 (h1_Pa2 / h2_Pa2); |
| 82 |
if (isdefined (h1minusH2)) |
| 83 |
RealTier_addPoint (him.get(), 0.5 * (my t [i - 1] + my t [i]), h1minusH2); |
| 84 |
} |
| 85 |
} |
| 86 |
return him; |
| 87 |
} catch (MelderError) { |
| 88 |
Melder_throw (me, U" & ", you, U": not converted to H1minusH2Tier."); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/* End of file H1minusH2Tier.cpp */ |