test_LPC.praat 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # test_LPC.praat
  2. # djmw 20080122, 20110528, 20180318
  3. printline test_LPC
  4. @testLPCInterface
  5. # formants of straight tube
  6. fb5$ = ""
  7. space$ = ""
  8. for i to 5
  9. f[i] = (2*i-1)*500
  10. b[i] = i * 50
  11. fb5$ = fb5$ + space$ + string$ (f[i] ) + " " + string$ (b[i])
  12. space$ = " "
  13. endfor
  14. debug = 0
  15. pt = Create PitchTier: "pt", 0, 0.5
  16. Add point: 0, 150
  17. source = To Sound (pulse train): 44100, 1, 0.05, 2000, "no"
  18. ft = Create FormantTier: "ft", 0, 0.5
  19. Add point: 0, fb5$
  20. selectObject: ft, source
  21. soundpre = Filter
  22. sound = Resample: 10000, 50
  23. removeObject: pt, source, ft, soundpre
  24. for imethod to 4
  25. selectObject: sound
  26. if imethod = 1
  27. method$ = "auto"
  28. lpc = To LPC (autocorrelation)... 10 0.025 0.005 50
  29. elsif imethod = 2
  30. method$ = "covar"
  31. lpc = To LPC (covariance)... 10 0.025 0.005 50
  32. elsif imethod = 3
  33. method$ = "burg"
  34. lpc = To LPC (burg)... 10 0.025 0.005 50
  35. elsif imethod = 4
  36. method$ = "marple"
  37. lpc = To LPC (marple)... 10 0.025 0.005 50 1e-06 1e-06
  38. endif
  39. call get_formants 'lpc' 'method$'
  40. deltaf1 = deltaf
  41. deltab1 = deltab
  42. assert deltaf1 < 0.07 and deltab1 < 3.2; ('method$')
  43. select lpc
  44. plus sound
  45. lpcr = To LPC (robust)... 0.025 50 1.5 5 1e-06 no
  46. call get_formants lpcr 'method$':robust
  47. deltaf2 = deltaf
  48. deltab2 = deltab
  49. assert deltaf2 < 0.005 and deltab2 < 0.2; ('method$':robust)
  50. select lpc
  51. plus lpcr
  52. Remove
  53. endfor
  54. select sound
  55. Remove
  56. printline test_LPC OK
  57. procedure get_formants: .lpc, .method$
  58. selectObject: .lpc
  59. .formant = To Formant (keep all)
  60. deltaf = 0
  61. deltab = 0
  62. for .nf to 5
  63. .f[.nf] = Get quantile: .nf, 0, 0, "Hertz", 0.5
  64. .b[.nf] = Get quantile of bandwidth: .nf, 0, 0, "Hertz", 0.5
  65. if .nf <= 3
  66. deltaf += abs (f[.nf] - .f[.nf]) / f[.nf]
  67. deltab += abs (b[.nf]- .b[.nf]) / b[.nf]
  68. endif
  69. endfor
  70. deltaf /= 3
  71. deltab /= 3
  72. appendInfoLine: tab$, .method$, " deltaf = ", fixed$ (deltaf, 4), " deltab = ", fixed$ (deltab, 4),
  73. ... " ", fixed$ (.f[1], 0), " ", fixed$ (.b[1], 0), " ", fixed$ (.f[2], 0), " ", fixed$ (.b[2], 0),
  74. ... " ", fixed$ (.f[3], 0), " ", fixed$ (.b[3], 0), " ", fixed$ (.f[4], 0), " ", fixed$ (.b[4], 0),
  75. ... " ", fixed$ (.f[5], 0), " ", fixed$ (.b[5], 0)
  76. removeObject: .formant
  77. endproc
  78. procedure testLPCInterface
  79. .sound = Create Sound from formula: "noise", 1, 0, 1, 10000, "randomGauss(0,0.1)"
  80. .lpc = To LPC (autocorrelation): 10, 0.025, 0.005, 50
  81. appendInfoLine: tab$, "test LPC interface"
  82. appendInfoLine: tab$, tab$, "Query"
  83. .startTime = Get start time
  84. .t1 = Get time from frame number: 1
  85. .endTime = Get end time
  86. .totalDuration = Get total duration
  87. .numberOfFrames = Get number of frames
  88. .timeStep = Get time step
  89. for .iframe to .numberOfFrames
  90. .time = Get time from frame number: .iframe
  91. .frameNumber = Get frame number from time: .time
  92. .numberOfCoefficients = Get number of coefficients: .iframe
  93. endfor
  94. .samplingInterval = Get sampling interval
  95. appendInfoLine: tab$, tab$, "Modify"
  96. Shift times by: randomUniform (0.1, 0.9)
  97. Shift times to: "start time", .startTime
  98. Shift times to: "centre time", .startTime
  99. Shift times to: "end time", .endTime
  100. Scale times by: randomUniform (0.5, 2.5)
  101. Scale times to: .startTime, .endTime
  102. appendInfoLine: tab$, tab$, "Extract"
  103. for .i to 10
  104. selectObject: .lpc
  105. .bandwidthReduction = randomUniform (0, 200)
  106. .time = randomUniform (.startTime, .endTime)
  107. .spectrum = To Spectrum (slice): .time, 20, .bandwidthReduction, 50
  108. selectObject: .lpc
  109. .vocalTract1 = To VocalTract (slice): .time, 17.0
  110. selectObject: .lpc
  111. .glottalDamping = randomUniform (0.01, 0.4)
  112. .radiationDamping$ = if randomUniform (0,1) > 0.5 then "yes" else "no" fi
  113. .internalDamping$ = if randomUniform (0,1) > 0.5 then "yes" else "no" fi
  114. .vocalTract2 = To VocalTract (slice, special): .time, .glottalDamping, .radiationDamping$, .internalDamping$
  115. selectObject: .lpc
  116. .polynomial = To Polynomial (slice): .time
  117. removeObject: .spectrum, .vocalTract1, .vocalTract2, .polynomial
  118. endfor
  119. selectObject: .lpc
  120. .matrix = Down to Matrix (lpc)
  121. removeObject: .matrix
  122. appendInfoLine: tab$, tab$, "Analyse"
  123. selectObject: .lpc
  124. .formant1 = To Formant
  125. selectObject: .lpc
  126. .formant2 = To Formant (keep all)
  127. for .numberOfCoefficients to 12
  128. selectObject: .lpc
  129. .lfcc = To LFCC: .numberOfCoefficients
  130. removeObject: .lfcc
  131. endfor
  132. selectObject: .lpc
  133. .spectrogram = To Spectrogram: 20, 0, 50
  134. selectObject: .lpc
  135. .lineSpectralFrequencies = To LineSpectralFrequencies: 0.0
  136. removeObject: .spectrogram, .formant1, .formant2, .lineSpectralFrequencies, .sound, .lpc
  137. endproc