| 1 |
# test_PowerCepstrum.praat |
| 2 |
# djmw 20190910, 20220721, 20241201, 20251231 |
| 3 |
|
| 4 |
appendInfoLine: "test_PowerCepstrum.praat" |
| 5 |
|
| 6 |
@testCalibratedPowerCepstrums: 10 |
| 7 |
|
| 8 |
@testCalibratedPowerCepstrograms: 10 |
| 9 |
|
| 10 |
@test_trendSubtraction |
| 11 |
|
| 12 |
@draw_powercepstrum |
| 13 |
|
| 14 |
@testPowerCepstrogramSmoothing |
| 15 |
|
| 16 |
@calibratedPowerCepstrumSmoothing: 1 |
| 17 |
|
| 18 |
@test_perfectCosineSpectra |
| 19 |
|
| 20 |
@powerCepstrumSmoothingsOldAndNew |
| 21 |
|
| 22 |
@testSearchDomain |
| 23 |
|
| 24 |
appendInfoLine: "test_PowerCepstrum.praat OK" |
| 25 |
|
| 26 |
procedure testCalibratedPowerCepstrums: .ntries |
| 27 |
appendInfoLine: tab$, "testCalibratedPowerCepstrums" |
| 28 |
for .itry to .ntries |
| 29 |
.duration = randomUniform (0.05, 0.1) |
| 30 |
.approximateF0 = randomUniform (61, 330) |
| 31 |
.peak_dB = randomUniform (20, 70) |
| 32 |
.backgroundDistance_dB = randomUniform (10, 50) |
| 33 |
@testCalibratedPowerCepstrumCPP: .duration, .approximateF0, .peak_dB, .peak_dB - .backgroundDistance_dB |
| 34 |
endfor |
| 35 |
appendInfoLine: tab$, "testCalibratedPowerCepstrums OK" |
| 36 |
endproc |
| 37 |
|
| 38 |
procedure testCalibratedPowerCepstrograms: .ntries |
| 39 |
for .itry to .ntries |
| 40 |
.duration = randomUniform (0.3, 2.0) |
| 41 |
.approximateF0 = randomUniform (61, 330) |
| 42 |
.peak_dB = randomUniform (20, 70) |
| 43 |
.backgroundDistance_dB = randomUniform (10, 50) |
| 44 |
@testCalibratedPowerCepstrogram: .duration, .approximateF0, .peak_dB, .peak_dB - .backgroundDistance_dB |
| 45 |
endfor |
| 46 |
endproc |
| 47 |
|
| 48 |
procedure other |
| 49 |
selectObject: powercepstrogram |
| 50 |
cpps_subtractTrend = Get CPPS: "yes", 0, 0, 60, 333.3, 0.05, "parabolic", 0.001, 0.05, "Straight", "Robust slow" |
| 51 |
cpps_nosubtractTrend = Get CPPS: "no", 0, 0, 60, 333.3, 0.05, "parabolic", 0.001, 0.05, "Straight", "Robust slow" |
| 52 |
assert abs (cpps_subtractTrend - cpps_nosubtractTrend) < 1e-10 |
| 53 |
|
| 54 |
cpps = Get CPPS: "yes", 0.02, 0.0005, 60, 333.3, 0.05, "parabolic", 0.001, 0.05, "Straight", "Robust slow" |
| 55 |
assert cpps <= cpps_subtractTrend |
| 56 |
appendInfoLine: tab$, "CPPS from Cepstrogram: ", cpps, " dB" |
| 57 |
table = To Table (cepstral peak prominences): "yes", "yes", 6, 3, "yes", 3, 60, 330, 0.05, "parabolic", 0.001, 0.05, "Straight", "Robust slow" |
| 58 |
removeObject: table |
| 59 |
selectObject: powercepstrogram |
| 60 |
powercepstrum = To PowerCepstrum (slice): 0.1 |
| 61 |
peak_dB = Get peak: 60, 333, "Parabolic" |
| 62 |
peak_q = Get quefrency of peak: 60, 333, "Parabolic" |
| 63 |
endproc |
| 64 |
|
| 65 |
procedure old_commands |
| 66 |
selectObject: powercepstrum |
| 67 |
slope_old = Get tilt line slope: 0.001, 0, "Exponential decay", "Robust" |
| 68 |
slope = Get trend line slope: 0.001, 0, "Exponential decay", "Robust" |
| 69 |
assert (slope_old = slope) |
| 70 |
appendInfoLine: tab$, "old Slope: ", slope, " (exponential, robust)" |
| 71 |
intercept_old = Get tilt line intercept: 0.001, 0, "Straight", "Robust" |
| 72 |
intercept = Get trend line intercept: 0.001, 0, "Straight", "Robust" |
| 73 |
assert intercept_old = intercept |
| 74 |
appendInfoLine: tab$, "old Intercept: ", intercept, " dB (straight, robust)" |
| 75 |
rnr = Get rhamonics to noise ratio: 60, 333.3, 0.05 |
| 76 |
appendInfoLine: tab$, "old Rhamonics-to-noise-ratio: ", rnr |
| 77 |
endproc |
| 78 |
|
| 79 |
procedure test_perfectCosineSpectra |
| 80 |
appendInfoLine: tab$, "test_perfectCosineSpectra" |
| 81 |
for ipitch to 5 |
| 82 |
.f0 = randomInteger (600, 3300) / 10 |
| 83 |
@test_perfectCosine: .f0 |
| 84 |
endfor |
| 85 |
appendInfoLine: tab$, "test_perfectCosineSpectra OK" |
| 86 |
endproc |
| 87 |
|
| 88 |
procedure powerCepstrumSmoothingsOldAndNew |
| 89 |
appendInfoLine: tab$, "powerCepstrumSmoothings (old and new)" |
| 90 |
random_initializeWithSeedUnsafelyButPredictably (111) |
| 91 |
Debug: "no", -4 |
| 92 |
@testPowerCepstrumSmoothing: "Old smoothing (square)" |
| 93 |
Debug: "no", -5 |
| 94 |
@testPowerCepstrumSmoothing: "Gaussian smoothing (4 sigmas)" |
| 95 |
Debug: "no", 0 |
| 96 |
@testPowerCepstrumSmoothing: "Default smoothing (integration)" |
| 97 |
random_initializeSafelyAndUnpredictably () |
| 98 |
appendInfoLine: tab$, "powerCepstrumSmoothings (old and new) OK" |
| 99 |
endproc |
| 100 |
|
| 101 |
procedure test_trendSubtraction |
| 102 |
appendInfoLine: tab$, "test_trendSubtraction" |
| 103 |
.toneComplex = Create Sound as tone complex: "toneComplex", 0, 1, 44100, "cosine", 100, 0, 0, 0 |
| 104 |
.powercepstrogram = To PowerCepstrogram: 60, 0.002, 5000, 50 |
| 105 |
.powercepstrum = To PowerCepstrum (slice): 0.1 |
| 106 |
.trendType$# = { "Straight", "Exponential decay" } |
| 107 |
.fitMethod$# = { "Robust", "Robust slow" } |
| 108 |
.interpolation$# = { "none", "parabolic", "cubic" } |
| 109 |
.epsilon# = {1e-10, 1e-2, 1e-2} |
| 110 |
for .interpolation to size (.interpolation$#) |
| 111 |
.interpolation$ = .interpolation$# [.interpolation] |
| 112 |
.eps = .epsilon# [.interpolation] |
| 113 |
for .trendType to size (.trendType$#) |
| 114 |
.trendType$ = .trendType$# [.trendType] |
| 115 |
for .fitMethod to size (.fitMethod$#) |
| 116 |
.fitMethod$ = .fitMethod$# [.fitMethod] |
| 117 |
appendInfoLine: tab$, tab$, .interpolation$, "; ", .trendType$, "; ", .fitMethod$ |
| 118 |
selectObject: .powercepstrum |
| 119 |
.prominence1 = Get peak prominence: 60, 350, .interpolation$, |
| 120 |
... 0.001, 0.05, .trendType$, .fitMethod$ |
| 121 |
.trendRemoved = Subtract trend: 0.001, 0.05, .trendType$, .fitMethod$ |
| 122 |
.prominence2 = Get peak prominence: 60, 350, .interpolation$, |
| 123 |
... 0.001, 0.05, .trendType$, .fitMethod$ |
| 124 |
# the assertion fails for the least squares fit. |
| 125 |
assert abs ((.prominence1 - .prominence2)/.prominence1) < .eps ; '.prominence1' '.prominence2' '.fitMethod$' |
| 126 |
removeObject: .trendRemoved |
| 127 |
endfor |
| 128 |
endfor |
| 129 |
endfor |
| 130 |
removeObject: .toneComplex, .powercepstrogram, .powercepstrum |
| 131 |
appendInfoLine: tab$, "test_trendSubtraction: OK" |
| 132 |
endproc |
| 133 |
|
| 134 |
procedure draw_powercepstrum |
| 135 |
.sound = Create Sound as tone complex: "t", 0, 1, 44100, "cosine", |
| 136 |
... 100, 0, 0, 0 |
| 137 |
.powercepstrogram = To PowerCepstrogram: 60, 0.002, 5000, 50 |
| 138 |
.powercepstrum = To PowerCepstrum (slice): 0.1 |
| 139 |
Erase all |
| 140 |
Colour: "Black" |
| 141 |
Select outer viewport: 0, 5, 0, 4 |
| 142 |
Draw: 0, 0, 0, 0, "yes" |
| 143 |
Colour: "Green" |
| 144 |
Draw trend line: 0, 0, 0, 0, 0.001, 0, "Exponential decay", "Robust" |
| 145 |
Colour: "Red" |
| 146 |
Draw trend line: 0, 0, 0, 0, 0.001, 0, "Straight", "Least squares" |
| 147 |
Colour: "Black" |
| 148 |
removeObject: .sound, .powercepstrogram, .powercepstrum |
| 149 |
endproc |
| 150 |
|
| 151 |
procedure testPowerCepstrumSmoothing: .info$ |
| 152 |
appendInfoLine: tab$, tab$,"test smoothing PowerCepstrogram of noisy sound: ", .info$ |
| 153 |
.kg = Create KlattGrid from vowel: "a", 0.4, 125, 800, 50, 1200, 50, 2300, 100, 2800, 0.05, 1000 |
| 154 |
.cpps_min = 1000 |
| 155 |
.cpps_max = - .cpps_min |
| 156 |
.cpps_min1 = .cpps_min |
| 157 |
.cpps_max1 = - .cpps_min1 |
| 158 |
for .i to 10 |
| 159 |
selectObject: .kg |
| 160 |
.sounda = To Sound |
| 161 |
Formula: ~ self + randomGauss (0, 0.1) |
| 162 |
selectObject: .sounda |
| 163 |
.powercepstrogram = To PowerCepstrogram: 60, 0.002, 5000, 50 |
| 164 |
.powercepstrum = To PowerCepstrum (slice): 0.1 |
| 165 |
.cpps = Get peak prominence: 60, 333.3, "parabolic", 0.001, 0.05, "Straight", "Robust slow" |
| 166 |
.cpps_min = min (.cpps_min , .cpps) |
| 167 |
.cpps_max = max (.cpps_max , .cpps) |
| 168 |
.smooth = Smooth: 0.0005, 1 |
| 169 |
.cpps1 = Get peak prominence: 60, 333.3, "parabolic", 0.001, 0.05, "Straight", "Robust slow" |
| 170 |
.cpps_min1 = min (.cpps_min1 , .cpps1) |
| 171 |
.cpps_max1 = max (.cpps_max1 , .cpps1) |
| 172 |
removeObject: .powercepstrogram, .powercepstrum, .smooth, .sounda |
| 173 |
endfor |
| 174 |
appendInfoLine: tab$, tab$, fixed$ (.cpps_min, 2), " / " , fixed$ (.cpps_max, 2), " dB min / max; ", |
| 175 |
... fixed$ (.cpps_min1, 2), " / " , fixed$ (.cpps_max1, 2), "dB min / max after smoothing" |
| 176 |
removeObject: .kg |
| 177 |
appendInfoLine: tab$, tab$,"test smoothing of PowerCepstrogram OK" |
| 178 |
endproc |
| 179 |
|
| 180 |
procedure testPowerCepstrogramSmoothing |
| 181 |
appendInfoLine: tab$, "test PowerCepstrogram and PowerCepstrum smoothing" |
| 182 |
.toneComplex = Create Sound as tone complex: "toneComplex", 0, 1, 44100, "cosine", 100, 0, 0, 0 |
| 183 |
.powercepstrogram = To PowerCepstrogram: 60, 0.002, 5000, 50 |
| 184 |
.powercepstrum = To PowerCepstrum (slice): 0.1 |
| 185 |
for .ismooth to 5 |
| 186 |
.quefrencyAveragingWindow = randomUniform (0.0006, 0.0003) |
| 187 |
selectObject: .powercepstrogram |
| 188 |
# smooth only in y-direction |
| 189 |
.smoothcepstrogram = Smooth: 0.0000001, .quefrencyAveragingWindow |
| 190 |
.smoothpowercepstrum = To PowerCepstrum (slice): 0.1 |
| 191 |
.cpps = Get peak prominence: 60, 333.3, "parabolic", 0.001, 0.05, "Straight", "Robust slow" |
| 192 |
selectObject: .powercepstrum |
| 193 |
.smoothpowercepstrum2 = Smooth: .quefrencyAveragingWindow, 1 |
| 194 |
.cpps2 = Get peak prominence: 60, 333.3, "parabolic", 0.001, 0.05, "Straight", "Robust slow" |
| 195 |
assert .cpps == .cpps2 ; |
| 196 |
removeObject: .smoothcepstrogram, .smoothpowercepstrum, .smoothpowercepstrum2 |
| 197 |
endfor |
| 198 |
removeObject: .toneComplex, .powercepstrogram, .powercepstrum |
| 199 |
appendInfoLine: tab$, "test PowerCepstrogram and PowerCepstrum smoothing OK" |
| 200 |
endproc |
| 201 |
|
| 202 |
procedure test_perfectCosine: .f0 |
| 203 |
appendInfo: tab$, tab$, "Cosine (2*pi*x/", .f0, ") in Spectrum" |
| 204 |
# |
| 205 |
# Suppose a Spectrum has only real values x[i] then the display of this |
| 206 |
# spectrum in dB is S(f) = 10*log10 (2*df*x[i]^2/4e-10), for i=2..n-1 and 10*log10 (df*x[i]^2/4e-10) for i=1 & i=n |
| 207 |
# We want the Spectrum display in dB, S(f), to look like a perfect cos. |
| 208 |
# What are the underlying x[i] ? |
| 209 |
# S(f) only has real values. |
| 210 |
# S(f[i]) = offset + amplitude * cos(2*pi*f[i]/pitch) ; on a dB scale |
| 211 |
# |
| 212 |
# S(f[i]) = 10 log10 (d*x[i]^2*df/4e-10), where d=2 for i=2..n-1, d=1 for i=1 & i=n |
| 213 |
# |
| 214 |
# S(f[i]) = 10 log10((x[i]/2e-5)^2 * 2 *df), |
| 215 |
# = 20 log10((x[f]/2e-5) + 10 log10(2*df) i=2..n-1 |
| 216 |
# b2 = 10 log10(2*df), b1 = 10 log10(df) |
| 217 |
# 20 log10((x(f)/2e-5) = S(f[i]) - b2 |
| 218 |
# X(f)/2e-5 = 10^((S(f) - b2)/20) |
| 219 |
# X(f) = 2e-5 * 10^((S(f) - b2)/20) |
| 220 |
.amplitude = 1 |
| 221 |
.offset = 0 |
| 222 |
.duration = 1 |
| 223 |
.sound = Create Sound from formula: "s", 1, 0.0, 1, 44100, ~ .offset + .amplitude * cos(2*pi*.f0*x) |
| 224 |
.spectrum = To Spectrum: "yes" |
| 225 |
.spectrum2 = Copy: "c" |
| 226 |
@fillSpectrumWithCosine: .spectrum2, .f0 |
| 227 |
.pc = To PowerCepstrum |
| 228 |
.qAtPeak = Get quefrency of peak: 60, 330.0, "parabolic" |
| 229 |
.qtoF0 = 1.0 / .qAtPeak |
| 230 |
assert abs(.qtoF0 - .f0)/ .f0 < 0.005; q: '.qtoF0' , '.f0' |
| 231 |
appendInfoLine: " OK" |
| 232 |
removeObject: .sound, .spectrum, .spectrum2, .pc |
| 233 |
endproc |
| 234 |
|
| 235 |
procedure calibratedPowerCepstrumSmoothing: .duration |
| 236 |
appendInfo: tab$, "Calibrated smoothing (", .duration, ") s: " |
| 237 |
.sound = Create Sound from formula: "s", 1, 0.0, .duration, 10000, ~ 0 |
| 238 |
.spectrum = To Spectrum: "yes" |
| 239 |
.pc = To PowerCepstrum |
| 240 |
.dq = Get quefrency step |
| 241 |
.nbins = Get number of quefrency bins |
| 242 |
.value = 1000000.0 |
| 243 |
Formula: ~ .value |
| 244 |
for .ismooth to 5 |
| 245 |
selectObject: .pc |
| 246 |
.smoothing = ((.ismooth - 1) * 2 + 0.3) * .dq |
| 247 |
.smoothed = Smooth: .smoothing, 4 |
| 248 |
for .i to .nbins |
| 249 |
.rdif1 = abs (object[.smoothed, 1, .i] - .value) / .value |
| 250 |
assert .rdif1 < 1e-13 ; 1: '.rdif1' smooting = '.smoothing' |
| 251 |
endfor |
| 252 |
removeObject: .smoothed |
| 253 |
endfor |
| 254 |
selectObject: .pc |
| 255 |
# y = a x + b |
| 256 |
# y[1] = v; y[n] = 2*v |
| 257 |
# b = v; a = v/qend |
| 258 |
.qend = Get end quefrency |
| 259 |
.b = .value |
| 260 |
.a = .value / .qend |
| 261 |
Formula: ~ .a * x + .b |
| 262 |
.smooth2 = Smooth: 0.005, 1 |
| 263 |
.error = 0.007 / .duration |
| 264 |
for .i to .nbins |
| 265 |
.rdif3 = abs (object[.smooth2, 1, .i] - object[.pc, 1, .i]) / object[.pc, 1, .i]; '.rdif' |
| 266 |
assert .rdif3 < .error; 3: '.rdif3' |
| 267 |
endfor |
| 268 |
removeObject: .sound, .spectrum, .pc, .smooth2 |
| 269 |
appendInfoLine: "OK" |
| 270 |
endproc |
| 271 |
|
| 272 |
# |
| 273 |
# Placing a peak exactly in the middle of one bin in the PowerCepstrum and setting the |
| 274 |
# rest of the bins to a constant background value makes it easy |
| 275 |
# to check if the peak picking and the smoothing are done correctly. |
| 276 |
# Instead of the bin index to place the peak we use an approximate F0. |
| 277 |
# The quefrency of this approximateF0 will be quefrency = 1 / approximateF0. |
| 278 |
# The index of this quefrency can be queried by index_real = Get index from quefrency: quefrency |
| 279 |
# This index will be rounded to an integer value and its value will be set to 'peak_dB' |
| 280 |
# while the other values will be set to 'background_dB' |
| 281 |
# Therefore, if we query this PowerCepstrum for its peak value we should exactly get 'peak_dB', |
| 282 |
# a query to the quefrency of the peak should exacly give the quefrency at the bin index, |
| 283 |
# The CPP value should exactly give peak_dB - background_dB. |
| 284 |
# |
| 285 |
# The underlying values in the PowerCepstrum are amplitudes x[i]^2, however if we draw |
| 286 |
# a PowerCepstrum these values are displayed in dB's as 10 * log10 (x[i]^2). |
| 287 |
# The real values in the peak bins are therefore: |
| 288 |
# peak = 10^(peak_dB/10) |
| 289 |
# background = 10^(background_dB/10) |
| 290 |
# |
| 291 |
# After smoothing with a window 2*dq wide, the peak is now three bins wide (dq is the bin |
| 292 |
# width of the PowerCeptrum). |
| 293 |
# The values in the peak bin and in its left and right neihbour are |
| 294 |
# peak1 = (0.5*background + peak + 0.5*background)/2 |
| 295 |
# left1 = (0.5*background + background + 0.5*peak)/2 |
| 296 |
# right1 = (0.5*peak + background + 0.5*background)/2 (left and right are equal) |
| 297 |
# cpp1 = 10*log10 (peak1) - background_dB |
| 298 |
# The samples left and right from the peak will be pairwise equal. |
| 299 |
# After a second smoothing with the same window the peak will be 5 bins wide |
| 300 |
# peak2 = (0.5*left1 + peak + 0.5*right1)/2 = (peak + left)/2 |
| 301 |
# cpp2 = 10*log10 (peak2) - background_dB |
| 302 |
# A smoothing with 3*dq gives a peak with value |
| 303 |
# peak3 = (left + peak + right) /3 |
| 304 |
# cpp3 = 10*log10 (peak3) - background_dB |
| 305 |
# Etc. The process can be generalized but we will only test the three smoothings mentioned. |
| 306 |
# |
| 307 |
procedure testCalibratedPowerCepstrumCPP: .duration, .approximateF0, .peak_dB, .background_dB |
| 308 |
appendInfoLine: tab$, tab$, "testCalibratedPowerCepstrumCPP: Duration: ", .duration, " s, appr. F0 = ", fixed$(.approximateF0, 2), "," |
| 309 |
appendInfoLine: tab$, tab$, tab$, "peak= ", fixed$ (.peak_dB, 2), " dB, background = ", .background_dB, " dB" |
| 310 |
.sound = Create Sound from formula: "s", 1, 0.0, .duration, 10000, ~ 0 |
| 311 |
.spectrum = To Spectrum: "yes" |
| 312 |
.pc = To PowerCepstrum |
| 313 |
.dq = Get quefrency step |
| 314 |
.nbins = Get number of quefrency bins |
| 315 |
.peak = 10^(.peak_dB/10) |
| 316 |
.background = 10^(.background_dB/10) |
| 317 |
.peakIndex_real = Get index from quefrency: (1.0 / .approximateF0) |
| 318 |
.peakIndex = round (.peakIndex_real) |
| 319 |
.qindex = Get quefrency from index: .peakIndex |
| 320 |
.actualF0 = 1.0 / .qindex |
| 321 |
Formula: ~ if col = .peakIndex then .peak else .background fi |
| 322 |
@powercepstrum_assertions: .pc, .peakIndex, .peak, .background_dB, "Basic" |
| 323 |
|
| 324 |
# |
| 325 |
# Smoothing with a window 2*dq wide, the background wil stay at the same level |
| 326 |
# only the peak will be widened to three bins |
| 327 |
# |
| 328 |
.smooth1 = Smooth: 2*.dq, 1 |
| 329 |
.peak1 = 0.5*(.peak+.background) |
| 330 |
.left1 = 0.5 * (1.5*.background + 0.5*.peak) |
| 331 |
@powercepstrum_assertions: .smooth1, .peakIndex, .peak1, .background_dB, "Smoothing 2*dq (first)" |
| 332 |
|
| 333 |
# |
| 334 |
# Smoothing with a window 2*dq wide of the already once the smoothed powercepstrum, |
| 335 |
# the background wil stay at the same level, only the peak will be widened to 5 bins |
| 336 |
# |
| 337 |
.smooth2 = Smooth: 2*.dq, 1 |
| 338 |
.peak2 = 0.5*(.peak1+.left1) |
| 339 |
@powercepstrum_assertions: .smooth2, .peakIndex, .peak2, .background_dB, "Smoothing 2*dq (second)" |
| 340 |
|
| 341 |
# |
| 342 |
# Smoothing with a window 2.5*dq wide, the background wil stay at the same level |
| 343 |
# only the peak will be widened to 2 bins |
| 344 |
# |
| 345 |
|
| 346 |
selectObject: .pc |
| 347 |
.smooth25 = Smooth: 2.5*.dq, 1 |
| 348 |
.peak25 = (.peak + 1.5*.background) / 2.5 |
| 349 |
@powercepstrum_assertions: .smooth25, .peakIndex, .peak25, .background_dB, "Smoothing 2.5*dq" |
| 350 |
|
| 351 |
# |
| 352 |
# Smoothing with a window 3*dq wide, the background wil stay at the same level |
| 353 |
# only the peak will be widened to 5 bins |
| 354 |
# |
| 355 |
|
| 356 |
selectObject: .pc |
| 357 |
.smooth3 = Smooth: 3*.dq, 1 |
| 358 |
.peak3 = (.peak + 2.0*.background) / 3 |
| 359 |
@powercepstrum_assertions: .smooth3, .peakIndex, .peak3, .background_dB, "Smoothing 3*dq" |
| 360 |
|
| 361 |
|
| 362 |
removeObject: .sound, .spectrum, .pc, .smooth1, .smooth2, .smooth25, .smooth3 |
| 363 |
appendInfoLine: tab$, tab$, "testCalibratedPowerCepstrumCPP OK" |
| 364 |
endproc |
| 365 |
|
| 366 |
procedure powercepstrum_assertions: .pc, .peakIndex, .peak, .background_dB, .info$ |
| 367 |
appendInfoLine: tab$, tab$, .info$ |
| 368 |
selectObject: .pc |
| 369 |
.qindex = Get quefrency from index: .peakIndex |
| 370 |
.queriedPeak_dB = Get peak: 60, 350, "parabolic" |
| 371 |
.qpeak = Get quefrency of peak: 60, 350, "parabolic" |
| 372 |
.qmax = Get end quefrency |
| 373 |
.cpp = Get peak prominence: 60, 350, "parabolic", 0.0001, .qmax, "Exponential decay", "Robust" |
| 374 |
.peak_dB = 10 * log10 (.peak) |
| 375 |
.cppGiven = .peak_dB - .background_dB |
| 376 |
.rdif_dB = abs (.peak_dB - .queriedPeak_dB) / .peak_dB |
| 377 |
assert .rdif_dB < 1e-14; peak '.rdif_dB' |
| 378 |
.rdif_q = abs (.qpeak - .qindex) / .qpeak |
| 379 |
assert .rdif_q < 1e-14; q '.rdif_q' |
| 380 |
.rdif_cpp = abs (.cpp - .cppGiven)/.cppGiven |
| 381 |
assert .rdif_cpp < 1e-14; cpp '.rdif_cpp' |
| 382 |
endproc |
| 383 |
|
| 384 |
procedure testCalibratedPowerCepstrogram: .duration, .approximateF0, .peak_dB, .background_dB |
| 385 |
appendInfoLine: tab$, "Calibrated Cepstrogram of ", .duration, "s, f0: ",.approximateF0, " Hz, peak: ", |
| 386 |
... .peak_dB, " dB, background: ", .background_dB, " dB" |
| 387 |
.s = Create Sound from formula: "s", 1, 0, .duration, 44100, ~0 |
| 388 |
.powercepstrogram = To PowerCepstrogram: 60, 0.002, 5000, 50 |
| 389 |
.numberOfFrames = Get number of frames |
| 390 |
.powercepstrum = To PowerCepstrum (slice): 0.1 |
| 391 |
.qmax = Get end quefrency |
| 392 |
.dq = Get quefrency step |
| 393 |
.nbins = Get number of quefrency bins |
| 394 |
.peak = 10^(.peak_dB / 10) |
| 395 |
.background = 10^(.background_dB / 10) |
| 396 |
.peakIndex_real = Get index from quefrency: (1.0 / .approximateF0) |
| 397 |
.peakIndex = round (.peakIndex_real) |
| 398 |
.qindex = Get quefrency from index: .peakIndex |
| 399 |
.actualF0 = 1.0 / .qindex |
| 400 |
Formula: ~ if col = .peakIndex then .peak else .background fi |
| 401 |
selectObject: .powercepstrogram |
| 402 |
Formula: ~ object [.powercepstrum, 1, row] |
| 403 |
# |
| 404 |
# no smoothing, there is just one peak |
| 405 |
# |
| 406 |
.cppsGiven1 = .peak_dB - .background_dB |
| 407 |
.timeAveragingWindow = 0.0 |
| 408 |
.quefrencyAveragingWindow = 0.0 |
| 409 |
appendInfoLine: tab$, tab$, "Time window: ", .timeAveragingWindow, ", Quefrency window: dq, CPPS: ", .cppsGiven1 |
| 410 |
.cpps1 = Get CPPS: "yes", .timeAveragingWindow, .quefrencyAveragingWindow, 60, 350, 0.05, "parabolic", |
| 411 |
... 0.001, 0.005, "Exponential decay", "Robust" |
| 412 |
.rdif1 = abs (.cppsGiven1 - .cpps1)/.cppsGiven1 |
| 413 |
assert .rdif1 < 1e-14; CPPS='.rdif1' given='.cppsGiven1' cpps='.cpps1' |
| 414 |
# |
| 415 |
# Smoothing with a window 1*dq wide, the background will stay at the same level |
| 416 |
# only the peak will be widened to three bins |
| 417 |
# |
| 418 |
.timeAveragingWindow = 0.005 ; should not make any difference |
| 419 |
.quefrencyAveragingWindow = .dq |
| 420 |
.cppsGiven2 = .peak_dB - .background_dB |
| 421 |
appendInfoLine: tab$, tab$, "Time window: ", .timeAveragingWindow, ", Quefrency window: dq, CPPS: ", .cppsGiven2 |
| 422 |
.cpps2 = Get CPPS: "yes", .timeAveragingWindow, .quefrencyAveragingWindow, 60, 350, 0.05, "parabolic", |
| 423 |
... 0.001, 0.005, "Exponential decay", "Robust" |
| 424 |
.rdif2 = abs (.cppsGiven2 - .cpps2)/.cppsGiven2 |
| 425 |
assert .rdif2 < 1e-14; CPPS '.rdif2' given='.cppsGiven2' cpps='.cpps2' |
| 426 |
# |
| 427 |
# Smoothing with a window 4*dq wide, the background wil stay at the same level |
| 428 |
# only the peak will be widened but the peak will be at the center so parabolic interpolation |
| 429 |
# will go through the peak! |
| 430 |
# |
| 431 |
.quefrencyAveragingWindow = 4.0 * .dq |
| 432 |
.peak3 = (.peak + 3.0 * .background) / 4.0 |
| 433 |
.peak_dB3 = 10 * log10 (.peak3) |
| 434 |
.cppsGiven3 = .peak_dB3 - .background_dB |
| 435 |
appendInfoLine: tab$, tab$, "Time window: ", .timeAveragingWindow, ", Quefrency window: 4*dq, CPPS: ", .cppsGiven3 |
| 436 |
.cpps3 = Get CPPS: "yes", .timeAveragingWindow, .quefrencyAveragingWindow, 60, 350, 0.05, "parabolic", |
| 437 |
... 0.001, 0.005, "Exponential decay", "Robust" |
| 438 |
.rdif3 = abs (.cppsGiven3 - .cpps3) / .cppsGiven3 |
| 439 |
assert .rdif3 < 1e-14; CPPS '.rdif3' given='.cppsGiven3' cpps='.cpps3' |
| 440 |
|
| 441 |
for .ismooth to 0 |
| 442 |
.quefrencyAveragingWindow = randomUniform (0.0006, 0.0003) |
| 443 |
selectObject: .powercepstrogram |
| 444 |
# smooth only in y-direction |
| 445 |
.smoothcepstrogram = Smooth: 0.0000001, .quefrencyAveragingWindow |
| 446 |
.smoothpowercepstrum = To PowerCepstrum (slice): 0.1 |
| 447 |
.cpps = Get peak prominence: 60, 350, "parabolic", 0.001, .qmax, "Straight", "Robust slow" |
| 448 |
selectObject: .powercepstrum |
| 449 |
.smoothpowercepstrum2 = Smooth: .quefrencyAveragingWindow, 1 |
| 450 |
.cpps2 = Get peak prominence: 60, 350, "parabolic", 0.001, .qmax, "Straight", "Robust slow" |
| 451 |
assert .cpps == .cpps2; cpps='.cpps' cpps2='.cpps2' |
| 452 |
removeObject: .smoothcepstrogram, .smoothpowercepstrum, .smoothpowercepstrum2 |
| 453 |
endfor |
| 454 |
removeObject: .s, .powercepstrum, .powercepstrogram |
| 455 |
appendInfoLine: tab$, "Calibrated Cepstrogram: OK" |
| 456 |
endproc |
| 457 |
|
| 458 |
procedure fillSpectrumWithCosine: .spectrum, .f0 |
| 459 |
selectObject: .spectrum |
| 460 |
.df = Get bin width |
| 461 |
.b2 = 10*log10(2*.df) |
| 462 |
.s = 2e-5 |
| 463 |
.amplitude = 1 |
| 464 |
.offset = 0 |
| 465 |
Formula: ~ if row = 1 then .s * 10^((.offset + .amplitude * cos(2 * pi * x / .f0) -.b2)/20) else 0 fi |
| 466 |
endproc |
| 467 |
|
| 468 |
;@timingComparison |
| 469 |
procedure timingComparison |
| 470 |
random_initializeWithSeedUnsafelyButPredictably (7456238) |
| 471 |
.sound = Create Sound as tone complex: "toneComplex", 0, 1, 44100, "cosine", 100, 0, 0, 0 |
| 472 |
.pc = To PowerCepstrogram: 60, 0.002, 5000, 50 |
| 473 |
# version time CPPS |
| 474 |
# 6.1.55 14.497 29.0736 |
| 475 |
# 6.2.14 14.621 29.0736 |
| 476 |
# 6.4.01 4.684 29.0719 ?? |
| 477 |
# 6.4.23 15.200 29.0719 |
| 478 |
# 6.4.27 15.180 29.0582 |
| 479 |
|
| 480 |
# on the Mac: |
| 481 |
# 6.4.01 4.196 29.0773 |
| 482 |
# 6.4.23 4.285 29.0773 |
| 483 |
# 6.4.27 4.310 29.0774 |
| 484 |
# 6.4.39 0.070 28.4723 |
| 485 |
# 6.4.39 0.078 28.5216 |
| 486 |
# 6.4.40 0.072 28.4981 |
| 487 |
# 6.4.41 0.072 28.5701 |
| 488 |
|
| 489 |
stopwatch |
| 490 |
for i to 1 |
| 491 |
.cpps = Get CPPS: "yes", 0.02, 0.0005, 60, 330, 0.05, "parabolic", 0.001, 0.05, |
| 492 |
... "Exponential decay", "Robust slow" |
| 493 |
endfor |
| 494 |
writeInfoLine: fixed$ (stopwatch, 3), " ", .cpps |
| 495 |
random_initializeSafelyAndUnpredictably () |
| 496 |
endproc |
| 497 |
|
| 498 |
procedure testSearchDomain |
| 499 |
appendInfo: tab$, "testSearchDomain" |
| 500 |
.sound = Create Sound as tone complex: "tc", 0, 0.025, 16000, |
| 501 |
... "cosine", 100, 0, 0, 0 |
| 502 |
.spectrum = To Spectrum: "yes" |
| 503 |
.ps = To PowerCepstrum |
| 504 |
# This ps->xmax = 0.016; The maximum value for the peak search gives |
| 505 |
# 1/60=0.0166 which is larger than ps->xmax |
| 506 |
# For Praat versions 6.3.39 until 6.4.49 the following query would give an |
| 507 |
# error message because of this. |
| 508 |
# This error was reported by James Kirby 20250908 |
| 509 |
.peak = Get quefrency of peak: 60, 333.3, "parabolic" |
| 510 |
# the following would also result in an error |
| 511 |
.peak = Get peak in quefrency interval: 1./333.3, 1.0/60.0, "parabolic" |
| 512 |
removeObject: .sound, .spectrum, .ps |
| 513 |
appendInfoLine: " OK" |
| 514 |
endproc |
| 515 |
|
| 516 |
|
| 517 |
|