| 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" |
| 20 |
#include "Sound_and_Spectrum.h" |
| 21 |
|
| 22 |
autoSpectrum Sound_to_Spectrum_resampled (Sound me, integer interpolationDepth) { |
| 23 |
const double xmin_saved = my xmin, xmax_saved = my xmax; |
| 24 |
try { |
| 25 |
const integer fftNumberOfSamples = Melder_iroundUpToPowerOfTwo (my nx); |
| 26 |
if (fftNumberOfSamples == my nx) |
| 27 |
return Sound_to_Spectrum (me, true); // FFT without resampling |
| 28 |
const double samplingFrequency = 1.0 / my dx; |
| 29 |
const double df = samplingFrequency / my 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 |
my xmin = 0.0; |
| 41 |
my xmax = my nx * my dx; |
| 42 |
autoSound resampled = Sound_resample (me, upSamplingFrequency, interpolationDepth); |
| 43 |
my xmin = xmin_saved; |
| 44 |
my xmax = xmax_saved; |
| 45 |
autoSpectrum extendedSpectrum = Sound_to_Spectrum (resampled.get(), true); // FFT after resampling |
| 46 |
const integer numberOfFrequencies = my nx / 2 + 1; |
| 47 |
autoSpectrum thee = Spectrum_create (0.5 * samplingFrequency, numberOfFrequencies); |
| 48 |
thy dx = df; // override, just in case my nx is odd |
| 49 |
thy z.get() <<= extendedSpectrum -> z.part (1, 2, 1, numberOfFrequencies); |
| 50 |
if (my nx % 2 == 0) |
| 51 |
thy z [2] [numberOfFrequencies] = 0.0; // set imaginary value at Nyquist to zero |
| 52 |
return thee; |
| 53 |
} catch (MelderError) { |
| 54 |
my xmin = xmin_saved; |
| 55 |
my xmax = xmax_saved; |
| 56 |
Melder_throw (me, U": could not convert to Spectrum by resampling."); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
autoSound Spectrum_to_Sound_resampled (Spectrum me, integer interpolationDepth) { |
| 61 |
try { |
| 62 |
const integer fftNumberOfSamples = Melder_iroundUpToPowerOfTwo (my nx - 1); |
| 63 |
autoSound thee; |
| 64 |
if (fftNumberOfSamples == my nx - 1) |
| 65 |
return Spectrum_to_Sound (me); // FFT without resampling |
| 66 |
const integer newNumberOfFrequencies = fftNumberOfSamples + 1; |
| 67 |
autoSpectrum extendedSpectrum = Spectrum_create (my xmax, newNumberOfFrequencies); |
| 68 |
extendedSpectrum -> z.part (1, 2, 1, my nx) <<= my z.get(); |
| 69 |
extendedSpectrum -> dx = my dx; |
| 70 |
autoSound upsampled = Spectrum_to_Sound (extendedSpectrum.get()); |
| 71 |
return Sound_resample (upsampled.get(), Melder_iround (2.0 * my xmax), interpolationDepth); |
| 72 |
} catch (MelderError) { |
| 73 |
Melder_throw (me, U": could not convert to Sound by resampling."); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/* End of file Sound_and_Spectrum_dft.cpp */ |