| 1 | /* Excitation.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2008,2011,2012,2015-2018,2022,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.h"cxx . . $ `<file>/fon/Excitation.h`/ |
| 20 | |
| 21 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (Excitation, Vector, 2); |
| 22 | |
| 23 | void structExcitationcxx . . $ structExcitation# :: v1_infocxx . . $ structExcitation#v1_info(49f6e7a06ebc5aa8). () { |
| 24 | structDaatacxx . . $ structDaata# :: v1_infocxx . . $ structThing#v1_info(49f6e7a06ebc5aa8). (); |
| 25 | constVECcxx . . $ constVEC# y = zcxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (1); |
| 26 | integercxx . . $ integer# numberOfMaxima = 0; |
| 27 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U"Loudness: ", Melder_halfcxx . . $ Melder_half(f05d861d9dbec9b2). (Excitation_getLoudnesscxx . . $ Excitation_getLoudness(3ef9a452e79b71fe). (this)), U" sones"); |
| 28 | for (integercxx . . $ integer# i = 2; i < nxcxx . . $ structSampled#nx.; i ++) if (y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] > y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i - 1] && y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i] >= y [cxx . . $ constvector#`operator[]`(e616e229aa94bfcf).i + 1]) { |
| 29 | if (++ numberOfMaxima > 15) |
| 30 | break; |
| 31 | double i_real; |
| 32 | double strength = NUMimproveMaximumcxx . . $ NUMimproveMaximum(c86a71e1ecac36cd). (y, i, NUM_PEAK_INTERPOLATE_SINC70cxx . . $ `melder/NUMinterpol.h:35:9`!, & i_real); |
| 33 | double formant_bark = x1cxx . . $ structSampled#x1. + (i_real - 1.0) * dxcxx . . $ structSampled#dx.; |
| 34 | MelderInfo_writecxx . . $ MelderInfo_write(3eb398314feba58b). (U"Peak at ", Melder_singlecxx . . $ Melder_single(f05d861d9dbec9b2). (formant_bark), U" Bark"); |
| 35 | MelderInfo_writecxx . . $ MelderInfo_write(3eb398314feba58b). (U", ", (integercxx . . $ integer#) NUMbarkToHertzcxx . . $ NUMbarkToHertz(447c67c6676f75c3). (formant_bark), U" Hz"); |
| 36 | MelderInfo_writeLinecxx . . $ MelderInfo_writeLine(3eb398314feba58b). (U", ", Melder_halfcxx . . $ Melder_half(f05d861d9dbec9b2). (strength), U" phon."); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | autoExcitationcxx . . $ autoExcitation# Excitation_createcxx . . $ Excitation_create(f4e2e4701e37a6f3). (double frequencyStep, integercxx . . $ integer# numberOfFrequencies) { |
| 41 | try { |
| 42 | autoExcitationcxx . . $ autoExcitation# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Excitation); |
| 43 | Matrix_initcxx . . $ Matrix_init(9dd6c49f9830a091). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), |
| 44 | 0.0, double (numberOfFrequencies) * frequencyStep, numberOfFrequencies, frequencyStep, 0.5 * frequencyStep, |
| 45 | 1.0, 1.0, 1, 1.0, 1.0); |
| 46 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 47 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 48 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"Excitation not created."); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | double Excitation_getDistancecxx . . $ Excitation_getDistance(4e69860fa87da271). (Excitationcxx . . $ Excitation# me, Excitationcxx . . $ Excitation# thee) { |
| 53 | longdoublecxx . . $ longdouble# distance = 0.0, mean = 0.0; |
| 54 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! nx == thycxx . . $ `melder/melder.h:45:9`! nx); |
| 55 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) { |
| 56 | double dper = mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] - thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i]; |
| 57 | mean += dper; |
| 58 | distance += dper * dper; |
| 59 | } |
| 60 | mean /= double (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); |
| 61 | distance /= double (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.); |
| 62 | /* distance -= mean * mean; */ |
| 63 | return sqrtcxx . . $ sqrt(fee4f1d22bc8e617). ((double) distance); |
| 64 | } |
| 65 | |
| 66 | double Excitation_getLoudnesscxx . . $ Excitation_getLoudness(3ef9a452e79b71fe). (Excitationcxx . . $ Excitation# me) { |
| 67 | longdoublecxx . . $ longdouble# loudness = 0.0; |
| 68 | for (integercxx . . $ integer# i = 1; i <= mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.; i ++) |
| 69 | /* Sones = 2 ** ((Phones - 40) / 10) */ |
| 70 | loudness += powcxx . . $ pow(b71928d39fde4295). (2.0, (mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] - 40.0) / 10.0); |
| 71 | return mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx. * (double) loudness; |
| 72 | } |
| 73 | |
| 74 | void Excitation_drawcxx . . $ Excitation_draw(6d0d37e0c0dac415). (Excitationcxx . . $ Excitation# me, Graphicscxx . . $ Graphics# g, |
| 75 | double fmin, double fmax, double minimum, double maximum, bool garnish) |
| 76 | { |
| 77 | if (fmax <= fmin) { |
| 78 | fmin = mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.; |
| 79 | fmax = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.; |
| 80 | } |
| 81 | integercxx . . $ integer# ifmin, ifmax; |
| 82 | Matrix_getWindowSamplesXcxx . . $ Matrix_getWindowSamplesX(c738979be11bd51a). (me, fmin, fmax, & ifmin, & ifmax); |
| 83 | if (maximum <= minimum) |
| 84 | Matrix_getWindowExtremacxx . . $ Matrix_getWindowExtrema(d50591dd897af896). (me, ifmin, ifmax, 1, 1, & minimum, & maximum); |
| 85 | if (maximum <= minimum) { minimum -= 20.0; maximum += 20.0; } |
| 86 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 87 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, fmin, fmax, minimum, maximum); |
| 88 | Graphics_functioncxx . . $ Graphics_function(1c6279e2c8b8beb1). (g, & mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).1] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).0], ifmin, ifmax, |
| 89 | Matrix_columnToXcxx . . $ Matrix_columnToX(89d6d8659270b8b7). (me, double (ifmin)), Matrix_columnToXcxx . . $ Matrix_columnToX(89d6d8659270b8b7). (me, double (ifmax))); |
| 90 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 91 | if (garnish) { |
| 92 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 93 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (g, true, U"Frequency (Bark)"); |
| 94 | Graphics_textLeftcxx . . $ Graphics_textLeft(48cf1f690ec61203). (g, true, U"Excitation (phon)"); |
| 95 | Graphics_marksBottomEverycxx . . $ Graphics_marksBottomEvery(d1b58b4f93ec67ea). (g, 1.0, 5.0, true, true, false); |
| 96 | Graphics_marksLeftEverycxx . . $ Graphics_marksLeftEvery(d1b58b4f93ec67ea). (g, 1.0, 20.0, true, true, false); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | autoMatrixcxx . . $ autoMatrix# Excitation_to_Matrixcxx . . $ Excitation_to_Matrix(a12bcb8aaf119d95). (Excitationcxx . . $ Excitation# me) { |
| 101 | try { |
| 102 | autoMatrixcxx . . $ autoMatrix# thee = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Matrix); |
| 103 | mycxx . . $ `melder/melder.h:44:9`! structMatrixcxx . . $ structMatrix# :: v1_copycxx . . $ structMatrix#v1_copy(42659c2335f2eda7). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); // BUG: safe, but compiler should be able to check |
| 104 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 105 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 106 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Matrix."); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | autoExcitationcxx . . $ autoExcitation# Matrix_to_Excitationcxx . . $ Matrix_to_Excitation(cc6b48c4f3ca70b0). (Matrixcxx . . $ Matrix# me) { |
| 111 | try { |
| 112 | autoExcitationcxx . . $ autoExcitation# thee = Thing_newcxx . . $ `sys/Thing.h:514:9`! (Excitation); |
| 113 | mycxx . . $ `melder/melder.h:44:9`! structMatrixcxx . . $ structMatrix# :: v1_copycxx . . $ structMatrix#v1_copy(42659c2335f2eda7). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 114 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 115 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 116 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Excitation."); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | /* End of file Excitation.cpp */ |