# NOT RUN {
fs <- 1000 # sampling frequency
secs <- 10 # number of seconds
t <- seq(0, secs, 1/fs) # time series
x <- sin(100 * 2 * pi * t) + runif(length(t)) # 100 Hz signal + noise
m <- 32 # n of points desired
f0 <- 75; f1 <- 175; # desired freq range
w <- exp(-1i * 2 * pi * (f1 - f0) / ((m - 1) * fs)) # freq step of f1-f0/m
a <- exp(1i * 2 * pi * f0 / fs); # starting at freq f0
y <- czt(x, m, w, a)
# compare DFT and FFT
fs <- 1000
h <- as.numeric(fir1(100, 125/(fs / 2), type = "low"))
m <- 1024
y <- stats::fft(postpad(h, m))
f1 <- 75; f2 <- 175;
w <- exp(-1i * 2 * pi * (f2 - f1) / (m * fs))
a <- exp(1i * 2 * pi * f1 / fs)
z <- czt(h, m, w, a)
fn <- seq(0, m - 1, 1) / m
fy <- fs * fn
fz = (f2 - f1) * fn + f1
plot(fy, 10 * log10(abs(y)), type = "l", xlim = c(50, 200),
xlab = "Frequency", ylab = "Magnitude (dB")
lines(fz, 10 * log10(abs(z)), col = "red")
legend("topright", legend = c("FFT", "CZT"), col=1:2, lty = 1)
# }
Run the code above in your browser using DataLab