# How to apply a Hanning filter to a time series?

134 views (last 30 days)
Lorenzo Melito on 28 Nov 2019
Commented: Lorenzo Melito on 4 Dec 2019
I am analyzing a time series of location of a given feature (b) over time (date). Both b and date are (786x1) vectors; b collects the position of the feature in meters, while date collects the dates of each detected location (the dates are in datenum at the moment). Each element of date represents a single day, so that the entire time series spans across a bit more than two years. Note that, in some days, the position of the feature could not be identified, so b presents some NaNs in the corresponding days. You can see the plotted data in the figure below.
Following a research approach from a paper I have read recently, I am trying to (I cite the paper) "year-average the time series by applying a Hanning (cosine-squared) filter with a half-width of 365 days". I presume the authors are meaning that I have to window this time series with a Hann window. I am no expert of digital filters, so I searched through this forum, and an answer for another question suggested me to try a convolution. First, I have removed the NaN elements in b and the corresponding dates in date.
not_nan = ~isnan(b);
date2 = date(not_nan); b2 = b(not_nan);
Supposing that the window width is 10 samples (days), I define a Hann window of 10 elements and convolute b2:
win = hann(10);
filtered = conv(b2,win,'same');
plot(date2,b2,'.'); hold on;
plot(date2,filtered);
While I believe that this convolution is somehow doing the trick, it seems that the filtered time series is amplified with respect to the original series and the amplification grows as the Hann window width grows. When I define a 365-days Hann window, moreover, the amplification goes really high. I have tried to demean the original time series before windowing it, but the issue still remains.
Can you please point me in the right direction?
Thank you for any help.

Raunak Gupta on 4 Dec 2019
Hi,
In my understanding it is required to smooth the data as I can see from the step that are taken in the problem. The hann filter will generate a filter that has positive value for all points in the window length (Because of the squared cosine). So, convolving with it will only amplify the value as surrounding data values will be added. Even subtracting the mean will not help here.
Instead of this you may try smoothdata for smoothing out the data with a specific ‘method’ and ‘window’ which meets the requirements.
Hope this helps.

#### 1 Comment

Lorenzo Melito on 4 Dec 2019
Hello Raunak,
thank you for your answer. Yes, what I need to do is some sort of data smoothing in order to extract only the dominant (annual) trend of the data. The paper I am referring to for my research did this smoothing with a hann filter, so at first I preferred to stay consistent with their approach; but I now see where the problem lies with the hann window. I will try smoothdata as you suggested.
Thank you again!

R2019a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by