| 1 | /* Sound_to_Formant.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2010-2012,2014-2021,2024-2026 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 | /* |
| 20 | * pb 2002/07/16 GPL |
| 21 | * pb 2003/05/15 replaced memcof with NUMburg |
| 22 | * pb 2003/09/18 default time step is 4 times oversampling |
| 23 | * pb 2006/05/10 better handling of interruption in Sound_to_Formant |
| 24 | * pb 2006/05/10 better handling of NULL from Polynomial_to_Roots |
| 25 | * pb 2007/01/26 made compatible with stereo Sounds |
| 26 | * pb 2007/03/30 changed float to double (against compiler warnings) |
| 27 | * pb 2010/12/13 removed some style bugs |
| 28 | * pb 2011/06/08 C++ |
| 29 | */ |
| 30 | |
| 31 | #include "Sound_to_Formant.h"cxx . . $ `<file>/fon/Sound_to_Formant.h`/ |
| 32 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 33 | #include "Polynomial.h"cxx . . $ `<file>/dwsys/Polynomial.h`/ |
| 34 | #include "Roots.h"cxx . . $ `<file>/dwsys/Roots.h`/ |
| 35 | |
| 36 | static void burgcxx . . $ burg(f29000ce6c01db07). (constVECcxx . . $ constVEC# samples, VECcxx . . $ VEC# coefficients, |
| 37 | Formant_Framecxx . . $ Formant_Frame# frame, double nyquistFrequency, double safetyMargin) |
| 38 | { |
| 39 | double a0 = VECburgcxx . . $ VECburg(5935962a19f9e6ab). (coefficients, samples); |
| 40 | (void) a0; |
| 41 | /* |
| 42 | Convert LP coefficients to polynomial. |
| 43 | */ |
| 44 | autoPolynomialcxx . . $ autoPolynomial# polynomial = Polynomial_createcxx . . $ Polynomial_create(7da2dc008a3770b9). (-1, 1, coefficients.sizecxx . . $ vector#size.); |
| 45 | for (integercxx . . $ integer# i = 1; i <= coefficients.sizecxx . . $ vector#size.; i ++) |
| 46 | polynomial ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = - coefficients [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).coefficients.sizecxx . . $ vector#size. - i + 1]; |
| 47 | polynomial ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).coefficients.sizecxx . . $ vector#size. + 1] = 1.0; |
| 48 | |
| 49 | /* |
| 50 | Find the roots of the polynomial. |
| 51 | */ |
| 52 | autoRootscxx . . $ autoRoots# roots = Polynomial_to_Rootscxx . . $ Polynomial_to_Roots(877cc1a78227ca1). (polynomial.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 53 | Roots_fixIntoUnitCirclecxx . . $ Roots_fixIntoUnitCircle(ed161181b88d194d). (roots.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 54 | |
| 55 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (frame -> numberOfFormants == 0 && NUMisEmpty (frame -> formant.get())); |
| 56 | |
| 57 | /* |
| 58 | First pass: count the formants. |
| 59 | The roots come in conjugate pairs, so we need only count those above the real axis. |
| 60 | */ |
| 61 | for (integercxx . . $ integer# iroot = 1; iroot <= roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfRootscxx . . $ structRoots#numberOfRoots.; iroot ++) |
| 62 | if (roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() >= 0.0) { |
| 63 | const double f = fabscxx . . $ fabs(fee4f1d22bc8e617). (atan2cxx . . $ atan2(b71928d39fde4295). (roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(), roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].realcxx . . $ std/complex#real(21b6c25e127938e2).())) * nyquistFrequency / NUMpicxx . . $ NUMpi.; |
| 64 | if (f >= safetyMargin && f <= nyquistFrequency - safetyMargin) |
| 65 | frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. ++; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | Create space for formant data. |
| 70 | */ |
| 71 | if (frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. > 0) |
| 72 | frame -> formantcxx . . $ structFormant_Frame#formant. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <structFormant_Formantcxx . . $ structFormant_Formant#> (frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.); |
| 73 | |
| 74 | /* |
| 75 | Second pass: fill in the formants. |
| 76 | */ |
| 77 | int iformant = 0; |
| 78 | for (integercxx . . $ integer# iroot = 1; iroot <= roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). numberOfRootscxx . . $ structRoots#numberOfRoots.; iroot ++) |
| 79 | if (roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() >= 0.0) { |
| 80 | const double f = fabscxx . . $ fabs(fee4f1d22bc8e617). (atan2cxx . . $ atan2(b71928d39fde4295). (roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(), roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].realcxx . . $ std/complex#real(21b6c25e127938e2).())) * nyquistFrequency / NUMpicxx . . $ NUMpi.; |
| 81 | if (f >= safetyMargin && f <= nyquistFrequency - safetyMargin) { |
| 82 | Formant_Formantcxx . . $ Formant_Formant# formant = & frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).++ iformant]; |
| 83 | formant -> frequencycxx . . $ structFormant_Formant#frequency. = f; |
| 84 | formant -> bandwidthcxx . . $ structFormant_Formant#bandwidth. = - |
| 85 | logcxx . . $ log(fee4f1d22bc8e617). (normcxx . . $ std/norm(bb57395c51337e15). (roots ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot])) * nyquistFrequency / NUMpicxx . . $ NUMpi.; |
| 86 | } |
| 87 | } |
| 88 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (iformant == frame -> numberOfFormants); // may fail if some frequency is NaN |
| 89 | } |
| 90 | |
| 91 | static int findOneZerocxx . . $ findOneZero(ec93b33715308292). (integercxx . . $ integer# ijt, double vcx [], double a, double b, double *zero) { |
| 92 | double fa = 0.0, fb = 0.0; |
| 93 | for (integercxx . . $ integer# k = ijt; k >= 0; k --) { |
| 94 | fa = vcx [k] + a * fa; |
| 95 | fb = vcx [k] + b * fb; |
| 96 | } |
| 97 | if (fa * fb >= 0.0) { // there should be a zero between a and b |
| 98 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). ( |
| 99 | U"There is no zero between ,", Melder_singlecxx . . $ Melder_single(f05d861d9dbec9b2). (a), |
| 100 | U" and ", Melder_singlecxx . . $ Melder_single(f05d861d9dbec9b2). (b), |
| 101 | U".\n The function values are ", Melder_singlecxx . . $ Melder_single(f05d861d9dbec9b2). (fa), |
| 102 | U" and ", Melder_singlecxx . . $ Melder_single(f05d861d9dbec9b2). (fb), |
| 103 | U", respectively." |
| 104 | ); |
| 105 | return 0; |
| 106 | } |
| 107 | double x, fx; |
| 108 | do { |
| 109 | //x = fa == fb ? 0.5 * (a + b) : a + fa * (a - b) / (fb - fa); // interpolation |
| 110 | x = 0.5 * (a + b); // simple bisection |
| 111 | fx = 0.0; |
| 112 | for (integercxx . . $ integer# k = ijt; k >= 0; k --) |
| 113 | fx = vcx [k] + x * fx; |
| 114 | if (fa * fx > 0.0) { |
| 115 | a = x; |
| 116 | fa = fx; |
| 117 | } else { |
| 118 | b = x; |
| 119 | //fb = fx; // comment out if simple bisection |
| 120 | } |
| 121 | } while (fabscxx . . $ fabs(fee4f1d22bc8e617). (fx) > 1e-5); |
| 122 | *zero = x; |
| 123 | return 1; // OK |
| 124 | } |
| 125 | |
| 126 | static int findNewZeroescxx . . $ findNewZeroes(270122c281e05a8d). (integercxx . . $ integer# ijt, double ppORIG [], integercxx . . $ integer# degree, |
| 127 | double zeroes []) // In / out |
| 128 | { |
| 129 | static double cosa [7] [7] = { |
| 130 | { 1, 0, 0, 0, 0, 0, 0 }, |
| 131 | { 0, 2, 0, 0, 0, 0, 0 }, |
| 132 | { -2, 0, 4, 0, 0, 0, 0 }, |
| 133 | { 0, -6, 0, 8, 0, 0, 0 }, |
| 134 | { 2, 0, -16, 0, 16, 0, 0 }, |
| 135 | { 0, 10, 0, -40, 0, 32, 0 }, |
| 136 | { -2, 0, 36, 0, -96, 0, 64 } }; |
| 137 | double pp [33], newZeroes [33], px [33]; |
| 138 | integercxx . . $ integer# const half_degree = (degree + 1) / 2; |
| 139 | for (integercxx . . $ integer# vt = 0; vt <= half_degree; vt ++) |
| 140 | pp [vt] = ppORIG [vt]; |
| 141 | if (! (degree & 1)) |
| 142 | for (integercxx . . $ integer# vt = 1; vt <= half_degree; vt ++) |
| 143 | pp [vt] -= pp [vt - 1]; |
| 144 | for (integercxx . . $ integer# i = 0; i <= half_degree; i ++) |
| 145 | px [i] = cosa [half_degree] [i]; |
| 146 | for (integercxx . . $ integer# pt = half_degree - 1; pt >= 0; pt --) |
| 147 | for (integercxx . . $ integer# vt = 0; vt <= half_degree; vt ++) |
| 148 | px [vt] += pp [half_degree - pt] * cosa [pt] [vt]; |
| 149 | /* Fill an array with the new zeroes, which lie between the old zeroes. */ |
| 150 | newZeroes [0] = 1.0; |
| 151 | for (integercxx . . $ integer# i = 1; i <= half_degree; i ++) { |
| 152 | if (! findOneZerocxx . . $ findOneZero(ec93b33715308292). (ijt, px, zeroes [i - 1], zeroes [i], & newZeroes [i])) { |
| 153 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). ( |
| 154 | U"Degree ", degree, |
| 155 | U" not completed." |
| 156 | ); |
| 157 | return 0; |
| 158 | } |
| 159 | } |
| 160 | newZeroes [half_degree + 1] = -1.0; |
| 161 | /* |
| 162 | Grow older. |
| 163 | */ |
| 164 | for (integercxx . . $ integer# i = 0; i <= half_degree + 1; i ++) |
| 165 | zeroes [i] = newZeroes [i]; |
| 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | static int splitLevinsoncxx . . $ splitLevinson(7bd2c87165dbfac7). ( |
| 170 | constVECcxx . . $ constVEC# samples, // the windowed signal xw [1..nx] |
| 171 | integercxx . . $ integer# ncof, // the coefficients cof [1..ncof] |
| 172 | Formant_Framecxx . . $ Formant_Frame# frame, double nyquistFrequency) // put the results here |
| 173 | { |
| 174 | int result = 1; |
| 175 | double rx [100], zeroes [33]; |
| 176 | for (integercxx . . $ integer# i = 0; i <= 32; i ++) |
| 177 | zeroes [i] = 0.0; |
| 178 | /* |
| 179 | Compute the autocorrelation of the windowed signal. |
| 180 | */ |
| 181 | for (integercxx . . $ integer# i = 0; i < ncof; i ++) { |
| 182 | rx [i] = 0.0; |
| 183 | for (integercxx . . $ integer# j = 1; j <= samples.sizecxx . . $ constvector#size. - i; j ++) |
| 184 | rx [i] += samples [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j] * samples [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).j + i]; |
| 185 | } |
| 186 | /* |
| 187 | Normalize autocorrelation; |
| 188 | (should we also divide by the autocorrelation of the window?). |
| 189 | */ |
| 190 | for (integercxx . . $ integer# i = 1; i < ncof; i ++) |
| 191 | rx [i] /= rx [0]; rx [0] = 1.0; |
| 192 | |
| 193 | /* |
| 194 | Compute zeroes. |
| 195 | */ |
| 196 | { |
| 197 | double tau = 0.5 * rx [0]; |
| 198 | double pnu [33], pk [33], pkz [33]; |
| 199 | for (integercxx . . $ integer# i = 0; i <= 32; i ++) |
| 200 | pkz [i] = pk [i] = 0.0; |
| 201 | pkz [0] = 1.0; |
| 202 | pk [0] = 1.0; |
| 203 | pk [1] = 1.0; |
| 204 | for (integercxx . . $ integer# degree = 1; degree < ncof; degree ++) { |
| 205 | integercxx . . $ integer# t = degree / 2; |
| 206 | double tauk = rx [0] + rx [degree], alfak; |
| 207 | for (integercxx . . $ integer# it = 1; it <= t; it ++) |
| 208 | tauk += pk [it] * ( 2 * it == degree ? rx [it] : rx [it] + rx [degree - it] ); |
| 209 | alfak = tauk / tau; |
| 210 | tau = tauk; |
| 211 | pnu [0] = 1.0; |
| 212 | integercxx . . $ integer# ijt = (degree + 1) / 2; |
| 213 | for (integercxx . . $ integer# it = ijt; it > 0; it --) |
| 214 | pnu [it] = pk [it] + pk [it - 1] - alfak * pkz [it - 1]; |
| 215 | if (2 * ijt == degree) |
| 216 | pnu [ijt + 1] = pnu [ijt]; |
| 217 | if (degree == 1) { |
| 218 | (void) 0; |
| 219 | } else if (degree == 2) { |
| 220 | zeroes [0] = 1.0; // starting values |
| 221 | zeroes [1] = 0.5 - 0.5 * pnu [1]; |
| 222 | zeroes [2] = -1.0; |
| 223 | } |
| 224 | else |
| 225 | if (! findNewZeroescxx . . $ findNewZeroes(270122c281e05a8d). (ijt, pnu, degree, zeroes)) { |
| 226 | result = 0; |
| 227 | goto loopEnd; |
| 228 | } |
| 229 | /* |
| 230 | Grow older. |
| 231 | */ |
| 232 | for (integercxx . . $ integer# i = 0; i <= 32; i ++) { |
| 233 | pkz [i] = pk [i]; |
| 234 | pk [i] = pnu [i]; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | loopEnd: |
| 239 | /* |
| 240 | First pass: count the poles. |
| 241 | */ |
| 242 | for (integercxx . . $ integer# i = 1; i <= ncof / 2; i ++) { |
| 243 | if (zeroes [i] == 0.0 || zeroes [i] == -1.0) |
| 244 | break; |
| 245 | frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. ++; |
| 246 | } |
| 247 | /* |
| 248 | Create space for formant data. |
| 249 | */ |
| 250 | if (frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. > 0) |
| 251 | frame -> formantcxx . . $ structFormant_Frame#formant. =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). newvectorzerocxx . . $ newvectorzero(d6d5973469733409). <structFormant_Formantcxx . . $ structFormant_Formant#> (frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.); |
| 252 | /* |
| 253 | Second pass: fill in the poles. |
| 254 | */ |
| 255 | integercxx . . $ integer# iformant = 0; |
| 256 | for (integercxx . . $ integer# i = 1; i <= ncof / 2; i ++) { |
| 257 | Formant_Formantcxx . . $ Formant_Formant# formant = & frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).++ iformant]; |
| 258 | if (zeroes [i] == 0.0 || zeroes [i] == -1.0) |
| 259 | break; |
| 260 | formant -> frequencycxx . . $ structFormant_Formant#frequency. = acoscxx . . $ acos(fee4f1d22bc8e617). (zeroes [i]) * nyquistFrequency / NUMpicxx . . $ NUMpi.; |
| 261 | formant -> bandwidthcxx . . $ structFormant_Formant#bandwidth. = 50.0; |
| 262 | } |
| 263 | |
| 264 | return result; |
| 265 | } |
| 266 | |
| 267 | void Formant_sortcxx . . $ Formant_sort(69449fdcf6481858). (Formantcxx . . $ Formant# me) { |
| 268 | for (integercxx . . $ integer# iframe = 1; iframe <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; iframe ++) { |
| 269 | Formant_Framecxx . . $ Formant_Frame# frame = & mycxx . . $ `melder/melder.h:44:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]; |
| 270 | integercxx . . $ integer# n = frame -> numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.; |
| 271 | for (integercxx . . $ integer# i = 1; i < n; i ++) { |
| 272 | double min = frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. frequencycxx . . $ structFormant_Formant#frequency.; |
| 273 | integercxx . . $ integer# imin = i; |
| 274 | for (integercxx . . $ integer# j = i + 1; j <= n; j ++) |
| 275 | if (frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]. frequencycxx . . $ structFormant_Formant#frequency. < min) { |
| 276 | min = frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]. frequencycxx . . $ structFormant_Formant#frequency.; |
| 277 | imin = j; |
| 278 | } |
| 279 | if (imin != i) { |
| 280 | const double min_bandwidth = frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imin]. bandwidthcxx . . $ structFormant_Formant#bandwidth.; |
| 281 | frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imin]. frequencycxx . . $ structFormant_Formant#frequency. = frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. frequencycxx . . $ structFormant_Formant#frequency.; |
| 282 | frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imin]. bandwidthcxx . . $ structFormant_Formant#bandwidth. = frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. bandwidthcxx . . $ structFormant_Formant#bandwidth.; |
| 283 | frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. frequencycxx . . $ structFormant_Formant#frequency. = min; |
| 284 | frame -> formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. bandwidthcxx . . $ structFormant_Formant#bandwidth. = min_bandwidth; |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | static autoFormantcxx . . $ autoFormant# Sound_to_Formant_any_inplacecxx . . $ Sound_to_Formant_any_inplace(b9e7059cc6a40b54). (Soundcxx . . $ Sound# me, double dt_in, integercxx . . $ integer# numberOfPoles, |
| 291 | double halfdt_window, int which, double preemphasisFrequency, double safetyMargin) |
| 292 | { |
| 293 | const double dt = ( dt_in > 0.0 ? dt_in : halfdt_window / 4.0 ); |
| 294 | const double physicalDuration = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 295 | double dt_window = 2.0 * halfdt_window; |
| 296 | integercxx . . $ integer# numberOfFrames = 1 + Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! ((physicalDuration - dt_window) / dt); |
| 297 | integercxx . . $ integer# nsamp_window = Melder_ifloorcxx . . $ `melder/melder_ftoi.h:33:9`! (dt_window / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.), halfnsamp_window = nsamp_window / 2; |
| 298 | |
| 299 | if (nsamp_window < numberOfPoles + 1) |
| 300 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Window too short."); |
| 301 | double t1 = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + 0.5 * (physicalDuration - mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. - (numberOfFrames - 1) * dt); // centre of first frame |
| 302 | if (numberOfFrames < 1) { |
| 303 | numberOfFrames = 1; |
| 304 | t1 = mycxx . . $ `melder/melder.h:44:9`! x1cxx . . $ structSampled#x1. + 0.5 * physicalDuration; |
| 305 | dt_window = physicalDuration; |
| 306 | nsamp_window = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; |
| 307 | } |
| 308 | autoFormantcxx . . $ autoFormant# thee = Formant_createcxx . . $ Formant_create(c4f62f8fc805b821). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., numberOfFrames, dt, t1, (numberOfPoles + 1) / 2); // e.g. 11 poles -> maximally 6 formants |
| 309 | |
| 310 | autoMelderProgresscxx . . $ autoMelderProgress# progresscxx . . $ autoMelderProgress#autoMelderProgress(ff00d524ab0355ec). (U"Sound to Formant..."); |
| 311 | |
| 312 | /* Pre-emphasis. */ |
| 313 | Sound_preEmphasize_inplacecxx . . $ Sound_preEmphasize_inplace(e59d264236af2337). (me, preemphasisFrequency); |
| 314 | |
| 315 | /* Gaussian window. */ |
| 316 | autoVECcxx . . $ autoVEC# window = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (nsamp_window); |
| 317 | for (integercxx . . $ integer# i = 1; i <= nsamp_window; i ++) { |
| 318 | const double imid = 0.5 * (nsamp_window + 1), edge = expcxx . . $ exp(fee4f1d22bc8e617). (-12.0); |
| 319 | window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = (expcxx . . $ exp(fee4f1d22bc8e617). (-48.0 * (i - imid) * (i - imid) / (nsamp_window + 1) / (nsamp_window + 1)) - edge) / (1.0 - edge); |
| 320 | } |
| 321 | |
| 322 | integercxx . . $ integer# maximumFrameLength = nsamp_window; |
| 323 | |
| 324 | MelderThread_PARALLELcxx . . $ `melder/MelderThread.h:173:9`! (numberOfFrames, 3) { |
| 325 | auto frameBuffer = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (maximumFrameLength); |
| 326 | auto coefficients = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfPoles); // superfluous if which==2, but nobody uses that anyway |
| 327 | MelderThread_FORcxx . . $ `melder/MelderThread.h:183:9`! (iframe) { |
| 328 | const double t = Sampled_indexToXcxx . . $ Sampled_indexToX(65bcd2c9acbb3dd5). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), iframe); |
| 329 | const integercxx . . $ integer# leftSample = Sampled_xToLowIndexcxx . . $ Sampled_xToLowIndex(405c2491bc3636e9). (me, t); |
| 330 | const integercxx . . $ integer# rightSample = leftSample + 1; |
| 331 | integercxx . . $ integer# startSample = rightSample - halfnsamp_window; |
| 332 | integercxx . . $ integer# endSample = leftSample + halfnsamp_window; |
| 333 | double maximumIntensity = 0.0; |
| 334 | Melder_clipLeftcxx . . $ Melder_clipLeft(6245cd023a7d1552). (1_integercxx . . $ `operator""_integer`(85240eb457c3721a)., & startSample); // this should not be more than a rounding problem |
| 335 | Melder_clipRightcxx . . $ Melder_clipRight(5b63ce711df1ade7). (& endSample, mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); // this should not be more than a rounding problem |
| 336 | for (integercxx . . $ integer# i = startSample; i <= endSample; i ++) { |
| 337 | const double value = Sampled_getValueAtSamplecxx . . $ Sampled_getValueAtSample(5092633e80007c54). (me, i, Sound_LEVEL_MONOcxx . . $ `fon/Sound.h:107:9`!, 0); |
| 338 | if (value * value > maximumIntensity) |
| 339 | maximumIntensity = value * value; |
| 340 | } |
| 341 | thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe]. intensitycxx . . $ structFormant_Frame#intensity. = maximumIntensity; |
| 342 | if (maximumIntensity == 0.0) |
| 343 | continue; // Burg cannot stand all zeroes |
| 344 | |
| 345 | /* Copy a pre-emphasized window to a frame. */ |
| 346 | const integercxx . . $ integer# actualFrameLength = endSample - startSample + 1; // should rarely be less than nsamp_window |
| 347 | VECcxx . . $ VEC# frame = frameBuffer.partcxx . . $ autovector#part(2d8d48654ca64e70). (1, actualFrameLength); |
| 348 | const integercxx . . $ integer# offset = startSample - 1; |
| 349 | for (integercxx . . $ integer# isamp = 1; isamp <= actualFrameLength; isamp ++) |
| 350 | frame [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).isamp] = Sampled_getValueAtSamplecxx . . $ Sampled_getValueAtSample(5092633e80007c54). (me, offset + isamp, Sound_LEVEL_MONOcxx . . $ `fon/Sound.h:107:9`!, 0) * window [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).isamp]; |
| 351 | |
| 352 | if (which == 1) { |
| 353 | burgcxx . . $ burg(f29000ce6c01db07). (framecxx . . $ constvector#constvector(eac9d6ec4c4bfecd)., coefficients.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), & thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe], 0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., safetyMargin); |
| 354 | } else if (which == 2) { |
| 355 | if (! splitLevinsoncxx . . $ splitLevinson(7bd2c87165dbfac7). (framecxx . . $ constvector#constvector(eac9d6ec4c4bfecd)., numberOfPoles, & thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iframe], 0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.)) { |
| 356 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 357 | Melder_casualcxx . . $ Melder_casual(6298c93241566110). (U"(Sound_to_Formant:)" |
| 358 | U" Analysis results of frame ", iframe, |
| 359 | U" will be wrong." |
| 360 | ); |
| 361 | } |
| 362 | } |
| 363 | if (MelderThread_IS_MASTERcxx . . $ `melder/MelderThread.h:199:9`!) { // then we can interact with the GUI |
| 364 | const double estimatedProgress = MelderThread_ESTIMATED_PROGRESScxx . . $ `melder/MelderThread.h:202:9`!; |
| 365 | Melder_progresscxx . . $ Melder_progress(3af686d0d3721642). (0.1 + 0.8 * estimatedProgress, |
| 366 | U"Sound to Formant: analysed approximately ", Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (numberOfFrames * estimatedProgress), |
| 367 | U" out of ", numberOfFrames, U" frames" |
| 368 | ); |
| 369 | } |
| 370 | } |
| 371 | } MelderThread_ENDPARALLELcxx . . $ `melder/MelderThread.h:189:9`! |
| 372 | |
| 373 | Formant_sortcxx . . $ Formant_sort(69449fdcf6481858). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 374 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 375 | } |
| 376 | |
| 377 | autoFormantcxx . . $ autoFormant# Sound_to_Formant_anycxx . . $ Sound_to_Formant_any(a5907595092e5f1a). (Soundcxx . . $ Sound# me, double dt, integercxx . . $ integer# numberOfPoles, double maximumFrequency, |
| 378 | double halfdt_window, int which, double preemphasisFrequency, double safetyMargin) |
| 379 | { |
| 380 | const double nyquist = 0.5 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.; |
| 381 | autoSoundcxx . . $ autoSound# soundcxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).; |
| 382 | if (maximumFrequency <= 0.0 || fabscxx . . $ fabs(fee4f1d22bc8e617). (maximumFrequency / nyquist - 1) < 1.0e-12) { |
| 383 | sound =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Data_copycxx . . $ Data_copy(97a542a26794c80e). (me); // will be modified |
| 384 | } else { |
| 385 | sound =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Sound_resamplecxx . . $ Sound_resample(872961da5f5e69e5). (me, maximumFrequency * 2, 50); |
| 386 | } |
| 387 | return Sound_to_Formant_any_inplacecxx . . $ Sound_to_Formant_any_inplace(b9e7059cc6a40b54). (sound.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), dt, numberOfPoles, halfdt_window, which, preemphasisFrequency, safetyMargin); |
| 388 | } |
| 389 | |
| 390 | autoFormantcxx . . $ autoFormant# Sound_to_Formant_burgcxx . . $ Sound_to_Formant_burg(d30006947cf564ad). (Soundcxx . . $ Sound# me, double dt, double nFormants, double maximumFrequency, double halfdt_window, double preemphasisFrequency) { |
| 391 | try { |
| 392 | return Sound_to_Formant_anycxx . . $ Sound_to_Formant_any(a5907595092e5f1a). (me, dt, Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (2.0 * nFormants), maximumFrequency, halfdt_window, 1, preemphasisFrequency, 50.0); |
| 393 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 394 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": formant analysis (Burg) not performed."); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | autoFormantcxx . . $ autoFormant# Sound_to_Formant_keepAllcxx . . $ Sound_to_Formant_keepAll(d30006947cf564ad). (Soundcxx . . $ Sound# me, double dt, double nFormants, double maximumFrequency, double halfdt_window, double preemphasisFrequency) { |
| 399 | try { |
| 400 | return Sound_to_Formant_anycxx . . $ Sound_to_Formant_any(a5907595092e5f1a). (me, dt, Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (2.0 * nFormants), maximumFrequency, halfdt_window, 1, preemphasisFrequency, 0.0); |
| 401 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 402 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": formant analysis (keep all) not performed."); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | autoFormantcxx . . $ autoFormant# Sound_to_Formant_willemscxx . . $ Sound_to_Formant_willems(d30006947cf564ad). (Soundcxx . . $ Sound# me, double dt, double nFormants, double maximumFrequency, double halfdt_window, double preemphasisFrequency) { |
| 407 | try { |
| 408 | return Sound_to_Formant_anycxx . . $ Sound_to_Formant_any(a5907595092e5f1a). (me, dt, Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (2.0 * nFormants), maximumFrequency, halfdt_window, 2, preemphasisFrequency, 50.0); |
| 409 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 410 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": formant analysis (Willems) not performed."); |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | /* End of file Sound_to_Formant.cpp */ |