# NOT RUN {
x <- as.matrix(seq_len(100) + 50 * cos(seq_len(100) * 2 * pi / 40))
X <- dct(x)
# Find which cosine coefficients are significant (approx.)
# zero the rest
nsig <- which(abs(X) < 1)
N <- length(X) - length(nsig) + 1
X[nsig] <- 0
# Reconstruct the signal and compare it to the original signal.
xx <- idct(X)
plot(x, type = "l")
lines(xx, col = "red")
legend("bottomright", legend = c("Original", paste("Reconstructed, N =", N)),
lty = 1, col = 1:2)
# }
Run the code above in your browser using DataLab