# Analyzing Cyclical Data with FFT

You can use the Fourier transform to analyze variations in data, such as an event in nature over a period time.

For almost 300 years, astronomers have tabulated the number and size of sunspots using the Zurich sunspot relative number. Plot the Zurich number over approximately the years 1700 to 2000.

```load sunspot.dat year = sunspot(:,1); relNums = sunspot(:,2); plot(year,relNums) xlabel('Year') ylabel('Zurich Number') title('Sunspot Data')``` To take a closer look at the cyclical nature of sunspot activity, plot the first 50 years of data.

```plot(year(1:50),relNums(1:50),'b.-'); xlabel('Year') ylabel('Zurich Number') title('Sunspot Data')``` The Fourier transform is a fundamental tool in signal processing that identifies frequency components in data. Using the `fft` function, take the Fourier transform of the Zurich data. Remove the first element of the output, which stores the sum of the data. Plot the remainder of the output, which contains a mirror image of complex Fourier coefficients about the real axis.

```y = fft(relNums); y(1) = []; plot(y,'ro') xlabel('real(y)') ylabel('imag(y)') title('Fourier Coefficients')``` Fourier coefficients on their own are difficult to interpret. A more meaningful measure of the coefficients is their magnitude squared, which is a measure of power. Since half of the coefficients are repeated in magnitude, you only need to compute the power on one half of the coefficients. Plot the power spectrum as a function of frequency, measured in cycles per year.

```n = length(y); power = abs(y(1:floor(n/2))).^2; % power of first half of transform data maxfreq = 1/2; % maximum frequency freq = (1:n/2)/(n/2)*maxfreq; % equally spaced frequency grid plot(freq,power) xlabel('Cycles/Year') ylabel('Power')``` Maximum sunspot activity happens less frequently than once per year. For a view of the cyclical activity that is easier to interpret, plot power as a function of period, measured in years per cycle. The plot reveals that sunspot activity peaks about once every 11 years.

```period = 1./freq; plot(period,power); xlim([0 50]); %zoom in on max power xlabel('Years/Cycle') ylabel('Power')```
{"ft_lang_label":"__label__en","ft_lang_prob":0.80857617,"math_prob":0.99076164,"size":2116,"snap":"2021-43-2021-49","text_gpt3_token_len":508,"char_repetition_ratio":0.12831439,"word_repetition_ratio":0.006451613,"special_character_ratio":0.25,"punctuation_ratio":0.13592233,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99815416,"pos_list":[0,1,2,3,4,5,6,7,8,9,10],"im_url_duplicate_count":[null,2,null,2,null,2,null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-22T20:33:42Z\",\"WARC-Record-ID\":\"<urn:uuid:266961a7-2477-4476-a84d-44e2ade8670f>\",\"Content-Length\":\"71756\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:0cc37d0f-3c4a-40f3-bef5-1ad52c5009c3>\",\"WARC-Concurrent-To\":\"<urn:uuid:1ae0991e-39ea-458b-bf8e-8e9ef5a6ff40>\",\"WARC-IP-Address\":\"184.25.188.167\",\"WARC-Target-URI\":\"https://in.mathworks.com/help/matlab/math/using-fft.html\",\"WARC-Payload-Digest\":\"sha1:ZPUXLJMALOKKW2CKAOVQOYBRBALSRZDZ\",\"WARC-Block-Digest\":\"sha1:EHMOTKVYN632YUVEXQH3TJ6CZKJ2QBX5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585518.54_warc_CC-MAIN-20211022181017-20211022211017-00182.warc.gz\"}"}