Debian Codegraph @

dwtools/MultiSampledSpectrogram.cpp

raw symbols SCIP
1 /* MultiSampledSpectrogram.cpp
2 *
3 * Copyright (C) 2021-2022 David Weenink
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "Collection.h"
20 #include "Formula.h"
21 #include "MultiSampledSpectrogram.h"
22
23 #include "oo_DESTROY.h"
24 #include "MultiSampledSpectrogram_def.h"
25 #include "oo_COPY.h"
26 #include "MultiSampledSpectrogram_def.h"
27 #include "oo_EQUAL.h"
28 #include "MultiSampledSpectrogram_def.h"
29 #include "oo_CAN_WRITE_AS_ENCODING.h"
30 #include "MultiSampledSpectrogram_def.h"
31 #include "oo_WRITE_TEXT.h"
32 #include "MultiSampledSpectrogram_def.h"
33 #include "oo_READ_TEXT.h"
34 #include "MultiSampledSpectrogram_def.h"
35 #include "oo_WRITE_BINARY.h"
36 #include "MultiSampledSpectrogram_def.h"
37 #include "oo_READ_BINARY.h"
38 #include "MultiSampledSpectrogram_def.h"
39 #include "oo_DESCRIPTION.h"
40 #include "MultiSampledSpectrogram_def.h"
41
42 Thing_implement (FrequencyBin, Matrix, 2);
43
44 double structFrequencyBin :: v_getValueAtSample (integer iframe, integer which, int unit) const {
45 if (unit == 0)
46 return ( which == 1 ? z [1] [iframe] : which == 2 ? z [2] [iframe] : undefined );
47 const double power = sqr (z [1] [iframe]) + sqr (z [2] [iframe]);
48 return ( unit == 1 ? power : unit == 2 ? 10.0 * log10 ((power + 1e-30) / 4e-10) : undefined );
49 }
50
51 Thing_implement_pureVirtual (MultiSampledSpectrogram, Sampled, 0);
52
53 void structMultiSampledSpectrogram :: v1_info () {
54 structDaata :: v1_info ();
55 MelderInfo_writeLine (U"Minimum frequency (Hz): ", v_myFrequencyUnitToHertz (xmin), U" Hz");
56 MelderInfo_writeLine (U"Maximum frequency (Hz): ", v_myFrequencyUnitToHertz (xmax), U" Hz");
57 MelderInfo_writeLine (U"First frequency (Hz): ", v_myFrequencyUnitToHertz (x1), U" Hz");
58 const integer numberOfFrequencies = frequencyBins.size;
59 MelderInfo_writeLine (U"Number of frequencies: ", numberOfFrequencies);
60 MelderInfo_writeLine (U"Number of frames in frequency bin 1: ", frequencyBins.at [1] -> nx);
61 MelderInfo_writeLine (U"Number of frames in frequency bin ", numberOfFrequencies, U": ", frequencyBins.at [numberOfFrequencies] -> nx);
62 MelderInfo_writeLine (U"Number of frames in all bins: ", MultiSampledSpectrogram_getNumberOfFrames (this));
63 }
64
65 double structMultiSampledSpectrogram :: v_getValueAtSample (integer ifreq, integer iframe , int unit) const {
66 FrequencyBin bin = frequencyBins.at [ifreq];
67 const double value = bin -> v_getValueAtSample (iframe, 1, unit);
68 return ( isdefined (value) ? our v_convertStandardToSpecialUnit (value, iframe, unit) : undefined );
69 }
70
71 double structMultiSampledSpectrogram :: v_myFrequencyUnitToHertz (double f) const {
72 return f;
73 }
74
75 double structMultiSampledSpectrogram :: v_hertzToMyFrequencyUnit (double f) const {
76 return f;
77 }
78
79 autoFrequencyBin FrequencyBin_create (double tmin, double tmax, integer nx, double dx, double x1) {
80 try {
81 autoFrequencyBin me = Thing_new (FrequencyBin);
82 Matrix_init (me.get(), tmin, tmax, nx, dx, x1, 0.5, 2.5, 2, 1.0, 1.0);
83 return me;
84 } catch (MelderError) {
85 Melder_throw (U"FrequencyBin not created.");
86 }
87 }
88
89 void FrequencyBin_formula (FrequencyBin me, conststring32 formula, Interpreter interpreter) {
90 try {
91 Formula_compile (interpreter, me, formula, kFormula_EXPRESSION_TYPE_NUMERIC, true);
92 Formula_Result result;
93 for (integer irow = 1; irow <= my ny; irow ++)
94 for (integer ibin = 1; ibin <= my nx; ibin ++) {
95 Formula_run (irow, ibin, & result);
96 my z [irow] [ibin] = result. numericResult;
97 }
98 } catch (MelderError) {
99 Melder_throw (me, U": formula not completed.");
100 }
101 }
102
103
104 // TODO:multiple rows!
105 double FrequencyBin_getValueAtX (FrequencyBin me, double x, kVector_valueInterpolation valueInterpolationType) {
106 const double leftEdge = my x1 - 0.5 * my dx, rightEdge = leftEdge + my nx * my dx;
107 if (x < leftEdge || x > rightEdge)
108 return undefined;
109 const integer interpolationDepth = kVector_valueInterpolation_to_interpolationDepth (valueInterpolationType);
110 const double index_real = (x - my x1) / my dx + 1.0;
111 return NUM_interpolate_sinc (my z.row (1), index_real, interpolationDepth);
112 }
113
114 autoSound FrequencyBin_to_Sound (FrequencyBin me) {
115 try {
116 autoSound thee = Sound_create (1, my xmin, my xmax, my nx, my dx, my x1);
117 thy z.row (1) <<= my z.row (1);
118 return thee;
119 } catch (MelderError) {
120 Melder_throw (me, U": cannot convert to Sound.");
121 }
122 }
123
124 autoAnalyticSound FrequencyBin_to_AnalyticSound (FrequencyBin me) {
125 try {
126 autoAnalyticSound thee = AnalyticSound_create (my xmin, my xmax, my nx, my dx, my x1);
127 thy z.get() <<= my z.get();
128 return thee;
129 } catch (MelderError) {
130 Melder_throw (me, U": cannot convert to AnalyticSound.");
131 }
132 }
133
134 void MultiSampledSpectrogram_init (MultiSampledSpectrogram me, double tmin, double tmax, double fmin, double fmax,
135 integer numberOfFrequencies, double df, double f1, double frequencyResolutionInBins)
136 {
137 my tmin = tmin;
138 my tmax = tmax;
139 my frequencyResolutionInBins = frequencyResolutionInBins;
140 Sampled_init (me, fmin, fmax, numberOfFrequencies, df, f1);
141 }
142
143 void MultiSampledSpectrogram_getFrequencyBand (MultiSampledSpectrogram me, integer index, double *out_flow, double *out_fhigh) {
144 const double centreFrequency = Sampled_indexToX (me, index);
145 if (out_flow) {
146 double flow = my v_myFrequencyUnitToHertz (centreFrequency - my frequencyResolutionInBins * my dx);
147 Melder_clipLeft (0.0, & flow);
148 *out_flow = flow;
149 }
150 if (out_fhigh) {
151 const double maximumFrequency = my v_myFrequencyUnitToHertz (my xmax);
152 double fhigh = my v_myFrequencyUnitToHertz (centreFrequency + my frequencyResolutionInBins * my dx);
153 Melder_clipRight (& fhigh, maximumFrequency);
154 *out_fhigh = fhigh;
155 }
156 }
157
158 void MultiSampledSpectrogram_formula (MultiSampledSpectrogram me, conststring32 formula, Interpreter interpreter) {
159 try {
160 for (integer ifreq = 1; ifreq <= my nx; ifreq ++) {
161 FrequencyBin frequencyBin = my frequencyBins.at [ifreq];
162 FrequencyBin_formula (frequencyBin, formula, interpreter);
163 }
164 } catch (MelderError) {
165 Melder_throw (me, U": formula not completed.");
166 }
167 }
168
169 void MultiSampledSpectrogram_formula_part (MultiSampledSpectrogram me, double fromTime, double toTime, double fromFrequency, double toFrequency, conststring32 formula, Interpreter interpreter) {
170 try {
171 const double fx1 = my v_myFrequencyUnitToHertz (my x1);
172 const double fnyquist = my v_myFrequencyUnitToHertz (my xmax);
173 if (fromFrequency == toFrequency && fromFrequency == 0.0) {
174 fromFrequency = fx1;
175 toFrequency = fnyquist;
176 }
177 Melder_require (toFrequency >= fx1 && fromFrequency <= toFrequency && fromFrequency < fnyquist,
178 U"At least one of the frequencies needs to be in the interval from ", fx1, U" to ", fnyquist, U" Hz.");
179 Melder_clip (fx1, & fromFrequency, fnyquist);
180 Melder_clip (fx1, & toFrequency, fnyquist);
181 const double flow = my v_hertzToMyFrequencyUnit (fromFrequency);
182 const double fhigh = my v_hertzToMyFrequencyUnit (toFrequency);
183 integer iflow, ifhigh;
184 if (Sampled_getWindowSamples (me, flow, fhigh, & iflow, & ifhigh) > 0)
185 for (integer ifreq = iflow; ifreq <= ifhigh; ifreq ++) {
186 FrequencyBin frequencyBin = my frequencyBins.at [ifreq];
187 Matrix_formula_part (frequencyBin, fromTime, toTime, 0.5, 2.5, formula, interpreter, nullptr);
188 }
189 } catch (MelderError) {
190 Melder_throw (me, U": formula not completed on part.");
191 }
192 }
193
194 integer MultiSampledSpectrogram_getNumberOfFrames (MultiSampledSpectrogram me) {
195 double numberOfFrames = 0;
196 for (integer ifreq = 1; ifreq <= my nx; ifreq ++) {
197 FrequencyBin frequencyBin = my frequencyBins.at [ifreq];
198 numberOfFrames += frequencyBin -> nx;
199 }
200 return numberOfFrames;
201 }
202
203 void MultiSampledSpectrogram_paintInside (MultiSampledSpectrogram me, Graphics g, double tmin, double tmax, double fmin, double fmax, double dBRange) {
204 integer itmin, itmax, ifmin, ifmax;
205 if (tmax <= tmin) {
206 tmin = my tmin;
207 tmax = my tmax;
208 }
209 if (fmax <= fmin) {
210 fmax = my xmax;
211 fmin = my xmin;
212 }
213 if (Sampled_getWindowSamples (me, fmin, fmax, & ifmin, & ifmax) == 0)
214 return;
215 const integer maximumNumberOfTimeFrames = Sampled_getWindowSamples (my frequencyBins.at [ifmax], tmin, tmax, & itmin, & itmax);
216 if (maximumNumberOfTimeFrames == 0)
217 return;
218 Graphics_setWindow (g, tmin, tmax, fmin, fmax);
219 autoMAT p = raw_MAT (1, maximumNumberOfTimeFrames);
220 /*
221 Find maximum power. No need for logarithm in the test
222 */
223 MelderExtremaWithInit powerExtrema;
224 for (integer ifreq = ifmin; ifreq <= ifmax; ifreq ++) {
225 FrequencyBin frequencyBin = my frequencyBins.at [ifreq];
226 if (Sampled_getWindowSamples (frequencyBin, tmin, tmax, & itmin, & itmax) == 0)
227 continue;
228 for (integer iframe = itmin; iframe <= itmax; iframe ++) {
229 double powerdB = frequencyBin -> v_getValueAtSample (iframe, 0, 2); // 10*log10 (power/..)
230 powerExtrema.update (powerdB);
231 }
232 }
233 if (powerExtrema.max == 0.0)
234 return; // empty
235 const double maximum = powerExtrema.max;
236 const double minimum = std::max (maximum - dBRange, powerExtrema.min);
237 double tmin_previousBin = undefined, tmax_previousBin = undefined;
238 for (integer ifreq = ifmin; ifreq <= ifmax; ifreq ++) {
239 FrequencyBin frequencyBin = my frequencyBins.at [ifreq];
240 const double dx = frequencyBin -> dx;
241 const integer numberOfFrames = Sampled_getWindowSamples (
242 frequencyBin, tmin - 0.4999 * dx, tmax + 0.4999 * dx, & itmin, & itmax);
243 if (numberOfFrames == 0)
244 continue;
245 p.resize (1, numberOfFrames);
246 integer index = 0;
247 for (integer iframe = itmin; iframe <= itmax; iframe ++)
248 p [1] [ ++ index] = frequencyBin -> v_getValueAtSample (iframe, 0, 2);
249 double tmin_bin = Sampled_indexToX (frequencyBin, itmin) - 0.5 * dx;
250 double tmax_bin = Sampled_indexToX (frequencyBin, itmax) + 0.5 * dx;
251 Melder_clipRight (& tmin_bin, tmin_previousBin); // clip against previous
252 Melder_clipLeft (tmax_previousBin, & tmax_bin);
253 tmin_previousBin = tmin_bin;
254 tmax_previousBin = tmax_bin;
255 const double freq = Sampled_indexToX (me, ifreq);
256 const double ymin = freq - 0.5 * my dx, ymax = freq + 0.5 * my dx;
257 Graphics_image (g, p.get(), tmin_bin, tmax_bin, ymin, ymax, minimum, maximum);
258 }
259 }
260
261 /* End of file MultiSampledSpectrogram.cpp */