| 1 | /* Spectrum_and_Spectrogram.cpp |
| 2 | * |
| 3 | * Copyright (C) 1992-2011,2014,2015,2017,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. |
| 13 | * See the GNU 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 | /* |
| 20 | * pb 1998/04/18 |
| 21 | * pb 2002/07/16 GPL |
| 22 | * pb 2011/06/10 C++ |
| 23 | */ |
| 24 | |
| 25 | #include "Spectrum_and_Spectrogram.h" |
| 26 | |
| 27 | autoSpectrum Spectrogram_to_Spectrum (Spectrogram me, double tim) { |
| 28 | try { |
| 29 | autoSpectrum thee = Spectrum_create (my ymax, my ny); |
| 30 | /* Override stupid Spectrum values. */ |
| 31 | thy xmin = my ymin; |
| 32 | thy xmax = my ymax; |
| 33 | thy x1 = my y1; // centre of first band, instead of 0 (makes it unFFTable) |
| 34 | thy dx = my dy; // frequency step |
| 35 | Melder_assert (my nx >= 1); // for Melder_clipped |
| 36 | const integer itime = Melder_clipped (1_integer, Sampled_xToNearestIndex (me, tim), my nx); |
| 37 | for (integer ifreq = 1; ifreq <= my ny; ifreq ++) { |
| 38 | const double value = my z [ifreq] [itime]; |
| 39 | if (value < 0.0) |
| 40 | Melder_throw (U"Negative values in spectrogram."); |
| 41 | thy z [1] [ifreq] = sqrt (value); |
| 42 | thy z [2] [ifreq] = 0.0; |
| 43 | } |
| 44 | return thee; |
| 45 | } catch (MelderError) { |
| 46 | Melder_throw (me, U": spectral slice not extracted."); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | autoSpectrogram Spectrum_to_Spectrogram (Spectrum me) { |
| 51 | try { |
| 52 | autoSpectrogram thee = Spectrogram_create (0, 1, 1, 1, 0.5, my xmin, my xmax, my nx, my dx, my x1); |
| 53 | for (integer i = 1; i <= my nx; i ++) |
| 54 | thy z [i] [1] = my z [1] [i] * my z [1] [i] + my z [2] [i] * my z [2] [i]; |
| 55 | return thee; |
| 56 | } catch (MelderError) { |
| 57 | Melder_throw (me, U": not converted to Spectrogram."); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /* End of file Spectrum_and_Spectrogram.cpp */ |