| 1 | /* Spectrum_and_AnalyticSound.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 "Sound_and_Spectrum.h" |
| 21 | #include "Spectrum_extensions.h" |
| 22 | |
| 23 | autoAnalyticSound Spectrum_to_AnalyticSound (Spectrum me) { |
| 24 | try { |
| 25 | autoSound him = Spectrum_to_Sound (me); |
| 26 | autoAnalyticSound thee = AnalyticSound_create (his xmin, his xmax, his nx, his dx, his x1); |
| 27 | thy z.row (1) <<= his z.row (1); |
| 28 | Spectrum_shiftPhaseBy90Degrees (me); |
| 29 | him = Spectrum_to_Sound (me); |
| 30 | thy z.row (2) <<= his z.row (1); |
| 31 | Spectrum_unshiftPhaseBy90Degrees (me); // restore phase |
| 32 | return thee; |
| 33 | } catch (MelderError) { |
| 34 | Melder_throw (me, U": could not create AnalyticSound."); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /* |
| 39 | |
| 40 | |
| 41 | */ |
| 42 | autoAnalyticSound Spectrum_to_AnalyticSound_demodulateBand (Spectrum me, integer startSample, integer endSample, double approximateOverSampling, const VEC & window) { |
| 43 | try { |
| 44 | Melder_clipLeft (1_integer, & startSample); |
| 45 | Melder_clipRight (& endSample, my nx); |
| 46 | Melder_require (startSample < endSample, |
| 47 | U"The start spectral sample should lie before the end spectral sample."); |
| 48 | const integer extraSpectralSample = ( startSample > 1 ? 1 : 0 ); // first spectral value cannot be imaginary |
| 49 | const integer numberOfSamplesFromSpectrum = endSample - startSample + 1; |
| 50 | Melder_require (window.size <= numberOfSamplesFromSpectrum, |
| 51 | U"The window size should not exceed the number of selected spectral values."); |
| 52 | const integer numberOfValuesInBand = extraSpectralSample + ( approximateOverSampling <= 1.0 ? numberOfSamplesFromSpectrum : |
| 53 | Melder_iroundUp (approximateOverSampling * numberOfSamplesFromSpectrum) ); |
| 54 | const integer numberOfSamplesFFT = Melder_iroundUpToPowerOfTwo (numberOfValuesInBand); |
| 55 | const integer numberOfFrequencies = numberOfSamplesFFT + 1; |
| 56 | const double demod_fmax = my dx * numberOfSamplesFFT; |
| 57 | autoSpectrum demodSpectrum = Spectrum_create (demod_fmax, numberOfFrequencies); |
| 58 | demodSpectrum -> z.part (1, 2, 1 + extraSpectralSample, numberOfSamplesFromSpectrum + extraSpectralSample) |
| 59 | <<= my z.part (1, 2, startSample, endSample); |
| 60 | if (window.size > 0) { |
| 61 | const integer startWindowing = ( startSample == 1 ? numberOfSamplesFromSpectrum - window.size + 1 : 2 ); |
| 62 | const integer endWindowing = startWindowing + window.size - 1; |
| 63 | demodSpectrum -> z.part (1, 2, startWindowing, endWindowing) *= window; |
| 64 | } |
| 65 | return Spectrum_to_AnalyticSound (demodSpectrum.get()); |
| 66 | } catch (MelderError) { |
| 67 | Melder_throw (me, U": could not create AnalyticSound."); |
| 68 | } |
| 69 | |
| 70 | } |
| 71 | /* End of file Spectrum_and_AnalyticSound.h */ |