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