Spectrum_and_Spectrogram.cpp 2.0 KB

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