| 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"cxx . . $ `<file>/LPC/Roots_and_Formant.h`/ |
| 20 | |
| 21 | static void Formant_Frame_sortcxx . . $ Formant_Frame_sort(cb0c59a7c75fccf5). (Formant_Framecxx . . $ Formant_Frame# me) { |
| 22 | const integercxx . . $ integer# n = mycxx . . $ `melder/melder.h:44:9`! numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants.; |
| 23 | for (integercxx . . $ integer# i = 1; i < n; i ++) { |
| 24 | double min = mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]. frequencycxx . . $ structFormant_Formant#frequency.; |
| 25 | integercxx . . $ integer# imin = i; |
| 26 | for (integercxx . . $ integer# j = i + 1; j <= n; j ++) |
| 27 | if (mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]. frequencycxx . . $ structFormant_Formant#frequency. < min) { |
| 28 | min = mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]. frequencycxx . . $ structFormant_Formant#frequency.; |
| 29 | imin = j; |
| 30 | } |
| 31 | if (imin != i) |
| 32 | stdcxx . . $ std/::swapcxx . . $ std/swap(c75b8fd57d7c2e06). (mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).imin], mycxx . . $ `melder/melder.h:44:9`! formantcxx . . $ structFormant_Frame#formant. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void Roots_into_Formant_Framecxx . . $ Roots_into_Formant_Frame(5aa6edc9255cb8f9). (constRootscxx . . $ constRoots# me, Formant_Framecxx . . $ Formant_Frame# thee, double samplingFrequency, double margin) { |
| 37 | /* |
| 38 | Determine the formants and bandwidths |
| 39 | */ |
| 40 | Melder_precxx . . $ `melder/melder_assert.h:40:10`! (mycxx . . $ `melder/melder.h:44:9`! numberOfRoots == mycxx . . $ `melder/melder.h:44:9`! roots.size); // check invariant |
| 41 | thycxx . . $ `melder/melder.h:45:9`! formantcxx . . $ structFormant_Frame#formant.. resizecxx . . $ autovector#resize(d91e89fbd57cd1). (0); |
| 42 | const double nyquistFrequency = 0.5 * samplingFrequency; |
| 43 | const double fLow = margin, fHigh = nyquistFrequency - margin; |
| 44 | for (integercxx . . $ integer# iroot = 1; iroot <= mycxx . . $ `melder/melder.h:44:9`! numberOfRootscxx . . $ structRoots#numberOfRoots.; iroot ++) { |
| 45 | if (mycxx . . $ `melder/melder.h:44:9`! rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].imagcxx . . $ std/complex#imag(21b6c25e127938e2).() < 0.0) |
| 46 | continue; |
| 47 | const double frequency = fabscxx . . $ fabs(fee4f1d22bc8e617). (atan2cxx . . $ atan2(b71928d39fde4295). (mycxx . . $ `melder/melder.h:44:9`! rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].imagcxx . . $ std/complex#imag(21b6c25e127938e2).(), mycxx . . $ `melder/melder.h:44:9`! rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot].realcxx . . $ std/complex#real(21b6c25e127938e2).())) * nyquistFrequency / NUMpicxx . . $ NUMpi.; |
| 48 | if (frequency >= fLow && frequency <= fHigh) { |
| 49 | const double bandwidth = - logcxx . . $ log(fee4f1d22bc8e617). (normcxx . . $ std/norm(bb57395c51337e15). (mycxx . . $ `melder/melder.h:44:9`! rootscxx . . $ structRoots#roots. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).iroot])) * nyquistFrequency / NUMpicxx . . $ NUMpi.; |
| 50 | Formant_Formantcxx . . $ Formant_Formant# newff = thycxx . . $ `melder/melder.h:45:9`! formantcxx . . $ structFormant_Frame#formant.. appendcxx . . $ autovector#append(155dc7f103cdb9db). (); |
| 51 | newff -> frequencycxx . . $ structFormant_Formant#frequency. = frequency; |
| 52 | newff -> bandwidthcxx . . $ structFormant_Formant#bandwidth. = bandwidth; |
| 53 | } |
| 54 | } |
| 55 | thycxx . . $ `melder/melder.h:45:9`! numberOfFormantscxx . . $ structFormant_Frame#numberOfFormants. = thycxx . . $ `melder/melder.h:45:9`! formantcxx . . $ structFormant_Frame#formant..sizecxx . . $ autovector#size.; // maintain invariant |
| 56 | Formant_Frame_sortcxx . . $ Formant_Frame_sort(cb0c59a7c75fccf5). (thee); |
| 57 | } |
| 58 | |
| 59 | /* End of file Roots_and_Formant.h */ |