| 1 |
# test_TableOfReal_extensions.praat |
| 2 |
# djmw 20151023, 20180911 |
| 3 |
|
| 4 |
eps = 2.3e-16 |
| 5 |
|
| 6 |
appendInfoLine: "test_TableOfReal_extensions.praat" |
| 7 |
|
| 8 |
@means_by_row_labels |
| 9 |
@centre_rows |
| 10 |
@centre_columns |
| 11 |
@normalize |
| 12 |
|
| 13 |
appendInfoLine: "test_TableOfReal_extensions.praat OK" |
| 14 |
|
| 15 |
procedure means_by_row_labels |
| 16 |
appendInfoLine: tab$, "means_by_row_labels" |
| 17 |
.nrows = 100 |
| 18 |
.ncols = 3 |
| 19 |
.tab1 = Create TableOfReal: "tab", .nrows, .ncols |
| 20 |
for .i to .nrows |
| 21 |
if (.i mod 2) = 0 |
| 22 |
Set row label (index): .i, "even" |
| 23 |
else |
| 24 |
Set row label (index): .i, "odd" |
| 25 |
endif |
| 26 |
endfor |
| 27 |
Formula: ~ if (row mod 2) = 0 then col else 2*col fi |
| 28 |
.tabm = To TableOfReal (means by row labels): "no" |
| 29 |
.nrowsm = Get number of rows |
| 30 |
.ncolsm = Get number of columns |
| 31 |
assert .nrowsm = 2 |
| 32 |
assert .ncolsm = .ncols |
| 33 |
for .i to .nrowsm |
| 34 |
.rowLabel$ = Get row label: .i |
| 35 |
for .j to .ncols |
| 36 |
if .rowLabel$ = "even" |
| 37 |
.dif = abs (object [.tabm, .i, .j] - .j) |
| 38 |
else |
| 39 |
.dif = abs (object [.tabm, .i, .j] - .j*2) |
| 40 |
endif |
| 41 |
assert .dif <= eps |
| 42 |
endfor |
| 43 |
endfor |
| 44 |
removeObject: .tab1, .tabm |
| 45 |
endproc |
| 46 |
|
| 47 |
procedure centre_rows |
| 48 |
appendInfoLine: tab$, "centre_rows" |
| 49 |
.eps = 3*2.3e-16 |
| 50 |
.nrow = 20 |
| 51 |
.ncol = 3 |
| 52 |
.tab = Create TableOfReal: "tab", .nrow, .ncol |
| 53 |
Formula: ~ if col=1 then row else if col = 2 then 0 else -row fi fi |
| 54 |
Formula: ~ self+1 |
| 55 |
Centre rows |
| 56 |
for .irow to .nrow |
| 57 |
.dif = abs (object [.tab, .irow, 1] - .irow) / .irow |
| 58 |
assert .dif < .eps |
| 59 |
.dif = abs (object [.tab, .irow, 3] + .irow) / .irow |
| 60 |
assert .dif < .eps |
| 61 |
assert abs (object [.tab, .irow, 2]) < .eps |
| 62 |
endfor |
| 63 |
removeObject: .tab |
| 64 |
endproc |
| 65 |
|
| 66 |
procedure centre_columns |
| 67 |
appendInfoLine: tab$, "centre_columns" |
| 68 |
.eps = 3*2.3e-16 |
| 69 |
.nrow = 3 |
| 70 |
.ncol = 20 |
| 71 |
.tab = Create TableOfReal: "tab", .nrow, .ncol |
| 72 |
Formula: ~ if row=1 then col else if row = 2 then 0 else -col fi fi |
| 73 |
Formula: ~ self+1 |
| 74 |
Centre columns |
| 75 |
for .icol to .ncol |
| 76 |
.dif = abs (object [.tab, 1, .icol] - .icol) / .icol |
| 77 |
assert .dif < .eps |
| 78 |
.dif = abs (object [.tab, 3, .icol] + .icol) / .icol |
| 79 |
assert .dif < .eps |
| 80 |
assert abs (object [.tab, 2, .icol]) < .eps |
| 81 |
endfor |
| 82 |
removeObject: .tab |
| 83 |
endproc |
| 84 |
|
| 85 |
procedure normalize |
| 86 |
# simple check |
| 87 |
appendInfoLine: tab$, "normalize" |
| 88 |
.nrow = 10 |
| 89 |
.ncol = 10 |
| 90 |
.tab = Create TableOfReal: "tab", .nrow, .ncol |
| 91 |
Formula: ~ row |
| 92 |
.norm = Get table norm |
| 93 |
# sum(k,1,n, k^2)=n(n+1)(2n+1)/6 |
| 94 |
.sumsq_column = .nrow *(.nrow+1)*(2*.nrow+1)/6 |
| 95 |
.normwanted = sqrt (.ncol*.sumsq_column) |
| 96 |
.eps =1e-15 |
| 97 |
assert 1-.eps < .norm/.normwanted && .norm/.normwanted < 1+.eps; before |
| 98 |
.nall = Copy: "all" |
| 99 |
Normalize table: 1.0 |
| 100 |
.norm = Get table norm |
| 101 |
assert 1-.eps < .norm && .norm < 1+.eps; after |
| 102 |
selectObject: .tab |
| 103 |
.rows = Copy: "rows" |
| 104 |
Normalize rows: 1.0 |
| 105 |
.norm = Get table norm |
| 106 |
.normwanted = sqrt (10) |
| 107 |
assert 1-.eps < .norm/.normwanted && .norm/.normwanted < 1+.eps; rows |
| 108 |
selectObject: .tab |
| 109 |
Normalize columns: 1.0 |
| 110 |
.norm = Get table norm |
| 111 |
.normwanted = sqrt (10) |
| 112 |
assert 1-.eps < .norm/.normwanted && .norm/.normwanted < 1+.eps; rows |
| 113 |
removeObject: .tab, .nall, .rows |
| 114 |
endproc |