1 /* Sound_and_MultiSampledSpectrogram.cpp
2  *
3  * Copyright (C) 2021 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 
Sound_to_ConstantQLog2FSpectrogram(Sound me,double lowestFrequency,double fmax,integer numberOfBinsPerOctave,double frequencyResolutionInBins,double timeOversamplingFactor,kSound_windowShape filterShape)25 autoConstantQLog2FSpectrogram Sound_to_ConstantQLog2FSpectrogram (Sound me, double lowestFrequency, double fmax,
26 	integer numberOfBinsPerOctave, double frequencyResolutionInBins, double timeOversamplingFactor,
27 	kSound_windowShape filterShape) {
28 	try {
29 		const double nyquistFrequency = 0.5 / my dx;
30 		if (fmax <= 0.0)
31 			fmax = nyquistFrequency;
32 		Melder_require (fmax  <= nyquistFrequency,
33 			U"The maximum frequency should not exceed the nyquist frequency (", nyquistFrequency, U" Hz).");
34 		Melder_require (lowestFrequency < fmax,
35 			U"The lowest frequency should be smaller than the maximum frequency (", fmax, U" Hz).");
36 		Melder_clipLeft (1.0, & timeOversamplingFactor);
37 		autoConstantQLog2FSpectrogram thee = ConstantQLog2FSpectrogram_create (my xmin, my xmax,
38 			lowestFrequency, fmax, numberOfBinsPerOctave, frequencyResolutionInBins);
39 		autoSpectrum him = Sound_and_MultiSampledSpectrogram_to_Spectrum (me, thee.get());
40 		Spectrum_into_MultiSampledSpectrogram (him.get(), thee.get(), timeOversamplingFactor, filterShape);
41 		return thee;
42 	} catch (MelderError) {
43 		Melder_throw (me, U": cannot create ConstantQLog2FSpectrogram.");
44 	}
45 }
46 
Sound_to_GaborSpectrogram(Sound me,double fmax,double filterBandwidth,double frequencyStep,double timeOversamplingFactor,kSound_windowShape filterShape)47 autoGaborSpectrogram Sound_to_GaborSpectrogram (Sound me, double fmax, double filterBandwidth,
48 	double frequencyStep, double timeOversamplingFactor, kSound_windowShape filterShape)
49 {
50 	try {
51 		const double nyquistFrequency = 0.5 / my dx;
52 		if (fmax <= 0.0)
53 			fmax = nyquistFrequency;
54 		Melder_require (fmax  <= nyquistFrequency,
55 			U"The maximum frequency should not exceed the nyquist frequency (", nyquistFrequency, U" Hz).");
56 		autoGaborSpectrogram thee = GaborSpectrogram_create (my xmin, my xmax, fmax, filterBandwidth, frequencyStep);
57 		autoSpectrum him = Sound_and_MultiSampledSpectrogram_to_Spectrum (me, thee.get());
58 		Spectrum_into_MultiSampledSpectrogram (him.get(), thee.get(), timeOversamplingFactor, filterShape);
59 		return thee;
60 	} catch (MelderError) {
61 		Melder_throw (me, U": cannot create GaborSpectrogram.");
62 	}
63 }
64 /* End of file Sound_and_MultiSampledSpectrogram.cpp */
65