Spectrogram.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Spectrogram.cpp
  2. *
  3. * Copyright (C) 1992-2008,2011,2012,2015-2018 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. #include "Spectrogram.h"
  19. Thing_implement (Spectrogram, Matrix, 2);
  20. void structSpectrogram :: v_info () {
  21. structDaata :: v_info ();
  22. MelderInfo_writeLine (U"Time domain:");
  23. MelderInfo_writeLine (U" Start time: ", xmin, U" seconds");
  24. MelderInfo_writeLine (U" End time: ", xmax, U" seconds");
  25. MelderInfo_writeLine (U" Total duration: ", xmax - xmin, U" seconds");
  26. MelderInfo_writeLine (U"Time sampling:");
  27. MelderInfo_writeLine (U" Number of time slices (frames): ", nx);
  28. MelderInfo_writeLine (U" Time step (frame distance): ", dx, U" seconds");
  29. MelderInfo_writeLine (U" First time slice (frame centre) at: ", x1, U" seconds");
  30. MelderInfo_writeLine (U"Frequency domain:");
  31. MelderInfo_writeLine (U" Lowest frequency: ", ymin, U" Hz");
  32. MelderInfo_writeLine (U" Highest frequency: ", ymax, U" Hz");
  33. MelderInfo_writeLine (U" Total bandwidth: ", ymax - ymin, U" Hz");
  34. MelderInfo_writeLine (U"Frequency sampling:");
  35. MelderInfo_writeLine (U" Number of frequency bands (bins): ", ny);
  36. MelderInfo_writeLine (U" Frequency step (bin width): ", dy, U" Hz");
  37. MelderInfo_writeLine (U" First frequency band around (bin centre at): ", y1, U" Hz");
  38. }
  39. autoSpectrogram Spectrogram_create (double tmin, double tmax, integer nt, double dt, double t1,
  40. double fmin, double fmax, integer nf, double df, double f1)
  41. {
  42. try {
  43. autoSpectrogram me = Thing_new (Spectrogram);
  44. Matrix_init (me.get(), tmin, tmax, nt, dt, t1, fmin, fmax, nf, df, f1);
  45. return me;
  46. } catch (MelderError) {
  47. Melder_throw (U"Spectrogram not created.");
  48. }
  49. }
  50. void Spectrogram_paintInside (Spectrogram me, Graphics g, double tmin, double tmax, double fmin, double fmax,
  51. double maximum, int autoscaling, double dynamic, double preemphasis, double dynamicCompression)
  52. {
  53. if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }
  54. if (fmax <= fmin) { fmin = my ymin; fmax = my ymax; }
  55. integer itmin, itmax, ifmin, ifmax;
  56. if (! Matrix_getWindowSamplesX (me, tmin - 0.49999 * my dx, tmax + 0.49999 * my dx, & itmin, & itmax) ||
  57. ! Matrix_getWindowSamplesY (me, fmin - 0.49999 * my dy, fmax + 0.49999 * my dy, & ifmin, & ifmax))
  58. return;
  59. Graphics_setWindow (g, tmin, tmax, fmin, fmax);
  60. autoNUMvector <double> preemphasisFactor (ifmin, ifmax);
  61. autoNUMvector <double> dynamicFactor (itmin, itmax);
  62. /* Pre-emphasis in place; also compute maximum after pre-emphasis. */
  63. for (integer ifreq = ifmin; ifreq <= ifmax; ifreq ++) {
  64. preemphasisFactor [ifreq] = (preemphasis / NUMln2) * log (ifreq * my dy / 1000.0);
  65. for (integer itime = itmin; itime <= itmax; itime ++) {
  66. double value = my z [ifreq] [itime]; // power
  67. value = (10.0/NUMln10) * log ((value + 1e-30) / 4.0e-10) + preemphasisFactor [ifreq]; // dB
  68. if (value > dynamicFactor [itime]) dynamicFactor [itime] = value; // local maximum
  69. my z [ifreq] [itime] = value;
  70. }
  71. }
  72. /* Compute global maximum. */
  73. if (autoscaling) {
  74. maximum = 0.0;
  75. for (integer itime = itmin; itime <= itmax; itime ++)
  76. if (dynamicFactor [itime] > maximum) maximum = dynamicFactor [itime];
  77. }
  78. /* Dynamic compression in place. */
  79. for (integer itime = itmin; itime <= itmax; itime ++) {
  80. dynamicFactor [itime] = dynamicCompression * (maximum - dynamicFactor [itime]);
  81. for (integer ifreq = ifmin; ifreq <= ifmax; ifreq ++)
  82. my z [ifreq] [itime] += dynamicFactor [itime];
  83. }
  84. Graphics_image (g, my z.at,
  85. itmin, itmax,
  86. Matrix_columnToX (me, itmin - 0.5),
  87. Matrix_columnToX (me, itmax + 0.5),
  88. ifmin, ifmax,
  89. Matrix_rowToY (me, ifmin - 0.5),
  90. Matrix_rowToY (me, ifmax + 0.5),
  91. maximum - dynamic, maximum
  92. );
  93. for (integer ifreq = ifmin; ifreq <= ifmax; ifreq ++)
  94. for (integer itime = itmin; itime <= itmax; itime ++) {
  95. double value = 4.0e-10 * exp ((my z [ifreq] [itime] - dynamicFactor [itime]
  96. - preemphasisFactor [ifreq]) * (NUMln10 / 10.0)) - 1e-30;
  97. my z [ifreq] [itime] = value > 0.0 ? value : 0.0;
  98. }
  99. }
  100. void Spectrogram_paint (Spectrogram me, Graphics g,
  101. double tmin, double tmax, double fmin, double fmax, double maximum, int autoscaling,
  102. double dynamic, double preemphasis, double dynamicCompression,
  103. bool garnish)
  104. {
  105. Graphics_setInner (g);
  106. Spectrogram_paintInside (me, g, tmin, tmax, fmin, fmax, maximum, autoscaling, dynamic, preemphasis, dynamicCompression);
  107. Graphics_unsetInner (g);
  108. if (garnish) {
  109. Graphics_drawInnerBox (g);
  110. Graphics_textBottom (g, true, U"Time (s)");
  111. Graphics_marksBottom (g, 2, true, true, false);
  112. Graphics_marksLeft (g, 2, true, true, false);
  113. Graphics_textLeft (g, true, U"Frequency (Hz)");
  114. }
  115. }
  116. autoSpectrogram Matrix_to_Spectrogram (Matrix me) {
  117. try {
  118. autoSpectrogram thee = Spectrogram_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
  119. matrixcopy_preallocated (thy z.get(), my z.get());
  120. return thee;
  121. } catch (MelderError) {
  122. Melder_throw (me, U": not converted to Spectrogram.");
  123. }
  124. }
  125. autoMatrix Spectrogram_to_Matrix (Spectrogram me) {
  126. try {
  127. autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
  128. matrixcopy_preallocated (thy z.get(), my z.get());
  129. return thee;
  130. } catch (MelderError) {
  131. Melder_throw (me, U": not converted to Matrix.");
  132. }
  133. }
  134. /* End of Spectrogram.cpp */