Debian Codegraph @

dwtools/Sound_and_Spectrum_dft.cpp

raw symbols SCIP
1 /* Sound_and_Spectrum_dft.cpp
2 *
3 * Copyright (C) 2021 David Weenink, Paul Boersma
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 "Sound_and_Spectrum_dft.h"cxx . . $ `<file>/dwtools/Sound_and_Spectrum_dft.h`/
20 #include "Sound_and_Spectrum.h"cxx . . $ `<file>/fon/Sound_and_Spectrum.h`/
21
22 autoSpectrumcxx . . $ autoSpectrum# Sound_to_Spectrum_resampledcxx . . $ Sound_to_Spectrum_resampled(9be288762b497db2). (Soundcxx . . $ Sound# me, integercxx . . $ integer# interpolationDepth) {
23 const double xmin_saved = mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., xmax_saved = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.;
24 try {
25 const integercxx . . $ integer# fftNumberOfSamples = Melder_iroundUpToPowerOfTwocxx . . $ Melder_iroundUpToPowerOfTwo(8ce0d1b217db7fd1). (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.);
26 if (fftNumberOfSamples == mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.)
27 return Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (me, true); // FFT without resampling
28 const double samplingFrequency = 1.0 / mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
29 const double df = samplingFrequency / mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.;
30 const double upSamplingFrequency = fftNumberOfSamples * df;
31 /*
32 Temporary domain correction:
33
34 For a sound with 44100 Hz sampling frequency, xmin = 0.0, xmax = 0.02531645569620253 and nx = 1116,
35 we would calculate fftNumberOfSamples as 2048.
36 Sound_resample would calculate numberOfValues as Melder_iround ((my xmax - my xmin) * newSamplingFrequency
37 which gives 2049. We have to make sure that these two different calculation result in the same number.
38 We can do this by adapting the sound's domain temporarily.
39 */
40 mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. = 0.0;
41 mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. * mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
42 autoSoundcxx . . $ autoSound# resampled = Sound_resamplecxx . . $ Sound_resample(872961da5f5e69e5). (me, upSamplingFrequency, interpolationDepth);
43 mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. = xmin_saved;
44 mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. = xmax_saved;
45 autoSpectrumcxx . . $ autoSpectrum# extendedSpectrum = Sound_to_Spectrumcxx . . $ Sound_to_Spectrum(e96f4d575d8eeb8c). (resampled.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), true); // FFT after resampling
46 const integercxx . . $ integer# numberOfFrequencies = mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. / 2 + 1;
47 autoSpectrumcxx . . $ autoSpectrum# thee = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (0.5 * samplingFrequency, numberOfFrequencies);
48 thycxx . . $ `melder/melder.h:45:9`! dxcxx . . $ structSampled#dx. = df; // override, just in case my nx is odd
49 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z..getcxx . . $ automatrix#get(b2b81450b5bdda66).() <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). extendedSpectrumcxx . . $ constmatrixview#constmatrixview(c98a2930b9469e58). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..partcxx . . $ automatrix#part(5c23e73480cfacfb). (1, 2, 1, numberOfFrequencies);
50 if (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. % 2 == 0)
51 thycxx . . $ `melder/melder.h:45:9`! zcxx . . $ structMatrix#z. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).2] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).numberOfFrequencies] = 0.0; // set imaginary value at Nyquist to zero
52 return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
53 } catch (MelderErrorcxx . . $ MelderError#) {
54 mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. = xmin_saved;
55 mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. = xmax_saved;
56 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": could not convert to Spectrum by resampling.");
57 }
58 }
59
60 autoSoundcxx . . $ autoSound# Spectrum_to_Sound_resampledcxx . . $ Spectrum_to_Sound_resampled(97494362fabfa5fa). (Spectrumcxx . . $ Spectrum# me, integercxx . . $ integer# interpolationDepth) {
61 try {
62 const integercxx . . $ integer# fftNumberOfSamples = Melder_iroundUpToPowerOfTwocxx . . $ Melder_iroundUpToPowerOfTwo(8ce0d1b217db7fd1). (mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1);
63 autoSoundcxx . . $ autoSound# theecxx . . $ autoSomeThing#autoSomeThing(49f6e7a06ebc5aa8).;
64 if (fftNumberOfSamples == mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx. - 1)
65 return Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (me); // FFT without resampling
66 const integercxx . . $ integer# newNumberOfFrequencies = fftNumberOfSamples + 1;
67 autoSpectrumcxx . . $ autoSpectrum# extendedSpectrum = Spectrum_createcxx . . $ Spectrum_create(a338b9c326abb5ed). (mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., newNumberOfFrequencies);
68 extendedSpectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). zcxx . . $ structMatrix#z..partcxx . . $ automatrix#part(5c23e73480cfacfb). (1, 2, 1, mycxx . . $ `melder/melder.h:44:9`! nxcxx . . $ structSampled#nx.) <<=cxx . . $ `operator<<=`(a7d93ba40fe22fe0). mycxx . . $ `melder/melder.h:44:9`! zcxx . . $ structMatrix#z..getcxx . . $ automatrix#get(b2b81450b5bdda66).();
69 extendedSpectrum ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). dxcxx . . $ structSampled#dx. = mycxx . . $ `melder/melder.h:44:9`! dxcxx . . $ structSampled#dx.;
70 autoSoundcxx . . $ autoSound# upsampled = Spectrum_to_Soundcxx . . $ Spectrum_to_Sound(b9577246d0f6b8d7). (extendedSpectrum.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).());
71 return Sound_resamplecxx . . $ Sound_resample(872961da5f5e69e5). (upsampled.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), Melder_iroundcxx . . $ `melder/melder_ftoi.h:78:9`! (2.0 * mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.), interpolationDepth);
72 } catch (MelderErrorcxx . . $ MelderError#) {
73 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": could not convert to Sound by resampling.");
74 }
75 }
76
77 /* End of file Sound_and_Spectrum_dft.cpp */