| 1 | /* Spectrum_to_Formant.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2011,2015-2017,2019,2020,2023 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 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "Spectrum_to_Formant.h"cxx . . $ `<file>/fon/Spectrum_to_Formant.h`/ |
| 20 | |
| 21 | autoFormantcxx . . $ autoFormant# Spectrum_to_Formantcxx . . $ Spectrum_to_Formant(7167eff2009fa9a2). (Spectrumcxx . . $ Spectrum# me, const integercxx . . $ integer# maxnFormants) { |
| 22 | try { |
| 23 | integercxx . . $ integer# nfreq = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; |
| 24 | autoVECcxx . . $ autoVEC# power = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (nfreq); |
| 25 | constexpr double fakeDuration = 1.0; |
| 26 | constexpr double fakeTimeStep = 1.0; |
| 27 | autoFormantcxx . . $ autoFormant# thee = Formant_createcxx . . $ Formant_create(c4f62f8fc805b821). (0.0, fakeDuration, 1, fakeTimeStep, 0.5 * fakeDuration, maxnFormants); |
| 28 | for (integercxx . . $ integer# i = 1; i <= nfreq; i ++) |
| 29 | power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] + mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] * mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 30 | for (integercxx . . $ integer# i = 2; i < nfreq; i ++) |
| 31 | if (power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] > power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - 1] && power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] >= power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1]) { |
| 32 | const double firstDerivative = power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1] - power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - 1]; |
| 33 | const double secondDerivative = 2.0 * power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] - power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i - 1] - power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i + 1]; |
| 34 | const Formant_Formantcxx . . $ Formant_Formant# formant = thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. formantcxx . . $ structFormant_Frame#formant.. appendcxx . . $ autovector#append(155dc7f103cdb9db).(); |
| 35 | formant -> frequencycxx . . $ structFormant_Formant#frequency. = mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * (i - 1 + 0.5 * firstDerivative / secondDerivative); |
| 36 | const double min3dB = 0.5 * (power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] + 0.125 * firstDerivative * firstDerivative / secondDerivative); |
| 37 | /* |
| 38 | Search to the left. |
| 39 | */ |
| 40 | integercxx . . $ integer# j = i - 1; |
| 41 | while (power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] > min3dB && j > 1) |
| 42 | j --; |
| 43 | if (power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] > min3dB) |
| 44 | formant -> bandwidthcxx . . $ structFormant_Formant#bandwidth. = formant -> frequencycxx . . $ structFormant_Formant#frequency.; |
| 45 | else |
| 46 | formant -> bandwidthcxx . . $ structFormant_Formant#bandwidth. = |
| 47 | formant -> frequencycxx . . $ structFormant_Formant#frequency. - |
| 48 | mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * (j - 1 + (min3dB - power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]) / (power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j + 1] - power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j])); |
| 49 | /* |
| 50 | Search to the right. |
| 51 | */ |
| 52 | j = i + 1; |
| 53 | while (power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] > min3dB && j < nfreq) |
| 54 | j ++; |
| 55 | if (power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] > min3dB) |
| 56 | formant -> bandwidthcxx . . $ structFormant_Formant#bandwidth. += mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. - formant -> frequencycxx . . $ structFormant_Formant#frequency.; |
| 57 | else |
| 58 | formant -> bandwidthcxx . . $ structFormant_Formant#bandwidth. += |
| 59 | mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * (j - 1 - (min3dB - power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]) / (power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j - 1] - power [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j])) - |
| 60 | formant -> frequencycxx . . $ structFormant_Formant#frequency.; |
| 61 | if (thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. formantcxx . . $ structFormant_Frame#formant..sizecxx . . $ autovector#size. == maxnFormants) |
| 62 | break; |
| 63 | } |
| 64 | thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. = thycxx . . $ `melder/melder.h:45:9`! framescxx . . $ structFormant#frames. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]. formantcxx . . $ structFormant_Frame#formant..sizecxx . . $ autovector#size.; // maintain invariant |
| 65 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 66 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 67 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Formant."); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /* End of file Spectrum_to_Formant.cpp */ |