File: PatternList.cpp

package info (click to toggle)
praat 6.4.27%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 206,060 kB
  • sloc: cpp: 1,409,811; ansic: 286,305; makefile: 946; python: 340; sh: 35
file content (121 lines) | stat: -rw-r--r-- 3,503 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* PatternList.cpp
 *
 * Copyright (C) 1993-2019 David Weenink
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This code is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this work. If not, see <http://www.gnu.org/licenses/>.
 */

/*
 djmw 20020813 GPL header
 djmw 20041203 Added _PatternList_checkElements.
 djmw 20071017 Melder_error<p>
  djmw 20110304 Thing_new
*/

#include "PatternList.h"

Thing_implement (PatternList, Matrix, 2);

bool _PatternList_checkElements (PatternList me) {
	for (integer i = 1; i <= my ny; i ++) {
		for (integer j = 1; j <= my nx; j ++)
			if (my z [i] [j] < 0 || my z [i] [j] > 1)
				return false;
	}
	return true;
}

autoPatternList PatternList_create (integer ny, integer nx) {
	try {
		autoPatternList me = Thing_new (PatternList);
		Matrix_init (me.get(), 1, nx, nx, 1, 1, 1, ny, ny, 1, 1);
		return me;
	} catch (MelderError) {
		Melder_throw (U"PatternList not created.");
	}
}

void PatternList_normalize (PatternList me, int choice, double pmin, double pmax) {
	if (pmin == pmax)
		(void) Matrix_getWindowExtrema (me, 1, my nx, 1, my ny, & pmin, & pmax);
	if (pmin == pmax)
		return;

	if (choice == 1) {
		for (integer i = 1; i <= my ny; i ++)
			for (integer j = 1; j <= my nx; j ++)
				my z [i] [j] = (my z [i] [j] - pmin) / (pmax - pmin);
	} else { /* default choice */
		for (integer i = 1; i <= my ny; i ++) {
			double sum = 0;
			for (integer j = 1; j <= my nx; j ++)
				sum += (my z [i] [j] -= pmin);
			for (integer j = 1; j <= my nx; j ++)
				my z [i] [j] *= 1.0 / sum;
		}
	}
}

void PatternList_draw (PatternList me, Graphics g, integer pattern, double xmin, double xmax, double ymin, double ymax, bool garnish) {
	Matrix_drawRows (me, g, xmin, xmax, pattern - 0.5, pattern + 0.5, ymin, ymax);
	if (garnish) {
		Graphics_drawInnerBox (g);
		Graphics_marksBottom (g, 2, true, true, false);
		Graphics_marksLeft (g, 2, true, true, false);
	}
}

autoPatternList Matrix_to_PatternList (Matrix me, integer join) {
	try {
		if (join < 1) join = 1;
		Melder_require (my ny % join == 0,
			U"Number of rows should be a multiple of join factor.");
		autoPatternList thee = PatternList_create (my ny / join, join * my nx);

		integer r = 0, c = 1;
		for (integer i = 1; i <= my ny; i ++) {
			if ( (i - 1) % join == 0) {
				r ++;
				c = 1;
			}
			for (integer j = 1; j <= my nx; j ++)
				thy z [r] [c ++] = my z [i] [j];
		}
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": not converted to PatternList.");
	}
}

autoMatrix PatternList_to_Matrix (PatternList me) {
	try {
		autoMatrix thee = Thing_new (Matrix);
		my structMatrix :: v1_copy (thee.get());
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": not converted to Matrix.");
	}
}

autoPatternList ActivationList_to_PatternList (ActivationList me) {
	try {
		autoPatternList thee = Thing_new (PatternList);
		my structMatrix :: v1_copy (thee.get());
		return thee;
	} catch (MelderError) {
		Melder_throw (me, U": not converted to PatternList.");
	}
}

/* End of file PatternList.cpp */