| 1 | /* FilterBank.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2017, 2023 David Weenink, Paul Boersma 2017 |
| 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 | /* |
| 20 | djmw 20010718 |
| 21 | djmw 20020813 GPL header |
| 22 | djmw 20030901 Added filter function drawing and frequency scale drawing. |
| 23 | djmw 20050731 +FilterBank_and_PCA_drawComponent |
| 24 | djmw 20071017 Melder_error<n> |
| 25 | djmw 20071201 Melder_warning<n> |
| 26 | djmw 20080122 float -> double |
| 27 | djmw 20110304 Thing_new |
| 28 | */ |
| 29 | |
| 30 | #include "Eigen_and_Matrix.h" |
| 31 | #include "FilterBank.h" |
| 32 | #include "Matrix_extensions.h" |
| 33 | #include "Sound_and_Spectrum.h" |
| 34 | #include "Sound_extensions.h" |
| 35 | #include "Sound_to_Pitch.h" |
| 36 | #include "NUM2.h" |
| 37 | |
| 38 | static double scaleFrequency (double f, int scale_from, int scale_to) { |
| 39 | double fhz = undefined; |
| 40 | |
| 41 | if (scale_from == scale_to) |
| 42 | return f; |
| 43 | |
| 44 | if (scale_from == FilterBank_HERTZ) |
| 45 | fhz = f; |
| 46 | else if (scale_from == FilterBank_BARK) |
| 47 | fhz = NUMbarkToHertz (f); |
| 48 | else if (scale_from == FilterBank_MEL) |
| 49 | fhz = MELTOHZ (f); |
| 50 | |
| 51 | |
| 52 | if (scale_to == FilterBank_HERTZ || isundef (fhz)) |
| 53 | return fhz; |
| 54 | |
| 55 | |
| 56 | if (scale_to == FilterBank_BARK) |
| 57 | f = NUMhertzToBark (fhz); |
| 58 | else if (scale_to == FilterBank_MEL) |
| 59 | f = HZTOMEL (fhz); |
| 60 | else |
| 61 | return undefined; |
| 62 | return f; |
| 63 | } |
| 64 | |
| 65 | static char32 const *GetFreqScaleText (int scale) { |
| 66 | char32 const *hertz = U"Frequency (Hz)"; |
| 67 | char32 const *bark = U"Frequency (Bark)"; |
| 68 | char32 const *mel = U"Frequency (mel)"; |
| 69 | char32 const *error = U"Frequency (undefined)"; |
| 70 | if (scale == FilterBank_HERTZ) |
| 71 | return hertz; |
| 72 | else if (scale == FilterBank_BARK) |
| 73 | return bark; |
| 74 | else if (scale == FilterBank_MEL) |
| 75 | return mel; |
| 76 | return error; |
| 77 | } |
| 78 | |
| 79 | static int checkLimits (Matrix me, int fromFreqScale, int toFreqScale, int *fromFilter, int *toFilter, double *zmin, double *zmax, int dbScale, double *ymin, double *ymax) { |
| 80 | |
| 81 | if (*fromFilter == 0) |
| 82 | *fromFilter = 1; |
| 83 | |
| 84 | if (*toFilter == 0) |
| 85 | *toFilter = my ny; |
| 86 | |
| 87 | if (*toFilter < *fromFilter) { |
| 88 | *fromFilter = 1; |
| 89 | *toFilter = my ny; |
| 90 | } |
| 91 | if (*fromFilter < 1) { |
| 92 | *fromFilter = 1; |
| 93 | } |
| 94 | if (*toFilter > my ny) |
| 95 | *toFilter = my ny; |
| 96 | if (*fromFilter > *toFilter) { |
| 97 | Melder_warning (U"Filter numbers should be in range [1, ", my ny, U"]"); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | if (*zmin < 0.0 || *zmax < 0.0) { |
| 102 | Melder_warning (U"Frequencies should be positive."); |
| 103 | return 0; |
| 104 | } |
| 105 | if (*zmax <= *zmin) { |
| 106 | *zmin = scaleFrequency (my ymin, fromFreqScale, toFreqScale); |
| 107 | *zmax = scaleFrequency (my ymax, fromFreqScale, toFreqScale); |
| 108 | } |
| 109 | |
| 110 | if (*ymax <= *ymin) { |
| 111 | *ymax = 1.0; |
| 112 | *ymin = 0.0; |
| 113 | if (dbScale) { |
| 114 | *ymax = 0.0; |
| 115 | *ymin = -60.0; |
| 116 | } |
| 117 | } |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | static double to_dB (double a, double factor, double ref_dB) { |
| 122 | if (a <= 0) |
| 123 | return ref_dB; |
| 124 | a = factor * log10 (a); |
| 125 | if (a < ref_dB) |
| 126 | a = ref_dB; |
| 127 | return a; |
| 128 | } |
| 129 | |
| 130 | static void setDrawingLimits (double *a, integer n, double amin, double amax, integer *ibegin, integer *iend) { |
| 131 | *ibegin = 0; |
| 132 | *iend = n + 1; |
| 133 | |
| 134 | integer lower = 1; |
| 135 | for (integer i = 1; i <= n; i ++) { |
| 136 | if (isundef (a [i])) { |
| 137 | if (lower == 0) { |
| 138 | // high frequency part |
| 139 | *iend = i; |
| 140 | break; |
| 141 | } |
| 142 | *ibegin = i; |
| 143 | continue; |
| 144 | } |
| 145 | lower = 0; |
| 146 | if (a [i] < amin) |
| 147 | a [i] = amin; |
| 148 | else if (a [i] > amax) |
| 149 | a [i] = amax; |
| 150 | } |
| 151 | |
| 152 | (*ibegin) ++; |
| 153 | (*iend) --; |
| 154 | } |
| 155 | |
| 156 | Thing_implement (FilterBank, Matrix, 2); |
| 157 | |
| 158 | Thing_implement (BarkFilter, FilterBank, 2); |
| 159 | |
| 160 | autoBarkFilter BarkFilter_create (double tmin, double tmax, integer nt, double dt, double t1, double fmin, double fmax, integer nf, double df, double f1) { |
| 161 | try { |
| 162 | autoBarkFilter me = Thing_new (BarkFilter); |
| 163 | Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1); |
| 164 | return me; |
| 165 | } catch (MelderError) { |
| 166 | Melder_throw (U"BarkFilter not created."); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | double FilterBank_getFrequencyInHertz (FilterBank /* me */, double f, int scale_from) { |
| 171 | return scaleFrequency (f, scale_from, FilterBank_HERTZ); |
| 172 | } |
| 173 | |
| 174 | double FilterBank_getFrequencyInBark (FilterBank /* me */, double f, int scale_from) { |
| 175 | return scaleFrequency (f, scale_from, FilterBank_BARK); |
| 176 | } |
| 177 | |
| 178 | double FilterBank_getFrequencyInMel (FilterBank /* me */, double f, int scale_from) { |
| 179 | return scaleFrequency (f, scale_from, FilterBank_MEL); |
| 180 | } |
| 181 | |
| 182 | int FilterBank_getFrequencyScale (FilterBank me) { |
| 183 | return my v_getFrequencyScale (); |
| 184 | } |
| 185 | |
| 186 | void FilterBank_drawFrequencyScales (FilterBank me, Graphics g, int horizontalScale, double xmin, double xmax, |
| 187 | int verticalScale, double ymin, double ymax, bool garnish) |
| 188 | { |
| 189 | const int myFreqScale = FilterBank_getFrequencyScale (me); |
| 190 | |
| 191 | if (xmin < 0 || xmax < 0 || ymin < 0 || ymax < 0) { |
| 192 | Melder_warning (U"Frequencies should be >= 0."); |
| 193 | return; |
| 194 | } |
| 195 | |
| 196 | if (xmin >= xmax) { |
| 197 | double xmint = my ymin; |
| 198 | double xmaxt = my ymax; |
| 199 | if (ymin < ymax) { |
| 200 | xmint = scaleFrequency (ymin, verticalScale, myFreqScale); |
| 201 | xmaxt = scaleFrequency (ymax, verticalScale, myFreqScale); |
| 202 | } |
| 203 | xmin = scaleFrequency (xmint, myFreqScale, horizontalScale); |
| 204 | xmax = scaleFrequency (xmaxt, myFreqScale, horizontalScale); |
| 205 | } |
| 206 | |
| 207 | if (ymin >= ymax) { |
| 208 | ymin = scaleFrequency (xmin, horizontalScale, verticalScale); |
| 209 | ymax = scaleFrequency (xmax, horizontalScale, verticalScale); |
| 210 | } |
| 211 | |
| 212 | const integer n = 2000; |
| 213 | autoVEC a = zero_VEC (n); |
| 214 | |
| 215 | Graphics_setInner (g); |
| 216 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 217 | |
| 218 | const double df = (xmax - xmin) / (n - 1); |
| 219 | |
| 220 | for (integer i = 1; i <= n; i ++) { |
| 221 | const double f = xmin + (i - 1) * df; |
| 222 | a [i] = scaleFrequency (f, horizontalScale, verticalScale); |
| 223 | } |
| 224 | |
| 225 | integer ibegin, iend; |
| 226 | setDrawingLimits (a.asArgumentToFunctionThatExpectsOneBasedArray(), n, ymin, ymax, & ibegin, & iend); |
| 227 | if (ibegin <= iend) { |
| 228 | const double fmin = xmin + (ibegin - 1) * df; |
| 229 | const double fmax = xmax - (n - iend) * df; |
| 230 | Graphics_function (g, a.asArgumentToFunctionThatExpectsOneBasedArray(), ibegin, iend, fmin, fmax); |
| 231 | } |
| 232 | Graphics_unsetInner (g); |
| 233 | |
| 234 | if (garnish) { |
| 235 | Graphics_drawInnerBox (g); |
| 236 | Graphics_marksLeft (g, 2, true, true, false); |
| 237 | Graphics_textLeft (g, true, GetFreqScaleText (verticalScale)); |
| 238 | Graphics_marksBottom (g, 2, true, true, false); |
| 239 | Graphics_textBottom (g, true, GetFreqScaleText (horizontalScale)); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | void FilterBank_paint (FilterBank me, Graphics g, double xmin, double xmax, |
| 244 | double ymin, double ymax, double minimum, double maximum, bool garnish) |
| 245 | { |
| 246 | Function_unidirectionalAutowindow (me, & xmin, & xmax); |
| 247 | SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax); |
| 248 | integer ixmin, ixmax, iymin, iymax; |
| 249 | (void) Matrix_getWindowSamplesX (me, xmin - 0.49999 * my dx, xmax + 0.49999 * my dx, & ixmin, & ixmax); |
| 250 | (void) Matrix_getWindowSamplesY (me, ymin - 0.49999 * my dy, ymax + 0.49999 * my dy, & iymin, & iymax); |
| 251 | if (maximum <= minimum) |
| 252 | (void) Matrix_getWindowExtrema (me, ixmin, ixmax, iymin, iymax, & minimum, & maximum); |
| 253 | if (maximum <= minimum) { |
| 254 | minimum -= 1.0; |
| 255 | maximum += 1.0; |
| 256 | } |
| 257 | if (xmin >= xmax || ymin >= ymax) |
| 258 | return; |
| 259 | Graphics_setInner (g); |
| 260 | Graphics_setWindow (g, xmin, xmax, ymin, ymax); |
| 261 | Graphics_image (g, my z.part (iymin, iymax, ixmin, ixmax), |
| 262 | Sampled_indexToX (me, ixmin - 0.5), Sampled_indexToX (me, ixmax + 0.5), |
| 263 | SampledXY_indexToY (me, iymin - 0.5), SampledXY_indexToY (me, iymax + 0.5), |
| 264 | minimum, maximum); |
| 265 | Graphics_unsetInner (g); |
| 266 | if (garnish) { |
| 267 | Graphics_drawInnerBox (g); |
| 268 | Graphics_marksLeft (g, 2, true, true, false); |
| 269 | Graphics_textLeft (g, true, GetFreqScaleText (my v_getFrequencyScale ())); |
| 270 | Graphics_marksBottom (g, 2, true, true, false); |
| 271 | Graphics_textBottom (g, true, U"Time (s)"); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | void BarkFilter_drawSekeyHansonFilterFunctions (BarkFilter me, Graphics g, int toFreqScale, int fromFilter, int toFilter, |
| 276 | double zmin, double zmax, int dbScale, double ymin, double ymax, bool garnish) |
| 277 | { |
| 278 | if (! checkLimits (me, FilterBank_BARK, toFreqScale, & fromFilter, & toFilter, & zmin, & zmax, dbScale, & ymin, & ymax)) |
| 279 | return; |
| 280 | const integer n = 1000; |
| 281 | autoVEC a = zero_VEC (n); |
| 282 | |
| 283 | Graphics_setInner (g); |
| 284 | Graphics_setWindow (g, zmin, zmax, ymin, ymax); |
| 285 | for (integer j = fromFilter; j <= toFilter; j ++) { |
| 286 | const double df = (zmax - zmin) / (n - 1); |
| 287 | const double zMid = Matrix_rowToY (me, j); |
| 288 | |
| 289 | for (integer i = 1; i <= n; i ++) { |
| 290 | const double f = zmin + (i - 1) * df; |
| 291 | double z = scaleFrequency (f, toFreqScale, FilterBank_BARK); |
| 292 | if (isundef (z)) { |
| 293 | a [i] = undefined; |
| 294 | } else { |
| 295 | z -= zMid + 0.215; |
| 296 | a [i] = 7.0 - 7.5 * z - 17.5 * sqrt (0.196 + z * z); |
| 297 | if (! dbScale) |
| 298 | a [i] = pow (10.0, a [i]); |
| 299 | } |
| 300 | } |
| 301 | integer ibegin, iend; |
| 302 | setDrawingLimits (a.asArgumentToFunctionThatExpectsOneBasedArray(), n, ymin, ymax, & ibegin, & iend); |
| 303 | if (ibegin <= iend) { |
| 304 | const double fmin = zmin + (ibegin - 1) * df; |
| 305 | const double fmax = zmax - (n - iend) * df; |
| 306 | Graphics_function (g, a.asArgumentToFunctionThatExpectsOneBasedArray(), ibegin, iend, fmin, fmax); |
| 307 | } |
| 308 | } |
| 309 | Graphics_unsetInner (g); |
| 310 | if (garnish) { |
| 311 | const double distance = ( dbScale ? 10.0 : 1.0 ); |
| 312 | const conststring32 ytext = ( dbScale ? U"Amplitude (dB)" : U"Amplitude" ); |
| 313 | Graphics_drawInnerBox (g); |
| 314 | Graphics_marksBottom (g, 2, true, true, false); |
| 315 | Graphics_marksLeftEvery (g, 1.0, distance, true, true, false); |
| 316 | Graphics_textLeft (g, true, ytext); |
| 317 | Graphics_textBottom (g, true, GetFreqScaleText (toFreqScale)); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | Thing_implement (MelFilter, FilterBank, 2); |
| 322 | |
| 323 | autoMelFilter MelFilter_create (double tmin, double tmax, integer nt, double dt, double t1, |
| 324 | double fmin, double fmax, integer nf, double df, double f1) |
| 325 | { |
| 326 | try { |
| 327 | autoMelFilter me = Thing_new (MelFilter); |
| 328 | Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1); |
| 329 | return me; |
| 330 | } catch (MelderError) { |
| 331 | Melder_throw (U"MelFilter not created."); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | /* |
| 336 | void FilterBank_drawFilters (FilterBank me, Graphics g, integer fromf, integer tof, |
| 337 | double xmin, double xmax, int xlinear, double ymin, double ymax, int ydb, |
| 338 | double (*tolinf)(double f), double (*tononlin) (double f), |
| 339 | double (*filteramp)(double f0, double b, double f)) |
| 340 | { |
| 341 | |
| 342 | }*/ |
| 343 | |
| 344 | void FilterBank_drawTimeSlice (FilterBank me, Graphics g, double t, |
| 345 | double fmin, double fmax, double min, double max, conststring32 xlabel, bool garnish) |
| 346 | { |
| 347 | Matrix_drawSliceY (me, g, t, fmin, fmax, min, max); |
| 348 | if (garnish) { |
| 349 | Graphics_drawInnerBox (g); |
| 350 | Graphics_marksBottom (g, 2, true, true, false); |
| 351 | Graphics_marksLeft (g, 2, true, true, false); |
| 352 | if (xlabel) |
| 353 | Graphics_textBottom (g, false, xlabel); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | void MelFilter_drawFilterFunctions (MelFilter me, Graphics g, int toFreqScale, int fromFilter, int toFilter, |
| 358 | double zmin, double zmax, int dbScale, double ymin, double ymax, bool garnish) |
| 359 | { |
| 360 | if (! checkLimits (me, FilterBank_MEL, toFreqScale, & fromFilter, & toFilter, & zmin, & zmax, dbScale, & ymin, & ymax)) |
| 361 | return; |
| 362 | const integer n = 1000; |
| 363 | autoVEC a = zero_VEC (n); |
| 364 | |
| 365 | Graphics_setInner (g); |
| 366 | Graphics_setWindow (g, zmin, zmax, ymin, ymax); |
| 367 | |
| 368 | for (integer j = fromFilter; j <= toFilter; j ++) { |
| 369 | const double df = (zmax - zmin) / (n - 1); |
| 370 | const double fc_mel = my y1 + (j - 1) * my dy; |
| 371 | const double fc_hz = MELTOHZ (fc_mel); |
| 372 | const double fl_hz = MELTOHZ (fc_mel - my dy); |
| 373 | const double fh_hz = MELTOHZ (fc_mel + my dy); |
| 374 | integer ibegin, iend; |
| 375 | |
| 376 | for (integer i = 1; i <= n; i ++) { |
| 377 | // Filterfunction: triangular on a linear frequency scale AND a linear amplitude scale. |
| 378 | const double f = zmin + (i - 1) * df; |
| 379 | const double z = scaleFrequency (f, toFreqScale, FilterBank_HERTZ); |
| 380 | if (isundef (z)) { |
| 381 | a [i] = undefined; |
| 382 | } else { |
| 383 | a [i] = NUMtriangularfilter_amplitude (fl_hz, fc_hz, fh_hz, z); |
| 384 | if (dbScale) |
| 385 | a [i] = to_dB (a [i], 10.0, ymin); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | setDrawingLimits (a.asArgumentToFunctionThatExpectsOneBasedArray(), n, ymin, ymax, &ibegin, &iend); |
| 390 | |
| 391 | if (ibegin <= iend) { |
| 392 | double fmin = zmin + (ibegin - 1) * df; |
| 393 | double fmax = zmax - (n - iend) * df; |
| 394 | Graphics_function (g, a.asArgumentToFunctionThatExpectsOneBasedArray(), ibegin, iend, fmin, fmax); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | Graphics_unsetInner (g); |
| 399 | |
| 400 | if (garnish) { |
| 401 | const double distance = ( dbScale ? 10.0 : 1.0 ); |
| 402 | const conststring32 ytext = ( dbScale ? U"Amplitude (dB)" : U"Amplitude" ); |
| 403 | Graphics_drawInnerBox (g); |
| 404 | Graphics_marksBottom (g, 2, true, true, false); |
| 405 | Graphics_marksLeftEvery (g, 1.0, distance, true, true, false); |
| 406 | Graphics_textLeft (g, true, ytext); |
| 407 | Graphics_textBottom (g, true, GetFreqScaleText (toFreqScale)); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | autoMatrix FilterBank_to_Matrix (FilterBank me) { |
| 412 | try { |
| 413 | autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 414 | thy z.all() <<= my z.all(); |
| 415 | return thee; |
| 416 | } catch (MelderError) { |
| 417 | Melder_throw (me, U": not converted to Matrix."); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | autoBarkFilter Matrix_to_BarkFilter (Matrix me) { |
| 422 | try { |
| 423 | autoBarkFilter thee = BarkFilter_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 424 | thy z.all() <<= my z.all(); |
| 425 | return thee; |
| 426 | } catch (MelderError) { |
| 427 | Melder_throw (me, U": not converted to BarkFilter."); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | autoMelFilter Matrix_to_MelFilter (Matrix me) { |
| 432 | try { |
| 433 | autoMelFilter thee = MelFilter_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 434 | thy z.all() <<= my z.all(); |
| 435 | return thee; |
| 436 | } catch (MelderError) { |
| 437 | Melder_throw (me, U": not converted to MelFilter."); |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | Thing_implement (FormantFilter, FilterBank, 2); |
| 442 | |
| 443 | autoFormantFilter FormantFilter_create (double tmin, double tmax, integer nt, double dt, double t1, double fmin, double fmax, integer nf, double df, double f1) { |
| 444 | try { |
| 445 | autoFormantFilter me = Thing_new (FormantFilter); |
| 446 | Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1); |
| 447 | return me; |
| 448 | } catch (MelderError) { |
| 449 | Melder_throw (U"FormantFilter not created."); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | void FormantFilter_drawFilterFunctions (FormantFilter me, Graphics g, double bandwidth, int toFreqScale, int fromFilter, int toFilter, |
| 454 | double zmin, double zmax, int dbScale, double ymin, double ymax, bool garnish) |
| 455 | { |
| 456 | if (! checkLimits (me, FilterBank_HERTZ, toFreqScale, & fromFilter, & toFilter, & zmin, & zmax, dbScale, & ymin, & ymax)) |
| 457 | return; |
| 458 | if (bandwidth <= 0.0) |
| 459 | Melder_warning (U"Bandwidth should be greater than zero."); |
| 460 | |
| 461 | const integer n = 1000; |
| 462 | autoVEC a = zero_VEC (n); |
| 463 | |
| 464 | Graphics_setInner (g); |
| 465 | Graphics_setWindow (g, zmin, zmax, ymin, ymax); |
| 466 | |
| 467 | for (integer j = fromFilter; j <= toFilter; j ++) { |
| 468 | const double df = (zmax - zmin) / (n - 1); |
| 469 | const double fc = my y1 + (j - 1) * my dy; |
| 470 | |
| 471 | for (integer i = 1; i <= n; i ++) { |
| 472 | const double f = zmin + (i - 1) * df; |
| 473 | const double z = scaleFrequency (f, toFreqScale, FilterBank_HERTZ); |
| 474 | if (isundef (z)) { |
| 475 | a [i] = undefined; |
| 476 | } else { |
| 477 | a [i] = NUMformantfilter_amplitude (fc, bandwidth, z); |
| 478 | if (dbScale) |
| 479 | a [i] = to_dB (a [i], 10.0, ymin); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | integer ibegin, iend; |
| 484 | setDrawingLimits (a.asArgumentToFunctionThatExpectsOneBasedArray(), n, ymin, ymax, & ibegin, & iend); |
| 485 | if (ibegin <= iend) { |
| 486 | const double fmin = zmin + (ibegin - 1) * df; |
| 487 | const double fmax = zmax - (n - iend) * df; |
| 488 | Graphics_function (g, a.asArgumentToFunctionThatExpectsOneBasedArray(), ibegin, iend, fmin, fmax); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | Graphics_unsetInner (g); |
| 493 | |
| 494 | if (garnish) { |
| 495 | double distance = ( dbScale ? 10.0 : 1.0 ); |
| 496 | conststring32 ytext = ( dbScale ? U"Amplitude (dB)" : U"Amplitude" ); |
| 497 | Graphics_drawInnerBox (g); |
| 498 | Graphics_marksBottom (g, 2, true, true, false); |
| 499 | Graphics_marksLeftEvery (g, 1.0, distance, true, true, false); |
| 500 | Graphics_textLeft (g, true, ytext); |
| 501 | Graphics_textBottom (g, true, GetFreqScaleText (toFreqScale)); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | autoFormantFilter Matrix_to_FormantFilter (Matrix me) { |
| 506 | try { |
| 507 | autoFormantFilter thee = FormantFilter_create (my xmin, my xmax, my nx, my dx, my x1, |
| 508 | my ymin, my ymax, my ny, my dy, my y1); |
| 509 | thy z.all() <<= my z.all(); |
| 510 | return thee; |
| 511 | } catch (MelderError) { |
| 512 | Melder_throw (me, U": not converted to FormantFilter."); |
| 513 | } |
| 514 | } |
| 515 | |
| 516 | autoSpectrum FormantFilter_to_Spectrum_slice (FormantFilter me, double t) { |
| 517 | try { |
| 518 | const double sqrtref = sqrt (FilterBank_DBREF); |
| 519 | const double factor2 = 2.0 * 10.0 * FilterBank_DBFAC; |
| 520 | autoSpectrum thee = Spectrum_create (my ymax, my ny); |
| 521 | |
| 522 | thy xmin = my ymin; |
| 523 | thy xmax = my ymax; |
| 524 | thy x1 = my y1; |
| 525 | thy dx = my dy; /* Frequency step. */ |
| 526 | |
| 527 | integer frame = Sampled_xToNearestIndex (me, t); |
| 528 | if (frame < 1) |
| 529 | frame = 1; |
| 530 | if (frame > my nx) |
| 531 | frame = my nx; |
| 532 | |
| 533 | for (integer i = 1; i <= my ny; i ++) { |
| 534 | /* |
| 535 | power = ref * 10 ^ (value / 10) |
| 536 | sqrt(power) = sqrt(ref) * 10 ^ (value / (2*10)) |
| 537 | */ |
| 538 | thy z [1] [i] = sqrtref * pow (10.0, my z [i] [frame] / factor2); |
| 539 | thy z [2] [i] = 0.0; |
| 540 | } |
| 541 | return thee; |
| 542 | } catch (MelderError) { |
| 543 | Melder_throw (me, U": Spectral slice not created."); |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | autoIntensity FilterBank_to_Intensity (FilterBank me) { |
| 548 | try { |
| 549 | autoIntensity thee = Intensity_create (my xmin, my xmax, my nx, my dx, my x1); |
| 550 | |
| 551 | const double db_ref = 10.0 * log10 (FilterBank_DBREF); |
| 552 | for (integer j = 1; j <= my nx; j ++) { |
| 553 | longdouble p = 0.0; |
| 554 | for (integer i = 1; i <= my ny; i ++) |
| 555 | p += FilterBank_DBREF * exp (NUMln10 * my z [i] [j] / 10.0); |
| 556 | thy z [1] [j] = 10.0 * log10 ((double) p) - db_ref; |
| 557 | } |
| 558 | return thee; |
| 559 | } catch (MelderError) { |
| 560 | Melder_throw (me, U": Intensity not created."); |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | void FilterBank_equalizeIntensities (FilterBank me, double intensity_db) { |
| 565 | for (integer j = 1; j <= my nx; j ++) { |
| 566 | longdouble p = 0.0; |
| 567 | for (integer i = 1; i <= my ny; i ++) |
| 568 | p += FilterBank_DBREF * exp (NUMln10 * my z [i] [j] / 10.0); |
| 569 | const double delta_db = intensity_db - 10.0 * log10 (p / FilterBank_DBREF); |
| 570 | |
| 571 | my z.column (j) += delta_db; |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | void FilterBank_PCA_drawComponent (FilterBank me, PCA thee, Graphics g, integer component, double dblevel, double frequencyOffset, double scale, double tmin, double tmax, double fmin, double fmax) { |
| 576 | Melder_require (component > 0 && component <= thy numberOfEigenvalues, U"Component should be in the range [1, ", thy numberOfEigenvalues, U"]."); |
| 577 | |
| 578 | // Scale Intensity |
| 579 | |
| 580 | autoFilterBank fcopy = Data_copy (me); |
| 581 | FilterBank_equalizeIntensities (fcopy.get(), dblevel); |
| 582 | autoMatrix him = Eigen_Matrix_to_Matrix_projectColumns (thee, fcopy.get(), component); |
| 583 | for (integer j = 1; j <= my nx; j ++) { |
| 584 | fcopy -> z [component] [j] = frequencyOffset + scale * fcopy -> z [component] [j]; |
| 585 | } |
| 586 | Matrix_drawRows (fcopy.get(), g, tmin, tmax, component - 0.5, component + 0.5, fmin, fmax); |
| 587 | } |
| 588 | |
| 589 | // Convert old types to new types |
| 590 | |
| 591 | autoMelSpectrogram MelFilter_to_MelSpectrogram (MelFilter me) { |
| 592 | try { |
| 593 | autoMelSpectrogram thee = MelSpectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 594 | for (integer i = 1; i <= my ny; i ++) { |
| 595 | for (integer j = 1; j <= my nx; j ++) { |
| 596 | thy z [i] [j] = 4e-10 * pow (10, my z [i] [j] / 10); |
| 597 | } |
| 598 | } |
| 599 | return thee; |
| 600 | } catch (MelderError) { |
| 601 | Melder_throw (U"MelSpectrogram not created."); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | autoBarkSpectrogram BarkFilter_to_BarkSpectrogram (BarkFilter me) { |
| 606 | try { |
| 607 | autoBarkSpectrogram thee = BarkSpectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 608 | for (integer i = 1; i <= my ny; i ++) { |
| 609 | for (integer j = 1; j <= my nx; j ++) { |
| 610 | thy z [i] [j] = 4e-10 * pow (10, my z [i] [j] / 10); |
| 611 | } |
| 612 | } |
| 613 | return thee; |
| 614 | } catch (MelderError) { |
| 615 | Melder_throw (U"BarkSpectrogram not created."); |
| 616 | } |
| 617 | } |
| 618 | |
| 619 | autoSpectrogram FormantFilter_to_Spectrogram (FormantFilter me) { |
| 620 | try { |
| 621 | autoSpectrogram thee = Spectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1); |
| 622 | for (integer i = 1; i <= my ny; i ++) |
| 623 | for (integer j = 1; j <= my nx; j ++) |
| 624 | thy z [i] [j] = 4e-10 * pow (10, my z [i] [j] / 10); |
| 625 | return thee; |
| 626 | } catch (MelderError) { |
| 627 | Melder_throw (U"Spectrogram not created."); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | /* MelFilter_and_MFCC.cpp */ |
| 632 | |
| 633 | autoMFCC MelFilter_to_MFCC (MelFilter me, integer numberOfCoefficients) { |
| 634 | try { |
| 635 | autoMAT cosinesTable = MATcosinesTable (my ny); |
| 636 | autoVEC x = raw_VEC (my ny); |
| 637 | autoVEC y = raw_VEC (my ny); |
| 638 | |
| 639 | //double fmax_mel = my y1 + (my ny - 1) * my dy; |
| 640 | numberOfCoefficients = ( numberOfCoefficients > my ny - 1 ? my ny - 1 : numberOfCoefficients ); |
| 641 | Melder_assert (numberOfCoefficients > 0); |
| 642 | // 20130220 new interpretation of maximumNumberOfCoefficients necessary for inverse transform |
| 643 | autoMFCC thee = MFCC_create (my xmin, my xmax, my nx, my dx, my x1, my ny - 1, my ymin, my ymax); |
| 644 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 645 | const CC_Frame cf = & thy frame [iframe]; |
| 646 | x.all() <<= my z.column (iframe); |
| 647 | VECcosineTransform_preallocated (y.get(), x.get(), cosinesTable.get()); |
| 648 | CC_Frame_init (cf, numberOfCoefficients); |
| 649 | for (integer icoef = 1; icoef <= numberOfCoefficients; icoef ++) |
| 650 | cf -> c [icoef] = y [icoef + 1]; |
| 651 | cf -> c0 = y [1]; |
| 652 | } |
| 653 | return thee; |
| 654 | } catch (MelderError) { |
| 655 | Melder_throw (me, U": no MFCC created."); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | autoMelFilter MFCC_to_MelFilter (MFCC me, integer first, integer last) { |
| 660 | try { |
| 661 | const integer nf = my maximumNumberOfCoefficients + 1; |
| 662 | autoMAT cosinesTable = MATcosinesTable (nf); |
| 663 | autoVEC x = raw_VEC (nf); |
| 664 | autoVEC y = raw_VEC (nf); |
| 665 | |
| 666 | if (first >= last) { |
| 667 | first = 0; |
| 668 | last = nf - 1; |
| 669 | } |
| 670 | Melder_require (first >= 0 && last <= nf, |
| 671 | U"MFCC_to_MelFilter: coefficients should be in interval [0,", my maximumNumberOfCoefficients, U"]."); |
| 672 | |
| 673 | const double df = (my fmax - my fmin) / (nf + 1); |
| 674 | autoMelFilter thee = MelFilter_create (my xmin, my xmax, my nx, my dx, my x1, my fmin, my fmax, nf, df, df); |
| 675 | |
| 676 | for (integer iframe = 1; iframe <= my nx; iframe ++) { |
| 677 | const CC_Frame cf = & my frame [iframe]; |
| 678 | const integer iend = std::min (last, cf -> numberOfCoefficients); |
| 679 | x [1] = ( first == 0 ? cf -> c0 : 0.0 ); |
| 680 | for (integer icoef = 1; icoef <= my maximumNumberOfCoefficients; icoef ++) |
| 681 | x [icoef + 1] = ( icoef < first || icoef > iend ? 0.0 : cf -> c [icoef] ); // zero extrapolation |
| 682 | VECinverseCosineTransform_preallocated (y.get(), x.get(), cosinesTable.get()); |
| 683 | thy z.column (iframe) <<= y.get(); |
| 684 | } |
| 685 | return thee; |
| 686 | } catch (MelderError) { |
| 687 | Melder_throw (me, U": no MelFilter created."); |
| 688 | } |
| 689 | } |
| 690 | |
| 691 | /* Sound_and_FilterBank.cpp */ |
| 692 | |
| 693 | // prototypes |
| 694 | autoSound FilterBank_as_Sound (FilterBank me); |
| 695 | |
| 696 | /* |
| 697 | The gaussian(x) = (exp(-48*((i-(n+1)/2)/(n+1))^2)-exp(-12))/(1-exp(-12)); |
| 698 | For power we need the area under the square of this window: |
| 699 | Integrate (gaussian(i)^2,i=1..n) = |
| 700 | |
| 701 | (sqrt(Pi)*sqrt(3)*sqrt(2)*erf(2*(n-1)*sqrt(3)*sqrt(2)/(n+1))*(n+1)+ |
| 702 | 24*exp(-24)*(n-1)+ |
| 703 | -4*sqrt(Pi)*sqrt(3)*exp(-12)*erf(2*(n-1)*sqrt(3)/(n+1))*(n+1))/ |
| 704 | (24 * (-1+exp(-12))^2) |
| 705 | To compare with the rectangular window we need to divide this by the |
| 706 | window width (n-1) x 1^2. |
| 707 | */ |
| 708 | static double gaussian_window_squared_correction (integer n) { |
| 709 | const double e12 = exp (-12.0), denum = (e12 - 1.0) * (e12 - 1.0) * 24.0 * (n - 1); |
| 710 | const double sqrt3 = sqrt (3.0), sqrt2 = sqrt (2.0), sqrtpi = sqrt (NUMpi); |
| 711 | const double arg1 = 2.0 * sqrt3 * (n - 1) / (n + 1), arg2 = arg1 * sqrt2; |
| 712 | const double p2 = sqrtpi * sqrt3 * sqrt2 * (1.0 - NUMerfcc (arg2)) * (n + 1); |
| 713 | const double p1 = 4.0 * sqrtpi * sqrt3 * e12 * (1.0 - NUMerfcc (arg1)) * (n + 1); |
| 714 | |
| 715 | return (p2 - p1 + 24.0 * (n - 1) * e12 * e12) / denum; |
| 716 | } |
| 717 | |
| 718 | static autoMatrix Sound_to_spectralpower (Sound me) { |
| 719 | try { |
| 720 | autoSpectrum s = Sound_to_Spectrum (me, true); |
| 721 | autoMatrix thee = Matrix_create (s -> xmin, s -> xmax, s -> nx, s -> dx, s -> x1, 1.0, 1.0, 1, 1.0, 1.0); |
| 722 | const double scale = 2.0 * s -> dx / (my xmax - my xmin); |
| 723 | |
| 724 | // factor '2' because of positive and negative frequencies |
| 725 | // s -> dx : width of frequency bin |
| 726 | // my xmax - my xmin : duration of sound |
| 727 | |
| 728 | constVEC re = s -> z.row (1), im = s -> z.row (2); |
| 729 | for (integer i = 1; i <= s -> nx; i ++) |
| 730 | thy z [1] [i] = scale * (re [i] * re [i] + im [i] * im [i]); |
| 731 | |
| 732 | // Frequency bins at 0 Hz and nyquist don't count for two. |
| 733 | |
| 734 | thy z [1] [1] *= 0.5; |
| 735 | thy z [1] [s -> nx] *= 0.5; |
| 736 | return thee; |
| 737 | } catch (MelderError) { |
| 738 | Melder_throw (me, U": no Matrix with spectral power created."); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | static int Sound_into_BarkFilter_frame (Sound me, BarkFilter thee, integer frame) { |
| 743 | autoMatrix pv = Sound_to_spectralpower (me); |
| 744 | const integer nf = pv -> nx; |
| 745 | |
| 746 | autoVEC z = raw_VEC (nf); |
| 747 | for (integer j = 1; j <= nf; j ++) |
| 748 | z [j] = NUMhertzToBark (pv -> x1 + (j - 1) * pv -> dx); |
| 749 | |
| 750 | for (integer i = 1; i <= thy ny; i ++) { |
| 751 | const double z0 = thy y1 + (i - 1) * thy dy; |
| 752 | const constVEC pow = pv -> z.row (1); // TODO ?? |
| 753 | longdouble p = 0.0; |
| 754 | for (integer j = 1; j <= nf; j ++) { |
| 755 | /* |
| 756 | Sekey & Hanson filter is defined in the power domain. |
| 757 | We therefore multiply the power with a (and not a^2). |
| 758 | integral (F(z),z=0..25) = 1.58/9 |
| 759 | */ |
| 760 | const double a = NUMsekeyhansonfilter_amplitude (z0, z [j]); |
| 761 | p += a * pow [j] ; |
| 762 | } |
| 763 | thy z [i] [frame] = double (p); |
| 764 | } |
| 765 | return 1; |
| 766 | } |
| 767 | |
| 768 | autoBarkFilter Sound_to_BarkFilter (Sound me, double analysisWidth, double dt, double f1_bark, double fmax_bark, double df_bark) { |
| 769 | try { |
| 770 | |
| 771 | const double nyquist = 0.5 / my dx, samplingFrequency = 2.0 * nyquist; |
| 772 | const double windowDuration = 2.0 * analysisWidth; /* gaussian window */ |
| 773 | const double zmax = NUMhertzToBark (nyquist); |
| 774 | double fmin_bark = 0.0; |
| 775 | |
| 776 | // Check defaults. |
| 777 | |
| 778 | if (f1_bark <= 0.0) |
| 779 | f1_bark = 1.0; |
| 780 | if (fmax_bark <= 0.0) |
| 781 | fmax_bark = zmax; |
| 782 | if (df_bark <= 0.0) |
| 783 | df_bark = 1.0; |
| 784 | |
| 785 | Melder_clipRight (& fmax_bark, zmax); |
| 786 | integer nf = Melder_iround ((fmax_bark - f1_bark) / df_bark); |
| 787 | Melder_require (nf > 0, U"The combination of filter parameters is not valid."); |
| 788 | |
| 789 | double t1; |
| 790 | integer numberOfFrames, frameErrorCount = 0; |
| 791 | Sampled_shortTermAnalysis (me, windowDuration, dt, & numberOfFrames, & t1); |
| 792 | autoSound sframe = Sound_createSimple (1, windowDuration, samplingFrequency); |
| 793 | autoSound window = Sound_createGaussian (windowDuration, samplingFrequency); |
| 794 | autoBarkFilter thee = BarkFilter_create (my xmin, my xmax, numberOfFrames, dt, t1, |
| 795 | fmin_bark, fmax_bark, nf, df_bark, f1_bark); |
| 796 | |
| 797 | autoMelderProgress progess (U"Sound to BarkFilter..."); |
| 798 | |
| 799 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 800 | const double t = Sampled_indexToX (thee.get(), iframe); |
| 801 | |
| 802 | Sound_into_Sound (me, sframe.get(), t - windowDuration / 2); |
| 803 | |
| 804 | Sounds_multiply (sframe.get(), window.get()); |
| 805 | |
| 806 | if (! Sound_into_BarkFilter_frame (sframe.get(), thee.get(), iframe)) |
| 807 | frameErrorCount ++; |
| 808 | |
| 809 | if ( (iframe % 10) == 1) |
| 810 | Melder_progress ( (double) iframe / numberOfFrames, U"BarkFilter analysis: frame ", |
| 811 | iframe, U" from ", numberOfFrames, U"."); |
| 812 | } |
| 813 | |
| 814 | if (frameErrorCount > 0) |
| 815 | Melder_warning (U"Analysis results of ", frameErrorCount, U" frame(s) out of ", |
| 816 | numberOfFrames, U" will be suspect."); |
| 817 | |
| 818 | const double ref = FilterBank_DBREF * gaussian_window_squared_correction (window -> nx); |
| 819 | |
| 820 | NUMdmatrix_to_dBs (thy z.get(), ref, FilterBank_DBFAC, FilterBank_DBFLOOR); |
| 821 | return thee; |
| 822 | } catch (MelderError) { |
| 823 | Melder_throw (me, U": no BarkFilter created."); |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | static int Sound_into_MelFilter_frame (Sound me, MelFilter thee, integer frame) { |
| 828 | autoMatrix pv = Sound_to_spectralpower (me); |
| 829 | |
| 830 | const double z1 = pv -> x1; |
| 831 | const double dz = pv -> dx; |
| 832 | const integer nf = pv -> nx; |
| 833 | const double df = thy dy; |
| 834 | for (integer i = 1; i <= thy ny; i ++) { |
| 835 | const double fc_mel = thy y1 + (i - 1) * df; |
| 836 | const double fc_hz = MELTOHZ (fc_mel); |
| 837 | const double fl_hz = MELTOHZ (fc_mel - df); |
| 838 | const double fh_hz = MELTOHZ (fc_mel + df); |
| 839 | const constVEC pow = pv -> z.row (1); |
| 840 | double p = 0.0; |
| 841 | for (integer j = 1; j <= nf; j ++) { |
| 842 | // Bin with a triangular filter the power (= amplitude-squared) |
| 843 | |
| 844 | const double f = z1 + (j - 1) * dz; |
| 845 | const double a = NUMtriangularfilter_amplitude (fl_hz, fc_hz, fh_hz, f); |
| 846 | p += a * pow [j]; |
| 847 | } |
| 848 | thy z [i] [frame] = p; |
| 849 | } |
| 850 | return 1; |
| 851 | } |
| 852 | |
| 853 | autoMelFilter Sound_to_MelFilter (Sound me, double analysisWidth, double dt, double f1_mel, double fmax_mel, double df_mel) { |
| 854 | try { |
| 855 | const double samplingFrequency = 1.0 / my dx, nyquist = 0.5 * samplingFrequency; |
| 856 | const double windowDuration = 2.0 * analysisWidth; /* gaussian window */ |
| 857 | const double fmin_mel = 0.0; |
| 858 | const double fbottom = HZTOMEL (100.0), fceiling = HZTOMEL (nyquist); |
| 859 | |
| 860 | // Check defaults. |
| 861 | |
| 862 | if (fmax_mel <= 0.0 || fmax_mel > fceiling) |
| 863 | fmax_mel = fceiling; |
| 864 | if (fmax_mel <= f1_mel) { |
| 865 | f1_mel = fbottom; |
| 866 | fmax_mel = fceiling; |
| 867 | } |
| 868 | if (f1_mel <= 0.0) |
| 869 | f1_mel = fbottom; |
| 870 | if (df_mel <= 0.0) |
| 871 | df_mel = 100.0; |
| 872 | |
| 873 | // Determine the number of filters. |
| 874 | |
| 875 | const integer nf = Melder_iround ((fmax_mel - f1_mel) / df_mel); |
| 876 | fmax_mel = f1_mel + nf * df_mel; |
| 877 | double t1; |
| 878 | integer numberOfFrames, frameErrorCount = 0; |
| 879 | Sampled_shortTermAnalysis (me, windowDuration, dt, & numberOfFrames, & t1); |
| 880 | autoSound sframe = Sound_createSimple (1, windowDuration, samplingFrequency); |
| 881 | autoSound window = Sound_createGaussian (windowDuration, samplingFrequency); |
| 882 | autoMelFilter thee = MelFilter_create (my xmin, my xmax, numberOfFrames, dt, t1, fmin_mel, fmax_mel, nf, df_mel, f1_mel); |
| 883 | |
| 884 | autoMelderProgress progress (U"Sound to MelFilter..."); |
| 885 | |
| 886 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 887 | const double t = Sampled_indexToX (thee.get(), iframe); |
| 888 | Sound_into_Sound (me, sframe.get(), t - windowDuration / 2); |
| 889 | Sounds_multiply (sframe.get(), window.get()); |
| 890 | if (! Sound_into_MelFilter_frame (sframe.get(), thee.get(), iframe)) |
| 891 | frameErrorCount ++; |
| 892 | if (iframe % 10 == 1) |
| 893 | Melder_progress ((double) iframe / numberOfFrames, U"Frame ", iframe, U" out of ", numberOfFrames, U"."); |
| 894 | } |
| 895 | |
| 896 | if (frameErrorCount) |
| 897 | Melder_warning (U"Analysis results of ", frameErrorCount, |
| 898 | U" frame(s) out of ", numberOfFrames, U" will be suspect."); |
| 899 | |
| 900 | // Window correction. |
| 901 | |
| 902 | const double ref = FilterBank_DBREF * gaussian_window_squared_correction (window -> nx); |
| 903 | |
| 904 | NUMdmatrix_to_dBs (thy z.get(), ref, FilterBank_DBFAC, FilterBank_DBFLOOR); |
| 905 | return thee; |
| 906 | } catch (MelderError) { |
| 907 | Melder_throw (me, U": no MelFilter created."); |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | /* |
| 912 | Analog formant filter response : |
| 913 | H(f) = i f B / (f1^2 - f^2 + i f B) |
| 914 | */ |
| 915 | static int Sound_into_FormantFilter_frame (Sound me, FormantFilter thee, integer frame, double bw) { |
| 916 | Melder_assert (bw > 0); |
| 917 | autoMatrix pv = Sound_to_spectralpower (me); |
| 918 | const double z1 = pv -> x1; |
| 919 | const double dz = pv -> dx; |
| 920 | const integer nf = pv -> nx; |
| 921 | |
| 922 | for (integer i = 1; i <= thy ny; i ++) { |
| 923 | const double fc = thy y1 + (i - 1) * thy dy; |
| 924 | const constVEC pow = pv -> z.row (1); |
| 925 | double p = 0.0; |
| 926 | for (integer j = 1; j <= nf; j ++) { |
| 927 | // H(f) = ifB / (fc^2 - f^2 + ifB) |
| 928 | // H(f)| = fB / sqrt ((fc^2 - f^2)^2 + f^2B^2) |
| 929 | //|H(f)|^2 = f^2B^2 / ((fc^2 - f^2)^2 + f^2B^2) |
| 930 | // = 1 / (((fc^2 - f^2) /fB)^2 + 1) |
| 931 | |
| 932 | const double f = z1 + (j - 1) * dz; |
| 933 | const double a = NUMformantfilter_amplitude (fc, bw, f); |
| 934 | p += a * pow [j]; |
| 935 | } |
| 936 | thy z [i] [frame] = p; |
| 937 | } |
| 938 | return 1; |
| 939 | } |
| 940 | |
| 941 | autoFormantFilter Sound_to_FormantFilter (Sound me, double analysisWidth, double dt, double f1_hz, double fmax_hz, double df_hz, double relative_bw, |
| 942 | double pitchFloor, double pitchCeiling) |
| 943 | { |
| 944 | try { |
| 945 | autoPitch thee = Sound_to_Pitch (me, dt, pitchFloor, pitchCeiling); |
| 946 | return Sound_Pitch_to_FormantFilter (me, thee.get(), analysisWidth, dt, f1_hz, fmax_hz, df_hz, relative_bw); |
| 947 | } catch (MelderError) { |
| 948 | Melder_throw (me, U": no FormantFilter created."); |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | autoFormantFilter Sound_Pitch_to_FormantFilter (Sound me, Pitch thee, double analysisWidth, double dt, double f1_hz, double fmax_hz, double df_hz, double relative_bw) { |
| 953 | try { |
| 954 | const double windowDuration = 2.0 * analysisWidth; // Gaussian window |
| 955 | const double nyquist = 0.5 / my dx, samplingFrequency = 2.0 * nyquist, fmin_hz = 0.0; |
| 956 | |
| 957 | Melder_require (my xmin >= thy xmin && my xmax <= thy xmax, |
| 958 | U"The domain of the Sound should be included in the domain of the Pitch."); |
| 959 | |
| 960 | double f0_median = Pitch_getQuantile (thee, thy xmin, thy xmax, 0.5, kPitch_unit::HERTZ); |
| 961 | |
| 962 | if (isundef (f0_median) || f0_median == 0.0) { |
| 963 | f0_median = 100.0; |
| 964 | Melder_warning (U"Pitch values undefined. Bandwith fixed to 100 Hz. "); |
| 965 | } |
| 966 | |
| 967 | if (f1_hz <= 0.0) |
| 968 | f1_hz = 100.0; |
| 969 | if (fmax_hz <= 0.0) |
| 970 | fmax_hz = nyquist; |
| 971 | if (df_hz <= 0.0) |
| 972 | df_hz = f0_median / 2.0; |
| 973 | if (relative_bw <= 0.0) |
| 974 | relative_bw = 1.1; |
| 975 | |
| 976 | fmax_hz = std::min (fmax_hz, nyquist); |
| 977 | const integer nf = Melder_iround ( (fmax_hz - f1_hz) / df_hz); |
| 978 | double t1; |
| 979 | integer numberOfFrames; |
| 980 | Sampled_shortTermAnalysis (me, windowDuration, dt, & numberOfFrames, & t1); |
| 981 | autoFormantFilter him = FormantFilter_create (my xmin, my xmax, numberOfFrames, dt, t1, |
| 982 | fmin_hz, fmax_hz, nf, df_hz, f1_hz); |
| 983 | |
| 984 | /* |
| 985 | Temporary objects. |
| 986 | */ |
| 987 | autoSound sframe = Sound_createSimple (1, windowDuration, samplingFrequency); |
| 988 | autoSound window = Sound_createGaussian (windowDuration, samplingFrequency); |
| 989 | autoMelderProgress progress (U"Sound & Pitch: To FormantFilter..."); |
| 990 | integer numberOfUndefinedPitches = 0; |
| 991 | for (integer iframe = 1; iframe <= numberOfFrames; iframe ++) { |
| 992 | const double t = Sampled_indexToX (him.get(), iframe); |
| 993 | double bandwidth, f0 = Pitch_getValueAtTime (thee, t, kPitch_unit::HERTZ, 0); |
| 994 | |
| 995 | if (isundef (f0) || f0 == 0.0) { |
| 996 | numberOfUndefinedPitches ++; |
| 997 | f0 = f0_median; |
| 998 | } |
| 999 | bandwidth = relative_bw * f0; |
| 1000 | Sound_into_Sound (me, sframe.get(), t - windowDuration / 2.0); |
| 1001 | Sounds_multiply (sframe.get(), window.get()); |
| 1002 | |
| 1003 | Sound_into_FormantFilter_frame (sframe.get(), him.get(), iframe, bandwidth); |
| 1004 | |
| 1005 | if (iframe % 10 == 1) |
| 1006 | Melder_progress ((double) iframe / numberOfFrames, U"Frame ", iframe, U" out of ", numberOfFrames, U"."); |
| 1007 | } |
| 1008 | |
| 1009 | const double ref = FilterBank_DBREF * gaussian_window_squared_correction (window -> nx); |
| 1010 | NUMdmatrix_to_dBs (his z.get(), ref, FilterBank_DBFAC, FilterBank_DBFLOOR); |
| 1011 | return him; |
| 1012 | } catch (MelderError) { |
| 1013 | Melder_throw (U"FormantFilter not created from Pitch & FormantFilter."); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | autoSound FilterBank_as_Sound (FilterBank me) { |
| 1018 | try { |
| 1019 | autoSound thee = Sound_create (my ny, my xmin, my xmax, my nx, my dx, my x1); |
| 1020 | for (integer i = 1; i <= my ny; i ++) { |
| 1021 | for (integer j = 1; j <= my nx; j ++) |
| 1022 | thy z [i] [j] = my z [i] [j]; |
| 1023 | } |
| 1024 | return thee; |
| 1025 | } catch (MelderError) { |
| 1026 | Melder_throw (me, U": no Sound created."); |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | autoSound FilterBanks_crossCorrelate (FilterBank me, FilterBank thee, enum kSounds_convolve_scaling scaling, enum kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain) { |
| 1031 | try { |
| 1032 | autoSound cc = Sounds_crossCorrelate ((Sound) me, (Sound) thee, scaling, signalOutsideTimeDomain); |
| 1033 | return cc; |
| 1034 | } catch (MelderError) { |
| 1035 | Melder_throw (me, U" and ", thee, U" not cross-correlated."); |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | autoSound FilterBanks_convolve (FilterBank me, FilterBank thee, enum kSounds_convolve_scaling scaling, enum kSounds_convolve_signalOutsideTimeDomain signalOutsideTimeDomain) { |
| 1040 | try { |
| 1041 | autoSound cc = Sounds_convolve ((Sound) me, (Sound) thee, scaling, signalOutsideTimeDomain); |
| 1042 | return cc; |
| 1043 | } catch (MelderError) { |
| 1044 | Melder_throw (me, U" and ", thee, U" not convolved."); |
| 1045 | } |
| 1046 | } |
| 1047 | |
| 1048 | /* End of file Filterbank.cpp */ |