Harmonicity.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Harmonicity.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 "Graphics.h"
  19. #include "Harmonicity.h"
  20. Thing_implement (Harmonicity, Vector, 2);
  21. static autoVEC Harmonicity_getSoundingValues (Harmonicity me, double tmin, double tmax) {
  22. if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }
  23. integer imin, imax;
  24. integer numberOfFrames = Sampled_getWindowSamples (me, tmin, tmax, & imin, & imax);
  25. if (numberOfFrames < 1)
  26. return autoVEC();
  27. autoVEC soundingValues = VECraw (numberOfFrames);
  28. integer numberOfSoundingFrames = 0;
  29. for (integer iframe = imin; iframe <= imax; iframe ++)
  30. if (my z [1] [iframe] != -200.0)
  31. soundingValues [++ numberOfSoundingFrames] = my z [1] [iframe];
  32. if (numberOfSoundingFrames < 1)
  33. return autoVEC();
  34. soundingValues.size = numberOfSoundingFrames; // shrink (without reallocation)
  35. return soundingValues;
  36. }
  37. double Harmonicity_getMean (Harmonicity me, double tmin, double tmax) {
  38. autoVEC soundingValues = Harmonicity_getSoundingValues (me, tmin, tmax);
  39. return NUMmean (soundingValues.get());
  40. }
  41. double Harmonicity_getStandardDeviation (Harmonicity me, double tmin, double tmax) {
  42. autoVEC soundingValues = Harmonicity_getSoundingValues (me, tmin, tmax);
  43. return NUMstdev (soundingValues.get());
  44. }
  45. double Harmonicity_getQuantile (Harmonicity me, double quantile) {
  46. autoVEC soundingValues = Harmonicity_getSoundingValues (me, 0.0, 0.0);
  47. VECsort_inplace (soundingValues.get());
  48. return NUMquantile (soundingValues.get(), quantile);
  49. }
  50. void structHarmonicity :: v_info () {
  51. structDaata :: v_info ();
  52. MelderInfo_writeLine (U"Time domain:");
  53. MelderInfo_writeLine (U" Start time: ", our xmin, U" seconds");
  54. MelderInfo_writeLine (U" End time: ", our xmax, U" seconds");
  55. MelderInfo_writeLine (U" Total duration: ", our xmax - our xmin, U" seconds");
  56. autoVEC soundingValues = Harmonicity_getSoundingValues (this, 0.0, 0.0);
  57. MelderInfo_writeLine (U"Time sampling:");
  58. MelderInfo_writeLine (U" Number of frames: ", our nx, U" (", soundingValues.size, U" sounding)");
  59. MelderInfo_writeLine (U" Time step: ", our dx, U" seconds");
  60. MelderInfo_writeLine (U" First frame centred at: ", our x1, U" seconds");
  61. if (soundingValues.size > 0) {
  62. MelderInfo_writeLine (U"Periodicity-to-noise ratios of sounding frames:");
  63. VECsort_inplace (soundingValues.get());
  64. MelderInfo_writeLine (U" Median ", Melder_single (NUMquantile (soundingValues.get(), 0.50)), U" dB");
  65. MelderInfo_writeLine (U" 10 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.10)), U" dB 90 %% = ",
  66. Melder_single (NUMquantile (soundingValues.get(), 0.90)), U" dB");
  67. MelderInfo_writeLine (U" 16 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.16)), U" dB 84 %% = ",
  68. Melder_single (NUMquantile (soundingValues.get(), 0.84)), U" dB");
  69. MelderInfo_writeLine (U" 25 % = ", Melder_single (NUMquantile (soundingValues.get(), 0.25)), U" dB 75 %% = ",
  70. Melder_single (NUMquantile (soundingValues.get(), 0.75)), U" dB");
  71. MelderInfo_writeLine (U"Minimum: ", Melder_single (soundingValues [1]), U" dB");
  72. MelderInfo_writeLine (U"Maximum: ", Melder_single (soundingValues [soundingValues.size]), U" dB");
  73. double mean, stdev;
  74. NUM_sum_mean_sumsq_variance_stdev (soundingValues.get(), nullptr, & mean, nullptr, nullptr, & stdev);
  75. MelderInfo_writeLine (U"Average: ", Melder_single (mean), U" dB");
  76. if (soundingValues.size > 1)
  77. MelderInfo_writeLine (U"Standard deviation: ", Melder_single (stdev), U" dB");
  78. }
  79. }
  80. autoHarmonicity Harmonicity_create (double tmin, double tmax, integer nt, double dt, double t1) {
  81. try {
  82. autoHarmonicity me = Thing_new (Harmonicity);
  83. Matrix_init (me.get(), tmin, tmax, nt, dt, t1, 1.0, 1.0, 1, 1.0, 1.0);
  84. return me;
  85. } catch (MelderError) {
  86. Melder_throw (U"Harmonicity not created.");
  87. }
  88. }
  89. autoMatrix Harmonicity_to_Matrix (Harmonicity me) {
  90. try {
  91. autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
  92. NUMvector_copyElements (my z [1], thy z [1], 1, my nx);
  93. return thee;
  94. } catch (MelderError) {
  95. Melder_throw (me, U"not converted to Matrix.");
  96. }
  97. }
  98. autoHarmonicity Matrix_to_Harmonicity (Matrix me) {
  99. try {
  100. autoHarmonicity thee = Harmonicity_create (my xmin, my xmax, my nx, my dx, my x1);
  101. NUMvector_copyElements (my z [1], thy z [1], 1, my nx);
  102. return thee;
  103. } catch (MelderError) {
  104. Melder_throw (me, U"not converted to Harmonicity.");
  105. }
  106. }
  107. /* End of file Harmonicity.cpp */