This example demonstrates the use of rPeaks to locate the peaks in monthly sunspot data.
First, load the packages we need
library(datasets) library(rPeaks) library(stats) library(graphics)
Next, generate the spectrum
smf <- abs(fft(spec.taper(as.vector(sunspot.month),p=0.5)))
Find the background for the spectrum
smb <- SpectrumBackground(smf,iterations=100)
Find the peaks
z <- SpectrumSearch(smf-smb)
Now, plot the results
plot(smf-smb,type="l",xlim=c(0,200)) lines(z$y,type="l",col="red") points(y=rep(-10,length(z$pos)),x=z$pos,col="green",pch="+",cex=2) text(100, 25000, paste(length(z$pos)/2," harmonics were found with ", z$pos[1],"-month base period",sep=""))
Note that r length(z$pos)/2
harmonics were found with a r z$pos[1]
month base period.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.