Debian Codegraph @

dwtest/test_LPC.praat

raw
1 # test_LPC.praat
2 # djmw 20080122, 20110528, 20180318
3
4 appendInfoLine: "test_LPC"
5
6 @testCornerCases
7
8 @testLPCInterface
9
10 @testDifferentAnalyses
11
12 @more
13
14 appendInfoLine: "test_LPC OK"
15
16 procedure testLPCInterface
17 .sound = Create Sound from formula: "noise", 1, 0, 1, 10000, "randomGauss(0,0.1)"
18 .lpc = To LPC (autocorrelation): 10, 0.025, 0.005, 50
19
20 appendInfoLine: tab$, "test LPC interface"
21 appendInfoLine: tab$, tab$, "Query"
22 .startTime = Get start time
23
24 .t1 = Get time from frame number: 1
25 .endTime = Get end time
26 .totalDuration = Get total duration
27 .numberOfFrames = Get number of frames
28 .timeStep = Get time step
29 for .iframe to .numberOfFrames
30 .time = Get time from frame number: .iframe
31 .frameNumber = Get frame number from time: .time
32 .numberOfCoefficients = Get number of coefficients: .iframe
33 endfor
34 .samplingInterval = Get sampling interval
35
36 appendInfoLine: tab$, tab$, "Modify"
37 Shift times by: randomUniform (0.1, 0.9)
38 Shift times to: "start time", .startTime
39 Shift times to: "centre time", .startTime
40 Shift times to: "end time", .endTime
41 Scale times by: randomUniform (0.5, 2.5)
42 Scale times to: .startTime, .endTime
43
44 appendInfoLine: tab$, tab$, "Extract"
45 for .i to 10
46 selectObject: .lpc
47 .bandwidthReduction = randomUniform (0, 200)
48 .time = randomUniform (.startTime, .endTime)
49 .spectrum = To Spectrum (slice): .time, 20, .bandwidthReduction, 50
50 selectObject: .lpc
51 .vocalTract1 = To VocalTract (slice): .time, 17.0
52 selectObject: .lpc
53 .glottalDamping = randomUniform (0.01, 0.4)
54 .radiationDamping$ = if randomUniform (0,1) > 0.5 then "yes" else "no" fi
55 .internalDamping$ = if randomUniform (0,1) > 0.5 then "yes" else "no" fi
56 .vocalTract2 = To VocalTract (slice, special): .time, .glottalDamping, .radiationDamping$, .internalDamping$
57 selectObject: .lpc
58 .polynomial = To Polynomial (slice): .time
59 removeObject: .spectrum, .vocalTract1, .vocalTract2, .polynomial
60 endfor
61 selectObject: .lpc
62 .matrix = Down to Matrix (lpc)
63 removeObject: .matrix
64
65 appendInfoLine: tab$, tab$, "Analyse"
66 selectObject: .lpc
67 .formant1 = To Formant
68 selectObject: .lpc
69 .formant2 = To Formant (keep all)
70 for .numberOfCoefficients to 12
71 selectObject: .lpc
72 .lfcc = To LFCC: .numberOfCoefficients
73 removeObject: .lfcc
74 endfor
75 selectObject: .lpc
76 .spectrogram = To Spectrogram: 20, 0, 50
77 selectObject: .lpc
78 .lineSpectralFrequencies = To LineSpectralFrequencies: 0.0
79 removeObject: .spectrogram, .formant1, .formant2, .lineSpectralFrequencies, .sound, .lpc
80 endproc
81
82 procedure testCornerCases
83 appendInfoLine: tab$, "Test corner cases"
84 .samplingFrequency = 10000
85 .predictionOrder = 20
86 .shorty = Create Sound from formula: "Shorty", 1, 0,
87 ... 0.004, .samplingFrequency, ~ 1/2 * sin(2*pi*377*x) + randomGauss(0,0.1)
88 asserterror Your sound is shorter than two window lengths.
89 ... Either your sound is too short or your window is too long.
90 .lpc = To LPC (burg): .predictionOrder, 0.025, 0.005, 50
91 .notEnoughSamples = Create Sound from formula: "Shorty", 1, 0,
92 ... 0.002, .samplingFrequency, ~ 1/2 * sin(2*pi*377*x) + randomGauss(0,0.1)
93
94 .error$ = "Analysis window duration too short."
95 ... + " For a prediction order of " + string$ (.predictionOrder)
96 ... + ", the analysis window duration should be greater than "
97 ... + fixed$ (.predictionOrder / .samplingFrequency, 5)
98 ... + " s. Please increase the analysis window duration or "
99 ... + "lower the prediction order."
100
101 # asserterror cannot deal with variables, we need the evaluation quotes ' '
102 asserterror '.error$'
103 .lpc = To LPC (burg): .predictionOrder, 0.001, 0.005, 50
104
105 removeObject: .shorty,.notEnoughSamples
106 appendInfoLine: tab$, "Test corner cases OK"
107 endproc
108
109 procedure more
110 # formants of straight tube
111
112 fb5$ = ""
113 space$ = ""
114 for i to 5
115 f[i] = (2*i-1)*500
116 b[i] = i * 50
117 fb5$ = fb5$ + space$ + string$ (f[i] ) + " " + string$ (b[i])
118 space$ = " "
119 endfor
120
121 debug = 0
122
123 pt = Create PitchTier: "pt", 0, 0.5
124 Add point: 0, 150
125 source = To Sound (pulse train): 44100, 1, 0.05, 2000, "no"
126 ft = Create FormantTier: "ft", 0, 0.5
127 Add point: 0, fb5$
128
129 selectObject: ft, source
130 soundpre = Filter
131
132 sound = Resample: 10000, 50
133
134 removeObject: pt, source, ft, soundpre
135
136 for imethod to 4
137 selectObject: sound
138 if imethod = 1
139 method$ = "auto"
140 lpc = To LPC (autocorrelation)... 10 0.025 0.005 50
141 elsif imethod = 2
142 method$ = "covar"
143 lpc = To LPC (covariance)... 10 0.025 0.005 50
144 elsif imethod = 3
145 method$ = "burg"
146 lpc = To LPC (burg)... 10 0.025 0.005 50
147 elsif imethod = 4
148 method$ = "marple"
149 lpc = To LPC (marple)... 10 0.025 0.005 50 1e-06 1e-06
150 endif
151 call get_formants 'lpc' 'method$'
152 deltaf1 = deltaf
153 deltab1 = deltab
154 assert deltaf1 < 0.07 and deltab1 < 3.2; ('method$')
155
156 select lpc
157 plus sound
158 lpcr = To LPC (robust)... 0.025 50 1.5 5 1e-06 no
159 call get_formants lpcr 'method$':robust
160 deltaf2 = deltaf
161 deltab2 = deltab
162 assert deltaf2 < 0.04 and deltab2 < 1.1; ('method$':robust) 'deltaf2' 'deltab2'
163 select lpc
164 plus lpcr
165 Remove
166 endfor
167
168 select sound
169 Remove
170 endproc
171
172 procedure get_formants: .lpc, .method$
173 selectObject: .lpc
174 .formant = To Formant (keep all)
175 deltaf = 0
176 deltab = 0
177 for .nf to 5
178 .f[.nf] = Get quantile: .nf, 0, 0, "Hertz", 0.5
179 .b[.nf] = Get quantile of bandwidth: .nf, 0, 0, "Hertz", 0.5
180 if .nf <= 3
181 deltaf += abs (f[.nf] - .f[.nf]) / f[.nf]
182 deltab += abs (b[.nf]- .b[.nf]) / b[.nf]
183 endif
184 endfor
185 deltaf /= 3
186 deltab /= 3
187 appendInfoLine: tab$, .method$, " deltaf = ", fixed$ (deltaf, 4), " deltab = ", fixed$ (deltab, 4),
188 ... " ", fixed$ (.f[1], 0), " ", fixed$ (.b[1], 0), " ", fixed$ (.f[2], 0), " ", fixed$ (.b[2], 0),
189 ... " ", fixed$ (.f[3], 0), " ", fixed$ (.b[3], 0), " ", fixed$ (.f[4], 0), " ", fixed$ (.b[4], 0),
190 ... " ", fixed$ (.f[5], 0), " ", fixed$ (.b[5], 0)
191 removeObject: .formant
192 endproc
193
194 procedure testDifferentAnalyses
195 appendInfoLine: tab$, "LPC algorithms:"
196 soundraw = Read from file: "sa1.wav"
197 sound = Resample: 10000, 50
198 appendInfo: tab$, tab$, "LPC autocorrelation:"
199 for order from 8 to 14
200 selectObject: sound
201 lpc = To LPC (autocorrelation): order, 0.025, 0.005, 50.0
202 appendInfo: " ", order
203 removeObject: lpc
204 endfor
205 appendInfo: newline$
206
207 appendInfo: tab$, tab$, "LPC covariance:"
208 for order from 8 to 14
209 selectObject: sound
210 lpc = To LPC (covariance): order, 0.025, 0.005, 50.0
211 appendInfo: " ", order
212 removeObject: lpc
213 endfor
214 appendInfo: newline$
215
216 appendInfo: tab$, tab$, "LPC burg:"
217 for order from 8 to 14
218 selectObject: sound
219 lpc = To LPC (burg): order, 0.025, 0.005, 50.0
220 appendInfo: " ", order
221 removeObject: lpc
222 endfor
223 appendInfo: newline$
224
225 appendInfo: tab$, tab$, "LPC marple:"
226 for order from 8 to 14
227 selectObject: sound
228 lpc = To LPC (marple): order, 0.025, 0.005, 50.0, 1.0e-6, 1.0e-6
229 appendInfo: " ", order
230 removeObject: lpc
231 endfor
232 appendInfo: newline$
233
234 appendInfo: tab$, tab$, "LPC robust:"
235 for order from 8 to 14
236 selectObject: sound
237 lpc = To LPC (robust): order, 0.025, 0.005, 50.0, 1.5, 5, 1.0e-6
238 appendInfo: " ", order
239 removeObject: lpc
240 endfor
241 appendInfo: newline$
242
243 appendInfo: tab$, tab$, "Sound & LPC robust:"
244 for order from 8 to 14
245 selectObject: sound
246 lpc = To LPC (autocorrelation): order, 0.025, 0.005, 50.0
247 selectObject: sound, lpc
248 lpc2 = To LPC (robust): 0.025, 50.0, 1.5, 5, 1.0e-6, "no"
249 appendInfo: " ", order
250 removeObject: lpc, lpc2
251 endfor
252 appendInfo: newline$
253 removeObject: soundraw, sound
254 appendInfoLine: tab$, "LPC algorithms: OK"
255
256 endproc
257
258