Cochleagram.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* Cochleagram.cpp
  2. *
  3. * Copyright (C) 1992-2005,2007,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 "Cochleagram.h"
  20. Thing_implement (Cochleagram, Matrix, 2);
  21. autoCochleagram Cochleagram_create (double tmin, double tmax, integer nt, double dt, double t1, double df, integer nf) {
  22. try {
  23. autoCochleagram me = Thing_new (Cochleagram);
  24. Matrix_init (me.get(), tmin, tmax, nt, dt, t1, 0.0, nf * df, nf, df, 0.5 * df);
  25. return me;
  26. } catch (MelderError) {
  27. Melder_throw (U"Cochleagram with ", nt, U" times and ", nf, U" frequencies not created.");
  28. }
  29. }
  30. void Cochleagram_paint (Cochleagram me, Graphics g, double tmin, double tmax, bool garnish) {
  31. static double border [1 + 12]
  32. { 0.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0 };
  33. try {
  34. autoCochleagram copy = Data_copy (me);
  35. if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }
  36. integer itmin, itmax;
  37. Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax);
  38. for (integer iy = 2; iy < my ny; iy ++)
  39. for (integer ix = itmin; ix <= itmax; ix ++)
  40. if (my z [iy] [ix] > my z [iy - 1] [ix] &&
  41. my z [iy] [ix] > my z [iy + 1] [ix])
  42. {
  43. copy -> z [iy - 1] [ix] += 10.0;
  44. copy -> z [iy] [ix] += 10.0;
  45. copy -> z [iy + 1] [ix] += 10.0;
  46. }
  47. Graphics_setInner (g);
  48. Graphics_setWindow (g, tmin, tmax, 0.0, my ny * my dy);
  49. Graphics_grey (g, copy -> z.at,
  50. itmin, itmax, Matrix_columnToX (me, itmin), Matrix_columnToX (me, itmax),
  51. 1, my ny, 0.5 * my dy, (my ny - 0.5) * my dy,
  52. 12, border);
  53. Graphics_unsetInner (g);
  54. if (garnish) {
  55. Graphics_drawInnerBox (g);
  56. Graphics_textBottom (g, true, U"Time (s)");
  57. Graphics_marksBottom (g, 2, true, true, false);
  58. Graphics_textLeft (g, true, U"Place (Bark)");
  59. Graphics_marksLeftEvery (g, 1.0, 5.0, true, true, false);
  60. }
  61. } catch (MelderError) {
  62. Melder_clearError ();
  63. }
  64. }
  65. double Cochleagram_difference (Cochleagram me, Cochleagram thee, double tmin, double tmax) {
  66. try {
  67. if (my nx != thy nx || my dx != thy dx || my x1 != thy x1)
  68. Melder_throw (U"Unequal time samplings.");
  69. Melder_require (my ny == thy ny,
  70. U"Unequal numbers of frequencies.");
  71. if (tmax <= tmin) { tmin = my xmin; tmax = my xmax; }
  72. integer itmin, itmax;
  73. integer nt = Matrix_getWindowSamplesX (me, tmin, tmax, & itmin, & itmax);
  74. Melder_require (nt > 0,
  75. U"Window too short.");
  76. longdouble diff = 0.0;
  77. for (integer itime = itmin; itime <= itmax; itime ++) {
  78. for (integer ifreq = 1; ifreq <= my ny; ifreq ++) {
  79. double d = my z [ifreq] [itime] - thy z [ifreq] [itime];
  80. diff += d * d;
  81. }
  82. }
  83. diff /= nt * my ny;
  84. return sqrt ((double) diff);
  85. } catch (MelderError) {
  86. Melder_throw (me, U" & ", thee, U": difference not computed.");
  87. }
  88. }
  89. autoCochleagram Matrix_to_Cochleagram (Matrix me) {
  90. try {
  91. autoCochleagram thee = Cochleagram_create (my xmin, my xmax, my nx, my dx, my x1, my dy, my ny);
  92. matrixcopy_preallocated (thy z.get(), my z.get());
  93. return thee;
  94. } catch (MelderError) {
  95. Melder_throw (me, U": not converted to Cochleagram.");
  96. }
  97. }
  98. autoMatrix Cochleagram_to_Matrix (Cochleagram me) {
  99. try {
  100. autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
  101. matrixcopy_preallocated (thy z.get(), my z.get());
  102. return thee;
  103. } catch (MelderError) {
  104. Melder_throw (me, U": not converted to Matrix.");
  105. }
  106. }
  107. /* End of file Cochleagram.cpp */