| 1 | /* Excitation_to_Formant.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2011,2015-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 "Excitation_to_Formant.h" |
| 20 | |
| 21 | autoFormant Excitation_to_Formant (Excitation me, integer maximumNumberOfFormants) { |
| 22 | try { |
| 23 | integer numberOfFrequencies = my nx; |
| 24 | double *p = & my z [1] [0]; |
| 25 | |
| 26 | autoFormant thee = Formant_create (0.0, 1.0, 1, 1.0, 0.5, maximumNumberOfFormants); |
| 27 | for (integer i = 2; i < numberOfFrequencies; i ++) |
| 28 | if (p [i] > p [i - 1] && p [i] >= p [i + 1]) { |
| 29 | double firstDerivative = p [i+1] - p [i-1], secondDerivative = 2 * p [i] - p [i-1] - p [i+1]; |
| 30 | Formant_Formant formant = thy frames [1]. formant. append(); |
| 31 | formant -> frequency = NUMbarkToHertz ( |
| 32 | my x1 + my dx * (i - 1 + 0.5 * firstDerivative / secondDerivative)); |
| 33 | double min3phon = p [i] + 0.125 * firstDerivative * firstDerivative / secondDerivative - 3.0; |
| 34 | /* |
| 35 | Search to the left. |
| 36 | */ |
| 37 | integer j = i - 1; |
| 38 | while (p [j] > min3phon && j > 1) |
| 39 | j --; |
| 40 | const double left = NUMbarkToHertz ( |
| 41 | p [j] > min3phon ? my xmin : my x1 + my dx * (j - 1 + (min3phon - p [j]) / (p [j + 1] - p [j]))); |
| 42 | /* |
| 43 | Search to the right. |
| 44 | */ |
| 45 | j = i + 1; |
| 46 | while (p [j] > min3phon && j < numberOfFrequencies) |
| 47 | j ++; |
| 48 | const double right = NUMbarkToHertz ( |
| 49 | p [j] > min3phon ? my xmax : my x1 + my dx * (j - 1 - (min3phon - p [j]) / (p [j - 1] - p [j]))); |
| 50 | formant -> bandwidth = right - left; |
| 51 | if (thy frames [1]. formant.size == thy maxnFormants) |
| 52 | break; |
| 53 | } |
| 54 | thy frames [1]. numberOfFormants = thy frames [1]. formant.size; // maintain invariant |
| 55 | return thee; |
| 56 | } catch (MelderError) { |
| 57 | Melder_throw (me, U": not converted to Formant."); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* End of file Excitation_to_Formant.cpp */ |