# NOT RUN {
## Simulate a speech signal with a 70 Hz glottal wave
f0 <- 70; fs = 10000 # 100 Hz fundamental, 10 kHz sampling rate
a <- Re(poly(0.985 * exp(1i * pi * c(0.1, -0.1, 0.3, -0.3))))
s <- 0.05 * runif(1024)
s[floor(seq(1, length(s), fs / f0))] <- 1
x <- filter(1, a, s)
## compute real cepstrum and min-phase of x
cep <- rceps(x, TRUE)
hx <- freqz(x, fs = fs)
hxm <- freqz (cep$ym, fs = fs)
len <- 1000 * trunc(min(length(x), length(cep$ym)) / 1000)
time <- 0:(len-1) * 1000 / fs
op <- par(mfcol = c(2, 2))
plot(time, x[1:len], type = "l", ylim = c(-10, 10),
xlab = "Time (ms)", ylab = "Amplitude",
main = "Original and reconstructed signals")
lines(time, cep$ym[1:len], col = "red")
legend("topright", legend = c("original", "reconstructed"),
lty = 1, col = c(1, 2))
plot(time, cep$y[1:len], type = "l",
xlab = "Quefrency (ms)", ylab = "Amplitude",
main = "Real cepstrum")
plot (hx$w, log(abs(hx$h)), type = "l",
xlab = "Frequency (Hz)", ylab = "Magnitude",
main = "Magnitudes are identical")
lines(hxm$w, log(abs(hxm$h)), col = "red")
legend("topright", legend = c("original", "reconstructed"),
lty = 1, col = c(1, 2))
phx <- unwrap(Arg(hx$h))
phym <- unwrap(Arg(hxm$h))
range <- c(round(min(phx, phym)), round(max(phx, phym)))
plot (hx$w, phx, type = "l", ylim = range,
xlab = "Frequency (Hz)", ylab = "Phase",
main = "Unwrapped phase")
lines(hxm$w, phym, col = "red")
legend("bottomright", legend = c("original", "reconstructed"),
lty = 1, col = c(1, 2))
par(op)
## confirm the magnitude spectrum is identical in the signal
## and the reconstruction and that there are peaks in the
## cepstrum at 14 ms intervals corresponding to an F0 of 70 Hz.
# }
Run the code above in your browser using DataLab