SPINET.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* SPINET.cpp
  2. *
  3. * Copyright (C) 1993-2017 David Weenink, 2015 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. 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 work. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /*
  19. djmw 20020813 GPL header
  20. djmw 20061212 Changed info to Melder_writeLine<x> format.
  21. djmw 20071012 Added: oo_CAN_WRITE_AS_ENCODING.h
  22. djmw 20080122 float -> double
  23. djmw 20110304 Thing_new
  24. */
  25. #include "SPINET.h"
  26. #include "Sound_extensions.h"
  27. #include "NUM2.h"
  28. #include "oo_DESTROY.h"
  29. #include "SPINET_def.h"
  30. #include "oo_COPY.h"
  31. #include "SPINET_def.h"
  32. #include "oo_EQUAL.h"
  33. #include "SPINET_def.h"
  34. #include "oo_CAN_WRITE_AS_ENCODING.h"
  35. #include "SPINET_def.h"
  36. #include "oo_WRITE_TEXT.h"
  37. #include "SPINET_def.h"
  38. #include "oo_WRITE_BINARY.h"
  39. #include "SPINET_def.h"
  40. #include "oo_READ_TEXT.h"
  41. #include "SPINET_def.h"
  42. #include "oo_READ_BINARY.h"
  43. #include "SPINET_def.h"
  44. #include "oo_DESCRIPTION.h"
  45. #include "SPINET_def.h"
  46. Thing_implement (SPINET, SampledXY, 0);
  47. static integer SampledXY_getWindowExtrema (SampledXY me, double **z, integer ixmin, integer ixmax, integer iymin, integer iymax, double *minimum, double *maximum) {
  48. /*
  49. Function:
  50. compute the minimum and maximum values of z over all samples inside [ixmin, ixmax] * [iymin, iymax].
  51. Arguments:
  52. if ixmin = 0, start at first column; if ixmax = 0, end at last column (same for iymin and iymax).
  53. Return value:
  54. the number of samples inside the window.
  55. Postconditions:
  56. if result == 0, *minimum and *maximum are not changed;
  57. */
  58. if (ixmin == 0) {
  59. ixmin = 1;
  60. }
  61. if (ixmax == 0) {
  62. ixmax = my nx;
  63. }
  64. if (iymin == 0) {
  65. iymin = 1;
  66. }
  67. if (iymax == 0) {
  68. iymax = my ny;
  69. }
  70. if (ixmin > ixmax || iymin > iymax) {
  71. return 0;
  72. }
  73. *minimum = *maximum = z [iymin] [ixmin];
  74. for (integer iy = iymin; iy <= iymax; iy ++)
  75. for (integer ix = ixmin; ix <= ixmax; ix ++) {
  76. if (z [iy] [ix] < *minimum) {
  77. *minimum = z [iy] [ix];
  78. }
  79. if (z [iy] [ix] > *maximum) {
  80. *maximum = z [iy] [ix];
  81. }
  82. }
  83. return (ixmax - ixmin + 1) * (iymax - iymin + 1);
  84. }
  85. void structSPINET :: v_info () {
  86. structDaata :: v_info ();
  87. double miny, maxy, mins, maxs;
  88. if (! SampledXY_getWindowExtrema (this, y, 1, nx, 1, ny, & miny, & maxy) ||
  89. ! SampledXY_getWindowExtrema (this, s, 1, nx, 1, ny, & mins, & maxs)) {
  90. return;
  91. }
  92. MelderInfo_writeLine (U"Minimum power: ", miny);
  93. MelderInfo_writeLine (U"Maximum power: ", maxy);
  94. MelderInfo_writeLine (U"Minimum power rectified: ", mins);
  95. MelderInfo_writeLine (U"Maximum powerrectified: ", maxs);
  96. }
  97. autoSPINET SPINET_create (double tmin, double tmax, integer nt, double dt, double t1, double minimumFrequency, double maximumFrequency, integer nFilters, double excitationErbProportion, double inhibitionErbProportion) {
  98. try {
  99. autoSPINET me = Thing_new (SPINET);
  100. double minErb = NUMhertzToErb (minimumFrequency);
  101. double maxErb = NUMhertzToErb (maximumFrequency);
  102. double dErb = (maxErb - minErb) / nFilters;
  103. SampledXY_init (me.get(), tmin, tmax, nt, dt, t1, minErb - dErb / 2.0, maxErb + dErb / 2.0, nFilters, dErb, minErb);
  104. my y = NUMmatrix<double> (1, nFilters, 1, nt);
  105. my s = NUMmatrix<double> (1, nFilters, 1, nt);
  106. my gamma = 4;
  107. my excitationErbProportion = excitationErbProportion;
  108. my inhibitionErbProportion = inhibitionErbProportion;
  109. return me;
  110. } catch (MelderError) {
  111. Melder_throw (U"SPINET not created.");
  112. }
  113. }
  114. void SPINET_spectralRepresentation (SPINET me, Graphics g, double fromTime, double toTime, double fromErb, double toErb, double minimum, double maximum, int enhanced, int garnish) {
  115. double **z = enhanced ? my s : my y;
  116. autoMatrix thee = Matrix_create (my xmin, my xmax, my nx, my dx, my x1, my ymin, my ymax, my ny, my dy, my y1);
  117. for (integer j = 1; j <= my ny; j ++) {
  118. for (integer i = 1; i <= my nx; i ++) {
  119. thy z [j] [i] = z [j] [i];
  120. }
  121. }
  122. Matrix_paintCells (thee.get(), g, fromTime, toTime, fromErb, toErb, minimum, maximum);
  123. if (garnish) {
  124. Graphics_drawInnerBox (g);
  125. Graphics_textBottom (g, true, U"Time (s)");
  126. Graphics_marksBottom (g, 2, true, true, false);
  127. Graphics_textLeft (g, true, U"Frequency (ERB)");
  128. Graphics_marksLeft (g, 2, true, true, false);
  129. Graphics_textTop (g, false, enhanced ? U"Cooperative interaction output" : U"Gammatone filterbank output");
  130. }
  131. }
  132. void SPINET_drawSpectrum (SPINET me, Graphics g, double time, double fromErb, double toErb, double minimum, double maximum, int enhanced, int garnish) {
  133. integer ifmin, ifmax, icol = Sampled_xToLowIndex (me, time); // ppgb: don't use Sampled2_xToColumn for integer rounding
  134. double **z = enhanced ? my s : my y;
  135. if (icol < 1 || icol > my nx) {
  136. return;
  137. }
  138. if (toErb <= fromErb) {
  139. fromErb = my ymin;
  140. toErb = my ymax;
  141. }
  142. SampledXY_getWindowSamplesY (me, fromErb, toErb, &ifmin, &ifmax);
  143. autoNUMvector<double> spec (1, my ny);
  144. for (integer i = 1; i <= my ny; i ++) {
  145. spec [i] = z [i] [icol];
  146. }
  147. if (maximum <= minimum) {
  148. NUMvector_extrema (spec.peek(), ifmin, ifmax, &minimum, &maximum);
  149. }
  150. if (maximum <= minimum) {
  151. minimum -= 1;
  152. maximum += 1;
  153. }
  154. for (integer i = ifmin; i <= ifmax; i ++) {
  155. if (spec [i] > maximum) {
  156. spec [i] = maximum;
  157. } else if (spec [i] < minimum) {
  158. spec [i] = minimum;
  159. }
  160. }
  161. Graphics_setInner (g);
  162. Graphics_setWindow (g, fromErb, toErb, minimum, maximum);
  163. Graphics_function (g, spec.peek(), ifmin, ifmax, SampledXY_indexToY (me, ifmin), SampledXY_indexToY (me, ifmax));
  164. Graphics_unsetInner (g);
  165. if (garnish) {
  166. Graphics_drawInnerBox (g);
  167. Graphics_textBottom (g, true, U"Frequency (ERB)");
  168. Graphics_marksBottom (g, 2, true, true, false);
  169. Graphics_textLeft (g, true, U"strength");
  170. Graphics_marksLeft (g, 2, true, true, false);
  171. }
  172. }
  173. /* End of file SPINET.cpp */