| 1 | /* Roots_and_Formant.cpp |
| 2 | * |
| 3 | * Copyright (C) 1994-2026 David Weenink |
| 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. 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 | #include "Roots_and_Formant.h" |
| 20 | |
| 21 | static void Formant_Frame_sort (Formant_Frame me) { |
| 22 | const integer n = my numberOfFormants; |
| 23 | for (integer i = 1; i < n; i ++) { |
| 24 | double min = my formant [i]. frequency; |
| 25 | integer imin = i; |
| 26 | for (integer j = i + 1; j <= n; j ++) |
| 27 | if (my formant [j]. frequency < min) { |
| 28 | min = my formant [j]. frequency; |
| 29 | imin = j; |
| 30 | } |
| 31 | if (imin != i) |
| 32 | std::swap (my formant [imin], my formant [i]); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void Roots_into_Formant_Frame (constRoots me, Formant_Frame thee, double samplingFrequency, double margin) { |
| 37 | /* |
| 38 | Determine the formants and bandwidths |
| 39 | */ |
| 40 | Melder_pre (my numberOfRoots == my roots.size); // check invariant |
| 41 | thy formant. resize (0); |
| 42 | const double nyquistFrequency = 0.5 * samplingFrequency; |
| 43 | const double fLow = margin, fHigh = nyquistFrequency - margin; |
| 44 | for (integer iroot = 1; iroot <= my numberOfRoots; iroot ++) { |
| 45 | if (my roots [iroot].imag() < 0.0) |
| 46 | continue; |
| 47 | const double frequency = fabs (atan2 (my roots [iroot].imag(), my roots [iroot].real())) * nyquistFrequency / NUMpi; |
| 48 | if (frequency >= fLow && frequency <= fHigh) { |
| 49 | const double bandwidth = - log (norm (my roots [iroot])) * nyquistFrequency / NUMpi; |
| 50 | Formant_Formant newff = thy formant. append (); |
| 51 | newff -> frequency = frequency; |
| 52 | newff -> bandwidth = bandwidth; |
| 53 | } |
| 54 | } |
| 55 | thy numberOfFormants = thy formant.size; // maintain invariant |
| 56 | Formant_Frame_sort (thee); |
| 57 | } |
| 58 | |
| 59 | /* End of file Roots_and_Formant.h */ |