Debian Codegraph @

fon/Excitation.cpp

raw
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"
20
21 Thing_implement (Excitation, Vector, 2);
22
23 void structExcitation :: v1_info () {
24 structDaata :: v1_info ();
25 constVEC y = z.row (1);
26 integer numberOfMaxima = 0;
27 MelderInfo_writeLine (U"Loudness: ", Melder_half (Excitation_getLoudness (this)), U" sones");
28 for (integer i = 2; i < nx; i ++) if (y [i] > y [i - 1] && y [i] >= y [i + 1]) {
29 if (++ numberOfMaxima > 15)
30 break;
31 double i_real;
32 double strength = NUMimproveMaximum (y, i, NUM_PEAK_INTERPOLATE_SINC70, & i_real);
33 double formant_bark = x1 + (i_real - 1.0) * dx;
34 MelderInfo_write (U"Peak at ", Melder_single (formant_bark), U" Bark");
35 MelderInfo_write (U", ", (integer) NUMbarkToHertz (formant_bark), U" Hz");
36 MelderInfo_writeLine (U", ", Melder_half (strength), U" phon.");
37 }
38 }
39
40 autoExcitation Excitation_create (double frequencyStep, integer numberOfFrequencies) {
41 try {
42 autoExcitation me = Thing_new (Excitation);
43 Matrix_init (me.get(),
44 0.0, double (numberOfFrequencies) * frequencyStep, numberOfFrequencies, frequencyStep, 0.5 * frequencyStep,
45 1.0, 1.0, 1, 1.0, 1.0);
46 return me;
47 } catch (MelderError) {
48 Melder_throw (U"Excitation not created.");
49 }
50 }
51
52 double Excitation_getDistance (Excitation me, Excitation thee) {
53 longdouble distance = 0.0, mean = 0.0;
54 Melder_assert (my nx == thy nx);
55 for (integer i = 1; i <= my nx; i ++) {
56 double dper = my z [1] [i] - thy z [1] [i];
57 mean += dper;
58 distance += dper * dper;
59 }
60 mean /= double (my nx);
61 distance /= double (my nx);
62 /* distance -= mean * mean; */
63 return sqrt ((double) distance);
64 }
65
66 double Excitation_getLoudness (Excitation me) {
67 longdouble loudness = 0.0;
68 for (integer i = 1; i <= my nx; i ++)
69 /* Sones = 2 ** ((Phones - 40) / 10) */
70 loudness += pow (2.0, (my z [1] [i] - 40.0) / 10.0);
71 return my dx * (double) loudness;
72 }
73
74 void Excitation_draw (Excitation me, Graphics g,
75 double fmin, double fmax, double minimum, double maximum, bool garnish)
76 {
77 if (fmax <= fmin) {
78 fmin = my xmin;
79 fmax = my xmax;
80 }
81 integer ifmin, ifmax;
82 Matrix_getWindowSamplesX (me, fmin, fmax, & ifmin, & ifmax);
83 if (maximum <= minimum)
84 Matrix_getWindowExtrema (me, ifmin, ifmax, 1, 1, & minimum, & maximum);
85 if (maximum <= minimum) { minimum -= 20.0; maximum += 20.0; }
86 Graphics_setInner (g);
87 Graphics_setWindow (g, fmin, fmax, minimum, maximum);
88 Graphics_function (g, & my z [1] [0], ifmin, ifmax,
89 Matrix_columnToX (me, double (ifmin)), Matrix_columnToX (me, double (ifmax)));
90 Graphics_unsetInner (g);
91 if (garnish) {
92 Graphics_drawInnerBox (g);
93 Graphics_textBottom (g, true, U"Frequency (Bark)");
94 Graphics_textLeft (g, true, U"Excitation (phon)");
95 Graphics_marksBottomEvery (g, 1.0, 5.0, true, true, false);
96 Graphics_marksLeftEvery (g, 1.0, 20.0, true, true, false);
97 }
98 }
99
100 autoMatrix Excitation_to_Matrix (Excitation me) {
101 try {
102 autoMatrix thee = Thing_new (Matrix);
103 my structMatrix :: v1_copy (thee.get()); // BUG: safe, but compiler should be able to check
104 return thee;
105 } catch (MelderError) {
106 Melder_throw (me, U": not converted to Matrix.");
107 }
108 }
109
110 autoExcitation Matrix_to_Excitation (Matrix me) {
111 try {
112 autoExcitation thee = Thing_new (Excitation);
113 my structMatrix :: v1_copy (thee.get());
114 return thee;
115 } catch (MelderError) {
116 Melder_throw (me, U": not converted to Excitation.");
117 }
118 }
119
120 /* End of file Excitation.cpp */