| 1 |
/* Sound_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 "Sound_and_MultiSampledSpectrogram.h" |
| 20 |
#include "Sound_and_Spectrum.h" |
| 21 |
#include "Spectrum_extensions.h" |
| 22 |
#include "Spectrum_and_MultiSampledSpectrogram.h" |
| 23 |
#include "NUM2.h" |
| 24 |
|
| 25 |
|
| 26 |
autoSound MultiSampledSpectrogram_to_Sound (MultiSampledSpectrogram me) { |
| 27 |
try { |
| 28 |
autoSpectrum spectrum = MultiSampledSpectrogram_to_Spectrum (me); |
| 29 |
autoSound thee = Spectrum_to_Sound (spectrum.get()); |
| 30 |
const double synthesizedDuration = thy xmax - thy xmin; |
| 31 |
const double wantedDuration = my tmax - my tmin; |
| 32 |
if (synthesizedDuration > wantedDuration) { |
| 33 |
/* |
| 34 |
The number of samples in the original was not a power of 2 and to apply the FFT the number of samples had to be extended. |
| 35 |
*/ |
| 36 |
autoSound part = Sound_extractPart (thee.get(), 0.0, wantedDuration, kSound_windowShape::RECTANGULAR, 1.0, false); |
| 37 |
part -> xmin = my tmin; |
| 38 |
part -> xmax = my tmax; |
| 39 |
thee = part.move(); |
| 40 |
} else if (synthesizedDuration == wantedDuration) { |
| 41 |
thy xmin = my tmin; |
| 42 |
thy xmax = my tmax; |
| 43 |
} else { |
| 44 |
Melder_throw (U"The synthesized number of samples is too low!"); |
| 45 |
} |
| 46 |
return thee; |
| 47 |
|
| 48 |
} catch (MelderError) { |
| 49 |
Melder_throw (me, U": could not create Sound."); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
autoConstantQLog2FSpectrogram Sound_to_ConstantQLog2FSpectrogram (Sound me, double lowestFrequency, double fmax, |
| 54 |
integer numberOfBinsPerOctave, double frequencyResolutionInBins, double timeOversamplingFactor, |
| 55 |
kSound_windowShape filterShape) { |
| 56 |
try { |
| 57 |
const double nyquistFrequency = 0.5 / my dx; |
| 58 |
if (fmax <= 0.0 || fmax > nyquistFrequency) |
| 59 |
fmax = nyquistFrequency; |
| 60 |
Melder_require (lowestFrequency < fmax, |
| 61 |
U"The lowest frequency should be smaller than the maximum frequency (", fmax, U" Hz)."); |
| 62 |
Melder_require (frequencyResolutionInBins > 0.0, |
| 63 |
U"The frequency resolution should be larger than zero."); |
| 64 |
Melder_clipLeft (1.0, & timeOversamplingFactor); |
| 65 |
autoConstantQLog2FSpectrogram thee = ConstantQLog2FSpectrogram_create (my xmin, my xmax, |
| 66 |
lowestFrequency, fmax, numberOfBinsPerOctave, frequencyResolutionInBins); |
| 67 |
autoSpectrum him = Sound_and_MultiSampledSpectrogram_to_Spectrum (me, thee.get()); |
| 68 |
Spectrum_into_MultiSampledSpectrogram (him.get(), thee.get(), timeOversamplingFactor, filterShape); |
| 69 |
return thee; |
| 70 |
} catch (MelderError) { |
| 71 |
Melder_throw (me, U": cannot create ConstantQLog2FSpectrogram."); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
autoGaborSpectrogram Sound_to_GaborSpectrogram (Sound me, double fmax, double filterBandwidth, |
| 76 |
double frequencyStep, double timeOversamplingFactor, kSound_windowShape filterShape) |
| 77 |
{ |
| 78 |
try { |
| 79 |
const double nyquistFrequency = 0.5 / my dx; |
| 80 |
bool resample = true; |
| 81 |
if (fmax <= 0.0 || fmax >= nyquistFrequency) { |
| 82 |
fmax = nyquistFrequency; |
| 83 |
resample = false; |
| 84 |
} |
| 85 |
autoGaborSpectrogram thee = GaborSpectrogram_create (my xmin, my xmax, fmax, filterBandwidth, frequencyStep); |
| 86 |
autoSound resampled; |
| 87 |
if (resample) |
| 88 |
resampled = Sound_resample (me, 2.0 * fmax, 50); |
| 89 |
autoSpectrum him = Sound_to_Spectrum ((resample ? resampled.get() : me), true); |
| 90 |
Spectrum_into_MultiSampledSpectrogram (him.get(), thee.get(), timeOversamplingFactor, filterShape); |
| 91 |
return thee; |
| 92 |
} catch (MelderError) { |
| 93 |
Melder_throw (me, U": cannot create GaborSpectrogram."); |
| 94 |
} |
| 95 |
} |
| 96 |
/* End of file Sound_and_MultiSampledSpectrogram.cpp */ |