Debian Codegraph @

dwtools/Spectrum_and_MultiSampledSpectrogram.cpp

raw symbols SCIP
1 /* Spectrum_and_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 "Spectrum_and_AnalyticSound.h"
20 #include "Spectrum_and_MultiSampledSpectrogram.h"
21 #include "Sound_and_Spectrum.h"
22 #include "Sound_extensions.h"
23 #include "Spectrum_extensions.h"
24 #include "NUM2.h"
25
26 void Spectrum_into_MultiSampledSpectrogram (Spectrum me, MultiSampledSpectrogram thee, double approximateTimeOverSampling,
27 kSound_windowShape filterShape)
28 {
29 try {
30 const integer maximumFilterSize = my nx;
31 autoVEC window = raw_VEC (maximumFilterSize);
32 thy numberOfSpectralValues = my nx;
33 thy frequencyAmplifications = zero_VEC (thy numberOfSpectralValues);
34 for (integer ifreq = 1; ifreq <= thy nx; ifreq ++) {
35 double spectrum_fmin, spectrum_fmax;
36 MultiSampledSpectrogram_getFrequencyBand (thee, ifreq, & spectrum_fmin, & spectrum_fmax);
37 integer spectrum_imin, spectrum_imax;
38 const integer numberOfSpectralValues = Sampled_getWindowSamples (me, spectrum_fmin, spectrum_fmax, & spectrum_imin, & spectrum_imax);
39 Melder_require (numberOfSpectralValues > 0,
40 U"The number of spectral values for bin number ", ifreq, U" should be larger than zero.");
41 window.resize (numberOfSpectralValues);
42 windowShape_into_VEC (filterShape, window.get());
43 autoAnalyticSound him = Spectrum_to_AnalyticSound_demodulateBand (me, spectrum_imin, spectrum_imax, approximateTimeOverSampling, window.get());
44 thy frequencyAmplifications.part (spectrum_imin, spectrum_imax) += window.get();
45 autoFrequencyBin bin = FrequencyBin_create (thy tmin, thy tmax, his nx, his dx, his x1);
46 bin -> z = his z.move();
47 thy frequencyBins. addItem_move (bin.move());
48 if (ifreq == 1) {
49 /*
50 DC_BIN:
51 Fill with values from 0 Hz to the mid of the first window.
52 Multiply with a window only the part that overlaps with the first half of the first window.
53 */
54 const integer numberOfRestSamples = Sampled_getWindowSamples (me, my xmin, 0.5 * (spectrum_fmin + spectrum_fmax), & spectrum_imin, & spectrum_imax);
55 const integer newWindowSize = std::min (numberOfRestSamples, window.size / 2);
56 VEC partialWindow = window.part (window.size - newWindowSize + 1, window.size);
57 him = Spectrum_to_AnalyticSound_demodulateBand (me, spectrum_imin, spectrum_imax, approximateTimeOverSampling, partialWindow);
58 thy frequencyAmplifications.part (spectrum_imax - newWindowSize + 1, spectrum_imax) += partialWindow;
59 if (numberOfRestSamples > newWindowSize)
60 thy frequencyAmplifications.part (1, spectrum_imax - newWindowSize ) += 1.0;
61 thy zeroBin = FrequencyBin_create (thy tmin, thy tmax, his nx, his dx, his x1);
62 thy zeroBin -> z = his z.move();
63 }
64 if (ifreq == thy nx) {
65 /*
66 NYQUIST_BIN:
67 Fill with the part from the mid of the last window to the end.
68 Multiply with a window only the part that overlaps with the last half of the last window.
69 */
70 const integer numberOfRestSamples = Sampled_getWindowSamples (me, 0.5 * (spectrum_fmin + spectrum_fmax), my xmax, & spectrum_imin, & spectrum_imax);
71 const integer newWindowSize = std::min (numberOfRestSamples, window.size / 2);
72 VEC partialWindow = window.part (1, newWindowSize);
73 him = Spectrum_to_AnalyticSound_demodulateBand (me, spectrum_imin, spectrum_imax, approximateTimeOverSampling, partialWindow);
74 thy frequencyAmplifications.part (spectrum_imin, spectrum_imin + newWindowSize - 1) += partialWindow;
75 if (numberOfRestSamples > newWindowSize)
76 thy frequencyAmplifications.part (spectrum_imin + newWindowSize, spectrum_imax) += 1.0;
77 thy nyquistBin = FrequencyBin_create (thy tmin, thy tmax, his nx, his dx, his x1);
78 thy nyquistBin -> z = his z.move();
79 }
80 }
81 Melder_assert (thy frequencyBins.size == thy nx); // check invariant
82 } catch (MelderError) {
83 Melder_throw (me, U": cannot calculate MultiSampledSpectrogram.");
84 }
85 }
86
87
88 autoSpectrum MultiSampledSpectrogram_to_Spectrum (MultiSampledSpectrogram me) {
89 try {
90 const double duration = my tmax - my tmin;
91 const double nyquistFrequency = my v_myFrequencyUnitToHertz (my xmax);
92 const double samplingFrequency = 2.0 * nyquistFrequency;
93 const integer numberOfSamples = Melder_iround (duration * samplingFrequency);
94 const integer numberOfFFTSamples = Melder_clippedLeft (2_integer, Melder_iroundUpToPowerOfTwo (numberOfSamples));
95 const integer numberOfSpectralValues = numberOfFFTSamples / 2 + 1;
96 Melder_assert (numberOfSpectralValues == my numberOfSpectralValues);
97 autoSpectrum thee = Spectrum_create (nyquistFrequency, numberOfSpectralValues);
98 for (integer ifreq = 1; ifreq <= my nx; ifreq ++) {
99 integer iflow, ifhigh;
100 const FrequencyBin frequencyBin = my frequencyBins.at [ifreq];
101 double flow, fhigh;
102 MultiSampledSpectrogram_getFrequencyBand (me, ifreq, & flow, & fhigh);
103 auto fillSpectrumPart = [&] (FrequencyBin fbin) {
104 autoSound sound = FrequencyBin_to_Sound (fbin);
105 autoSpectrum filter = Sound_to_Spectrum (sound.get(), false);
106 (void) Sampled_getWindowSamples (thee.get(), flow, fhigh, & iflow, & ifhigh);
107 const integer extraSample = ( iflow > 1 ? 1 : 0 );
108 thy z.part (1, 2, iflow, ifhigh) += filter -> z.part (1, 2, 1 + extraSample, ifhigh - iflow + 1 + extraSample);
109 };
110 fillSpectrumPart (frequencyBin);
111 if (ifreq == 1) {
112 fhigh = 0.5 * (flow + fhigh);
113 flow = 0.0;
114 fillSpectrumPart (my zeroBin.get());
115 }
116 if (ifreq == my nx) {
117 flow = 0.5 * (flow + fhigh);
118 fhigh = my v_myFrequencyUnitToHertz (my xmax);
119 fillSpectrumPart (my nyquistBin.get());
120 }
121 }
122 /*
123 Make sure the imaginary part of the last spectral value is zero.
124 */
125 thy z [2] [numberOfSpectralValues] = 0.0;
126 /*
127 Correct for windowing effects
128 */
129 for (integer ifreq = 1; ifreq <= my frequencyAmplifications.size; ifreq ++)
130 if (my frequencyAmplifications [ifreq] > 0.0) {
131 thy z [1] [ifreq] /= my frequencyAmplifications [ifreq];
132 thy z [2] [ifreq] /= my frequencyAmplifications [ifreq];
133 }
134
135 return thee;
136 } catch (MelderError) {
137 Melder_throw (me, U": cannot convert to Spectrum.");
138 }
139 }
140
141 autoSpectrum Sound_and_MultiSampledSpectrogram_to_Spectrum (Sound me, MultiSampledSpectrogram thee) {
142 try {
143 autoSpectrum him;
144 const double nyquistFrequency = 0.5 / my dx;
145 const double maximumFrequency = thy v_myFrequencyUnitToHertz (thy xmax);
146 if (maximumFrequency < nyquistFrequency) {
147 autoSound resampled = Sound_resample (me, 2.0 * maximumFrequency, 50);
148 him = Sound_to_Spectrum (resampled.get(), true);
149 } else {
150 him = Sound_to_Spectrum (me, true);
151 }
152 return him;
153 } catch (MelderError) {
154 Melder_throw (me, U" and", thee, U" cannot create a Spectrum.");
155 }
156 }
157
158 autoSound MultiSampledSpectrogram_to_Sound_frequencyBin (MultiSampledSpectrogram me, integer frequencyBinNumber) {
159 try {
160 Melder_require (frequencyBinNumber > 0 && frequencyBinNumber <= my nx,
161 U"The frequency bin number should be in the interval from 1 to ", my nx, U".");
162 FrequencyBin frequencyBin = my frequencyBins.at [frequencyBinNumber];
163 return FrequencyBin_to_Sound (frequencyBin);
164 } catch (MelderError) {
165 Melder_throw (me, U": could not create Sound from frequency bin ", frequencyBinNumber, U".");
166 }
167 }
168
169
170 autoAnalyticSound MultiSampledSpectrogram_to_AnalyticSound_frequencyBin (MultiSampledSpectrogram me, integer frequencyBinNumber) {
171 try {
172 Melder_require (frequencyBinNumber > 0 && frequencyBinNumber <= my nx,
173 U"The frequency bin number should be in the interval from 1 to ", my nx, U".");
174 FrequencyBin frequencyBin = my frequencyBins.at [frequencyBinNumber];
175 return FrequencyBin_to_AnalyticSound (frequencyBin);
176 } catch (MelderError) {
177 Melder_throw (me, U": could not create Sound from frequency bin ", frequencyBinNumber, U".");
178 }
179 }
180
181 /* End of file Spectrum_and_MultiSampledSpectrogram.cpp */