IntensityTier.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* IntensityTier.cpp
  2. *
  3. * Copyright (C) 1992-2011,2015,2016,2017 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 "IntensityTier.h"
  19. Thing_implement (IntensityTier, RealTier, 0);
  20. autoIntensityTier IntensityTier_create (double tmin, double tmax) {
  21. try {
  22. autoIntensityTier me = Thing_new (IntensityTier);
  23. RealTier_init (me.get(), tmin, tmax);
  24. return me;
  25. } catch (MelderError) {
  26. Melder_throw (U"IntensityTier not created.");
  27. }
  28. }
  29. void IntensityTier_draw (IntensityTier me, Graphics g, double tmin, double tmax,
  30. double ymin, double ymax, conststring32 method, int garnish)
  31. {
  32. RealTier_draw (me, g, tmin, tmax, ymin, ymax, garnish, method, U"Intensity (dB)");
  33. }
  34. autoIntensityTier PointProcess_upto_IntensityTier (PointProcess me, double intensity) {
  35. try {
  36. autoIntensityTier thee = PointProcess_upto_RealTier (me, intensity, classIntensityTier).static_cast_move<structIntensityTier>();
  37. return thee;
  38. } catch (MelderError) {
  39. Melder_throw (me, U": not converted to IntensityTier.");
  40. }
  41. }
  42. autoIntensityTier Intensity_downto_IntensityTier (Intensity me) {
  43. try {
  44. autoIntensityTier thee = Vector_to_RealTier (me, 1, classIntensityTier).static_cast_move<structIntensityTier>();
  45. return thee;
  46. } catch (MelderError) {
  47. Melder_throw (me, U": not converted to IntensityTier.");
  48. }
  49. }
  50. autoIntensityTier Intensity_to_IntensityTier_peaks (Intensity me) {
  51. try {
  52. autoIntensityTier thee = Vector_to_RealTier_peaks (me, 1, classIntensityTier).static_cast_move<structIntensityTier>();
  53. return thee;
  54. } catch (MelderError) {
  55. Melder_throw (me, U": peaks not converted to IntensityTier.");
  56. }
  57. }
  58. autoIntensityTier Intensity_to_IntensityTier_valleys (Intensity me) {
  59. try {
  60. autoIntensityTier thee = Vector_to_RealTier_valleys (me, 1, classIntensityTier).static_cast_move<structIntensityTier>();
  61. return thee;
  62. } catch (MelderError) {
  63. Melder_throw (me, U": valleys not converted to IntensityTier.");
  64. }
  65. }
  66. autoIntensityTier Intensity_PointProcess_to_IntensityTier (Intensity me, PointProcess pp) {
  67. try {
  68. autoIntensityTier temp = Intensity_downto_IntensityTier (me);
  69. autoIntensityTier thee = IntensityTier_PointProcess_to_IntensityTier (temp.get(), pp);
  70. return thee;
  71. } catch (MelderError) {
  72. Melder_throw (me, U" & ", pp, U": not converted to IntensityTier.");
  73. }
  74. }
  75. autoIntensityTier IntensityTier_PointProcess_to_IntensityTier (IntensityTier me, PointProcess pp) {
  76. try {
  77. if (my points.size == 0) Melder_throw (U"No intensity points.");
  78. autoIntensityTier thee = IntensityTier_create (pp -> xmin, pp -> xmax);
  79. for (integer i = 1; i <= pp -> nt; i ++) {
  80. double time = pp -> t [i];
  81. double value = RealTier_getValueAtTime (me, time);
  82. RealTier_addPoint (thee.get(), time, value);
  83. }
  84. return thee;
  85. } catch (MelderError) {
  86. Melder_throw (me, U" & ", pp, U": not converted to IntensityTier.");
  87. }
  88. }
  89. autoTableOfReal IntensityTier_downto_TableOfReal (IntensityTier me) {
  90. return RealTier_downto_TableOfReal (me, U"Time (s)", U"Intensity (dB)");
  91. }
  92. void Sound_IntensityTier_multiply_inplace (Sound me, IntensityTier intensity) {
  93. if (intensity -> points.size == 0) return;
  94. for (integer isamp = 1; isamp <= my nx; isamp ++) {
  95. double t = my x1 + (isamp - 1) * my dx;
  96. double factor = pow (10, RealTier_getValueAtTime (intensity, t) / 20);
  97. for (integer channel = 1; channel <= my ny; channel ++) {
  98. my z [channel] [isamp] *= factor;
  99. }
  100. }
  101. }
  102. autoSound Sound_IntensityTier_multiply (Sound me, IntensityTier intensity, int scale) {
  103. try {
  104. autoSound thee = Data_copy (me);
  105. Sound_IntensityTier_multiply_inplace (thee.get(), intensity);
  106. if (scale) Vector_scale (thee.get(), 0.9);
  107. return thee;
  108. } catch (MelderError) {
  109. Melder_throw (me, U": not multiplied with ", intensity, U".");
  110. }
  111. }
  112. /* End of file IntensityTier.cpp */