Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Sampling frequency
- fs <- 1000
- # Sampling period
- ts <- 1/fs
- # Length of signal
- l <- 1500
- # Time vector
- t <- seq(0, l - 1)*ts
- # Signal
- s <- 0.4*sin(2*pi*50*t) + sin(2*pi*120*t)
- # Noise
- x <- s + 2*rnorm(length(t))
- # Plot signal
- qplot(t, x) + geom_line()
- # FFT
- y <- fft(x)
- # Two-sided spectrum
- p2 <- abs(y/l)
- # Convert to one-sided spectrum
- p1 <- p2[1:(l/2 + 1)]
- p1[2:(length(p1) - 1)] <- 2*p1[2:(length(p1) - 1)]
- # Define frequency
- f <- fs*seq(0, l/2)/l
- # Plot spectrum
- qplot(f, p1) + geom_line()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement