约翰和伊戈尔的回答都是正确的,但我认为他们缺少一些示例代码。
library(dtt)
par(mfrow=c(3, 1), mar=c(2, 3, 1, 0.1), mgp=c(2, 0.8, 0))
set.seed(1)
N <- 60
v <- sin(seq(0, pi*2*4, l=N))/2 +
sin(seq(0, pi*2*7, l=N))/3 +
sin(seq(0, pi*2*15, l=N))/2 +
runif(N, -1, 1)/40 +
runif(N, -1, 1)/40
plot(v, type="o")
DCT-I:
v.dct1 <- dct(v, variant=1)
w <- c(v, v[(N - 1):2])
w.dct1 <- 0.5 * Re(fft(w)[1:N])
plot(v.dct1, type="l", col="#00000088")
abline(h=0, col="#00000044")
lines(w.dct1, col=2, lty=3, lwd=2)
legend("topright", c("dtt::dct", "fft"), bty="n", col=1:2, lwd=1)
legend("topleft", "DCT-I", bty="n")
DCT-II:
v.dct2 <- dct(v, variant=2)
P <- exp(complex(imaginary=pi / 2 / N)*(seq(2*N)-1))
w <- c(v, v[N:1])
w.dct2 <- 0.5 * Re(fft(w)[1:N]/P)
plot(v.dct2, type="l", col="#00000088")
abline(h=0, col="#00000044")
lines(w.dct2, col=2, lty=3, lwd=2)
legend("topright", c("dtt::dct", "fft"), bty="n", col=1:2, lwd=1)
legend("topleft", "DCT-II", bty="n")