PatternList.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* PatternList.cpp
  2. *
  3. * Copyright (C) 1993-2017 David Weenink
  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 20041203 Added _PatternList_checkElements.
  21. djmw 20071017 Melder_error<p>
  22. djmw 20110304 Thing_new
  23. */
  24. #include "PatternList.h"
  25. Thing_implement (PatternList, Matrix, 2);
  26. int _PatternList_checkElements (PatternList me) {
  27. for (integer i = 1; i <= my ny; i ++) {
  28. for (integer j = 1; j <= my nx; j ++) {
  29. if (my z [i] [j] < 0 || my z [i] [j] > 1) {
  30. return 0;
  31. }
  32. }
  33. }
  34. return 1;
  35. }
  36. void PatternList_init (PatternList me, integer ny, integer nx) {
  37. my ny = ny;
  38. my nx = nx;
  39. Matrix_init (me, 1, nx, nx, 1, 1, 1, ny, ny, 1, 1);
  40. }
  41. autoPatternList PatternList_create (integer ny, integer nx) {
  42. try {
  43. autoPatternList me = Thing_new (PatternList);
  44. PatternList_init (me.get(), ny, nx);
  45. return me;
  46. } catch (MelderError) {
  47. Melder_throw (U"PatternList not created.");
  48. }
  49. }
  50. void PatternList_normalize (PatternList me, int choice, double pmin, double pmax) {
  51. if (pmin == pmax) {
  52. (void) Matrix_getWindowExtrema (me, 1, my nx, 1, my ny, & pmin, & pmax);
  53. }
  54. if (pmin == pmax) {
  55. return;
  56. }
  57. if (choice == 1) {
  58. for (integer i = 1; i <= my ny; i ++) {
  59. for (integer j = 1; j <= my nx; j ++) {
  60. my z [i] [j] = (my z [i] [j] - pmin) / (pmax - pmin);
  61. }
  62. }
  63. } else { /* default choice */
  64. for (integer i = 1; i <= my ny; i ++) {
  65. double sum = 0;
  66. for (integer j = 1; j <= my nx; j ++) {
  67. sum += (my z [i] [j] -= pmin);
  68. }
  69. for (integer j = 1; j <= my nx; j ++) {
  70. my z [i] [j] *= 1.0 / sum;
  71. }
  72. }
  73. }
  74. }
  75. void PatternList_draw (PatternList me, Graphics g, integer pattern, double xmin, double xmax, double ymin, double ymax, int garnish) {
  76. Matrix_drawRows (me, g, xmin, xmax, pattern - 0.5, pattern + 0.5, ymin, ymax);
  77. if (garnish) {
  78. Graphics_drawInnerBox (g);
  79. Graphics_marksBottom (g, 2, true, true, false);
  80. Graphics_marksLeft (g, 2, true, true, false);
  81. }
  82. }
  83. autoPatternList Matrix_to_PatternList (Matrix me, integer join) {
  84. try {
  85. if (join < 1) {
  86. join = 1;
  87. }
  88. Melder_require (my ny % join == 0, U"Number of rows should be a multiple of join factor.");
  89. autoPatternList thee = PatternList_create (my ny / join, join * my nx);
  90. integer r = 0, c = 1;
  91. for (integer i = 1; i <= my ny; i ++) {
  92. if ( (i - 1) % join == 0) {
  93. r ++;
  94. c = 1;
  95. }
  96. for (integer j = 1; j <= my nx; j ++) {
  97. thy z [r] [c ++] = my z [i] [j];
  98. }
  99. }
  100. return thee;
  101. } catch (MelderError) {
  102. Melder_throw (me, U": not converted to PatternList.");
  103. }
  104. }
  105. autoMatrix PatternList_to_Matrix (PatternList me) {
  106. try {
  107. autoMatrix thee = Thing_new (Matrix);
  108. my structMatrix :: v_copy (thee.get());
  109. return thee;
  110. } catch (MelderError) {
  111. Melder_throw (me, U": not converted to Matrix.");
  112. }
  113. }
  114. autoPatternList ActivationList_to_PatternList (ActivationList me) {
  115. try {
  116. autoPatternList thee = Thing_new (PatternList);
  117. my structMatrix :: v_copy (thee.get());
  118. return thee;
  119. } catch (MelderError) {
  120. Melder_throw (me, U": not converted to PatternList.");
  121. }
  122. }
  123. /* End of file PatternList.cpp */