| 1 | /* VocalTract_to_Spectrum.cpp |
| 2 | * |
| 3 | * Copyright (C) 1991-2005,2008,2011,2015-2018,2020 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 | /* |
| 20 | * pb 1991/02/28 Pascal and Fortran versions |
| 21 | * pb 1994/03/23 C version |
| 22 | * pb 2002/06/04 |
| 23 | * pb 2002/07/16 GPL |
| 24 | * pb 2008/01/19 double |
| 25 | * pb 2011/06/12 C++ |
| 26 | */ |
| 27 | |
| 28 | #include "VocalTract_to_Spectrum.h"cxx . . $ `<file>/fon/VocalTract_to_Spectrum.h`/ |
| 29 | |
| 30 | #define cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`! 353.0 |
| 31 | #define rhocxx . . $ `fon/VocalTract_to_Spectrum.cpp:31:9`! 1.14 |
| 32 | #define mucxx . . $ `fon/VocalTract_to_Spectrum.cpp:32:9`! 1.86e-5 |
| 33 | #define lambdacxx . . $ `fon/VocalTract_to_Spectrum.cpp:33:9`! 0.0055 |
| 34 | #define cpcxx . . $ `fon/VocalTract_to_Spectrum.cpp:34:9`! 240.0 |
| 35 | #define etacxx . . $ `fon/VocalTract_to_Spectrum.cpp:35:9`! 1.4 |
| 36 | #define shapeFactorcxx . . $ `fon/VocalTract_to_Spectrum.cpp:36:9`! 2.0 |
| 37 | |
| 38 | static void TUBE_transfercxx . . $ TUBE_transfer(684789b0aa2e973a). (double area [], integercxx . . $ integer# numberOfSections, double sectionLength, double frequency, |
| 39 | double *re, double *im, /* Output. */ |
| 40 | double glottalDamping, /* 0 or 0.1 */ |
| 41 | bool hasRadiationDamping, bool hasInternalDamping) |
| 42 | /* (Re,Im) := (air current at lips) / (air current at glottis) */ |
| 43 | { |
| 44 | double omega = 2.0 * NUMpicxx . . $ NUMpi. * frequency; |
| 45 | if (hasInternalDamping) { |
| 46 | double bareResistance = sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (mucxx . . $ `fon/VocalTract_to_Spectrum.cpp:32:9`! * rhocxx . . $ `fon/VocalTract_to_Spectrum.cpp:31:9`! / 2.0) * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (omega); |
| 47 | double bareConductance = (etacxx . . $ `fon/VocalTract_to_Spectrum.cpp:35:9`! - 1.0) / (rhocxx . . $ `fon/VocalTract_to_Spectrum.cpp:31:9`! * cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`! * cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`!) * |
| 48 | sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (lambdacxx . . $ `fon/VocalTract_to_Spectrum.cpp:33:9`! / (2.0 * cpcxx . . $ `fon/VocalTract_to_Spectrum.cpp:34:9`! * rhocxx . . $ `fon/VocalTract_to_Spectrum.cpp:31:9`!)) * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (omega); |
| 49 | dcomplexcxx . . $ dcomplex# ccxx . . $ std/complex#complex(bc12c9ec15586e6a). { glottalDamping * area [1] / (rhocxx . . $ `fon/VocalTract_to_Spectrum.cpp:31:9`! * cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`!), 0.0 }; |
| 50 | dcomplexcxx . . $ dcomplex# dcxx . . $ std/complex#complex(bc12c9ec15586e6a). { 1.0, 0.0 }; |
| 51 | for (integercxx . . $ integer# section = 1; section <= numberOfSections; section ++) { |
| 52 | double a = area [section]; |
| 53 | double perimeter = shapeFactorcxx . . $ `fon/VocalTract_to_Spectrum.cpp:36:9`! * 2.0 * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (NUMpicxx . . $ NUMpi. * a); |
| 54 | double perimeter_by_a2 = perimeter / (a * a); |
| 55 | double inertance = rhocxx . . $ `fon/VocalTract_to_Spectrum.cpp:31:9`! / a; |
| 56 | double compliance = a / (rhocxx . . $ `fon/VocalTract_to_Spectrum.cpp:31:9`! * cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`! * cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`!); |
| 57 | dcomplexcxx . . $ dcomplex# cascadecxx . . $ std/complex#complex(bc12c9ec15586e6a). { perimeter_by_a2 * bareResistance, omega * inertance + perimeter_by_a2 * bareResistance }; |
| 58 | dcomplexcxx . . $ dcomplex# parallelcxx . . $ std/complex#complex(bc12c9ec15586e6a). { perimeter * bareConductance, omega * compliance }; |
| 59 | dcomplexcxx . . $ dcomplex# gamma = dcomplex_sqrtcxx . . $ dcomplex_sqrt(b85d93c470fe5288). (dcomplex_mulcxx . . $ dcomplex_mul(ce4e3faad6c748f3). (cascadecxx . . $ std/complex#complex(20ef1611af677bca)., parallelcxx . . $ std/complex#complex(20ef1611af677bca).)); |
| 60 | dcomplexcxx . . $ dcomplex# impedance = dcomplex_divcxx . . $ dcomplex_div(ce4e3faad6c748f3). (gammacxx . . $ std/complex#complex(20ef1611af677bca)., parallelcxx . . $ std/complex#complex(20ef1611af677bca).); |
| 61 | gamma =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). dcomplex_rmulcxx . . $ dcomplex_rmul(bdae0e633a00aab1). (sectionLength, gammacxx . . $ std/complex#complex(20ef1611af677bca).); |
| 62 | dcomplexcxx . . $ dcomplex# help = dcomplex_rmulcxx . . $ dcomplex_rmul(bdae0e633a00aab1). (0.5, dcomplex_expcxx . . $ dcomplex_exp(b85d93c470fe5288). (gammacxx . . $ std/complex#complex(20ef1611af677bca).)); |
| 63 | dcomplexcxx . . $ dcomplex# help1 = dcomplex_divcxx . . $ dcomplex_div(ce4e3faad6c748f3). ({cxx . . $ std/complex#complex(bc12c9ec15586e6a). 0.25, 0.0 }, helpcxx . . $ std/complex#complex(20ef1611af677bca).); |
| 64 | dcomplexcxx . . $ dcomplex# sinhg = dcomplex_subcxx . . $ dcomplex_sub(ce4e3faad6c748f3). (helpcxx . . $ std/complex#complex(20ef1611af677bca)., help1cxx . . $ std/complex#complex(20ef1611af677bca).); |
| 65 | dcomplexcxx . . $ dcomplex# coshg = dcomplex_addcxx . . $ dcomplex_add(ce4e3faad6c748f3). (helpcxx . . $ std/complex#complex(20ef1611af677bca)., help1cxx . . $ std/complex#complex(20ef1611af677bca).); |
| 66 | help =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). dcomplex_addcxx . . $ dcomplex_add(ce4e3faad6c748f3). (dcomplex_mulcxx . . $ dcomplex_mul(ce4e3faad6c748f3). (ccxx . . $ std/complex#complex(20ef1611af677bca)., coshgcxx . . $ std/complex#complex(20ef1611af677bca).), dcomplex_divcxx . . $ dcomplex_div(ce4e3faad6c748f3). (dcomplex_mulcxx . . $ dcomplex_mul(ce4e3faad6c748f3). (dcxx . . $ std/complex#complex(20ef1611af677bca)., sinhgcxx . . $ std/complex#complex(20ef1611af677bca).), impedancecxx . . $ std/complex#complex(20ef1611af677bca).)); |
| 67 | d =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). dcomplex_addcxx . . $ dcomplex_add(ce4e3faad6c748f3). (dcomplex_mulcxx . . $ dcomplex_mul(ce4e3faad6c748f3). (dcxx . . $ std/complex#complex(20ef1611af677bca)., coshgcxx . . $ std/complex#complex(20ef1611af677bca).), dcomplex_mulcxx . . $ dcomplex_mul(ce4e3faad6c748f3). (dcomplex_mulcxx . . $ dcomplex_mul(ce4e3faad6c748f3). (impedancecxx . . $ std/complex#complex(20ef1611af677bca)., ccxx . . $ std/complex#complex(20ef1611af677bca).), sinhgcxx . . $ std/complex#complex(20ef1611af677bca).)); |
| 68 | c =cxx . . $ std/complex#`operator=`(a69abc4e8167d246). help; |
| 69 | } |
| 70 | if (hasRadiationDamping) { |
| 71 | double ka = omega * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (area [numberOfSections] / NUMpicxx . . $ NUMpi.) / cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`!; |
| 72 | double z = rhocxx . . $ `fon/VocalTract_to_Spectrum.cpp:31:9`! * cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`! / area [numberOfSections]; |
| 73 | double radiationResistance = z * ka * ka / 2.0; |
| 74 | double radiationReactance = z * 8.0 * ka / 3.0 / NUMpicxx . . $ NUMpi.; |
| 75 | *re = d.realcxx . . $ std/complex#real(21b6c25e127938e2).() + c.realcxx . . $ std/complex#real(21b6c25e127938e2).() * radiationResistance - c.imagcxx . . $ std/complex#imag(21b6c25e127938e2).() * radiationReactance; |
| 76 | *im = d.imagcxx . . $ std/complex#imag(21b6c25e127938e2).() + c.imagcxx . . $ std/complex#imag(21b6c25e127938e2).() * radiationResistance + c.realcxx . . $ std/complex#real(21b6c25e127938e2).() * radiationReactance; |
| 77 | } else { *re = d.realcxx . . $ std/complex#real(21b6c25e127938e2).(); *im = d.imagcxx . . $ std/complex#imag(21b6c25e127938e2).(); }; |
| 78 | } else { |
| 79 | double c_re, c_im; |
| 80 | double angle = omega * sectionLength / cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`!, cosAngle = coscxx . . $ cos(fee4f1d22bc8e617). (angle), sinAngle = sincxx . . $ sin(fee4f1d22bc8e617). (angle); |
| 81 | /* The parallel conductance of the glottis is due to air escaping */ |
| 82 | /* through it. It is approximated as 0.1 A / rho C */ |
| 83 | c_re = glottalDamping * cosAngle; |
| 84 | c_im = sinAngle; |
| 85 | *re = cosAngle; |
| 86 | *im = glottalDamping * sinAngle; |
| 87 | for (integercxx . . $ integer# section = 1; section < numberOfSections; section ++) { |
| 88 | double k = area [section] / area [section + 1]; |
| 89 | double reDummy = c_re * k * cosAngle - *im * sinAngle; |
| 90 | double imDummy = c_im * k * cosAngle + *re * sinAngle; |
| 91 | *re = *re * cosAngle - c_im * k * sinAngle; |
| 92 | *im = *im * cosAngle + c_re * k * sinAngle; |
| 93 | c_re = reDummy; |
| 94 | c_im = imDummy; |
| 95 | } |
| 96 | /* Radiation impedance at the lips. */ |
| 97 | /* Energy radiates from the mouth. This loses energy to the formants. */ |
| 98 | if (hasRadiationDamping) { |
| 99 | double ka = omega * sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (area [numberOfSections] / NUMpicxx . . $ NUMpi.) / cccxx . . $ `fon/VocalTract_to_Spectrum.cpp:30:9`!; |
| 100 | double radiationResistance = ka * ka / 2; |
| 101 | double radiationReactance = 8.0 * ka / 3.0 / NUMpicxx . . $ NUMpi.; |
| 102 | *re += c_re * radiationResistance - c_im * radiationReactance; |
| 103 | *im += c_re * radiationReactance + c_im * radiationResistance; |
| 104 | } |
| 105 | } |
| 106 | /* Divide into 1.0. BUG: this is not division into 1. */ |
| 107 | { |
| 108 | double power = *re * *re + *im * *im; |
| 109 | if (power != 0.0) { *re = *re / power; *im = *im / power; } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | autoSpectrumcxx . . $ autoSpectrum# VocalTract_to_Spectrumcxx . . $ VocalTract_to_Spectrum(bc47d1a1ea8004d6). |
| 114 | (VocalTractcxx . . $ VocalTract# me, integercxx . . $ integer# numberOfFrequencies, double maximumFrequency, |
| 115 | double glottalDamping, bool hasRadiationDamping, bool hasInternalDamping) |
| 116 | { |
| 117 | try { |
| 118 | autoSpectrumcxx . . $ autoSpectrum# thee = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (maximumFrequency, numberOfFrequencies); |
| 119 | for (integercxx . . $ integer# ifreq = 1; ifreq <= numberOfFrequencies; ifreq ++) { |
| 120 | TUBE_transfercxx . . $ TUBE_transfer(684789b0aa2e973a). (& mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).0], mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx., mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx., |
| 121 | (ifreq - 0.9999) * maximumFrequency / (numberOfFrequencies - 1), |
| 122 | & thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq], & thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq], |
| 123 | glottalDamping, hasRadiationDamping, hasInternalDamping); |
| 124 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq] *= 0.02; |
| 125 | thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).ifreq] *= 0.02; |
| 126 | } |
| 127 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 128 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 129 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Spectrum."); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /* End of file VocalTract_to_Spectrum.cpp */ |