Find Peaks in Data
Use the findpeaks
function to find values and locations of local maxima in a set of data.
The file spots_num
contains the average number of sunspots observed every year from 1749 to 2012. Find the maxima and their years of occurrence. Plot them along with the data.
load("spots_num") [pks,locs] = findpeaks(avSpots); plot(year,avSpots,year(locs),pks,"o") xlabel("Year") ylabel("Sunspot Number") axis tight
Some peaks are very close to each other. The ones that are not recur at regular intervals. There are roughly five such peaks per 50-year period.
To make a better estimate of the cycle duration, use findpeaks
again, but this time restrict the peak-to-peak separation to at least six years. Compute the mean interval between maxima.
[pks,locs] = findpeaks(avSpots,MinPeakDistance=6); plot(year,avSpots,year(locs),pks,"o") xlabel("Year") ylabel("Sunspot Number") axis tight legend(["Data" "peaks"],Location="NorthWest")
meanCycle = mean(diff(locs))
meanCycle = 10.8696
It is known that solar activity cycles roughly every 11 years. Check by using the Fourier transform. Remove the mean of the signal to concentrate on its fluctuations. Recall that the sample rate is measured in years. Use frequencies up to the Nyquist frequency.
fs = 1; Nf = 512; df = fs/Nf; f = 0:df:fs/2-df; trSpots = fftshift(fft(avSpots-mean(avSpots),Nf)); dBspots = mag2db(abs(trSpots(Nf/2+1:Nf))); plot(f,dBspots) xline(1/meanCycle,Color="#77AC30") xlabel("Frequency (year^{-1})") ylabel("| FFT | (dB)") ylim([20 85]) text(1/meanCycle + 0.02,25,"<== 1/"+num2str(meanCycle))
The Fourier transform indeed peaks at the expected frequency, confirming the 11-year conjecture. You also can find the period by locating the highest peak of the Fourier transform. The two estimates coincide quite well.
[pk,MaxFreq] = findpeaks(dBspots,NPeaks=1,SortStr="descend");
Period = 1/f(MaxFreq)
Period = 10.8936
hold on plot(f(MaxFreq),pk,"o") hold off legend(["Fourier transform" "1/meanCycle" "1/Period"])