How to do interpolation only if data interval is > than 3h?

2 views (last 30 days)
I have Raw water height data (y) at irregular intervals(x) for a few years of measurement. I need to interpolate these data at a regular interval of 10 minutes. I want to make sure that if the time difference between the two data y(i) and y(i+1) is less than 3 hours the interpolation (interp) takes place regularly at 10 minutes, while if the interval between the data y(i) and y(i+1) is greater than 3 hours NaN appears in the interpolated data (interp).
This is the code and the variable I have written so far.
Raw=Raw_Cuccio_validati; %This are the Raw Data
t1 = datetime(2000,01,1,0,10,0); %initial time
t2 = datetime(2007,01,01,00,00,0); %final time
t_ibrido = t1:minutes(10):t2; %10 min interval
Table_interp=datevec(t_ibrido');
Table_interp(:,7)= datenum(t_ibrido'); %I add a column with time_data in numerical format
%% INTERPOLATION(MAKIMA)-10 MIN
num_iniziale=datenum(t1);
num_finale = datenum(t2);
num_10=datenum(0,0,0,0,10,0);
num_3h=datenum(0,0,0,3,0,0);
x = Raw(:,7); %THIS ARE MY TIME-IRREGULR DATA
y = Raw(:,8); %THIS ARE corresponding WATER HEIGHT DATA
xq = num_iniziale:num_10:num_finale; %INTERVAL OF 10 MIN
%Place the for loop here
%if y(i+1) -y(i) < num_3h
interp = makima(x,y,xq);
%else interp (i)=NaN
%add the result toTable_interp row 8
Table_interp(:,8)=interp;
Can anyone help me?
Thank you very much for yours answers,
Flavio

Accepted Answer

Peter Perkins
Peter Perkins on 24 Mar 2022
StarStrider's suggestion of datetime and retime is the way to go. Also, like SS, I am mostly guessing at what you are asking for.
First things first: these days, any time you find yourself reaching for datenum, you should stop yourself:
But to answer your question: I think you need to do this in two steps. Do the interpolation (you've done that), and then find locations to overwrite. Here are some irregular data.
Time = datetime(2022,3,24,16,[0 2 4 9 11 16 18 20],0)';
X = rand(size(Time));
tt = timetable(Time,X)
tt = 8×1 timetable
Time X ____________________ _________ 24-Mar-2022 16:00:00 0.036312 24-Mar-2022 16:02:00 0.9018 24-Mar-2022 16:04:00 0.51345 24-Mar-2022 16:09:00 0.15401 24-Mar-2022 16:11:00 0.73382 24-Mar-2022 16:16:00 0.0027704 24-Mar-2022 16:18:00 0.12419 24-Mar-2022 16:20:00 0.47077
Interpolate to make them regular at 1min steps.
ttMin = retime(tt,"minutely","makima")
ttMin = 21×1 timetable
Time X ____________________ ________ 24-Mar-2022 16:00:00 0.036312 24-Mar-2022 16:01:00 0.6438 24-Mar-2022 16:02:00 0.9018 24-Mar-2022 16:03:00 0.71345 24-Mar-2022 16:04:00 0.51345 24-Mar-2022 16:05:00 0.39193 24-Mar-2022 16:06:00 0.27753 24-Mar-2022 16:07:00 0.18794 24-Mar-2022 16:08:00 0.14087 24-Mar-2022 16:09:00 0.15401 24-Mar-2022 16:10:00 0.45496 24-Mar-2022 16:11:00 0.73382 24-Mar-2022 16:12:00 0.66153 24-Mar-2022 16:13:00 0.4818 24-Mar-2022 16:14:00 0.26512 24-Mar-2022 16:15:00 0.081952
You want to not interpolate when the gap is more than a certain size. Find those locations.
Mask = [false; diff(tt.Time) > minutes(3)];
There are probably better ways to do this, but this way shows you what's going on.
ttMin.HadData(tt.Time) = 1;
ttMask = timetable(Time,Mask);
ttMin = synchronize(ttMin,ttMask,"minutely","next")
ttMin = 21×3 timetable
Time X HadData Mask ____________________ ________ _______ _____ 24-Mar-2022 16:00:00 0.036312 1 false 24-Mar-2022 16:01:00 0.6438 0 false 24-Mar-2022 16:02:00 0.9018 1 false 24-Mar-2022 16:03:00 0.71345 0 false 24-Mar-2022 16:04:00 0.51345 1 false 24-Mar-2022 16:05:00 0.39193 0 true 24-Mar-2022 16:06:00 0.27753 0 true 24-Mar-2022 16:07:00 0.18794 0 true 24-Mar-2022 16:08:00 0.14087 0 true 24-Mar-2022 16:09:00 0.15401 1 true 24-Mar-2022 16:10:00 0.45496 0 false 24-Mar-2022 16:11:00 0.73382 1 false 24-Mar-2022 16:12:00 0.66153 0 true 24-Mar-2022 16:13:00 0.4818 0 true 24-Mar-2022 16:14:00 0.26512 0 true 24-Mar-2022 16:15:00 0.081952 0 true
Just need to unmaskl the gap ends, and then overwrite the gaps.
ttMin.Mask(tt.Time) = false
ttMin = 21×3 timetable
Time X HadData Mask ____________________ ________ _______ _____ 24-Mar-2022 16:00:00 0.036312 1 false 24-Mar-2022 16:01:00 0.6438 0 false 24-Mar-2022 16:02:00 0.9018 1 false 24-Mar-2022 16:03:00 0.71345 0 false 24-Mar-2022 16:04:00 0.51345 1 false 24-Mar-2022 16:05:00 0.39193 0 true 24-Mar-2022 16:06:00 0.27753 0 true 24-Mar-2022 16:07:00 0.18794 0 true 24-Mar-2022 16:08:00 0.14087 0 true 24-Mar-2022 16:09:00 0.15401 1 false 24-Mar-2022 16:10:00 0.45496 0 false 24-Mar-2022 16:11:00 0.73382 1 false 24-Mar-2022 16:12:00 0.66153 0 true 24-Mar-2022 16:13:00 0.4818 0 true 24-Mar-2022 16:14:00 0.26512 0 true 24-Mar-2022 16:15:00 0.081952 0 true
ttMin.X(ttMin.Mask) = NaN
ttMin = 21×3 timetable
Time X HadData Mask ____________________ ________ _______ _____ 24-Mar-2022 16:00:00 0.036312 1 false 24-Mar-2022 16:01:00 0.6438 0 false 24-Mar-2022 16:02:00 0.9018 1 false 24-Mar-2022 16:03:00 0.71345 0 false 24-Mar-2022 16:04:00 0.51345 1 false 24-Mar-2022 16:05:00 NaN 0 true 24-Mar-2022 16:06:00 NaN 0 true 24-Mar-2022 16:07:00 NaN 0 true 24-Mar-2022 16:08:00 NaN 0 true 24-Mar-2022 16:09:00 0.15401 1 false 24-Mar-2022 16:10:00 0.45496 0 false 24-Mar-2022 16:11:00 0.73382 1 false 24-Mar-2022 16:12:00 NaN 0 true 24-Mar-2022 16:13:00 NaN 0 true 24-Mar-2022 16:14:00 NaN 0 true 24-Mar-2022 16:15:00 NaN 0 true
  2 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!