Debian Codegraph @

dwtools/Spectrogram_extensions.cpp

raw symbols SCIP
1 /* Spectrogram_extensions.cpp
2 *
3 * Copyright (C) 2014-2024 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 /*
20 djmw 20140913
21 */
22
23 #include "Eigen_and_Matrix.h"
24 #include "Spectrogram_extensions.h"
25 #include "Matrix_extensions.h"
26 #include "NUM2.h"
27
28 Thing_implement (BandFilterSpectrogram, Matrix, 2);
29
30 void structBandFilterSpectrogram :: v1_info () {
31 structDaata :: v1_info ();
32 MelderInfo_writeLine (U"Time domain:");
33 MelderInfo_writeLine (U" Start time: ", xmin, U" seconds");
34 MelderInfo_writeLine (U" End time: ", xmax, U" seconds");
35 MelderInfo_writeLine (U" Total duration: ", xmax - xmin, U" seconds");
36 MelderInfo_writeLine (U"Time sampling:");
37 MelderInfo_writeLine (U" Number of time slices (frames): ", nx);
38 MelderInfo_writeLine (U" Time step (frame distance): ", dx, U" seconds");
39 MelderInfo_writeLine (U" First time slice (frame centre) at: ", x1, U" seconds");
40 }
41
42 void structBarkSpectrogram :: v1_info () {
43 structBandFilterSpectrogram :: v1_info ();
44 MelderInfo_writeLine (U"Frequency domain:");
45 MelderInfo_writeLine (U" Lowest frequency: ", ymin, U" ", v_getFrequencyUnit ());
46 MelderInfo_writeLine (U" Highest frequency: ", ymax, U" ", v_getFrequencyUnit ());
47 MelderInfo_writeLine (U" Total bandwidth: ", ymax - ymin, U" ", v_getFrequencyUnit ());
48 MelderInfo_writeLine (U"Frequency sampling:");
49 MelderInfo_writeLine (U" Number of frequency bands (bins): ", ny);
50 MelderInfo_writeLine (U" Frequency step (bin width): ", dy, U" ", v_getFrequencyUnit ());
51 MelderInfo_writeLine (U" First frequency band around (bin centre at): ", y1, U" ", v_getFrequencyUnit ());
52 }
53
54 void structMelSpectrogram :: v1_info () {
55 structBandFilterSpectrogram :: v1_info ();
56 MelderInfo_writeLine (U"Frequency domain:");
57 MelderInfo_writeLine (U" Lowest frequency: ", ymin, U" ", v_getFrequencyUnit ());
58 MelderInfo_writeLine (U" Highest frequency: ", ymax, U" ", v_getFrequencyUnit ());
59 MelderInfo_writeLine (U" Total bandwidth: ", ymax - ymin, U" ", v_getFrequencyUnit ());
60 MelderInfo_writeLine (U"Frequency sampling:");
61 MelderInfo_writeLine (U" Number of frequency bands (bins): ", ny);
62 MelderInfo_writeLine (U" Frequency step (bin width): ", dy, U" ", v_getFrequencyUnit ());
63 MelderInfo_writeLine (U" First frequency band around (bin centre at): ", y1, U" ", v_getFrequencyUnit ());
64 }
65
66 // Preconditions: 1 <= iframe <= nx; 1 <= irow <= ny
67 double structBandFilterSpectrogram :: v_getValueAtSample (integer iframe, integer ifreq, int units) const {
68 double val = undefined;
69 if (units == 0)
70 val = z [ifreq] [iframe];
71 else {
72 val = -300.0; // minimum dB value
73 if (z [ifreq] [iframe] > 0.0)
74 val = 10.0 * log10 (z [ifreq] [iframe] / 4e-10); // power values
75 }
76 return val;
77 }
78
79 Thing_implement (BarkSpectrogram, BandFilterSpectrogram, 2);
80
81 // dbs = scaleFactor * log10 (value/reference);
82 // if (dbs < floor_db) { dbs = floor_dB }
83 autoMatrix Spectrogram_to_Matrix_dB (Spectrogram me, double reference, double scaleFactor, double floor_dB) {
84 try {
85 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
86 for (integer i = 1; i <= my ny; i ++) {
87 for (integer j = 1; j <= my nx; j ++) {
88 Melder_require (my z [i] [j] >= 0.0,
89 U"Power in Spectrogram should be positive.");
90 thy z [i] [j] = std::max (floor_dB, scaleFactor * log10 (my z [i] [j] / reference));
91 }
92 }
93 return thee;
94 } catch (MelderError) {
95 Melder_throw (U"Matrix with dB values not created.");
96 }
97 }
98
99 /* Precondition: 1. CC object has been created but individual frames not yet initialized
100 * 2. Domains and number of frames conform
101 * Steps:
102 * 1. transform power-spectra to dB-spectra
103 * 2. cosine transform of dB-spectrum
104 */
105 void BandFilterSpectrogram_into_CC (BandFilterSpectrogram me, CC thee, integer numberOfCoefficients) {
106 autoMAT cosinesTable = MATcosinesTable (my ny);
107 autoVEC x = raw_VEC (my ny);
108 autoVEC y = raw_VEC (my ny);
109 numberOfCoefficients = numberOfCoefficients > my ny - 1 ? my ny - 1 : numberOfCoefficients;
110 Melder_assert (numberOfCoefficients > 0);
111 // 20130220 new interpretation of maximumNumberOfCoefficients: necessary for the inverse transform
112 for (integer frame = 1; frame <= my nx; frame ++) {
113 const CC_Frame ccframe = & thy frame [frame];
114 for (integer i = 1; i <= my ny; i ++)
115 x [i] = my v_getValueAtSample (frame, i, 1); // z [i] [frame];
116 VECcosineTransform_preallocated (y.get(), x.get(), cosinesTable.get());
117 CC_Frame_init (ccframe, numberOfCoefficients);
118 for (integer i = 1; i <= numberOfCoefficients; i ++)
119 ccframe -> c [i] = y [i + 1];
120 ccframe -> c0 = y [1];
121 }
122 }
123
124 // Preconditions: Domains and number of frames conform
125 // 0 <= first <= last <= my ny-1
126 void CC_into_BandFilterSpectrogram (CC me, BandFilterSpectrogram thee, integer first, integer last, bool use_c0) {
127 const integer nf = my maximumNumberOfCoefficients + 1;
128 autoMAT cosinesTable = MATcosinesTable (nf);
129 autoVEC x = raw_VEC (nf);
130 autoVEC y = raw_VEC (nf);
131 for (integer frame = 1; frame <= my nx; frame ++) {
132 const CC_Frame ccframe = & my frame [frame];
133 const integer iend = std::min (last, ccframe -> numberOfCoefficients);
134 x [1] = use_c0 ? ccframe -> c0 : 0;
135 for (integer i = 1; i <= my maximumNumberOfCoefficients; i ++)
136 x [i + 1] = ( i < first || i > iend ? 0.0 : ccframe -> c [i] );
137 VECinverseCosineTransform_preallocated (y.get(), x.get(), cosinesTable.get());
138 for (integer i = 1; i <= nf; i ++)
139 thy z [i] [frame] = BandFilterSpectrogram_DBREF * pow (10, y [i] / BandFilterSpectrogram_DBFAC);
140 }
141 }
142
143 autoMelSpectrogram MFCC_to_MelSpectrogram (MFCC me, integer first, integer last, bool c0) {
144 try {
145 if (first == 0 && last == 0) { // defaults
146 first = 1;
147 last = my maximumNumberOfCoefficients;
148 }
149 if (first < 1)
150 first = 1;
151 if (last > my maximumNumberOfCoefficients)
152 last = my maximumNumberOfCoefficients;
153 if (first > last) {
154 first = 1;
155 last = my maximumNumberOfCoefficients;
156 }
157 const double df = (my fmax - my fmin) / (my maximumNumberOfCoefficients + 1 + 1);
158 autoMelSpectrogram thee = MelSpectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my fmin, my fmax, my maximumNumberOfCoefficients + 1, df, df);
159 CC_into_BandFilterSpectrogram (me, thee.get(), first, last, c0);
160 return thee;
161 } catch (MelderError) {
162 Melder_throw (me, U"MelSpectrogram not created.");
163 }
164 }
165
166 autoMFCC MelSpectrogram_to_MFCC (MelSpectrogram me, integer numberOfCoefficients) {
167 try {
168 if (numberOfCoefficients <= 0 || numberOfCoefficients > my ny - 1)
169 numberOfCoefficients = my ny - 1;
170 /*
171 20130220 new interpretation of maximumNumberOfCoefficients necessary for the inverse transform!
172 In the creation of the MFCC we need to set it at the maximum, ny - 1.
173 In the conversion to CC we fill it with possibly less numbers!
174 */
175 autoMFCC thee = MFCC_create (my xmin, my xmax, my nx, my dx, my x1, my ny - 1, my ymin, my ymax);
176 BandFilterSpectrogram_into_CC (me, thee.get(), numberOfCoefficients);
177 return thee;
178 } catch (MelderError) {
179 Melder_throw (me, U": MFCC not created.");
180 }
181 }
182
183 autoBarkSpectrogram BarkSpectrogram_create (double tmin, double tmax, integer nt, double dt, double t1, double fmin, double fmax, integer nf, double df, double f1) {
184 try {
185 autoBarkSpectrogram me = Thing_new (BarkSpectrogram);
186 Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1);
187 return me;
188 } catch (MelderError) {
189 Melder_throw (U"BarkSpectrogram not created.");
190 }
191 }
192
193 double BandFilterSpectrogram_getFrequencyInHertz (BandFilterSpectrogram me, double f) {
194 return my v_frequencyToHertz (f);
195 }
196
197 // xmin, xmax in hz versus bark/mel or lin
198 void BandFilterSpectrogram_drawFrequencyScale (BandFilterSpectrogram me, Graphics g, double xmin, double xmax, double ymin, double ymax, bool garnish) {
199 if (xmin < 0 || xmax < 0 || ymin < 0 || ymax < 0) {
200 Melder_warning (U"Frequencies should be >= 0.");
201 return;
202 }
203
204 // scale is in hertz
205 if (xmin >= xmax) { // autoscaling
206 xmin = 0;
207 xmax = my v_frequencyToHertz (my ymax);
208 }
209
210 if (ymin >= ymax) { // autoscaling
211 ymin = my ymin;
212 ymax = my ymax;
213 }
214
215 constexpr integer n = 2000;
216
217 Graphics_setInner (g);
218 Graphics_setWindow (g, xmin, xmax, ymin, ymax);
219
220 const double dx = (xmax - xmin) / (n - 1);
221 double x1 = xmin, y1 = my v_hertzToFrequency (x1);
222 for (integer i = 2; i <= n; i ++) {
223 const double x2 = x1 + dx, y2 = my v_hertzToFrequency (x2);
224 if (isdefined (y1) && isdefined (y2)) {
225 double xo1, yo1, xo2, yo2;
226 if (NUMclipLineWithinRectangle (x1, y1, x2, y2, xmin, ymin, xmax, ymax, & xo1, & yo1, & xo2, & yo2))
227 Graphics_line (g, xo1, yo1, xo2, yo2);
228 }
229 x1 = x2;
230 y1 = y2;
231 }
232 Graphics_unsetInner (g);
233
234 if (garnish) {
235 Graphics_drawInnerBox (g);
236 Graphics_marksLeft (g, 2, true, true, false);
237 Graphics_textLeft (g, true, Melder_cat (U"Frequency (", my v_getFrequencyUnit (), U")"));
238 Graphics_marksBottom (g, 2, true, true, false);
239 Graphics_textBottom (g, true, U"Frequency (Hz)");
240 }
241 }
242
243 void BandFilterSpectrogram_paintImage (BandFilterSpectrogram me, Graphics g,
244 double xmin, double xmax, double ymin, double ymax, double minimum, double maximum, bool garnish)
245 {
246 Function_unidirectionalAutowindow (me, & xmin, & xmax);
247 SampledXY_unidirectionalAutowindowY (me, & ymin, & ymax);
248 integer ixmin, ixmax, iymin, iymax;
249 (void) Matrix_getWindowSamplesX (me, xmin - 0.49999 * my dx, xmax + 0.49999 * my dx, &ixmin, &ixmax);
250 (void) Matrix_getWindowSamplesY (me, ymin - 0.49999 * my dy, ymax + 0.49999 * my dy, &iymin, &iymax);
251 autoMatrix thee = Spectrogram_to_Matrix_dB ((Spectrogram) me, 4e-10, 10, -100);
252 if (maximum <= minimum)
253 (void) Matrix_getWindowExtrema (thee.get(), ixmin, ixmax, iymin, iymax, & minimum, & maximum);
254 if (maximum <= minimum) {
255 minimum -= 1.0;
256 maximum += 1.0;
257 }
258 if (xmin >= xmax || ymin >= ymax)
259 return;
260 Graphics_setInner (g);
261 Graphics_setWindow (g, xmin, xmax, ymin, ymax);
262 Graphics_image (g, thy z.part (iymin, iymax, ixmin, ixmax),
263 Sampled_indexToX (thee.get(), ixmin - 0.5), Sampled_indexToX (thee.get(), ixmax + 0.5),
264 SampledXY_indexToY (thee.get(), iymin - 0.5), SampledXY_indexToY (thee.get(), iymax + 0.5),
265 minimum, maximum
266 );
267
268 Graphics_unsetInner (g);
269 if (garnish) {
270 Graphics_drawInnerBox (g);
271 Graphics_marksLeft (g, 2, true, true, false);
272 Graphics_textLeft (g, true, Melder_cat (U"Frequency (", my v_getFrequencyUnit (), U")"));
273 Graphics_marksBottom (g, 2, true, true, false);
274 Graphics_textBottom (g, true, U"Time (s)");
275 }
276 }
277
278 void BandFilterSpectrogram_drawSpectrumAtNearestTimeSlice (BandFilterSpectrogram me, Graphics g,
279 double time, double fmin, double fmax, double dBmin, double dBmax, bool garnish) {
280 if (time < my xmin || time > my xmax)
281 return;
282 if (fmin == 0.0 && fmax == 0.0) { // autoscaling
283 fmin = my ymin;
284 fmax = my ymax;
285 }
286 if (fmax <= fmin) {
287 fmin = my ymin;
288 fmax = my ymax;
289 }
290 integer icol = Matrix_xToNearestColumn (me, time);
291 if (icol < 1)
292 icol = 1;
293 if (icol > my nx)
294 icol = my nx;
295 autoVEC spectrum = raw_VEC (my ny);
296 for (integer i = 1; i <= my ny; i ++)
297 spectrum [i] = my v_getValueAtSample (icol, i, 1); // dB's
298 integer iymin, iymax;
299 if (Matrix_getWindowSamplesY (me, fmin, fmax, & iymin, & iymax) < 2) // too few values
300 return;
301 if (dBmin == dBmax) { // autoscaling
302 dBmin = spectrum [iymin];
303 dBmax = dBmin;
304 for (integer i = iymin + 1; i <= iymax; i ++) {
305 if (spectrum [i] < dBmin)
306 dBmin = spectrum [i];
307 else if (spectrum [i] > dBmax)
308 dBmax = spectrum [i];
309 }
310 if (dBmin == dBmax) {
311 dBmin -= 1.0;
312 dBmax += 1.0;
313 }
314 }
315 Graphics_setWindow (g, fmin, fmax, dBmin, dBmax);
316 Graphics_setInner (g);
317
318 double x1 = my y1 + (iymin -1) * my dy, y1 = spectrum [iymin];
319 for (integer i = iymin + 1; i <= iymax - 1; i ++) {
320 double x2 = my y1 + (i -1) * my dy, y2 = spectrum [i];
321 double xo1, yo1, xo2, yo2;
322 if (NUMclipLineWithinRectangle (x1, y1, x2, y2, fmin, dBmin, fmax, dBmax, & xo1, & yo1, & xo2, & yo2))
323 Graphics_line (g, xo1, yo1, xo2, yo2);
324 x1 = x2;
325 y1 = y2;
326 }
327 Graphics_unsetInner (g);
328
329 if (garnish) {
330 Graphics_drawInnerBox (g);
331 Graphics_marksBottom (g, 2, true, true, false);
332 Graphics_marksLeft (g, 2, true, true, false);
333 Graphics_textLeft (g, true, U"Power (dB)");
334 Graphics_textBottom (g, true, Melder_cat (U"Frequency (", my v_getFrequencyUnit (), U")"));
335 }
336 }
337
338 void BarkSpectrogram_drawSekeyHansonFilterFunctions (BarkSpectrogram me, Graphics g, bool xIsHertz, integer fromFilter, integer toFilter,
339 double zmin, double zmax, bool yscale_dB, double ymin, double ymax, bool garnish)
340 {
341 double xmin = zmin, xmax = zmax;
342 if (zmin >= zmax) {
343 zmin = my ymin;
344 zmax = my ymax;
345 xmin = ( xIsHertz ? my v_frequencyToHertz (zmin) : zmin );
346 xmax = ( xIsHertz ? my v_frequencyToHertz (zmax) : zmax );
347 }
348 if (xIsHertz) {
349 zmin = my v_hertzToFrequency (xmin);
350 zmax = my v_hertzToFrequency (xmax);
351 }
352 if (ymin >= ymax) {
353 ymin = ( yscale_dB ? -60.0 : 0.0 );
354 ymax = ( yscale_dB ? 0.0 : 1.0 );
355 }
356 fromFilter = fromFilter <= 0 ? 1 : fromFilter;
357 if (toFilter <= 0 || toFilter > my ny)
358 toFilter = my ny;
359 if (fromFilter > toFilter) {
360 fromFilter = 1;
361 toFilter = my ny;
362 }
363 const integer n = xIsHertz ? 1000 : 500;
364 autoVEC xz = raw_VEC (n), xhz = raw_VEC (n), y = raw_VEC (n);
365
366 Graphics_setInner (g);
367 Graphics_setWindow (g, xmin, xmax, ymin, ymax);
368
369 const double dz = (zmax - zmin) / (n - 1);
370 for (integer iz = 1; iz <= n; iz ++) {
371 const double f = zmin + (iz - 1) * dz;
372 xz [iz] = f;
373 xhz [iz] = my v_frequencyToHertz (f); // just in case we need the linear scale
374 }
375 for (integer ifilter = fromFilter; ifilter <= toFilter; ifilter ++) {
376 const double zMid = Matrix_rowToY (me, ifilter);
377 for (integer iz = 1; iz <= n; iz ++) {
378 const double z = xz [iz] - (zMid - 0.215);
379 const double amp = 7.0 - 7.5 * z - 17.5 * sqrt (0.196 + z * z);
380 y [iz] = ( yscale_dB ? amp : pow (10.0, amp / 10.0) );
381 }
382 double x1 = ( xIsHertz ? xhz [1] : xz [1] ), y1 = y [1];
383 for (integer iz = 2; iz <= n; iz ++) {
384 const double x2 = ( xIsHertz ? xhz [iz] : xz [iz] ), y2 = y [iz];
385 if (isdefined (x1) && isdefined (x2)) {
386 double xo1, yo1, xo2, yo2;
387 if (NUMclipLineWithinRectangle (x1, y1, x2, y2, xmin, ymin, xmax, ymax, & xo1, & yo1, & xo2, & yo2))
388 Graphics_line (g, xo1, yo1, xo2, yo2);
389 }
390 x1 = x2;
391 y1 = y2;
392 }
393 }
394 Graphics_unsetInner (g);
395
396 if (garnish) {
397 double distance = yscale_dB ? 10.0 : 0.5;
398 Graphics_drawInnerBox (g);
399 Graphics_marksBottom (g, 2, true, true, false);
400 Graphics_marksLeftEvery (g, 1.0, distance, true, true, false);
401 Graphics_textLeft (g, true, yscale_dB ? U"Amplitude (dB)" : U"Amplitude");
402 Graphics_textBottom (g, true, Melder_cat (U"Frequency (", xIsHertz ? U"Hz" : my v_getFrequencyUnit (), U")"));
403 }
404 }
405
406 Thing_implement (MelSpectrogram, BandFilterSpectrogram, 2);
407
408 autoMelSpectrogram MelSpectrogram_create (double tmin, double tmax, integer nt, double dt, double t1, double fmin, double fmax, integer nf, double df, double f1) {
409 try {
410 autoMelSpectrogram me = Thing_new (MelSpectrogram);
411 Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1);
412 return me;
413 } catch (MelderError) {
414 Melder_throw (U"MelSpectrogram not created.");
415 }
416 }
417
418 void BandFilterSpectrogram_drawTimeSlice (BandFilterSpectrogram me, Graphics g,
419 double t, double fmin, double fmax, double min, double max, conststring32 xlabel, bool garnish) {
420 Matrix_drawSliceY (me, g, t, fmin, fmax, min, max);
421 if (garnish) {
422 Graphics_drawInnerBox (g);
423 Graphics_marksBottom (g, 2, true, true, false);
424 Graphics_marksLeft (g, 2, true, true, false);
425 if (xlabel)
426 Graphics_textBottom (g, false, xlabel);
427 }
428 }
429
430 void MelSpectrogram_drawTriangularFilterFunctions (MelSpectrogram me, Graphics g,
431 bool xIsHertz, integer fromFilter, integer toFilter, double zmin, double zmax, bool yscale_dB, double ymin, double ymax, bool garnish)
432 {
433 double xmin = zmin, xmax = zmax;
434 if (zmin >= zmax) {
435 zmin = my ymin;
436 zmax = my ymax; // mel
437 xmin = xIsHertz ? my v_frequencyToHertz (zmin) : zmin;
438 xmax = xIsHertz ? my v_frequencyToHertz (zmax) : zmax;
439 }
440 if (xIsHertz) {
441 zmin = my v_hertzToFrequency (xmin);
442 zmax = my v_hertzToFrequency (xmax);
443 }
444
445 if (ymin >= ymax) {
446 ymin = yscale_dB ? -60.0 : 0.0;
447 ymax = yscale_dB ? 0.0 : 1.0;
448 }
449 if (fromFilter <= 0)
450 fromFilter = 1;
451 if (toFilter <= 0 || toFilter > my ny)
452 toFilter = my ny;
453 if (fromFilter > toFilter) {
454 fromFilter = 1;
455 toFilter = my ny;
456 }
457
458 const integer n = ( xIsHertz ? 1000 : 500 );
459 autoVEC xz = raw_VEC (n), xhz = raw_VEC (n), y = raw_VEC (n);
460
461 Graphics_setInner (g);
462 Graphics_setWindow (g, xmin, xmax, ymin, ymax);
463
464 double dz = (zmax - zmin) / (n - 1);
465 for (integer iz = 1; iz <= n; iz ++) {
466 const double f = zmin + (iz - 1) * dz;
467 xz [iz] = f;
468 xhz [iz] = my v_frequencyToHertz (f); // just in case we need the linear scale
469 }
470
471 for (integer ifilter = fromFilter; ifilter <= toFilter; ifilter ++) {
472 const double zc = Matrix_rowToY (me, ifilter), zl = zc - my dy, zh = zc + my dy;
473 double xo1, yo1, xo2, yo2;
474 if (yscale_dB) {
475 for (integer iz = 1; iz <= n; iz ++) {
476 const double z = xz [iz];
477 const double amp = NUMtriangularfilter_amplitude (zl, zc, zh, z);
478 y [iz] = yscale_dB ? (amp > 0.0 ? 20.0 * log10 (amp) : ymin - 10.0) : amp;
479 }
480 double x1 = ( xIsHertz ? xhz [1] : xz [1] ), y1 = y [1];
481 if (isdefined (y1)) {
482 for (integer iz = 1; iz <= n; iz ++) {
483 const double x2 = ( xIsHertz ? xhz [iz] : xz [iz] ), y2 = y [iz];
484 if (isdefined (y2)) {
485 if (NUMclipLineWithinRectangle (x1, y1, x2, y2, xmin, ymin, xmax, ymax, & xo1, & yo1, & xo2, & yo2))
486 Graphics_line (g, xo1, yo1, xo2, yo2);
487 }
488 x1 = x2;
489 y1 = y2;
490 }
491 }
492 } else {
493 const double x1 = xIsHertz ? my v_frequencyToHertz (zl) : zl;
494 const double x2 = xIsHertz ? my v_frequencyToHertz (zc) : zc;
495 if (NUMclipLineWithinRectangle (x1, 0, x2, 1, xmin, ymin, xmax, ymax, & xo1, & yo1, & xo2, & yo2))
496 Graphics_line (g, xo1, yo1, xo2, yo2);
497 const double x3 = xIsHertz ? my v_frequencyToHertz (zh) : zh;
498 if (NUMclipLineWithinRectangle (x2, 1, x3, 0, xmin, ymin, xmax, ymax, & xo1, & yo1, & xo2, & yo2))
499 Graphics_line (g, xo1, yo1, xo2, yo2);
500 }
501 }
502 Graphics_unsetInner (g);
503
504 if (garnish) {
505 Graphics_drawInnerBox (g);
506 Graphics_marksBottom (g, 2, true, true, false);
507 Graphics_marksLeftEvery (g, 1.0, yscale_dB ? 10.0 : 0.5, true, true, false);
508 Graphics_textLeft (g, true, yscale_dB ? U"Amplitude (dB)" : U"Amplitude");
509 Graphics_textBottom (g, true, Melder_cat (U"Frequency (", ( xIsHertz ? U"Hz" : my v_getFrequencyUnit () ), U")"));
510 }
511 }
512
513 autoMatrix BandFilterSpectrogram_to_Matrix (BandFilterSpectrogram me, int to_dB) {
514 try {
515 const int units = to_dB ? 1 : 0;
516 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
517 for (integer i = 1; i <= my ny; i ++) {
518 for (integer j = 1; j <= my nx; j ++)
519 thy z [i] [j] = my v_getValueAtSample (j, i, units);
520 }
521 return thee;
522 } catch (MelderError) {
523 Melder_throw (me, U": not converted to Matrix.");
524 }
525 }
526
527 autoBarkSpectrogram Matrix_to_BarkSpectrogram (Matrix me) {
528 try {
529 autoBarkSpectrogram thee = BarkSpectrogram_create (my xmin, my xmax, my nx, my dx, my x1,
530 my ymin, my ymax, my ny, my dy, my y1);
531 thy z.all() <<= my z.all();
532 return thee;
533 } catch (MelderError) {
534 Melder_throw (me, U": not converted to BarkSpectrogram.");
535 }
536 }
537
538 autoMelSpectrogram Matrix_to_MelSpectrogram (Matrix me) {
539 try {
540 autoMelSpectrogram thee = MelSpectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
541 thy z.all() <<= my z.all();
542 return thee;
543 } catch (MelderError) {
544 Melder_throw (me, U": not converted to MelSpectrogram.");
545 }
546 }
547
548 autoIntensity BandFilterSpectrogram_to_Intensity (BandFilterSpectrogram me) {
549 try {
550 autoIntensity thee = Intensity_create (my xmin, my xmax, my nx, my dx, my x1);
551 for (integer j = 1; j <= my nx; j ++) {
552 longdouble p = 0.0;
553 for (integer i = 1; i <= my ny; i ++)
554 p += my z [i] [j]; // add power
555 thy z [1] [j] = BandFilterSpectrogram_DBFAC * log10 ((double) p / BandFilterSpectrogram_DBREF);
556 }
557 return thee;
558 } catch (MelderError) {
559 Melder_throw (me, U": Intensity not created.");
560 }
561 }
562
563 void BandFilterSpectrogram_equalizeIntensities (BandFilterSpectrogram me, double intensity_db) {
564 for (integer j = 1; j <= my nx; j ++) {
565 longdouble p = 0.0;
566 for (integer i = 1; i <= my ny; i ++)
567 p += my z [i] [j];
568 const double delta_db = intensity_db - BandFilterSpectrogram_DBFAC * log10 ((double) p / BandFilterSpectrogram_DBREF);
569 const double factor = pow (10.0, delta_db / 10.0);
570 for (integer i = 1; i <= my ny; i ++)
571 my z [i] [j] *= factor;
572 }
573 }
574
575 #if 0
576 static void BandFilterSpectrogram_PCA_drawComponent (BandFilterSpectrogram me, PCA thee, Graphics g, integer component, double dblevel, double frequencyOffset, double scale, double tmin, double tmax, double fmin, double fmax) {
577 Melder_require (component > 0 && component <= thy numberOfEigenvalues,
578 U"Component too large.");
579 /*
580 Scale Intensity
581 */
582 autoBandFilterSpectrogram fcopy = Data_copy (me);
583 BandFilterSpectrogram_equalizeIntensities (fcopy.get(), dblevel);
584 autoMatrix mdb = Spectrogram_to_Matrix_dB ((Spectrogram) fcopy.get(), BandFilterSpectrogram_DBREF, BandFilterSpectrogram_DBFAC, BandFilterSpectrogram_DBFLOOR);
585 autoMatrix him = Eigen_Matrix_to_Matrix_projectColumns (thee, mdb.get(), component);
586 for (integer j = 1; j <= my nx; j ++)
587 his z [component] [j] = frequencyOffset + scale * his z [component] [j];
588 Matrix_drawRows (him.get(), g, tmin, tmax, component - 0.5, component + 0.5, fmin, fmax);
589 }
590 #endif
591
592 void Spectrogram_drawLongTermFlatness (Spectrogram me, Graphics g, double tmin, double tmax, double minimumFlatness_db,
593 double longtimeWindow, double shorttimeWindow, double fmin, double fmax, bool garnish)
594 {
595 Melder_require (minimumFlatness_db < 0,
596 U"The minimum flatness should be a negative number.");
597 autoMatrix thee = Spectrogram_getLongtermSpectralFlatness (me, longtimeWindow, shorttimeWindow, fmin, fmax);
598 Function_unidirectionalAutowindow (me, & tmin, & tmax);
599 integer itmin, itmax;
600 Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax);
601 double ymax = 0.0, ymin = minimumFlatness_db;
602 Graphics_setInner (g);
603 Graphics_setWindow (g, tmin, tmax, ymin, ymax);
604 Graphics_function (g, & thy z [1] [0], itmin, itmax,
605 Matrix_columnToX (me, itmin), Matrix_columnToX (thee.get(), itmax));
606 Graphics_unsetInner (g);
607 if (garnish) {
608 Graphics_drawInnerBox (g);
609 Graphics_textBottom (g, true, U"Time (s)");
610 Graphics_marksBottom (g, 2, true, true, false);
611 Graphics_markLeft (g, ymin, true, true, false, nullptr);
612 Graphics_markLeft (g, ymax, true, true, false, nullptr);
613 }
614 }
615 /*
616 Implementation of Yanna Ma & Akinori Nishihara (2013), Efficient voice activity detection algorithm
617 using long-term spectral flatness measure, EURASIP Journal on Audio, Speech, and Music Processing.
618 */
619 autoMatrix Spectrogram_getLongtermSpectralFlatness (Spectrogram me, double longtimeWindow, double shorttimeWindow,
620 double fmin, double fmax) {
621 try {
622 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1,
623 0.5, 1.5, 1, 1.0, 1.0);
624 VEC longtermSpectralFlatness = thy z.row (1);
625 const integer numberOfShorttermFrames = Melder_ifloor (shorttimeWindow / my dx);
626 /*
627 For the time being we just round the longtimeWindow to the nearest number of frames to compute
628 the geometric mean and the arithmetic mean because this is not very critical.
629 */
630 const integer numberOfLongtermFrames = Melder_round_tieUp (longtimeWindow / my dx);
631 const double halfWindwow = 0.5 * shorttimeWindow;
632 const integer ifreqFrom = std::max (1_integer, Melder_ifloor (fmin / my dy));
633 const integer ifreqTo = std::min (my ny, Melder_ifloor (fmax / my dy));
634 Melder_require (ifreqFrom < ifreqTo,
635 U"The low frequency threshold should be smaller than the high frequency threshold.");
636 for (integer ifreq = ifreqFrom; ifreq <= ifreqTo; ifreq ++) {
637 VEC frequencyBins = my z [1];
638 /*
639 Average across time Eq. (4)
640 */
641 if (numberOfShorttermFrames > 1) { // for Eq. (4)
642 for (integer iframe = 1; iframe <= my nx; iframe ++) {
643 const double xmid = Sampled_indexToX (me, iframe);
644 frequencyBins [iframe] = Sampled_getMean (me, xmid - halfWindwow, xmid + halfWindwow, ifreq, 0, true);
645 }
646 } else
647 frequencyBins <<= my z.row (ifreq);
648 /*
649 Get the geometric versus arithmetic mean of the power spectrum, Eqs. (2) and (3)
650 */
651 for (integer iframe = 1; iframe <= my nx; iframe ++) {
652 const integer frameFrom = iframe - numberOfLongtermFrames + 1;
653 const integer iframeFrom = std::max (1_integer, frameFrom);
654 longdouble log10_geometricMean = 0.0, arithmeticMean = 0.0;
655 integer count = 0;
656 for (integer i = iframeFrom; i <= iframe; i ++) {
657 if (frequencyBins [i] > 0.0) {
658 log10_geometricMean += log10 (frequencyBins [i]);
659 arithmeticMean += frequencyBins [i];
660 count ++;
661 }
662 }
663 if (count > 0) {
664 log10_geometricMean /= count;
665 arithmeticMean /= count;
666 }
667 const double log_gm_div_am = ( arithmeticMean <= 0.0 ? 0.0 : double (log10_geometricMean - log10 (double (arithmeticMean))) );
668 longtermSpectralFlatness [iframe] += log_gm_div_am; // Eq. (1)
669 }
670 }
671 return thee;
672 } catch (MelderError) {
673 Melder_throw (me, U": cannot determine long term spectral flatness.");
674 }
675 }
676
677 /*
678 * MelSpectrograms_to_DTW (MelSpectrogram me, MelSpectrogram thee, dtw-params);
679 * comparison on the basis of mfcc
680 * BarkSpectrograms_to_DTW (BarkSpectrogram me, BarkSpectrogram thee, dtw-params);
681 * comparison on the basis of bfcc!
682 */
683
684
685 /* End of file Spectrogram_extensions.cpp */