Debian Codegraph @

LPC/FormantModelerList.cpp

raw symbols SCIP
1 /* FormantModelerList.cpp
2 *
3 * Copyright (C) 2020 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 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. 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 "FormantModelerList.h"
20
21 #include "oo_DESTROY.h"
22 #include "FormantModelerList_def.h"
23 #include "oo_COPY.h"
24 #include "FormantModelerList_def.h"
25 #include "oo_EQUAL.h"
26 #include "FormantModelerList_def.h"
27 #include "oo_CAN_WRITE_AS_ENCODING.h"
28 #include "FormantModelerList_def.h"
29 #include "oo_WRITE_BINARY.h"
30 #include "FormantModelerList_def.h"
31 #include "oo_READ_BINARY.h"
32 #include "FormantModelerList_def.h"
33 #include "oo_WRITE_TEXT.h"
34 #include "FormantModelerList_def.h"
35 #include "oo_READ_TEXT.h"
36 #include "FormantModelerList_def.h"
37 #include "oo_DESCRIPTION.h"
38 #include "FormantModelerList_def.h"
39 /*
40 void structFormantModelerListDrawingSpecification :: v1_writeBinary (FILE *_filePointer_) {}
41 void structFormantModelerList :: v1_writeBinary (FILE *_filePointer_) {}
42 void structFormantModelerListDrawingSpecification :: v1_readBinary (FILE *_filePointer_, int _formatVersion_) {}
43 void structFormantModelerList :: v1_readBinary (FILE *_filePointer_, int _formatVersion_) {}
44
45 */
46 void structFormantModelerList :: v1_info () {
47 // do nothing
48 };
49
50 Thing_implement (FormantModelerList, Function, 0);
51 Thing_implement (FormantModelerListDrawingSpecification, Daata, 0);
52
53 autoFormantModelerList FormantPath_to_FormantModelerList (FormantPath me, double startTime, double endTime, constINTVEC const& numberOfParametersPerTrack) {
54 try {
55 autoFormantModelerList thee = Thing_new (FormantModelerList);
56 thy xmin = startTime;
57 thy xmax = endTime;
58 Melder_require (numberOfParametersPerTrack.size > 0,
59 U"The number of items in the parameter list should be larger than zero.");
60 thy numberOfTracksPerModel = numberOfParametersPerTrack.size;
61 integer numberOfZeros = 0;
62 for (integer ipar = 1; ipar <= numberOfParametersPerTrack.size; ipar ++) {
63 const integer value = numberOfParametersPerTrack [ipar];
64 Melder_require (value >= 0,
65 U"Numbers in the 'Number of parameter list' should be positive.");
66 if (value == 0)
67 numberOfZeros += 1;
68 }
69 thy numberOfParametersPerTrack = copy_INTVEC (numberOfParametersPerTrack);
70 thy numberOfTracksPerModel = thy numberOfParametersPerTrack.size;
71 thy numberOfModelers = my formantCandidates.size;
72 for (integer imodel = 1; imodel <= thy numberOfModelers; imodel ++) {
73 Formant formanti = (Formant) my formantCandidates.at [imodel];
74 autoFormantModeler fm = Formant_to_FormantModeler (formanti, startTime, endTime, thy numberOfParametersPerTrack.get());
75 Thing_setName (fm.get(), Melder_fixed (my ceilings [imodel], 0));
76 thy formantModelers. addItem_move (fm.move());
77 }
78 thy drawingSpecification = FormantModelerList_to_FormantModelerListDrawingSpecification (thee.get(), 0);
79 return thee;
80 } catch (MelderError) {
81 Melder_throw (me, U": FormantModelerList not created.");
82 }
83 }
84
85 void FormantModelerList_showBest3 (FormantModelerList me) {
86 autoINTVEC best3 = FormantModelerList_getBest3 (me);
87 INTVEC drawingOrder = my drawingSpecification -> drawingOrder.get();
88 drawingOrder.part (1, 3) <<= best3.all();
89 my drawingSpecification -> numberOfModelersToDraw = 3;
90 }
91
92 void FormantModelerList_markBest3 (FormantModelerList me) {
93 /*
94 3 The smoothest F1 score
95 2 The smoothest F1 & F2 score
96 1 the smoothest F1 & F2 & F3 score
97 */
98 autoINTVEC best3 = FormantModelerList_getBest3 (me);
99 for (integer imodel = 1; imodel <= my numberOfModelers; imodel ++) {
100 autoMelderString best;
101 if (imodel == best3 [1]) {
102 MelderString_append (& best, U"F123");
103 }
104 if (imodel == best3 [2]) {
105 MelderString_append (& best, ( best.string && best.string [0] ? U"&F12" : U"F12" ));
106 }
107 if (imodel == best3 [3]) {
108 MelderString_append (& best, ( best.string && best.string [0] ? U"&F1" : U"F1" ));
109 }
110 my drawingSpecification -> midTopText [imodel] = Melder_dup (best.string);
111 }
112 }
113
114 integer FormantModelerList_getBestModelIndex (FormantModelerList me, integer fromTrack, integer toTrack) {
115 double wmin = std::numeric_limits<double>::max();
116 integer best = 0;
117 for (integer imodel = 1; imodel <= my numberOfModelers; imodel ++) {
118 FormantModeler fm = my formantModelers.at [imodel];
119 const double w = FormantModeler_getStress (fm, fromTrack, toTrack, 0, my varianceExponent);
120 if (w < wmin) {
121 wmin = w;
122 best = imodel;
123 }
124 }
125 return best;
126 }
127
128 autoINTVEC FormantModelerList_getBest3 (FormantModelerList me) {
129 /*
130 3 The least stress F1 score
131 2 The least (summed) stress F1 & F2 score
132 1 the least (summed) stress F1 & F2 & F3 score
133 */
134 autoINTVEC best = raw_INTVEC (3);
135 double stressF1, stressF1F2, stressF1F2F3;
136 stressF1 = stressF1F2 = stressF1F2F3 = std::numeric_limits<double>::max();
137 for (integer imodel = 1; imodel <= my numberOfModelers; imodel ++) {
138 FormantModeler fm = my formantModelers.at [imodel];
139 double stress = FormantModeler_getStress (fm, 1, 1, 0, my varianceExponent);
140 if (stress < stressF1) {
141 stressF1 = stress;
142 best [3] = imodel;
143 }
144 stress = FormantModeler_getStress (fm, 1, 2, 0, my varianceExponent);
145 if (stress < stressF1F2) {
146 stressF1F2 = stress;
147 best [2] = imodel;
148 }
149 stress = FormantModeler_getStress (fm, 1, 3, 0, my varianceExponent);
150 if (stress < stressF1F2F3) {
151 stressF1F2F3 = stress;
152 best [1] = imodel;
153 }
154 }
155 return best;
156 }
157
158 static void getMatrixGridLayout (integer numberOfModels, integer *out_numberOfRows, integer *out_numberOfColums) {
159 integer ncol = 1;
160 integer nrow = 3;
161 if (numberOfModels > 3) {
162 nrow = 1 + Melder_ifloor (sqrt (numberOfModels - 0.5));
163 ncol = 1 + Melder_ifloor ((numberOfModels - 1) / nrow);
164 }
165 if (out_numberOfRows)
166 *out_numberOfRows = nrow;
167 if (out_numberOfColums)
168 *out_numberOfColums = ncol;
169 }
170
171 void FormantModelerList_getMatrixGridLayout (FormantModelerList me, integer *out_numberOfRows, integer *out_numberOfColums) {
172 getMatrixGridLayout (my drawingSpecification -> numberOfModelersToDraw, out_numberOfRows, out_numberOfColums);
173 }
174
175 void FormantModelerListDrawingSpecification_showAll (FormantModelerListDrawingSpecification me) {
176 my numberOfModelersToDraw = my numberOfModelers;
177 to_INTVEC_out (my drawingOrder.get());
178 }
179
180 integer FormantModelerListDrawingSpecification_getNumberOfShown (FormantModelerListDrawingSpecification me) {
181 return my numberOfModelersToDraw;
182 }
183
184 void FormantModelerListDrawingSpecification_setModelerColours (FormantModelerListDrawingSpecification me, conststring32 oddFormantColour_string, conststring32 evenFormantColour_string, conststring32 selectedCandidateColour_string) {
185 my oddFormantColour = MelderColour_fromColourNameOrNumberStringOrRGBString (oddFormantColour_string);
186 my evenFormantColour = MelderColour_fromColourNameOrNumberStringOrRGBString (evenFormantColour_string);
187 my selectedCandidateColour = MelderColour_fromColourNameOrNumberStringOrRGBString (selectedCandidateColour_string);
188 }
189
190 autoFormantModelerListDrawingSpecification FormantModelerList_to_FormantModelerListDrawingSpecification (FormantModelerList me, integer defaultModeler) {
191 try {
192 autoFormantModelerListDrawingSpecification thee = Thing_new (FormantModelerListDrawingSpecification);
193 thy numberOfModelers = my numberOfModelers;
194 thy drawingOrder = to_INTVEC (my numberOfModelers);
195 thy numberOfModelersToDraw = my numberOfModelers;
196 thy boxLineWidth = 4.0;
197 thy oddFormantColour = Melder_RED;
198 thy evenFormantColour = Melder_MAROON;
199 thy selectedCandidateColour = Melder_RED;
200 thy midTopText_colour = Melder_PURPLE;
201 autoSTRVEC midTopText (my numberOfModelers);
202 for (integer imodel = 1; imodel <= my numberOfModelers; imodel ++)
203 midTopText [imodel] = Melder_dup (U"");
204 thy midTopText = copy_STRVEC (midTopText.get());
205 return thee;
206 } catch (MelderError) {
207 Melder_throw (U"No FormantModelerListDrawingSpecification created.");
208 }
209 }
210
211 void FormantModelerList_drawInMatrixGrid (FormantModelerList me, Graphics g, integer nrow, integer ncol, kGraphicsMatrixOrigin origin, double spaceBetweenFraction_x, double spaceBetweenFraction_y, integer fromFormant, integer toFormant, double fmax, double yGridLineEvery_Hz, double xCursor, double yCursor, integer numberOfParameters, bool drawErrorBars, double barwidth_s, bool drawEstimated, bool garnish) {
212 if (nrow <= 0 || ncol <= 0)
213 FormantModelerList_getMatrixGridLayout (me, & nrow, & ncol);
214 const double fmin = 0.0;
215 double x1NDC, x2NDC, y1NDC, y2NDC;
216 Graphics_inqViewport (g, & x1NDC, & x2NDC, & y1NDC, & y2NDC);
217 const double fontSize_old = Graphics_inqFontSize (g), newFontSize = 8.0;
218 auto getXtick = [] (Graphics gg, double fontSize) {
219 const double margin = 2.8 * fontSize * gg -> resolution / 72.0;
220 const double wDC = (gg -> d_x2DC - gg -> d_x1DC) / (gg -> d_x2wNDC - gg -> d_x1wNDC) * (gg -> d_x2NDC - gg -> d_x1NDC);
221 double dx = 1.5 * margin / wDC;
222 const double xTick = 0.06 * dx;
223 Melder_clipRight (& dx, 0.4);
224 return xTick / (1.0 - 2.0 * dx);
225 };
226 auto getYtick = [] (Graphics gg, double fontSize) {
227 const double margin = 2.8 * fontSize * gg -> resolution / 72.0;
228 const double hDC = integer_abs (gg->d_y2DC - gg->d_y1DC) / (gg->d_y2wNDC - gg->d_y1wNDC) * (gg->d_y2NDC - gg-> d_y1NDC);
229 double dy = margin / hDC;
230 const double yTick = 0.09 * dy;
231 Melder_clipRight (& dy, 0.4);
232 return yTick / (1.0 - 2.0 * dy);
233 };
234 const bool fillUp = ( origin == kGraphicsMatrixOrigin::BOTTOM_LEFT || origin == kGraphicsMatrixOrigin::BOTTOM_RIGHT );
235 const bool rightToLeft = ( origin == kGraphicsMatrixOrigin::TOP_RIGHT || origin ==kGraphicsMatrixOrigin:: BOTTOM_RIGHT );
236 const double vp_width = x2NDC - x1NDC, vp_height = y2NDC - y1NDC;
237 const double vpi_width = vp_width / (ncol + (ncol - 1) * spaceBetweenFraction_x);
238 const double vpi_height = vp_height / (nrow + (nrow - 1) * spaceBetweenFraction_y);
239 for (integer index = 1; index <= my drawingSpecification->numberOfModelersToDraw; index ++) {
240 const integer irow1 = 1 + (index - 1) / ncol; // left-to-right + top-to-bottom
241 const integer icol1 = 1 + (index - 1) % ncol;
242 const integer icol = ( rightToLeft ? ncol - icol1 + 1 : icol1 );
243 const integer irow = ( fillUp ? nrow - irow1 + 1 : irow1 );
244 const double vpi_x1 = x1NDC + (icol - 1) * vpi_width * (1.0 + spaceBetweenFraction_x);
245 const double vpi_x2 = vpi_x1 + vpi_width;
246 const double vpi_y2 = y2NDC - (irow - 1) * vpi_height * (1.0 + spaceBetweenFraction_y);
247 const double vpi_y1 = vpi_y2 - vpi_height;
248 const integer imodel = my drawingSpecification -> drawingOrder [index];
249 FormantModeler fm = my formantModelers.at [imodel];
250 Graphics_setViewport (g, vpi_x1, vpi_x2, vpi_y1, vpi_y2);
251 Graphics_setWindow (g, fm -> xmin, fm -> xmax, 0.0, fmax);
252 FormantModeler_speckle_inside (fm, g, fm -> xmin, fm -> xmax, fmax, fromFormant, toFormant,
253 drawEstimated, 0.0, drawErrorBars, my drawingSpecification -> oddFormantColour, my drawingSpecification -> evenFormantColour);
254
255 Graphics_setLineWidth (g, my drawingSpecification -> boxLineWidth);
256 Graphics_setColour (g, (imodel == my drawingSpecification -> selectedCandidate ?
257 my drawingSpecification -> selectedCandidateColour : Melder_BLACK ));
258 Graphics_rectangle (g, fm -> xmin, fm -> xmax, fmin, fmax);
259 Graphics_setLineType (g, Graphics_DRAWN);
260 Graphics_setColour (g, Melder_BLACK);
261 Graphics_setLineWidth (g, 1.0);
262 /*
263 Mark name & roughness
264 */
265 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::RIGHT, Graphics_HALF);
266 Graphics_text (g, fm -> xmax - 0.05 * (fm -> xmax - fm -> xmin),
267 fmax - 0.05 * fmax, fm -> name.get());
268 double w = FormantModeler_getStress (fm, fromFormant, toFormant, 0, my varianceExponent);
269 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::LEFT, Graphics_HALF);
270 Graphics_text (g, fm -> xmin + 0.05 * (fm -> xmax - fm -> xmin),
271 fmax - 0.05 * fmax, Melder_fixed (w, 2));
272 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_HALF);
273 conststring32 midTopText = my drawingSpecification -> midTopText [imodel].get();
274 if (midTopText && midTopText [0]) {
275 Graphics_setColour (g,my drawingSpecification -> midTopText_colour);
276 Graphics_text (g, fm -> xmin + 0.5 * (fm -> xmax - fm -> xmin),
277 fmax - 0.05 * fmax, my drawingSpecification -> midTopText [imodel].get());
278 Graphics_setColour (g, Melder_BLACK);
279 }
280
281 if (garnish) {
282 const double xTick = (double) getXtick (g, newFontSize) * (fm -> xmax - fm -> xmin);
283 const double yTick = (double) getYtick (g, newFontSize) * (fmax - 0.0);
284 if (icol == 1 && irow % 2 == 1) {
285 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::RIGHT, Graphics_HALF);
286 Graphics_line (g, fm -> xmin - xTick, fmax, fm -> xmin, fmax);
287 Graphics_text (g, fm -> xmin - xTick, fmax, Melder_iround (fmax));
288 Graphics_line (g, fm -> xmin - xTick, 0.0, fm -> xmin, 0.0);
289 Graphics_text (g, fm -> xmin - xTick, 0.0, U"0.0");
290 } else if (icol == ncol && irow % 2 == 0) {
291 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::LEFT, Graphics_HALF);
292 Graphics_text (g, fm -> xmax, fmax, Melder_iround (fmax));
293 Graphics_text (g, fm -> xmax, 0.0, U"0.0");
294 }
295 if (irow == 1 && icol % 2 == 0) {
296 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_BOTTOM);
297 Graphics_line (g, fm -> xmin, fmax, fm -> xmin, fmax + yTick);
298 Graphics_text (g, fm -> xmin, fmax + yTick, Melder_fixed (fm -> xmin, 3));
299 Graphics_line (g, fm -> xmax, fmax, fm -> xmax, fmax + yTick);
300 Graphics_text (g, fm -> xmax, fmax + yTick, Melder_fixed (fm -> xmax, 3));
301 } else if (irow == nrow && icol % 2 == 1) {
302 Graphics_setTextAlignment (g, kGraphics_horizontalAlignment::CENTRE, Graphics_TOP);
303 Graphics_line (g, fm -> xmin, 0.0, fm -> xmin, 0.0 - yTick);
304 Graphics_text (g, fm -> xmin, 0.0 - yTick, Melder_fixed (fm -> xmin, 3));
305 Graphics_line (g, fm -> xmax, 0.0, fm -> xmax, 0.0 - yTick);
306 Graphics_text (g, fm -> xmax, 0.0 - yTick, Melder_fixed (fm -> xmax, 3));
307 }
308 double yGridLine_Hz = yGridLineEvery_Hz;
309 Graphics_setLineType (g, Graphics_DOTTED);
310 while (yGridLine_Hz < 0.95 * fmax) {
311 Graphics_line (g, fm -> xmin, yGridLine_Hz, fm -> xmax, yGridLine_Hz);
312 yGridLine_Hz += yGridLineEvery_Hz;
313 }
314 /*
315 Cursors
316 */
317 Graphics_setColour (g, Melder_RED);
318 Graphics_setLineType (g, Graphics_DASHED);
319 if (xCursor > fm -> xmin && xCursor <= fm -> xmax)
320 Graphics_line (g, xCursor, 0.0, xCursor, fmax);
321 if (yCursor > 0.0 && yCursor < fmax)
322 Graphics_line (g, fm -> xmin, yCursor, fm -> xmax, yCursor);
323 Graphics_setColour (g, Melder_BLACK);
324 Graphics_setLineType (g, Graphics_DRAWN);
325 }
326 }
327 Graphics_setFontSize (g, fontSize_old);
328 Graphics_setViewport (g, x1NDC, x2NDC, y1NDC, y2NDC);
329 }
330
331 /* End of file FormantModelerList.cpp */