I’m working through an online example that explains spectral analysis in R. Let’s say you have a complex wave:
t <- seq(0,200,by=0.1)
x <- cos(2*pi*t/16) + 0.75*sin(2*pi*t/5)
then the periodogram:
del<-0.1 # sampling interval
x.spec <- spectrum(x,log="no",span=10,plot=FALSE)
spx <- x.spec$freq/del
spy <- 2*x.spec$spec
plot(spy~spx,xlab="frequency",ylab="spectral density",type="l")
tells you that the frequency from the $cos(2*pi*t/16)$ harmonic dominates the wave. So, from the periodogram, you can know the frequency, and you can have a rough idea of the magnitude of the amplitude that corresponds to the frequency.
Let’s say you were only given the output of x (a time series) and you did not know the underlying harmonic formulae that compose the complex wave. Is there a way to extract the formula for, say, the 5 most dominant waveforms from a given complex time series?
I’d like to recreate something like this figure 1, where the complex waveform from the time series is visualized as the sum of individual harmonic waves. However, I’m stuck on how to retrieve the harmonic equations to visualize each harmonic individually.
Rhas a Fast Fourier Transform built in: see the help forfft. https://stats.stackexchange.com/questions/16117 has a little more about this. You basically read the dominant frequencies off the peaks in a (smoothed) periodogram--that's all the information you need. The rest is routine coding and plotting. – whuber May 18 '22 at 21:50yt <- cos(2*pi*t/16) + 0.75*cos(2*pi*t/5)should result in a periodogram with dominant frequencies 1/16 and 1/5, and amplitudes 1 and 0.75. However,FFT <-fft(yt)and thenFFT <- abs(FFT/FFT[1])plotted into a periodogramplot(FFT, type = "l”)has none of these values. – giopico May 19 '22 at 15:07