Debian Codegraph @

dwtest/test_Formant_slopes.praat

raw
1 # test_Formant_slopes.praat
2 # djmw 20210112
3
4 appendInfoLine: "test_Formant_slopes.praat"
5
6 sound = Create Sound from formula: "sineWithNoise", 1, 0, 0.2, 44100, "1/2 * sin(2*pi*377*x) + randomGauss(0,0.1)"
7 formant = To Formant (burg): 0, 5, 5000, 0.025, 50
8 Formula (bandwidths): ~ 100
9 tmin = 0.1
10 tmax = 0.2
11
12 info_exp = 1
13 info_par = 1
14 info_sig = 1
15 for i to 10
16 @test_exponential
17 @test_parabolical
18 @test_sigmoid
19 endfor
20
21 removeObject: sound, formant
22 appendInfoLine: "test_Formant_slopes.praat OK"
23
24 procedure test_exponential
25 .model$ = "Exp"
26 .pi# = {randomUniform (300, 700), randomUniform (-200, 200), randomUniform (-20, 20)}
27 .p# = .pi#
28 Formula (frequencies): ~ if x>=tmin and x <= tmax then .p#[1] + .p#[2]*exp(.p#[3]*(x - tmin)) + 1000 * (row - 1) else 0 fi
29 for .iformant to 3
30 .slope# = List formant slope: .iformant, tmin, tmax, "Exponential plus constant"
31 .p# [1] = .pi#[1] + 1000 * (.iformant - 1)
32 .flocus = .p#[1] + .p#[2]
33 .ftarget = .p#[1] + .p#[2] * exp (.p#[3] * (tmax - tmin))
34 .slope = (.ftarget - .flocus) / (tmax - tmin)
35 .truth# = {.slope, 0, .flocus, .ftarget, .p# [1], .p# [2], .p# [3]}
36 @extraInfo: info_exp, .truth#, .slope#, .model$ + string$ (.iformant)+ ": "
37 assert .slope# [2] > 0.99
38 endfor
39 endproc
40
41 procedure test_parabolical
42 .model$ = "Par"
43 .pi# = {randomUniform (300, 700), randomUniform (-200, 200), randomUniform (-20, 20)}
44 .p# = .pi#
45 Formula (frequencies): ~if x>=tmin and x <= tmax then .p#[1] + .p#[2]*(x - tmin)+ .p#[3]*(x-tmin)^2 + 1000 * (row - 1) else 0 fi
46 ;Formula (frequencies): ~ self+randomGauss (0, row*1) ;
47 for .iformant to 3
48 .slope# = List formant slope: .iformant, tmin, tmax, "Parabolic"
49 .p# [1] = .pi# [1] + 1000 * (.iformant -1)
50 .flocus = .p# [1]
51 .ftarget = .p# [1] + .p# [2] * (tmax - tmin) + .p# [3] * (tmax - tmin)^2
52 .slope = (.ftarget - .flocus) / (tmax - tmin)
53 .truth# = {.slope, 0, .flocus, .ftarget, .p# [1], .p# [2], .p# [3]}
54 @extraInfo: info_par, .truth#, .slope#, .model$+string$ (.iformant)+": "
55 assert .slope# [2] > 0.99
56 endfor
57 endproc
58
59 procedure test_sigmoid
60 .model$ = "Sig"
61 .pi# = {randomUniform (300, 700), randomUniform (-200, 200), randomUniform (tmin, tmax), randomUniform (0.1, 10)}
62 .p# = .pi#
63 Formula (frequencies): ~ if x>=tmin and x <= tmax then .p# [1] + .p# [2] / (1 + exp (- (x - .p# [3])/.p# [4])) + 1000 * (row - 1) else 0 fi
64 for .iformant to 3
65 .slope# = List formant slope: .iformant, tmin, tmax, "Sigmoid plus constant"
66 .p# [1] = .pi# [1] + 1000 * (.iformant -1)
67 .flocus = .p# [1] + .p# [2] / (1 + exp (- (- .p# [3])/.p# [4]))
68 .ftarget = .p# [1] + .p# [2] / (1 + exp (- (tmax -tmin - .p# [3])/.p# [4]))
69 .slope = (.ftarget - .flocus) / (tmax - tmin)
70 .truth# = {.slope, 0, .flocus, .ftarget, .p# [1], .p# [2], .p# [3], .p# [4]}
71 @extraInfo: info_sig, .truth#, .slope#, .model$+string$ (.iformant)+": "
72 assert .slope# [2] > 0.99
73 endfor
74 endproc
75
76 procedure extraInfo: .want_info, .truth#, .slope#, .text$
77 if .want_info
78 appendInfo: tab$ + .text$, " slope=(", fixed$(.truth#[1],1), ", ", fixed$(.slope#[1],1), ") "," r^2=", fixed$(.slope#[2],3),
79 ... newline$, tab$, tab$, "a=(", fixed$(.truth#[5],1), ", ", fixed$(.slope#[5],1), ")",
80 ... " b=(", fixed$(.truth#[6],1), ", ", fixed$(.slope#[6],1), ") ",
81 ... " c=(", fixed$(.truth#[7],1), ", ", fixed$(.slope#[7],1), ") "
82 if size (.slope#) == 8
83 appendInfo: " d=(", fixed$(.truth#[8],1), ", ", fixed$(.slope#[8],1), ") "
84 endif
85 appendInfo: newline$
86 endif
87 endproc