Debian Codegraph @

dwtools/Spectrogram_extensions.cpp

raw
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 autoLineSegmentClipper clipper = LineSegmentClipper_create (xmin, xmax, ymin, ymax);
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 x1c = x1, y1c = y1, x2c = x2, y2c = y2;
226 if (clipper -> clip (x1c, y1c, x2c, y2c))
227 Graphics_line (g, x1c, y1c, x2c, y2c);
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 autoLineSegmentClipper clipper = LineSegmentClipper_create (fmin, dBmin, fmax, dBmax);
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 x1c = x1, y1c = y1, x2c = x2, y2c = y2;
322 if (clipper -> clip (x1c, y1c, x2c, y2c))
323 Graphics_line (g, x1c, y1c, x2c, y2c);
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 autoLineSegmentClipper clipper = LineSegmentClipper_create (xmin, xmax, ymin, ymax);
376 for (integer ifilter = fromFilter; ifilter <= toFilter; ifilter ++) {
377 const double zMid = Matrix_rowToY (me, ifilter);
378 for (integer iz = 1; iz <= n; iz ++) {
379 const double z = xz [iz] - (zMid - 0.215);
380 const double amp = 7.0 - 7.5 * z - 17.5 * sqrt (0.196 + z * z);
381 y [iz] = ( yscale_dB ? amp : pow (10.0, amp / 10.0) );
382 }
383 double x1 = ( xIsHertz ? xhz [1] : xz [1] ), y1 = y [1];
384 for (integer iz = 2; iz <= n; iz ++) {
385 const double x2 = ( xIsHertz ? xhz [iz] : xz [iz] ), y2 = y [iz];
386 if (isdefined (x1) && isdefined (x2)) {
387 double x1c = x1, y1c = y1, x2c = x2, y2c = y2;
388 if (clipper -> clip (x1c, y1c, x2c, y2c))
389 Graphics_line (g, x1c, y1c, x2c, y2c);
390 }
391 x1 = x2;
392 y1 = y2;
393 }
394 }
395 Graphics_unsetInner (g);
396
397 if (garnish) {
398 double distance = yscale_dB ? 10.0 : 0.5;
399 Graphics_drawInnerBox (g);
400 Graphics_marksBottom (g, 2, true, true, false);
401 Graphics_marksLeftEvery (g, 1.0, distance, true, true, false);
402 Graphics_textLeft (g, true, yscale_dB ? U"Amplitude (dB)" : U"Amplitude");
403 Graphics_textBottom (g, true, Melder_cat (U"Frequency (", xIsHertz ? U"Hz" : my v_getFrequencyUnit (), U")"));
404 }
405 }
406
407 Thing_implement (MelSpectrogram, BandFilterSpectrogram, 2);
408
409 autoMelSpectrogram MelSpectrogram_create (double tmin, double tmax, integer nt, double dt, double t1, double fmin, double fmax, integer nf, double df, double f1) {
410 try {
411 autoMelSpectrogram me = Thing_new (MelSpectrogram);
412 Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1);
413 return me;
414 } catch (MelderError) {
415 Melder_throw (U"MelSpectrogram not created.");
416 }
417 }
418
419 void BandFilterSpectrogram_drawTimeSlice (BandFilterSpectrogram me, Graphics g,
420 double t, double fmin, double fmax, double min, double max, conststring32 xlabel, bool garnish) {
421 Matrix_drawSliceY (me, g, t, fmin, fmax, min, max);
422 if (garnish) {
423 Graphics_drawInnerBox (g);
424 Graphics_marksBottom (g, 2, true, true, false);
425 Graphics_marksLeft (g, 2, true, true, false);
426 if (xlabel)
427 Graphics_textBottom (g, false, xlabel);
428 }
429 }
430
431 void MelSpectrogram_drawTriangularFilterFunctions (MelSpectrogram me, Graphics g,
432 bool xIsHertz, integer fromFilter, integer toFilter, double zmin, double zmax, bool yscale_dB, double ymin, double ymax, bool garnish)
433 {
434 double xmin = zmin, xmax = zmax;
435 if (zmin >= zmax) {
436 zmin = my ymin;
437 zmax = my ymax; // mel
438 xmin = xIsHertz ? my v_frequencyToHertz (zmin) : zmin;
439 xmax = xIsHertz ? my v_frequencyToHertz (zmax) : zmax;
440 }
441 if (xIsHertz) {
442 zmin = my v_hertzToFrequency (xmin);
443 zmax = my v_hertzToFrequency (xmax);
444 }
445
446 if (ymin >= ymax) {
447 ymin = yscale_dB ? -60.0 : 0.0;
448 ymax = yscale_dB ? 0.0 : 1.0;
449 }
450 if (fromFilter <= 0)
451 fromFilter = 1;
452 if (toFilter <= 0 || toFilter > my ny)
453 toFilter = my ny;
454 if (fromFilter > toFilter) {
455 fromFilter = 1;
456 toFilter = my ny;
457 }
458
459 const integer n = ( xIsHertz ? 1000 : 500 );
460 autoVEC xz = raw_VEC (n), xhz = raw_VEC (n), y = raw_VEC (n);
461
462 Graphics_setInner (g);
463 Graphics_setWindow (g, xmin, xmax, ymin, ymax);
464
465 double dz = (zmax - zmin) / (n - 1);
466 for (integer iz = 1; iz <= n; iz ++) {
467 const double f = zmin + (iz - 1) * dz;
468 xz [iz] = f;
469 xhz [iz] = my v_frequencyToHertz (f); // just in case we need the linear scale
470 }
471 autoLineSegmentClipper clipper = LineSegmentClipper_create (xmin, xmax, ymin, ymax);
472 for (integer ifilter = fromFilter; ifilter <= toFilter; ifilter ++) {
473 const double zc = Matrix_rowToY (me, ifilter), zl = zc - my dy, zh = zc + my dy;
474 double xo1, yo1, xo2, yo2;
475 if (yscale_dB) {
476 for (integer iz = 1; iz <= n; iz ++) {
477 const double z = xz [iz];
478 const double amp = NUMtriangularfilter_amplitude (zl, zc, zh, z);
479 y [iz] = yscale_dB ? (amp > 0.0 ? 20.0 * log10 (amp) : ymin - 10.0) : amp;
480 }
481 double x1 = ( xIsHertz ? xhz [1] : xz [1] ), y1 = y [1];
482 if (isdefined (y1)) {
483 for (integer iz = 1; iz <= n; iz ++) {
484 const double x2 = ( xIsHertz ? xhz [iz] : xz [iz] ), y2 = y [iz];
485 if (isdefined (y2)) {
486 double x1c = x1, y1c = y1, x2c = x2, y2c = y2;
487 if (clipper -> clip (x1c, y1c, x2c, y2c))
488 Graphics_line (g, x1c, y1c, x2c, y2c);
489 }
490 x1 = x2;
491 y1 = y2;
492 }
493 }
494 } else {
495 const double x1 = xIsHertz ? my v_frequencyToHertz (zl) : zl;
496 const double x2 = xIsHertz ? my v_frequencyToHertz (zc) : zc;
497 double x1c = x1, y1c = 0.0, x2c = x2, y2c = 1.0;
498 if (clipper->clip (x1c, y1c, x2c, y2c))
499 Graphics_line (g, x1c, y1c, x2c, y2c);
500 const double x3 = xIsHertz ? my v_frequencyToHertz (zh) : zh;
501 x1c = x2, y1c = 1.0, x2c = x3, y2c = 0.0;
502 if (clipper -> clip (x1c, y1c, x2c, y2c))
503 Graphics_line (g, x1c, y1c, x2c, y2c);
504 }
505 }
506 Graphics_unsetInner (g);
507
508 if (garnish) {
509 Graphics_drawInnerBox (g);
510 Graphics_marksBottom (g, 2, true, true, false);
511 Graphics_marksLeftEvery (g, 1.0, yscale_dB ? 10.0 : 0.5, true, true, false);
512 Graphics_textLeft (g, true, yscale_dB ? U"Amplitude (dB)" : U"Amplitude");
513 Graphics_textBottom (g, true, Melder_cat (U"Frequency (", ( xIsHertz ? U"Hz" : my v_getFrequencyUnit () ), U")"));
514 }
515 }
516
517 autoMatrix BandFilterSpectrogram_to_Matrix (BandFilterSpectrogram me, int to_dB) {
518 try {
519 const int units = to_dB ? 1 : 0;
520 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
521 for (integer i = 1; i <= my ny; i ++) {
522 for (integer j = 1; j <= my nx; j ++)
523 thy z [i] [j] = my v_getValueAtSample (j, i, units);
524 }
525 return thee;
526 } catch (MelderError) {
527 Melder_throw (me, U": not converted to Matrix.");
528 }
529 }
530
531 autoBarkSpectrogram Matrix_to_BarkSpectrogram (Matrix me) {
532 try {
533 autoBarkSpectrogram thee = BarkSpectrogram_create (my xmin, my xmax, my nx, my dx, my x1,
534 my ymin, my ymax, my ny, my dy, my y1);
535 thy z.all() <<= my z.all();
536 return thee;
537 } catch (MelderError) {
538 Melder_throw (me, U": not converted to BarkSpectrogram.");
539 }
540 }
541
542 autoMelSpectrogram Matrix_to_MelSpectrogram (Matrix me) {
543 try {
544 autoMelSpectrogram thee = MelSpectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
545 thy z.all() <<= my z.all();
546 return thee;
547 } catch (MelderError) {
548 Melder_throw (me, U": not converted to MelSpectrogram.");
549 }
550 }
551
552 autoIntensity BandFilterSpectrogram_to_Intensity (BandFilterSpectrogram me) {
553 try {
554 autoIntensity thee = Intensity_create (my xmin, my xmax, my nx, my dx, my x1);
555 for (integer j = 1; j <= my nx; j ++) {
556 longdouble p = 0.0;
557 for (integer i = 1; i <= my ny; i ++)
558 p += my z [i] [j]; // add power
559 thy z [1] [j] = BandFilterSpectrogram_DBFAC * log10 ((double) p / BandFilterSpectrogram_DBREF);
560 }
561 return thee;
562 } catch (MelderError) {
563 Melder_throw (me, U": Intensity not created.");
564 }
565 }
566
567 void BandFilterSpectrogram_equalizeIntensities (BandFilterSpectrogram me, double intensity_db) {
568 for (integer j = 1; j <= my nx; j ++) {
569 longdouble p = 0.0;
570 for (integer i = 1; i <= my ny; i ++)
571 p += my z [i] [j];
572 const double delta_db = intensity_db - BandFilterSpectrogram_DBFAC * log10 ((double) p / BandFilterSpectrogram_DBREF);
573 const double factor = pow (10.0, delta_db / 10.0);
574 for (integer i = 1; i <= my ny; i ++)
575 my z [i] [j] *= factor;
576 }
577 }
578
579 #if 0
580 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) {
581 Melder_require (component > 0 && component <= thy numberOfEigenvalues,
582 U"Component too large.");
583 /*
584 Scale Intensity
585 */
586 autoBandFilterSpectrogram fcopy = Data_copy (me);
587 BandFilterSpectrogram_equalizeIntensities (fcopy.get(), dblevel);
588 autoMatrix mdb = Spectrogram_to_Matrix_dB ((Spectrogram) fcopy.get(), BandFilterSpectrogram_DBREF, BandFilterSpectrogram_DBFAC, BandFilterSpectrogram_DBFLOOR);
589 autoMatrix him = Eigen_Matrix_to_Matrix_projectColumns (thee, mdb.get(), component);
590 for (integer j = 1; j <= my nx; j ++)
591 his z [component] [j] = frequencyOffset + scale * his z [component] [j];
592 Matrix_drawRows (him.get(), g, tmin, tmax, component - 0.5, component + 0.5, fmin, fmax);
593 }
594 #endif
595
596 void Spectrogram_drawLongTermFlatness (Spectrogram me, Graphics g, double tmin, double tmax, double minimumFlatness_db,
597 double longtimeWindow, double shorttimeWindow, double fmin, double fmax, bool garnish)
598 {
599 Melder_require (minimumFlatness_db < 0,
600 U"The minimum flatness should be a negative number.");
601 autoMatrix thee = Spectrogram_getLongtermSpectralFlatness (me, longtimeWindow, shorttimeWindow, fmin, fmax);
602 Function_unidirectionalAutowindow (me, & tmin, & tmax);
603 integer itmin, itmax;
604 Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax);
605 double ymax = 0.0, ymin = minimumFlatness_db;
606 Graphics_setInner (g);
607 Graphics_setWindow (g, tmin, tmax, ymin, ymax);
608 Graphics_function (g, & thy z [1] [0], itmin, itmax,
609 Matrix_columnToX (me, itmin), Matrix_columnToX (thee.get(), itmax));
610 Graphics_unsetInner (g);
611 if (garnish) {
612 Graphics_drawInnerBox (g);
613 Graphics_textBottom (g, true, U"Time (s)");
614 Graphics_marksBottom (g, 2, true, true, false);
615 Graphics_markLeft (g, ymin, true, true, false, nullptr);
616 Graphics_markLeft (g, ymax, true, true, false, nullptr);
617 }
618 }
619 /*
620 Implementation of Yanna Ma & Akinori Nishihara (2013), Efficient voice activity detection algorithm
621 using long-term spectral flatness measure, EURASIP Journal on Audio, Speech, and Music Processing.
622 */
623 autoMatrix Spectrogram_getLongtermSpectralFlatness (Spectrogram me, double longtimeWindow, double shorttimeWindow,
624 double fmin, double fmax) {
625 try {
626 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1,
627 0.5, 1.5, 1, 1.0, 1.0);
628 VEC longtermSpectralFlatness = thy z.row (1);
629 const integer numberOfShorttermFrames = Melder_ifloor (shorttimeWindow / my dx);
630 /*
631 For the time being we just round the longtimeWindow to the nearest number of frames to compute
632 the geometric mean and the arithmetic mean because this is not very critical.
633 */
634 const integer numberOfLongtermFrames = Melder_round_tieUp (longtimeWindow / my dx);
635 const double halfWindwow = 0.5 * shorttimeWindow;
636 const integer ifreqFrom = std::max (1_integer, Melder_ifloor (fmin / my dy));
637 const integer ifreqTo = std::min (my ny, Melder_ifloor (fmax / my dy));
638 Melder_require (ifreqFrom < ifreqTo,
639 U"The low frequency threshold should be smaller than the high frequency threshold.");
640 for (integer ifreq = ifreqFrom; ifreq <= ifreqTo; ifreq ++) {
641 VEC frequencyBins = my z [1];
642 /*
643 Average across time Eq. (4)
644 */
645 if (numberOfShorttermFrames > 1) { // for Eq. (4)
646 for (integer iframe = 1; iframe <= my nx; iframe ++) {
647 const double xmid = Sampled_indexToX (me, iframe);
648 frequencyBins [iframe] = Sampled_getMean (me, xmid - halfWindwow, xmid + halfWindwow, ifreq, 0, true);
649 }
650 } else
651 frequencyBins <<= my z.row (ifreq);
652 /*
653 Get the geometric versus arithmetic mean of the power spectrum, Eqs. (2) and (3)
654 */
655 for (integer iframe = 1; iframe <= my nx; iframe ++) {
656 const integer frameFrom = iframe - numberOfLongtermFrames + 1;
657 const integer iframeFrom = std::max (1_integer, frameFrom);
658 longdouble log10_geometricMean = 0.0, arithmeticMean = 0.0;
659 integer count = 0;
660 for (integer i = iframeFrom; i <= iframe; i ++) {
661 if (frequencyBins [i] > 0.0) {
662 log10_geometricMean += log10 (frequencyBins [i]);
663 arithmeticMean += frequencyBins [i];
664 count ++;
665 }
666 }
667 if (count > 0) {
668 log10_geometricMean /= count;
669 arithmeticMean /= count;
670 }
671 const double log_gm_div_am = ( arithmeticMean <= 0.0 ? 0.0 : double (log10_geometricMean - log10 (double (arithmeticMean))) );
672 longtermSpectralFlatness [iframe] += log_gm_div_am; // Eq. (1)
673 }
674 }
675 return thee;
676 } catch (MelderError) {
677 Melder_throw (me, U": cannot determine long term spectral flatness.");
678 }
679 }
680
681 /*
682 * MelSpectrograms_to_DTW (MelSpectrogram me, MelSpectrogram thee, dtw-params);
683 * comparison on the basis of mfcc
684 * BarkSpectrograms_to_DTW (BarkSpectrogram me, BarkSpectrogram thee, dtw-params);
685 * comparison on the basis of bfcc!
686 */
687
688
689 /* End of file Spectrogram_extensions.cpp */