Deseasonalizing data with fourier analysis using MATLAB 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;
%
load(fullfile(matlabroot,'examples','econ','Data_Accidental.mat'))
%
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?
 A: 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.
