# Deseasonalizing data with fourier analysis using MATLAB [closed]

I am trying to eliminate seasonality from my data using Fourier analysis in MATLAB. Following this post https://stackoverflow.com/questions/19285684/fast-fourier-transform-for-deasonalizing-data-in-matlab?answertab=oldest#tab-top I came up with this code:

clear;
clc;
close all;
%
%
y = Data;
T = length(y);
years = linspace(1973,1979,T);
ts = y;
points_in_year = 12;
%
tim = (dates - dates(1))/(dates(2)-dates(1));  % <-- acquisition times for your *new* data, normalized
NFpick = [2 7 13]; % <-- channels you picked to build the detrending baseline (peaks to be eliminated)
% Compute the trend
mu = mean(ts);
Nchannels = length(ts);      % <-- size of time domain data
Mpick = 2*length(NFpick);
X(:,1:2:Mpick) = cos(2*pi*(NFpick-1)'/Nchannels*tim')';
X(:,2:2:Mpick) = sin(-2*pi*(NFpick-1)'/Nchannels*tim')';
X = [ones(T,1), X];
beta = X\ts;
trend = X*beta;
detrended = y - trend + mu;
figure
plot(dates,detrended)


Notice that I am trying to avoid the use of fft. My problem is that I am not obtaining the same answer that https://www.mathworks.com/help/econ/seasonal-adjustment.html even changing the channels correspondent to the peaks I want to eliminate.

What is wrong with my approach?
How should I change my code to eliminate the seasonality using Fourier analysis?

## closed as off-topic by Michael Chernick, gung♦, Nick Cox, John, whuber♦Jan 19 '17 at 22:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – Michael Chernick, gung, Nick Cox, John, whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

• On a first look, this is too much about code review (off topic) and too little about statistics (on topic). Could you help us by formulating your question more in terms of statistical concepts and problems and less in terms of code? – Richard Hardy Jan 19 '17 at 19:15
• Thank you for the fast response. My question is how can I eliminate seasonality from time series like the one in the example using Fourier analysis? Is it possible to obtain the same response with Fourier analysis than using an stable seasonal filter ? – Datanalyst Jan 19 '17 at 19:23
• This still looks like it's about code to me. – gung Jan 19 '17 at 19:46

As shown by Prof. Pollock [1], frequency domain filters are an interesting method for signal extraction (and in particular for seasonal adjustment) in economic time series.

Rather than following code, I would recommend you to first review the statistical framework and rationale behind this approach. This will most likely help you to develop your own routine, modify an existing piece of code or to formulate a question more suited to this site.

Following the notation of the document referenced below, frequency-domain filters are based on the Fourier coefficients:

$$\displaystyle{ \zeta_j = \frac{1}{T} \sum_{t=0}^{T-1} y_t e^{-i\omega_j t}\,dt \,, \quad j=0,... ,T-1 \,. }$$

Each coefficient $\zeta_j$ is related to a cycle of frequency $\omega_j$. Thus, a natural way to filter certain frequencies is to set equal to zero those coefficients that are related to frequencies that do not belong to the target component (e.g. seasonal) and then synthesise the target component, $s_t$, by means of the inverse transform:

$$\displaystyle{s_t = \sum_{j=0}^{T-1} \zeta_t e^{i\omega_j t}\,dt \,, \quad t=1,... ,T \,.}$$

I would recommend you to review the reference given below and further material (including the software IDEOLOG) available at Prof. Pollock's website. For a quick illustration you may also see the post signal extraction in time series, as simple as that? where I replicate one of the examples given in the reference paper.

[1] D.S.G. Pollock. IDEOLOG: A Program for Filtering Econometric Data. A Synopsis of Alternative Methods. URL http://www.le.ac.uk/users/dsgp1/ERCSTUFF/ideolog.pdf.

• I found the paper very interesting and helpful !! I followed the paper and I was able to reproduce figures 12 and 13. However, I was not able to reproduce figure 14. I wonder if you could add alittle more explanation on how especifically figure 14 is elaborated. – Datanalyst Jan 19 '17 at 22:48