Debian Codegraph @

fon/Spectrogram.cpp

raw symbols SCIP
1 /* Spectrogram.cpp
2 *
3 * Copyright (C) 1992-2008,2011,2012,2015-2020,2022-2024,2026 Paul Boersma, 2026 yjzxkxdn (colour schemes)
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 3 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 "Spectrogram.h"
20 #include "enums_getText.h"
21 #include "Spectrogram_enums.h"
22 #include "enums_getValue.h"
23 #include "Spectrogram_enums.h"
24
25 Thing_implement (Spectrogram, Matrix, 2);
26
27 void structSpectrogram :: v1_info () {
28 structDaata :: v1_info ();
29 MelderInfo_writeLine (U"Time domain:");
30 MelderInfo_writeLine (U" Start time: ", xmin, U" seconds");
31 MelderInfo_writeLine (U" End time: ", xmax, U" seconds");
32 MelderInfo_writeLine (U" Total duration: ", xmax - xmin, U" seconds");
33 MelderInfo_writeLine (U"Time sampling:");
34 MelderInfo_writeLine (U" Number of time slices (frames): ", nx);
35 MelderInfo_writeLine (U" Time step (frame distance): ", dx, U" seconds");
36 MelderInfo_writeLine (U" First time slice (frame centre) at: ", x1, U" seconds");
37 MelderInfo_writeLine (U"Frequency domain:");
38 MelderInfo_writeLine (U" Lowest frequency: ", ymin, U" Hz");
39 MelderInfo_writeLine (U" Highest frequency: ", ymax, U" Hz");
40 MelderInfo_writeLine (U" Total bandwidth: ", ymax - ymin, U" Hz");
41 MelderInfo_writeLine (U"Frequency sampling:");
42 MelderInfo_writeLine (U" Number of frequency bands (bins): ", ny);
43 MelderInfo_writeLine (U" Frequency step (bin width): ", dy, U" Hz");
44 MelderInfo_writeLine (U" First frequency band around (bin centre at): ", y1, U" Hz");
45 }
46
47 autoSpectrogram Spectrogram_create (double tmin, double tmax, integer nt, double dt, double t1,
48 double fmin, double fmax, integer nf, double df, double f1)
49 {
50 try {
51 autoSpectrogram me = Thing_new (Spectrogram);
52 Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1);
53 return me;
54 } catch (MelderError) {
55 Melder_throw (U"Spectrogram not created.");
56 }
57 }
58
59 void Spectrogram_paintInside (const constSpectrogram me, const Graphics g,
60 /* mutable autowindow */ double tmin, /* mutable autowindow */ double tmax,
61 /* mutable autowindow */ double fmin, /* mutable autowindow */ double fmax,
62 /* mutable autoscaling */ double maximum,
63 const bool autoscaling,
64 const double dynamic,
65 const double preemphasis_dbPerOctave,
66 const double dynamicCompression,
67 kSpectrogram_colourMap colourMap,
68 bool invertColours
69 ) {
70 Function_unidirectionalAutowindow (me, & tmin, & tmax);
71 SampledXY_unidirectionalAutowindowY (me, & fmin, & fmax);
72 integer itmin, itmax, ifmin, ifmax;
73 const integer nt = Matrix_getWindowSamplesX (me, tmin - 0.49999 * my dx, tmax + 0.49999 * my dx, & itmin, & itmax);
74 const integer nf = Matrix_getWindowSamplesY (me, fmin - 0.49999 * my dy, fmax + 0.49999 * my dy, & ifmin, & ifmax);
75 if (nt == 0 || nf == 0)
76 return;
77 Graphics_setWindow (g, tmin, tmax, fmin, fmax);
78 autoVEC dynamicFactors = zero_VEC (nt);
79 autoMAT part = part_MAT (my z.get(), ifmin, ifmax, itmin, itmax);
80 const integer ifshift = ifmin - 1;
81 /*
82 Pre-emphasis in place; also compute maximum after pre-emphasis.
83 */
84 for (integer ifreq = 1; ifreq <= nf; ifreq ++) {
85 const double frequency = Matrix_rowToY (me, ifreq + ifshift);
86 /*
87 The preemphasis term for a frequency of 1000 Hz is always 0 dB;
88 the preemphasis term (in dB) for a frequency f is then
89 preemphasis_dbPerOctave * log2 (f / 1000 Hz)
90 To speed this up, we convert to natural logarithms, according to
91 log2 (x) = ln (x) / ln (2)
92 Before taking the logarithm, we add a tiny number, namely 1e-308,
93 so that for a frequency of 0 Hz the preemphasis term is typically 6.0 * log2 (1e-308) = -6139 dB,
94 or 0 dB in case the preemphasis is 0 dB/octave.
95 For purposes of preemphasis, negative frequencies (which don't normally occur) are treated as 0 Hz.
96 */
97 const double preemphasisTerm_db = (preemphasis_dbPerOctave / NUMln2) * log (Melder_clippedLeft (0.0, frequency) / 1000.0 + 1e-308);
98 for (integer itime = 1; itime <= nt; itime ++) {
99 const double psd_pascal2PerHz = part [ifreq] [itime];
100 /*
101 The power spectral density in dB is
102 psd_db = 10.0 * log10 (psd_pascal2PerHz / referencePsd_pascal2)
103 To speed this up, we convert to natural logarithms, according to
104 log10 (x) = ln (x) / ln (10)
105 Before taking the logarithm, we add a tiny number, namely 1e-308,
106 so that in silence the PSD (before adding preemphasis) is -3080 dB;
107 this becomes visible if you `Paint` the Spectrogram under the following settings:
108 - `autoscaling` off
109 - `maximum` 0.0 dB/Hz
110 - `pre-emphasis` 0 dB/oct
111 - `dynamic compression` 0
112 Then when the `dynamic range` passes 3080 dB, you will start to see some light grey.
113 */
114 constexpr double oneByReferencePsd_pascal2 = 1.0 / 4.0e-10;
115 const double psd_db = (10.0/NUMln10) * log (oneByReferencePsd_pascal2 * psd_pascal2PerHz + 1e-308) + preemphasisTerm_db;
116 if (psd_db > dynamicFactors [itime])
117 dynamicFactors [itime] = psd_db; // local maximum
118 part [ifreq] [itime] = psd_db;
119 }
120 }
121 /*
122 Compute global maximum.
123 */
124 if (autoscaling) {
125 maximum = 0.0;
126 for (integer itime = 1; itime <= nt; itime ++)
127 if (dynamicFactors [itime] > maximum)
128 maximum = dynamicFactors [itime];
129 }
130 /*
131 Dynamic compression in place.
132 */
133 for (integer itime = 1; itime <= nt; itime ++) {
134 dynamicFactors [itime] = dynamicCompression * (maximum - dynamicFactors [itime]);
135 for (integer ifreq = 1; ifreq <= nf; ifreq ++)
136 part [ifreq] [itime] += dynamicFactors [itime];
137 }
138 /*
139 Calculate the minimum and maximum values for normalization
140 */
141 double min_val = maximum - dynamic;
142 double max_val = maximum;
143 double range_val = max_val - min_val;
144
145 if (range_val <= 0.0) {
146 range_val = 1.0;
147 }
148 /*
149 Select different drawing methods based on the type of color mapping
150 */
151 if (colourMap == kSpectrogram_colourMap::GREY) {
152 Graphics_image (g, part.all(),
153 Matrix_columnToX (me, itmin - 0.5),
154 Matrix_columnToX (me, itmax + 0.5),
155 Matrix_rowToY (me, ifmin - 0.5),
156 Matrix_rowToY (me, ifmax + 0.5),
157 min_val, max_val
158 );
159 } else {
160 auto colourMatrix = automatrix<MelderColour>(nf, nt, MelderArray::kInitializationType::RAW);
161
162 /*
163 Define color mapping function
164 */
165 auto getColour = [&](double value) -> MelderColour {
166 double t = (value - min_val) / range_val;
167 t = std::max(0.0, std::min(1.0, t));
168
169 MelderColour colour;
170 colour.transparency = 0.0;
171
172 /*
173 Helper lambda for piecewise linear interpolation
174 */
175 auto interp = [](double t, const std::vector<std::pair<double, double>>& points) -> double {
176 if (t <= points.front().first) return points.front().second;
177 if (t >= points.back().first) return points.back().second;
178
179 for (size_t i = 0; i < points.size() - 1; ++i) {
180 if (t >= points[i].first && t <= points[i + 1].first) {
181 double t0 = points[i].first, v0 = points[i].second;
182 double t1 = points[i + 1].first, v1 = points[i + 1].second;
183 if (t1 == t0) return v0;
184 double frac = (t - t0) / (t1 - t0);
185 return v0 + frac * (v1 - v0);
186 }
187 }
188 return 0.0;
189 };
190
191 switch (colourMap) {
192 case kSpectrogram_colourMap::VIRIDIS: {
193 // Viridis (Matplotlib)
194 constexpr double viridis[][3] = {
195 {0.267004, 0.004874, 0.329415},
196 {0.268510, 0.009605, 0.335427},
197 {0.269944, 0.014625, 0.341379},
198 {0.271305, 0.019942, 0.347269},
199 {0.272594, 0.025563, 0.353093},
200 {0.273809, 0.031497, 0.358853},
201 {0.274952, 0.037752, 0.364543},
202 {0.276022, 0.044167, 0.370164},
203 {0.277018, 0.050344, 0.375715},
204 {0.277941, 0.056324, 0.381191},
205 {0.278791, 0.062145, 0.386592},
206 {0.279566, 0.067836, 0.391917},
207 {0.280267, 0.073417, 0.397163},
208 {0.280894, 0.078907, 0.402329},
209 {0.281446, 0.084320, 0.407414},
210 {0.281924, 0.089666, 0.412415},
211 {0.282327, 0.094955, 0.417331},
212 {0.282656, 0.100196, 0.422160},
213 {0.282910, 0.105393, 0.426902},
214 {0.283091, 0.110553, 0.431554},
215 {0.283197, 0.115680, 0.436115},
216 {0.283229, 0.120777, 0.440584},
217 {0.283187, 0.125848, 0.444960},
218 {0.283072, 0.130895, 0.449241},
219 {0.282884, 0.135920, 0.453427},
220 {0.282623, 0.140926, 0.457517},
221 {0.282290, 0.145912, 0.461510},
222 {0.281887, 0.150881, 0.465405},
223 {0.281412, 0.155834, 0.469201},
224 {0.280868, 0.160771, 0.472899},
225 {0.280255, 0.165693, 0.476498},
226 {0.279574, 0.170599, 0.479997},
227 {0.278826, 0.175490, 0.483397},
228 {0.278012, 0.180367, 0.486697},
229 {0.277134, 0.185228, 0.489898},
230 {0.276194, 0.190074, 0.493001},
231 {0.275191, 0.194905, 0.496005},
232 {0.274128, 0.199721, 0.498911},
233 {0.273006, 0.204520, 0.501721},
234 {0.271828, 0.209303, 0.504434},
235 {0.270595, 0.214069, 0.507052},
236 {0.269308, 0.218818, 0.509577},
237 {0.267968, 0.223549, 0.512008},
238 {0.266580, 0.228262, 0.514349},
239 {0.265145, 0.232956, 0.516599},
240 {0.263663, 0.237631, 0.518762},
241 {0.262138, 0.242286, 0.520837},
242 {0.260571, 0.246922, 0.522828},
243 {0.258965, 0.251537, 0.524736},
244 {0.257322, 0.256130, 0.526563},
245 {0.255645, 0.260703, 0.528312},
246 {0.253935, 0.265254, 0.529983},
247 {0.252194, 0.269783, 0.531579},
248 {0.250425, 0.274290, 0.533103},
249 {0.248629, 0.278775, 0.534556},
250 {0.246811, 0.283237, 0.535941},
251 {0.244972, 0.287675, 0.537260},
252 {0.243113, 0.292092, 0.538516},
253 {0.241237, 0.296485, 0.539709},
254 {0.239346, 0.300855, 0.540844},
255 {0.237441, 0.305202, 0.541921},
256 {0.235526, 0.309527, 0.542944},
257 {0.233603, 0.313828, 0.543914},
258 {0.231674, 0.318106, 0.544834},
259 {0.229739, 0.322361, 0.545706},
260 {0.227802, 0.326594, 0.546532},
261 {0.225863, 0.330805, 0.547314},
262 {0.223925, 0.334994, 0.548053},
263 {0.221989, 0.339161, 0.548752},
264 {0.220057, 0.343307, 0.549413},
265 {0.218130, 0.347432, 0.550038},
266 {0.216210, 0.351535, 0.550627},
267 {0.214298, 0.355619, 0.551184},
268 {0.212395, 0.359683, 0.551710},
269 {0.210503, 0.363727, 0.552206},
270 {0.208623, 0.367752, 0.552675},
271 {0.206756, 0.371758, 0.553117},
272 {0.204903, 0.375746, 0.553533},
273 {0.203063, 0.379716, 0.553925},
274 {0.201239, 0.383670, 0.554294},
275 {0.199430, 0.387607, 0.554642},
276 {0.197636, 0.391528, 0.554969},
277 {0.195860, 0.395433, 0.555276},
278 {0.194100, 0.399323, 0.555565},
279 {0.192357, 0.403199, 0.555836},
280 {0.190631, 0.407061, 0.556089},
281 {0.188923, 0.410910, 0.556326},
282 {0.187231, 0.414746, 0.556547},
283 {0.185556, 0.418570, 0.556753},
284 {0.183898, 0.422383, 0.556944},
285 {0.182256, 0.426184, 0.557120},
286 {0.180629, 0.429975, 0.557282},
287 {0.179019, 0.433756, 0.557430},
288 {0.177423, 0.437527, 0.557565},
289 {0.175841, 0.441290, 0.557685},
290 {0.174274, 0.445044, 0.557792},
291 {0.172719, 0.448791, 0.557885},
292 {0.171176, 0.452530, 0.557965},
293 {0.169646, 0.456262, 0.558030},
294 {0.168126, 0.459988, 0.558082},
295 {0.166617, 0.463708, 0.558119},
296 {0.165117, 0.467423, 0.558141},
297 {0.163625, 0.471133, 0.558148},
298 {0.162142, 0.474838, 0.558140},
299 {0.160665, 0.478540, 0.558115},
300 {0.159194, 0.482237, 0.558073},
301 {0.157729, 0.485932, 0.558013},
302 {0.156270, 0.489624, 0.557936},
303 {0.154815, 0.493313, 0.557840},
304 {0.153364, 0.497000, 0.557724},
305 {0.151918, 0.500685, 0.557587},
306 {0.150476, 0.504369, 0.557430},
307 {0.149039, 0.508051, 0.557250},
308 {0.147607, 0.511733, 0.557049},
309 {0.146180, 0.515413, 0.556823},
310 {0.144759, 0.519093, 0.556572},
311 {0.143343, 0.522773, 0.556295},
312 {0.141935, 0.526453, 0.555991},
313 {0.140536, 0.530132, 0.555659},
314 {0.139147, 0.533812, 0.555298},
315 {0.137770, 0.537492, 0.554906},
316 {0.136408, 0.541173, 0.554483},
317 {0.135066, 0.544853, 0.554029},
318 {0.133743, 0.548535, 0.553541},
319 {0.132444, 0.552216, 0.553018},
320 {0.131172, 0.555899, 0.552459},
321 {0.129933, 0.559582, 0.551864},
322 {0.128729, 0.563265, 0.551229},
323 {0.127568, 0.566949, 0.550556},
324 {0.126453, 0.570633, 0.549841},
325 {0.125394, 0.574318, 0.549086},
326 {0.124395, 0.578002, 0.548287},
327 {0.123463, 0.581687, 0.547445},
328 {0.122606, 0.585371, 0.546557},
329 {0.121831, 0.589055, 0.545623},
330 {0.121148, 0.592739, 0.544641},
331 {0.120565, 0.596422, 0.543611},
332 {0.120092, 0.600104, 0.542530},
333 {0.119738, 0.603785, 0.541400},
334 {0.119512, 0.607464, 0.540218},
335 {0.119423, 0.611141, 0.538982},
336 {0.119483, 0.614817, 0.537692},
337 {0.119699, 0.618490, 0.536347},
338 {0.120081, 0.622161, 0.534946},
339 {0.120638, 0.625828, 0.533488},
340 {0.121380, 0.629492, 0.531973},
341 {0.122312, 0.633153, 0.530398},
342 {0.123444, 0.636809, 0.528763},
343 {0.124780, 0.640461, 0.527068},
344 {0.126326, 0.644107, 0.525311},
345 {0.128087, 0.647749, 0.523491},
346 {0.130067, 0.651384, 0.521608},
347 {0.132268, 0.655014, 0.519661},
348 {0.134692, 0.658636, 0.517649},
349 {0.137339, 0.662252, 0.515571},
350 {0.140210, 0.665859, 0.513427},
351 {0.143303, 0.669459, 0.511215},
352 {0.146616, 0.673050, 0.508936},
353 {0.150148, 0.676631, 0.506589},
354 {0.153894, 0.680203, 0.504172},
355 {0.157851, 0.683765, 0.501686},
356 {0.162016, 0.687316, 0.499129},
357 {0.166383, 0.690856, 0.496502},
358 {0.170948, 0.694384, 0.493803},
359 {0.175707, 0.697900, 0.491033},
360 {0.180653, 0.701402, 0.488189},
361 {0.185783, 0.704891, 0.485273},
362 {0.191090, 0.708366, 0.482284},
363 {0.196571, 0.711827, 0.479221},
364 {0.202219, 0.715272, 0.476084},
365 {0.208030, 0.718701, 0.472873},
366 {0.214000, 0.722114, 0.469588},
367 {0.220124, 0.725509, 0.466226},
368 {0.226397, 0.728888, 0.462789},
369 {0.232815, 0.732247, 0.459277},
370 {0.239374, 0.735588, 0.455688},
371 {0.246070, 0.738910, 0.452024},
372 {0.252899, 0.742211, 0.448284},
373 {0.259857, 0.745492, 0.444467},
374 {0.266941, 0.748751, 0.440573},
375 {0.274149, 0.751988, 0.436601},
376 {0.281477, 0.755203, 0.432552},
377 {0.288921, 0.758394, 0.428426},
378 {0.296479, 0.761561, 0.424223},
379 {0.304148, 0.764704, 0.419943},
380 {0.311925, 0.767822, 0.415586},
381 {0.319809, 0.770914, 0.411152},
382 {0.327796, 0.773980, 0.406640},
383 {0.335885, 0.777018, 0.402049},
384 {0.344074, 0.780029, 0.397381},
385 {0.352360, 0.783011, 0.392636},
386 {0.360741, 0.785964, 0.387814},
387 {0.369214, 0.788888, 0.382914},
388 {0.377779, 0.791781, 0.377939},
389 {0.386433, 0.794644, 0.372886},
390 {0.395174, 0.797475, 0.367757},
391 {0.404001, 0.800275, 0.362552},
392 {0.412913, 0.803041, 0.357269},
393 {0.421908, 0.805774, 0.351910},
394 {0.430983, 0.808473, 0.346476},
395 {0.440137, 0.811138, 0.340967},
396 {0.449368, 0.813768, 0.335384},
397 {0.458674, 0.816363, 0.329727},
398 {0.468053, 0.818921, 0.323998},
399 {0.477504, 0.821444, 0.318195},
400 {0.487026, 0.823929, 0.312321},
401 {0.496615, 0.826376, 0.306377},
402 {0.506271, 0.828786, 0.300362},
403 {0.515992, 0.831158, 0.294279},
404 {0.525776, 0.833491, 0.288127},
405 {0.535621, 0.835785, 0.281908},
406 {0.545524, 0.838039, 0.275626},
407 {0.555484, 0.840254, 0.269281},
408 {0.565498, 0.842430, 0.262877},
409 {0.575563, 0.844566, 0.256415},
410 {0.585678, 0.846661, 0.249897},
411 {0.575563, 0.844566, 0.256415},
412 {0.585678, 0.846661, 0.249897},
413 {0.595839, 0.848717, 0.243329},
414 {0.606045, 0.850733, 0.236712},
415 {0.616293, 0.852709, 0.230052},
416 {0.626579, 0.854645, 0.223353},
417 {0.636902, 0.856542, 0.216620},
418 {0.647257, 0.858400, 0.209861},
419 {0.657642, 0.860219, 0.203082},
420 {0.595839, 0.848717, 0.243329},
421 {0.606045, 0.850733, 0.236712},
422 {0.616293, 0.852709, 0.230052},
423 {0.626579, 0.854645, 0.223353},
424 {0.636902, 0.856542, 0.216620},
425 {0.647257, 0.858400, 0.209861},
426 {0.657642, 0.860219, 0.203082},
427 {0.668054, 0.861999, 0.196293},
428 {0.606045, 0.850733, 0.236712},
429 {0.616293, 0.852709, 0.230052},
430 {0.626579, 0.854645, 0.223353},
431 {0.636902, 0.856542, 0.216620},
432 {0.647257, 0.858400, 0.209861},
433 {0.657642, 0.860219, 0.203082},
434 {0.668054, 0.861999, 0.196293},
435 {0.626579, 0.854645, 0.223353},
436 {0.636902, 0.856542, 0.216620},
437 {0.647257, 0.858400, 0.209861},
438 {0.657642, 0.860219, 0.203082},
439 {0.668054, 0.861999, 0.196293},
440 {0.647257, 0.858400, 0.209861},
441 {0.657642, 0.860219, 0.203082},
442 {0.668054, 0.861999, 0.196293},
443 {0.678489, 0.863742, 0.189503},
444 {0.688944, 0.865448, 0.182725},
445 {0.668054, 0.861999, 0.196293},
446 {0.678489, 0.863742, 0.189503},
447 {0.688944, 0.865448, 0.182725},
448 {0.699415, 0.867117, 0.175971},
449 {0.678489, 0.863742, 0.189503},
450 {0.688944, 0.865448, 0.182725},
451 {0.699415, 0.867117, 0.175971},
452 {0.688944, 0.865448, 0.182725},
453 {0.699415, 0.867117, 0.175971},
454 {0.709898, 0.868751, 0.169257},
455 {0.720391, 0.870350, 0.162603},
456 {0.699415, 0.867117, 0.175971},
457 {0.709898, 0.868751, 0.169257},
458 {0.720391, 0.870350, 0.162603},
459 {0.709898, 0.868751, 0.169257},
460 {0.720391, 0.870350, 0.162603},
461 {0.730889, 0.871916, 0.156029},
462 {0.720391, 0.870350, 0.162603},
463 {0.730889, 0.871916, 0.156029},
464 {0.741388, 0.873449, 0.149561},
465 {0.751884, 0.874951, 0.143228},
466 {0.730889, 0.871916, 0.156029},
467 {0.741388, 0.873449, 0.149561},
468 {0.751884, 0.874951, 0.143228},
469 {0.762373, 0.876424, 0.137064},
470 {0.741388, 0.873449, 0.149561},
471 {0.751884, 0.874951, 0.143228},
472 {0.762373, 0.876424, 0.137064},
473 {0.772852, 0.877868, 0.131109},
474 {0.783315, 0.879285, 0.125405},
475 {0.762373, 0.876424, 0.137064},
476 {0.772852, 0.877868, 0.131109},
477 {0.783315, 0.879285, 0.125405},
478 {0.793760, 0.880678, 0.120005},
479 {0.772852, 0.877868, 0.131109},
480 {0.783315, 0.879285, 0.125405},
481 {0.793760, 0.880678, 0.120005},
482 {0.804182, 0.882046, 0.114965},
483 {0.814576, 0.883393, 0.110347},
484 {0.783315, 0.879285, 0.125405},
485 {0.793760, 0.880678, 0.120005},
486 {0.804182, 0.882046, 0.114965},
487 {0.814576, 0.883393, 0.110347},
488 {0.824940, 0.884720, 0.106217},
489 {0.793760, 0.880678, 0.120005},
490 {0.804182, 0.882046, 0.114965},
491 {0.814576, 0.883393, 0.110347},
492 {0.824940, 0.884720, 0.106217},
493 {0.835270, 0.886029, 0.102646},
494 {0.804182, 0.882046, 0.114965},
495 {0.814576, 0.883393, 0.110347},
496 {0.824940, 0.884720, 0.106217},
497 {0.835270, 0.886029, 0.102646},
498 {0.845561, 0.887322, 0.099702},
499 {0.855810, 0.888601, 0.097452},
500 {0.835270, 0.886029, 0.102646},
501 {0.845561, 0.887322, 0.099702},
502 {0.855810, 0.888601, 0.097452},
503 {0.845561, 0.887322, 0.099702},
504 {0.855810, 0.888601, 0.097452},
505 {0.855810, 0.888601, 0.097452},
506 {0.866013, 0.889868, 0.095953},
507 {0.876168, 0.891125, 0.095250},
508 {0.886271, 0.892374, 0.095374},
509 {0.896320, 0.893616, 0.096335},
510 {0.906311, 0.894855, 0.098125},
511 {0.916242, 0.896091, 0.100717},
512 {0.926106, 0.897330, 0.104071},
513 {0.935904, 0.898570, 0.108131},
514 {0.945636, 0.899815, 0.112838},
515 {0.955300, 0.901065, 0.118128},
516 {0.964894, 0.902323, 0.123941},
517 {0.974417, 0.903590, 0.130215},
518 {0.983868, 0.904867, 0.136897},
519 {0.993248, 0.906157, 0.143936}
520 };
521 constexpr int n = 256;
522 // Clamp t to [0, 1] to avoid out-of-bounds
523 double tt = std::max(0.0, std::min(1.0, t));
524 double idx = tt * (n - 1);
525 int i = static_cast<int>(idx);
526 if (i >= n - 1) {
527 i = n - 2;
528 idx = static_cast<double>(i);
529 }
530 double frac = idx - i;
531
532 colour.red = viridis[i][0] + frac * (viridis[i+1][0] - viridis[i][0]);
533 colour.green = viridis[i][1] + frac * (viridis[i+1][1] - viridis[i][1]);
534 colour.blue = viridis[i][2] + frac * (viridis[i+1][2] - viridis[i][2]);
535 break;
536 }
537 case kSpectrogram_colourMap::JET: {
538 // Jet (MATLAB)
539 const std::vector<std::pair<double, double>> red_pts = {
540 {0.00, 0.0},
541 {0.35, 0.0},
542 {0.66, 1.0},
543 {0.89, 1.0},
544 {1.00, 0.5}
545 };
546 const std::vector<std::pair<double, double>> green_pts = {
547 {0.000, 0.0},
548 {0.125, 0.0},
549 {0.375, 1.0},
550 {0.640, 1.0},
551 {0.910, 0.0},
552 {1.000, 0.0}
553 };
554 const std::vector<std::pair<double, double>> blue_pts = {
555 {0.00, 0.5},
556 {0.11, 1.0},
557 {0.34, 1.0},
558 {0.65, 0.0},
559 {1.00, 0.0}
560 };
561
562 colour.red = interp(t, red_pts);
563 colour.green = interp(t, green_pts);
564 colour.blue = interp(t, blue_pts);
565 break;
566 }
567 case kSpectrogram_colourMap::HOT: {
568 // Hot
569 // From _hot_data (note: only the first and second value in each tuple matter;
570 // Matplotlib uses (x, left_value, right_value), but for continuous colormaps they are equal)
571 const std::vector<std::pair<double, double>> red_pts = {
572 {0.0, 0.0416},
573 {0.365079, 1.0},
574 {1.0, 1.0}
575 };
576 const std::vector<std::pair<double, double>> green_pts = {
577 {0.0, 0.0},
578 {0.365079, 0.0},
579 {0.746032, 1.0},
580 {1.0, 1.0}
581 };
582 const std::vector<std::pair<double, double>> blue_pts = {
583 {0.0, 0.0},
584 {0.746032, 0.0},
585 {1.0, 1.0}
586 };
587
588 colour.red = interp(t, red_pts);
589 colour.green = interp(t, green_pts);
590 colour.blue = interp(t, blue_pts);
591 break;
592 }
593 case kSpectrogram_colourMap::COOL: {
594 // Cool
595 colour.red = t;
596 colour.green = 1.0 - t;
597 colour.blue = 1.0;
598 break;
599 }
600 case kSpectrogram_colourMap::SPRING: {
601 // Spring
602 colour.red = 1.0;
603 colour.green = t;
604 colour.blue = 1.0 - t;
605 break;
606 }
607 case kSpectrogram_colourMap::SUMMER: {
608 // Summer
609 colour.red = t;
610 colour.green = 0.5 + t/2.0;
611 colour.blue = 0.4;
612 break;
613 }
614 case kSpectrogram_colourMap::AUTUMN: {
615 // Autumn
616 colour.red = 1.0;
617 colour.green = t;
618 colour.blue = 0.0;
619 break;
620 }
621 case kSpectrogram_colourMap::WINTER: {
622 // Winter
623 colour.red = 0.0;
624 colour.green = t;
625 colour.blue = 1.0 - t/2.0;
626 break;
627 }
628 case kSpectrogram_colourMap::BONE: {
629 // Bone
630 const std::vector<std::pair<double, double>> red_pts = {
631 {0.0, 0.0},
632 {0.746032, 0.652778},
633 {1.0, 1.0}
634 };
635 const std::vector<std::pair<double, double>> green_pts = {
636 {0.0, 0.0},
637 {0.365079, 0.319444},
638 {0.746032, 0.777778},
639 {1.0, 1.0}
640 };
641 const std::vector<std::pair<double, double>> blue_pts = {
642 {0.0, 0.0},
643 {0.365079, 0.444444},
644 {1.0, 1.0}
645 };
646
647 colour.red = interp(t, red_pts);
648 colour.green = interp(t, green_pts);
649 colour.blue = interp(t, blue_pts);
650 break;
651 }
652 case kSpectrogram_colourMap::COPPER: {
653 // Copper
654 const std::vector<std::pair<double, double>> red_pts = {
655 {0.0, 0.0},
656 {0.809524, 1.0},
657 {1.0, 1.0}
658 };
659 const std::vector<std::pair<double, double>> green_pts = {
660 {0.0, 0.0},
661 {1.0, 0.7812}
662 };
663 const std::vector<std::pair<double, double>> blue_pts = {
664 {0.0, 0.0},
665 {1.0, 0.4975}
666 };
667
668 colour.red = interp(t, red_pts);
669 colour.green = interp(t, green_pts);
670 colour.blue = interp(t, blue_pts);
671 break;
672 }
673 case kSpectrogram_colourMap::PINK: {
674 // Pink
675 const std::vector<std::pair<double, double>> red_pts = {
676 {0.000000, 0.117800},
677 {0.015873, 0.195857},
678 {0.031746, 0.250661},
679 {0.047619, 0.295468},
680 {0.063492, 0.334324},
681 {0.079365, 0.369112},
682 {0.095238, 0.400892},
683 {0.111111, 0.430331},
684 {0.126984, 0.457882},
685 {0.142857, 0.483867},
686 {0.158730, 0.508525},
687 {0.174603, 0.532042},
688 {0.190476, 0.554563},
689 {0.206349, 0.576204},
690 {0.222222, 0.597061},
691 {0.238095, 0.617213},
692 {0.253968, 0.636729},
693 {0.269841, 0.655663},
694 {0.285714, 0.674066},
695 {0.301587, 0.691980},
696 {0.317460, 0.709441},
697 {0.333333, 0.726483},
698 {0.349206, 0.743134},
699 {0.365079, 0.759421},
700 {0.380952, 0.766356},
701 {0.396825, 0.773229},
702 {0.412698, 0.780042},
703 {0.428571, 0.786796},
704 {0.444444, 0.793492},
705 {0.460317, 0.800132},
706 {0.476190, 0.806718},
707 {0.492063, 0.813250},
708 {0.507937, 0.819730},
709 {0.523810, 0.826160},
710 {0.539683, 0.832539},
711 {0.555556, 0.838870},
712 {0.571429, 0.845154},
713 {0.587302, 0.851392},
714 {0.603175, 0.857584},
715 {0.619048, 0.863731},
716 {0.634921, 0.869835},
717 {0.650794, 0.875897},
718 {0.666667, 0.881917},
719 {0.682540, 0.887896},
720 {0.698413, 0.893835},
721 {0.714286, 0.899735},
722 {0.730159, 0.905597},
723 {0.746032, 0.911421},
724 {0.761905, 0.917208},
725 {0.777778, 0.922958},
726 {0.793651, 0.928673},
727 {0.809524, 0.934353},
728 {0.825397, 0.939999},
729 {0.841270, 0.945611},
730 {0.857143, 0.951190},
731 {0.873016, 0.956736},
732 {0.888889, 0.962250},
733 {0.904762, 0.967733},
734 {0.920635, 0.973185},
735 {0.936508, 0.978607},
736 {0.952381, 0.983999},
737 {0.968254, 0.989361},
738 {0.984127, 0.994695},
739 {1.000000, 1.000000},
740 };
741 const std::vector<std::pair<double, double>> green_pts = {
742 {0.000000, 0.000000},
743 {0.015873, 0.102869},
744 {0.031746, 0.145479},
745 {0.047619, 0.178174},
746 {0.063492, 0.205738},
747 {0.079365, 0.230022},
748 {0.095238, 0.251976},
749 {0.111111, 0.272166},
750 {0.126984, 0.290957},
751 {0.142857, 0.308607},
752 {0.158730, 0.325300},
753 {0.174603, 0.341178},
754 {0.190476, 0.356348},
755 {0.206349, 0.370899},
756 {0.222222, 0.384900},
757 {0.238095, 0.398410},
758 {0.253968, 0.411476},
759 {0.269841, 0.424139},
760 {0.285714, 0.436436},
761 {0.301587, 0.448395},
762 {0.317460, 0.460044},
763 {0.333333, 0.471405},
764 {0.349206, 0.482498},
765 {0.365079, 0.493342},
766 {0.380952, 0.517549},
767 {0.396825, 0.540674},
768 {0.412698, 0.562849},
769 {0.428571, 0.584183},
770 {0.444444, 0.604765},
771 {0.460317, 0.624669},
772 {0.476190, 0.643958},
773 {0.492063, 0.662687},
774 {0.507937, 0.680900},
775 {0.523810, 0.698638},
776 {0.539683, 0.715937},
777 {0.555556, 0.732828},
778 {0.571429, 0.749338},
779 {0.587302, 0.765493},
780 {0.603175, 0.781313},
781 {0.619048, 0.796819},
782 {0.634921, 0.812029},
783 {0.650794, 0.826960},
784 {0.666667, 0.841625},
785 {0.682540, 0.856040},
786 {0.698413, 0.870216},
787 {0.714286, 0.884164},
788 {0.730159, 0.897896},
789 {0.746032, 0.911421},
790 {0.761905, 0.917208},
791 {0.777778, 0.922958},
792 {0.793651, 0.928673},
793 {0.809524, 0.934353},
794 {0.825397, 0.939999},
795 {0.841270, 0.945611},
796 {0.857143, 0.951190},
797 {0.873016, 0.956736},
798 {0.888889, 0.962250},
799 {0.904762, 0.967733},
800 {0.920635, 0.973185},
801 {0.936508, 0.978607},
802 {0.952381, 0.983999},
803 {0.968254, 0.989361},
804 {0.984127, 0.994695},
805 {1.000000, 1.000000},
806 };
807 const std::vector<std::pair<double, double>> blue_pts = {
808 {0.000000, 0.000000},
809 {0.015873, 0.102869},
810 {0.031746, 0.145479},
811 {0.047619, 0.178174},
812 {0.063492, 0.205738},
813 {0.079365, 0.230022},
814 {0.095238, 0.251976},
815 {0.111111, 0.272166},
816 {0.126984, 0.290957},
817 {0.142857, 0.308607},
818 {0.158730, 0.325300},
819 {0.174603, 0.341178},
820 {0.190476, 0.356348},
821 {0.206349, 0.370899},
822 {0.222222, 0.384900},
823 {0.238095, 0.398410},
824 {0.253968, 0.411476},
825 {0.269841, 0.424139},
826 {0.285714, 0.436436},
827 {0.301587, 0.448395},
828 {0.317460, 0.460044},
829 {0.333333, 0.471405},
830 {0.349206, 0.482498},
831 {0.365079, 0.493342},
832 {0.380952, 0.503953},
833 {0.396825, 0.514344},
834 {0.412698, 0.524531},
835 {0.428571, 0.534522},
836 {0.444444, 0.544331},
837 {0.460317, 0.553966},
838 {0.476190, 0.563436},
839 {0.492063, 0.572750},
840 {0.507937, 0.581914},
841 {0.523810, 0.590937},
842 {0.539683, 0.599824},
843 {0.555556, 0.608581},
844 {0.571429, 0.617213},
845 {0.587302, 0.625727},
846 {0.603175, 0.634126},
847 {0.619048, 0.642416},
848 {0.634921, 0.650600},
849 {0.650794, 0.658682},
850 {0.666667, 0.666667},
851 {0.682540, 0.674556},
852 {0.698413, 0.682355},
853 {0.714286, 0.690066},
854 {0.730159, 0.697691},
855 {0.746032, 0.705234},
856 {0.761905, 0.727166},
857 {0.777778, 0.748455},
858 {0.793651, 0.769156},
859 {0.809524, 0.789314},
860 {0.825397, 0.808969},
861 {0.841270, 0.828159},
862 {0.857143, 0.846913},
863 {0.873016, 0.865261},
864 {0.888889, 0.883229},
865 {0.904762, 0.900837},
866 {0.920635, 0.918109},
867 {0.936508, 0.935061},
868 {0.952381, 0.951711},
869 {0.968254, 0.968075},
870 {0.984127, 0.984167},
871 {1.000000, 1.000000},
872 };
873 colour.red = interp(t, red_pts);
874 colour.green = interp(t, green_pts);
875 colour.blue = interp(t, blue_pts);
876 break;
877 }
878 case kSpectrogram_colourMap::PARULA: {
879 // Parula (MATLAB)
880 constexpr double parula[][3] = {
881 {0.2081, 0.1663, 0.5292},
882 {0.2116, 0.1898, 0.5777},
883 {0.2123, 0.2138, 0.6270},
884 {0.2081, 0.2386, 0.6771},
885 {0.1959, 0.2645, 0.7279},
886 {0.1707, 0.2919, 0.7792},
887 {0.1253, 0.3242, 0.8303},
888 {0.0591, 0.3598, 0.8683},
889 {0.0117, 0.3875, 0.8820},
890 {0.0060, 0.4086, 0.8828},
891 {0.0165, 0.4266, 0.8786},
892 {0.0329, 0.4430, 0.8720},
893 {0.0498, 0.4586, 0.8641},
894 {0.0629, 0.4737, 0.8554},
895 {0.0723, 0.4887, 0.8467},
896 {0.0779, 0.5040, 0.8384},
897 {0.0793, 0.5200, 0.8312},
898 {0.0749, 0.5375, 0.8263},
899 {0.0641, 0.5570, 0.8240},
900 {0.0488, 0.5772, 0.8228},
901 {0.0343, 0.5966, 0.8199},
902 {0.0265, 0.6137, 0.8135},
903 {0.0239, 0.6287, 0.8038},
904 {0.0231, 0.6418, 0.7913},
905 {0.0228, 0.6535, 0.7768},
906 {0.0267, 0.6642, 0.7607},
907 {0.0384, 0.6743, 0.7436},
908 {0.0590, 0.6838, 0.7254},
909 {0.0843, 0.6928, 0.7062},
910 {0.1133, 0.7015, 0.6859},
911 {0.1453, 0.7098, 0.6646},
912 {0.1801, 0.7177, 0.6424},
913 {0.2178, 0.7250, 0.6193},
914 {0.2586, 0.7317, 0.5954},
915 {0.3022, 0.7376, 0.5712},
916 {0.3482, 0.7424, 0.5473},
917 {0.3953, 0.7459, 0.5244},
918 {0.4420, 0.7481, 0.5033},
919 {0.4871, 0.7491, 0.4840},
920 {0.5300, 0.7491, 0.4661},
921 {0.5709, 0.7485, 0.4494},
922 {0.6099, 0.7473, 0.4337},
923 {0.6473, 0.7456, 0.4188},
924 {0.6834, 0.7435, 0.4044},
925 {0.7184, 0.7411, 0.3905},
926 {0.7525, 0.7384, 0.3768},
927 {0.7858, 0.7356, 0.3632},
928 {0.8185, 0.7327, 0.3498},
929 {0.8507, 0.7299, 0.3360},
930 {0.8824, 0.7274, 0.3217},
931 {0.9139, 0.7258, 0.3063},
932 {0.9450, 0.7261, 0.2886},
933 {0.9739, 0.7314, 0.2666},
934 {0.9938, 0.7455, 0.2403},
935 {0.9990, 0.7653, 0.2164},
936 {0.9955, 0.7861, 0.1967},
937 {0.9880, 0.8066, 0.1794},
938 {0.9789, 0.8271, 0.1633},
939 {0.9697, 0.8481, 0.1475},
940 {0.9626, 0.8705, 0.1309},
941 {0.9589, 0.8949, 0.1132},
942 {0.9598, 0.9218, 0.0948},
943 {0.9661, 0.9514, 0.0755},
944 {0.9763, 0.9831, 0.0538}
945 };
946 constexpr int n = 64;
947 double idx = t * (n - 1);
948 int i = std::min(static_cast<int>(idx), n - 2);
949 double frac = idx - i;
950 colour.red = parula[i][0] + frac * (parula[i+1][0] - parula[i][0]);
951 colour.green = parula[i][1] + frac * (parula[i+1][1] - parula[i][1]);
952 colour.blue = parula[i][2] + frac * (parula[i+1][2] - parula[i][2]);
953 break;
954 }
955 case kSpectrogram_colourMap::TURBO: {
956 // Turbo (Google)
957 constexpr double turbo[][3] = {
958 {0.18995, 0.07176, 0.23217},
959 {0.19483, 0.08339, 0.26149},
960 {0.19956, 0.09498, 0.29024},
961 {0.20415, 0.10652, 0.31844},
962 {0.20860, 0.11802, 0.34607},
963 {0.21291, 0.12947, 0.37314},
964 {0.21708, 0.14087, 0.39964},
965 {0.22111, 0.15223, 0.42558},
966 {0.22500, 0.16354, 0.45096},
967 {0.22875, 0.17481, 0.47578},
968 {0.23236, 0.18603, 0.50004},
969 {0.23582, 0.19720, 0.52373},
970 {0.23915, 0.20833, 0.54686},
971 {0.24234, 0.21941, 0.56942},
972 {0.24539, 0.23044, 0.59142},
973 {0.24830, 0.24143, 0.61286},
974 {0.25107, 0.25237, 0.63374},
975 {0.25369, 0.26327, 0.65406},
976 {0.25618, 0.27412, 0.67381},
977 {0.25853, 0.28492, 0.69300},
978 {0.26074, 0.29568, 0.71162},
979 {0.26280, 0.30639, 0.72968},
980 {0.26473, 0.31706, 0.74718},
981 {0.26652, 0.32768, 0.76412},
982 {0.26816, 0.33825, 0.78050},
983 {0.26967, 0.34878, 0.79631},
984 {0.27103, 0.35926, 0.81156},
985 {0.27226, 0.36970, 0.82624},
986 {0.27334, 0.38008, 0.84037},
987 {0.27429, 0.39043, 0.85393},
988 {0.27509, 0.40072, 0.86692},
989 {0.27576, 0.41097, 0.87936},
990 {0.27628, 0.42118, 0.89123},
991 {0.27667, 0.43134, 0.90254},
992 {0.27691, 0.44145, 0.91328},
993 {0.27701, 0.45152, 0.92347},
994 {0.27698, 0.46153, 0.93309},
995 {0.27680, 0.47151, 0.94214},
996 {0.27648, 0.48144, 0.95064},
997 {0.27603, 0.49132, 0.95857},
998 {0.27543, 0.50115, 0.96594},
999 {0.27469, 0.51094, 0.97275},
1000 {0.27381, 0.52069, 0.97899},
1001 {0.27273, 0.53040, 0.98461},
1002 {0.27106, 0.54015, 0.98930},
1003 {0.26878, 0.54995, 0.99303},
1004 {0.26592, 0.55979, 0.99583},
1005 {0.26252, 0.56967, 0.99773},
1006 {0.25862, 0.57958, 0.99876},
1007 {0.25425, 0.58950, 0.99896},
1008 {0.24946, 0.59943, 0.99835},
1009 {0.24427, 0.60937, 0.99697},
1010 {0.23874, 0.61931, 0.99485},
1011 {0.23288, 0.62923, 0.99202},
1012 {0.22676, 0.63913, 0.98851},
1013 {0.22039, 0.64901, 0.98436},
1014 {0.21382, 0.65886, 0.97959},
1015 {0.20708, 0.66866, 0.97423},
1016 {0.20021, 0.67842, 0.96833},
1017 {0.19326, 0.68812, 0.96190},
1018 {0.18625, 0.69775, 0.95498},
1019 {0.17923, 0.70732, 0.94761},
1020 {0.17223, 0.71680, 0.93981},
1021 {0.16529, 0.72620, 0.93161},
1022 {0.15844, 0.73551, 0.92305},
1023 {0.15173, 0.74472, 0.91416},
1024 {0.14519, 0.75381, 0.90496},
1025 {0.13886, 0.76279, 0.89550},
1026 {0.13278, 0.77165, 0.88580},
1027 {0.12698, 0.78037, 0.87590},
1028 {0.12151, 0.78896, 0.86581},
1029 {0.11639, 0.79740, 0.85559},
1030 {0.11167, 0.80569, 0.84525},
1031 {0.10738, 0.81381, 0.83484},
1032 {0.10357, 0.82177, 0.82437},
1033 {0.10026, 0.82955, 0.81389},
1034 {0.09750, 0.83714, 0.80342},
1035 {0.09532, 0.84455, 0.79299},
1036 {0.09377, 0.85175, 0.78264},
1037 {0.09287, 0.85875, 0.77240},
1038 {0.09267, 0.86554, 0.76230},
1039 {0.09320, 0.87211, 0.75237},
1040 {0.09451, 0.87844, 0.74265},
1041 {0.09662, 0.88454, 0.73316},
1042 {0.09958, 0.89040, 0.72393},
1043 {0.10342, 0.89600, 0.71500},
1044 {0.10815, 0.90142, 0.70599},
1045 {0.11374, 0.90673, 0.69651},
1046 {0.12014, 0.91193, 0.68660},
1047 {0.12733, 0.91701, 0.67627},
1048 {0.13526, 0.92197, 0.66556},
1049 {0.14391, 0.92680, 0.65448},
1050 {0.15323, 0.93151, 0.64308},
1051 {0.16319, 0.93609, 0.63137},
1052 {0.17377, 0.94053, 0.61938},
1053 {0.18491, 0.94484, 0.60713},
1054 {0.19659, 0.94901, 0.59466},
1055 {0.20877, 0.95304, 0.58199},
1056 {0.22142, 0.95692, 0.56914},
1057 {0.23449, 0.96065, 0.55614},
1058 {0.24797, 0.96423, 0.54303},
1059 {0.26180, 0.96765, 0.52981},
1060 {0.27597, 0.97092, 0.51653},
1061 {0.29042, 0.97403, 0.50321},
1062 {0.30513, 0.97697, 0.48987},
1063 {0.32006, 0.97974, 0.47654},
1064 {0.33517, 0.98234, 0.46325},
1065 {0.35043, 0.98477, 0.45002},
1066 {0.36581, 0.98702, 0.43688},
1067 {0.38127, 0.98909, 0.42386},
1068 {0.39678, 0.99098, 0.41098},
1069 {0.41229, 0.99268, 0.39826},
1070 {0.42778, 0.99419, 0.38575},
1071 {0.44321, 0.99551, 0.37345},
1072 {0.45854, 0.99663, 0.36140},
1073 {0.47375, 0.99755, 0.34963},
1074 {0.48879, 0.99828, 0.33816},
1075 {0.50362, 0.99879, 0.32701},
1076 {0.51822, 0.99910, 0.31622},
1077 {0.53255, 0.99919, 0.30581},
1078 {0.54658, 0.99907, 0.29581},
1079 {0.56026, 0.99873, 0.28623},
1080 {0.57357, 0.99817, 0.27712},
1081 {0.58646, 0.99739, 0.26849},
1082 {0.59891, 0.99638, 0.26038},
1083 {0.61088, 0.99514, 0.25280},
1084 {0.62233, 0.99366, 0.24579},
1085 {0.63323, 0.99195, 0.23937},
1086 {0.64362, 0.98999, 0.23356},
1087 {0.65394, 0.98775, 0.22835},
1088 {0.66428, 0.98524, 0.22370},
1089 {0.67462, 0.98246, 0.21960},
1090 {0.68494, 0.97941, 0.21602},
1091 {0.69525, 0.97610, 0.21294},
1092 {0.70553, 0.97255, 0.21032},
1093 {0.71577, 0.96875, 0.20815},
1094 {0.72596, 0.96470, 0.20640},
1095 {0.73610, 0.96043, 0.20504},
1096 {0.74617, 0.95593, 0.20406},
1097 {0.75617, 0.95121, 0.20343},
1098 {0.76608, 0.94627, 0.20311},
1099 {0.77591, 0.94113, 0.20310},
1100 {0.78563, 0.93579, 0.20336},
1101 {0.79524, 0.93025, 0.20386},
1102 {0.80473, 0.92452, 0.20459},
1103 {0.81410, 0.91861, 0.20552},
1104 {0.82333, 0.91253, 0.20663},
1105 {0.83241, 0.90627, 0.20788},
1106 {0.84133, 0.89986, 0.20926},
1107 {0.85010, 0.89328, 0.21074},
1108 {0.85868, 0.88655, 0.21230},
1109 {0.86709, 0.87968, 0.21391},
1110 {0.87530, 0.87267, 0.21555},
1111 {0.88331, 0.86553, 0.21719},
1112 {0.89112, 0.85826, 0.21880},
1113 {0.89870, 0.85087, 0.22038},
1114 {0.90605, 0.84337, 0.22188},
1115 {0.91317, 0.83576, 0.22328},
1116 {0.92004, 0.82806, 0.22456},
1117 {0.92666, 0.82025, 0.22570},
1118 {0.93301, 0.81236, 0.22667},
1119 {0.93909, 0.80439, 0.22744},
1120 {0.94489, 0.79634, 0.22800},
1121 {0.95039, 0.78823, 0.22831},
1122 {0.95560, 0.78005, 0.22836},
1123 {0.96049, 0.77181, 0.22811},
1124 {0.96507, 0.76352, 0.22754},
1125 {0.96931, 0.75519, 0.22663},
1126 {0.97323, 0.74682, 0.22536},
1127 {0.97679, 0.73842, 0.22369},
1128 {0.98000, 0.73000, 0.22161},
1129 {0.98289, 0.72140, 0.21918},
1130 {0.98549, 0.71250, 0.21650},
1131 {0.98781, 0.70330, 0.21358},
1132 {0.98986, 0.69382, 0.21043},
1133 {0.99163, 0.68408, 0.20706},
1134 {0.99314, 0.67408, 0.20348},
1135 {0.99438, 0.66386, 0.19971},
1136 {0.99535, 0.65341, 0.19577},
1137 {0.99607, 0.64277, 0.19165},
1138 {0.99654, 0.63193, 0.18738},
1139 {0.99675, 0.62093, 0.18297},
1140 {0.99672, 0.60977, 0.17842},
1141 {0.99644, 0.59846, 0.17376},
1142 {0.99593, 0.58703, 0.16899},
1143 {0.99517, 0.57549, 0.16412},
1144 {0.99419, 0.56386, 0.15918},
1145 {0.99297, 0.55214, 0.15417},
1146 {0.99153, 0.54036, 0.14910},
1147 {0.98987, 0.52854, 0.14398},
1148 {0.98799, 0.51667, 0.13883},
1149 {0.98590, 0.50479, 0.13367},
1150 {0.98360, 0.49291, 0.12849},
1151 {0.98108, 0.48104, 0.12332},
1152 {0.97837, 0.46920, 0.11817},
1153 {0.97545, 0.45740, 0.11305},
1154 {0.97234, 0.44565, 0.10797},
1155 {0.96904, 0.43399, 0.10294},
1156 {0.96555, 0.42241, 0.09798},
1157 {0.96187, 0.41093, 0.09310},
1158 {0.95801, 0.39958, 0.08831},
1159 {0.95398, 0.38836, 0.08362},
1160 {0.94977, 0.37729, 0.07905},
1161 {0.94538, 0.36638, 0.07461},
1162 {0.94084, 0.35566, 0.07031},
1163 {0.93612, 0.34513, 0.06616},
1164 {0.93125, 0.33482, 0.06218},
1165 {0.92623, 0.32473, 0.05837},
1166 {0.92105, 0.31489, 0.05475},
1167 {0.91572, 0.30530, 0.05134},
1168 {0.91024, 0.29599, 0.04814},
1169 {0.90463, 0.28696, 0.04516},
1170 {0.89888, 0.27824, 0.04243},
1171 {0.89298, 0.26981, 0.03993},
1172 {0.88691, 0.26152, 0.03753},
1173 {0.88066, 0.25334, 0.03521},
1174 {0.87422, 0.24526, 0.03297},
1175 {0.86760, 0.23730, 0.03082},
1176 {0.86079, 0.22945, 0.02875},
1177 {0.85380, 0.22170, 0.02677},
1178 {0.84662, 0.21407, 0.02487},
1179 {0.83926, 0.20654, 0.02305},
1180 {0.83172, 0.19912, 0.02131},
1181 {0.82399, 0.19182, 0.01966},
1182 {0.81608, 0.18462, 0.01809},
1183 {0.80799, 0.17753, 0.01660},
1184 {0.79971, 0.17055, 0.01520},
1185 {0.79125, 0.16368, 0.01387},
1186 {0.78260, 0.15693, 0.01264},
1187 {0.77377, 0.15028, 0.01148},
1188 {0.76476, 0.14374, 0.01041},
1189 {0.75556, 0.13731, 0.00942},
1190 {0.74617, 0.13098, 0.00851},
1191 {0.73661, 0.12477, 0.00769},
1192 {0.72686, 0.11867, 0.00695},
1193 {0.71692, 0.11268, 0.00629},
1194 {0.70680, 0.10680, 0.00571},
1195 {0.69650, 0.10102, 0.00522},
1196 {0.68602, 0.09536, 0.00481},
1197 {0.67535, 0.08980, 0.00449},
1198 {0.66449, 0.08436, 0.00424},
1199 {0.65345, 0.07902, 0.00408},
1200 {0.64223, 0.07380, 0.00401},
1201 {0.63082, 0.06868, 0.00401},
1202 {0.61923, 0.06367, 0.00410},
1203 {0.60746, 0.05878, 0.00427},
1204 {0.59550, 0.05399, 0.00453},
1205 {0.58336, 0.04931, 0.00486},
1206 {0.57103, 0.04474, 0.00529},
1207 {0.55852, 0.04028, 0.00579},
1208 {0.54583, 0.03593, 0.00638},
1209 {0.53295, 0.03169, 0.00705},
1210 {0.51989, 0.02756, 0.00780},
1211 {0.50664, 0.02354, 0.00863},
1212 {0.49321, 0.01963, 0.00955},
1213 {0.47960, 0.01583, 0.01055},
1214 };
1215 constexpr int n = 256;
1216 double idx = t * (n - 1);
1217 int i = std::min(static_cast<int>(idx), n - 2);
1218 double frac = idx - i;
1219 colour.red = turbo[i][0] + frac * (turbo[i+1][0] - turbo[i][0]);
1220 colour.green = turbo[i][1] + frac * (turbo[i+1][1] - turbo[i][1]);
1221 colour.blue = turbo[i][2] + frac * (turbo[i+1][2] - turbo[i][2]);
1222 break;
1223 }
1224 default: {
1225 colour.red = t;
1226 colour.green = t;
1227 colour.blue = t;
1228 break;
1229 }
1230 }
1231
1232 return colour;
1233 };
1234
1235 for (integer ifreq = 1; ifreq <= nf; ifreq ++)
1236 for (integer itime = 1; itime <= nt; itime ++)
1237 colourMatrix [ifreq] [itime] = getColour (part [ifreq] [itime]);
1238
1239 Graphics_image_colour (g, colourMatrix.get(),
1240 Matrix_columnToX (me, itmin - 0.5),
1241 Matrix_columnToX (me, itmax + 0.5),
1242 Matrix_rowToY (me, ifmin - 0.5),
1243 Matrix_rowToY (me, ifmax + 0.5),
1244 min_val, max_val
1245 );
1246 }
1247 }
1248
1249 void SpectrogramList_paintInside (const constSpectrogramList me, const Graphics g,
1250 /* mutable autowindow */ double tmin, /* mutable autowindow */ double tmax,
1251 /* mutable autowindow */ double fmin, /* mutable autowindow */ double fmax,
1252 /* mutable autoscaling */ double maximum,
1253 const bool autoscaling,
1254 const double dynamic,
1255 const double preemphasis_dbPerOctave,
1256 const double dynamicCompression,
1257 kSpectrogram_colourMap colourMap,
1258 bool invertColours
1259 ) {
1260 Spectrogram thee = my at [1];
1261 Melder_assert (thee);
1262 if (my size != 3) {
1263 Spectrogram_paintInside (thee, g, tmin, tmax, fmin, fmax, maximum, autoscaling,
1264 dynamic, preemphasis_dbPerOctave, dynamicCompression, colourMap, invertColours);
1265 return;
1266 }
1267 Function_unidirectionalAutowindow (thee, & tmin, & tmax);
1268 SampledXY_unidirectionalAutowindowY (thee, & fmin, & fmax);
1269 integer itmin, itmax, ifmin, ifmax;
1270 const integer nt = Matrix_getWindowSamplesX (thee, tmin - 0.49999 * thy dx, tmax + 0.49999 * thy dx, & itmin, & itmax);
1271 const integer nf = Matrix_getWindowSamplesY (thee, fmin - 0.49999 * thy dy, fmax + 0.49999 * thy dy, & ifmin, & ifmax);
1272 if (nt == 0 || nf == 0)
1273 return;
1274 Graphics_setWindow (g, tmin, tmax, fmin, fmax);
1275 autoVEC dynamicFactors = zero_VEC (nt);
1276 autoMAT part [1+3];
1277
1278 if (autoscaling)
1279 maximum = 0.0;
1280 for (integer ichan = 1; ichan <= 3; ichan ++) {
1281 Spectrogram she = my at [ichan];
1282 Melder_assert (she);
1283 part [ichan] = part_MAT (her z.get(), ifmin, ifmax, itmin, itmax);
1284 Melder_assert (part [ichan].nrow != 0 && part [ichan].ncol != 0);
1285 const integer ifshift = ifmin - 1;
1286 /*
1287 Pre-emphasis in place; also compute maximum after pre-emphasis.
1288 */
1289 for (integer ifreq = 1; ifreq <= nf; ifreq ++) {
1290 const double frequency = Matrix_rowToY (she, ifreq + ifshift);
1291 /*
1292 The preemphasis term for a frequency of 1000 Hz is always 0 dB;
1293 the preemphasis term (in dB) for a frequency f is then
1294 preemphasis_dbPerOctave * log2 (f / 1000 Hz)
1295 To speed this up, we convert to natural logarithms, according to
1296 log2 (x) = ln (x) / ln (2)
1297 Before taking the logarithm, we add a tiny number, namely 1e-308,
1298 so that for a frequency of 0 Hz the preemphasis term is typically 6.0 * log2 (1e-308) = -6139 dB,
1299 or 0 dB in case the preemphasis is 0 dB/octave.
1300 For purposes of preemphasis, negative frequencies (which don't normally occur) are treated as 0 Hz.
1301 */
1302 const double preemphasisTerm_db = (preemphasis_dbPerOctave / NUMln2) * log (Melder_clippedLeft (0.0, frequency) / 1000.0 + 1e-308);
1303 for (integer itime = 1; itime <= nt; itime ++) {
1304 const double psd_pascal2PerHz = part [ichan] [ifreq] [itime];
1305 /*
1306 The power spectral density in dB is
1307 psd_db = 10.0 * log10 (psd_pascal2PerHz / referencePsd_pascal2)
1308 To speed this up, we convert to natural logarithms, according to
1309 log10 (x) = ln (x) / ln (10)
1310 Before taking the logarithm, we add a tiny number, namely 1e-308,
1311 so that in silence the PSD (before adding preemphasis) is -3080 dB;
1312 this becomes visible if you `Paint` the Spectrogram under the following settings:
1313 - `autoscaling` off
1314 - `maximum` 0.0 dB/Hz
1315 - `pre-emphasis` 0 dB/oct
1316 - `dynamic compression` 0
1317 Then when the `dynamic range` passes 3080 dB, you will start to see some light grey.
1318 */
1319 constexpr double oneByReferencePsd_pascal2 = 1.0 / 4.0e-10;
1320 const double psd_db = (10.0/NUMln10) * log (oneByReferencePsd_pascal2 * psd_pascal2PerHz + 1e-308) + preemphasisTerm_db;
1321 if (psd_db > dynamicFactors [itime])
1322 dynamicFactors [itime] = psd_db; // local maximum over all channels (so that the relative strengths of the channels are preserved)
1323 part [ichan] [ifreq] [itime] = psd_db;
1324 }
1325 }
1326 /*
1327 Compute global maximum (over all times and channels).
1328 */
1329 if (autoscaling) {
1330 for (integer itime = 1; itime <= nt; itime ++)
1331 if (dynamicFactors [itime] > maximum)
1332 maximum = dynamicFactors [itime];
1333 }
1334 }
1335 /*
1336 Dynamic compression in place.
1337 */
1338 for (integer itime = 1; itime <= nt; itime ++)
1339 dynamicFactors [itime] = dynamicCompression * (maximum - dynamicFactors [itime]);
1340 for (integer ichan = 1; ichan <= 3; ichan ++)
1341 for (integer itime = 1; itime <= nt; itime ++)
1342 for (integer ifreq = 1; ifreq <= nf; ifreq ++)
1343 part [ichan] [ifreq] [itime] += dynamicFactors [itime];
1344
1345 automatrix <MelderColour> colourMatrix (nf, nt, MelderArray::kInitializationType::ZERO);
1346 for (integer irow = 1; irow <= nf; irow ++) {
1347 for (integer icol = 1; icol <= nt; icol ++) {
1348 colourMatrix [irow] [icol]. red = 1.0 - (part [1] [irow] [icol] - (maximum - dynamic)) / dynamic;
1349 colourMatrix [irow] [icol]. green = 1.0 - (part [2] [irow] [icol] - (maximum - dynamic)) / dynamic;
1350 colourMatrix [irow] [icol]. blue = 1.0 - (part [3] [irow] [icol] - (maximum - dynamic)) / dynamic;
1351 }
1352 }
1353 Graphics_image_colour (g, colourMatrix.all(),
1354 Matrix_columnToX (thee, itmin - 0.5),
1355 Matrix_columnToX (thee, itmax + 0.5),
1356 Matrix_rowToY (thee, ifmin - 0.5),
1357 Matrix_rowToY (thee, ifmax + 0.5),
1358 maximum - dynamic /* ignored */, maximum /* ignored */
1359 );
1360 }
1361
1362 void Spectrogram_paint (const constSpectrogram me, const Graphics g,
1363 const double tmin, const double tmax, const double fmin, const double fmax,
1364 const double maximum, const bool autoscaling,
1365 const double dynamic, const double preemphasis, const double dynamicCompression,
1366 kSpectrogram_colourMap colourMap, bool invertColours,
1367 const bool garnish)
1368 {
1369 Graphics_setInner (g);
1370 Spectrogram_paintInside (me, g, tmin, tmax, fmin, fmax, maximum, autoscaling, dynamic, preemphasis, dynamicCompression, colourMap, invertColours);
1371 Graphics_unsetInner (g);
1372 if (garnish) {
1373 Graphics_drawInnerBox (g);
1374 Graphics_textBottom (g, true, U"Time (s)");
1375 Graphics_marksBottom (g, 2, true, true, false);
1376 Graphics_marksLeft (g, 2, true, true, false);
1377 Graphics_textLeft (g, true, U"Frequency (Hz)");
1378 }
1379 }
1380
1381 void SpectrogramList_paint (const constSpectrogramList me, const Graphics g,
1382 const double tmin, const double tmax, const double fmin, const double fmax,
1383 const double maximum, const bool autoscaling,
1384 const double dynamic, const double preemphasis, const double dynamicCompression,
1385 kSpectrogram_colourMap colourMap, bool invertColours,
1386 const bool garnish)
1387 {
1388 Graphics_setInner (g);
1389 SpectrogramList_paintInside (me, g, tmin, tmax, fmin, fmax, maximum, autoscaling, dynamic, preemphasis, dynamicCompression, colourMap, invertColours);
1390 Graphics_unsetInner (g);
1391 if (garnish) {
1392 Graphics_drawInnerBox (g);
1393 Graphics_textBottom (g, true, U"Time (s)");
1394 Graphics_marksBottom (g, 2, true, true, false);
1395 Graphics_marksLeft (g, 2, true, true, false);
1396 Graphics_textLeft (g, true, U"Frequency (Hz)");
1397 }
1398 }
1399
1400 autoSpectrogram Matrix_to_Spectrogram (constMatrix me) {
1401 try {
1402 autoSpectrogram thee = Spectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
1403 thy z.all() <<= my z.all();
1404 return thee;
1405 } catch (MelderError) {
1406 Melder_throw (me, U": not converted to Spectrogram.");
1407 }
1408 }
1409
1410 autoMatrix Spectrogram_to_Matrix (constSpectrogram me) {
1411 try {
1412 autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
1413 thy z.all() <<= my z.all();
1414 return thee;
1415 } catch (MelderError) {
1416 Melder_throw (me, U": not converted to Matrix.");
1417 }
1418 }
1419
1420 Thing_implement (SpectrogramList, Ordered, 0);
1421
1422 /* End of Spectrogram.cpp */