30-day running running mean to the detrended timeseries

16 vues (au cours des 30 derniers jours)
Ali Saremi
Ali Saremi le 20 Mai 2021
Commenté : Star Strider le 24 Mai 2021
Dear Sir or Madam,
I hope you are well.
I have some houlry water level obersvations. First I needed to detrend them linearly which I did. Next step, I need to use a 30-day running mean to the detrended timeseries. I have figured out that I needed to use movmean function or smoothdata function to smooth the data using different windows, but I am not sure how to apply the 30-day running mean. Below is the code that I have been using. If someone could help me with that, I would much appreciate it. Also, I have attached my original data. Thank you.
Yours faithfully,
Ali
window=2;
MeanDetrendedData=smoothdata(DetrendedData,'movmean',window);
plot(DateNum,DitrendedData,DateNum,MeanDetrendedData)
datetick('x')

Réponse acceptée

Star Strider
Star Strider le 20 Mai 2021
Modifié(e) : Star Strider le 20 Mai 2021
This requires reading it as a table, converting it to a timetable and then use the retime function —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/623923/Observation%20Data.csv', 'VariableNamingRule','preserve');
TT1 = table2timetable(T1); % Copy & Convert To 'timetable'
TT1 = retime(TT1, 'hourly','fillwithmissing'); % Interpolate If Necessary & Require Hourly Time Step
TT1 = retime(TT1,'hourly',@(x)movmean(x,30*24)); % Use 'movmean'
figure
subplot(2,1,1)
plot(T1.Date, T1.Obs)
grid
xlabel('Time')
ylabel('Obs')
title('Original Table')
subplot(2,1,2)
plot(TT1.Date, TT1.Obs)
grid
xlabel('Time')
ylabel('Obs')
title('Timetable With ''movmean''')
I do not see much difference in the plots or numerical output, however it does not throw any errors, so I assume it is working correctly.
EDIT — (20 May 2021 at 14:06)
This is likely closer to what you want —
TT12 = movmean(TT1.Obs,30*24,'omitnan', 'Endpoints','shrink'); % Use 'movmean'
figure
subplot(2,1,1)
plot(T1.Date, T1.Obs)
grid
xlabel('Time')
ylabel('Obs')
title('Original Table')
subplot(2,1,2)
plot(TT1.Date, TT12)
grid
xlabel('Time')
ylabel('Obs')
title('Timetable With ''movmean''')
The ‘spikes’ are artifacts due to the discontinuities. Eliminating then would likely not be easy, and would likely involve simply detecting them using islocalmax and islocalmin, and then simply deleting them.
.
  2 commentaires
Ali Saremi
Ali Saremi le 24 Mai 2021
Thank you for helping me with this question.
Star Strider
Star Strider le 24 Mai 2021
As always, my pleasure!
It occurred to me later that filing the gaps with a constant (specifically 0) removes the ‘spikes’ at the discontinuities.
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/623923/Observation%20Data.csv', 'VariableNamingRule','preserve');
TT1 = table2timetable(T1); % Copy & Convert To 'timetable'
TT1 = retime(TT1, 'hourly','fillwithconstant'); % Interpolate If Necessary & Require Hourly Time Step
TT1 = retime(TT1,'hourly',@(x)movmean(x,30*24)); % Use 'movmean'
figure
subplot(2,1,1)
plot(T1.Date, T1.Obs)
grid
xlabel('Time')
ylabel('Obs')
title('Original Table')
subplot(2,1,2)
plot(TT1.Date, TT1.Obs)
grid
xlabel('Time')
ylabel('Obs')
title('Timetable With ''movmean''')
TT12 = movmean(TT1.Obs,30*24,'omitnan', 'Endpoints','shrink'); % Use 'movmean'
figure
subplot(2,1,1)
plot(T1.Date, T1.Obs)
grid
xlabel('Time')
ylabel('Obs')
title('Original Table')
subplot(2,1,2)
plot(TT1.Date, TT12)
grid
xlabel('Time')
ylabel('Obs')
title('Timetable With ''movmean''')
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Preprocessing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by