LineSpectralFrequencies.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* LineSpectralFrequencies.cpp
  2. *
  3. * Copyright (C) 2016-2018 David Weenink
  4. *
  5. * This program 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 program 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 program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. /*
  20. djmw 20160421 Initial version
  21. */
  22. #include "LineSpectralFrequencies.h"
  23. #include "NUM2.h"
  24. #include "oo_DESTROY.h"
  25. #include "LineSpectralFrequencies_def.h"
  26. #include "oo_COPY.h"
  27. #include "LineSpectralFrequencies_def.h"
  28. #include "oo_EQUAL.h"
  29. #include "LineSpectralFrequencies_def.h"
  30. #include "oo_CAN_WRITE_AS_ENCODING.h"
  31. #include "LineSpectralFrequencies_def.h"
  32. #include "oo_WRITE_TEXT.h"
  33. #include "LineSpectralFrequencies_def.h"
  34. #include "oo_WRITE_BINARY.h"
  35. #include "LineSpectralFrequencies_def.h"
  36. #include "oo_READ_TEXT.h"
  37. #include "LineSpectralFrequencies_def.h"
  38. #include "oo_READ_BINARY.h"
  39. #include "LineSpectralFrequencies_def.h"
  40. #include "oo_DESCRIPTION.h"
  41. #include "LineSpectralFrequencies_def.h"
  42. Thing_implement (LineSpectralFrequencies, Sampled, 1);
  43. void structLineSpectralFrequencies :: v_info () {
  44. structDaata :: v_info ();
  45. MelderInfo_writeLine (U"Time domain: ", xmin, U" to ", xmax, U" (s).");
  46. MelderInfo_writeLine (U"Number of frequencies: ", maximumNumberOfFrequencies);
  47. MelderInfo_writeLine (U"Number of frames: ", nx);
  48. MelderInfo_writeLine (U"Time step: ", dx, U" (s).");
  49. MelderInfo_writeLine (U"First frame at: ", x1, U" (s).");
  50. }
  51. void LineSpectralFrequencies_Frame_init (LineSpectralFrequencies_Frame me, integer numberOfFrequencies) {
  52. my frequencies = VECzero (numberOfFrequencies);
  53. my numberOfFrequencies = numberOfFrequencies;
  54. }
  55. void LineSpectralFrequencies_init (LineSpectralFrequencies me, double tmin, double tmax, integer nt, double dt, double t1, integer numberOfFrequencies, double maximumFrequency) {
  56. my maximumFrequency = maximumFrequency;
  57. my maximumNumberOfFrequencies = numberOfFrequencies;
  58. Sampled_init (me, tmin, tmax, nt, dt, t1);
  59. my d_frames = NUMvector<structLineSpectralFrequencies_Frame> (1, nt);
  60. }
  61. autoLineSpectralFrequencies LineSpectralFrequencies_create (double tmin, double tmax, integer nt, double dt, double t1, integer numberOfFrequencies, double maximumFrequency) {
  62. try {
  63. autoLineSpectralFrequencies me = Thing_new (LineSpectralFrequencies);
  64. LineSpectralFrequencies_init (me.get(), tmin, tmax, nt, dt, t1, numberOfFrequencies, maximumFrequency);
  65. return me;
  66. } catch (MelderError) {
  67. Melder_throw (U"LineSpectralFrequencies not created.");
  68. }
  69. }
  70. void LineSpectralFrequencies_drawFrequencies (LineSpectralFrequencies me, Graphics g, double tmin, double tmax, double fmin, double fmax, bool garnish) {
  71. if (tmax <= tmin) {
  72. tmin = my xmin;
  73. tmax = my xmax;
  74. }
  75. integer itmin, itmax;
  76. if (! Sampled_getWindowSamples (me, tmin, tmax, & itmin, & itmax)) {
  77. return;
  78. }
  79. if (fmax <= fmin) {
  80. double f1max, f2min;
  81. autoNUMvector<double> f1 (itmin, itmax), f2 (itmin, itmax);
  82. for (integer iframe = itmin; iframe <= itmax; iframe ++) {
  83. f1 [iframe] = my d_frames [iframe]. frequencies [1];
  84. f2 [iframe] = my d_frames [iframe]. frequencies [my d_frames [iframe] . numberOfFrequencies];
  85. }
  86. NUMvector_extrema (f1.peek(), itmin, itmax, & fmin, & f1max);
  87. NUMvector_extrema (f2.peek(), itmin, itmax, & f2min, & fmax);
  88. }
  89. if (fmax == fmin) {
  90. fmin = 0;
  91. fmax += 0.5;
  92. }
  93. Graphics_setInner (g);
  94. Graphics_setWindow (g, tmin, tmax, fmin, fmax);
  95. for (integer iframe = itmin; iframe <= itmax; iframe ++) {
  96. LineSpectralFrequencies_Frame lsf = & my d_frames [iframe];
  97. double x = Sampled_indexToX (me, iframe);
  98. for (integer ifreq = 1; ifreq <= lsf -> numberOfFrequencies; ifreq ++) {
  99. double y = lsf -> frequencies [ifreq];
  100. if (y >= fmin && y <= fmax) {
  101. Graphics_speckle (g, x, y);
  102. }
  103. }
  104. }
  105. Graphics_unsetInner (g);
  106. if (garnish) {
  107. Graphics_drawInnerBox (g);
  108. Graphics_textBottom (g, true, U"Time (seconds)");
  109. Graphics_textLeft (g, true, U"Frequency (Hz)");
  110. Graphics_marksBottom (g, 2, true, true, false);
  111. Graphics_marksLeft (g, 2, true, true, false);
  112. }
  113. }
  114. autoMatrix LineSpectralFrequencies_downto_Matrix (LineSpectralFrequencies me) {
  115. try {
  116. autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, 0.5, 0.5 + my maximumNumberOfFrequencies, my maximumNumberOfFrequencies, 1.0, 1.0);
  117. for (integer j = 1; j <= my nx; j ++) {
  118. LineSpectralFrequencies_Frame lsf = & my d_frames[j];
  119. for (integer i = 1; i <= lsf -> numberOfFrequencies; i ++) {
  120. thy z [i] [j] = lsf -> frequencies [i];
  121. }
  122. }
  123. return thee;
  124. } catch (MelderError) {
  125. Melder_throw (me, U": no Matrix with linear prediction coefficients created.");
  126. }
  127. }
  128. /* End of file LineSpectralFrequencies.cpp */