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(:,7)= datenum(t_ibrido'); %I add a column with time_data in numerical format
%% INTERPOLATION(MAKIMA)-10 MIN
num_finale = datenum(t2);
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
Can anyone help me?
Thank you very much for yours answers,
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)
Interpolate to make them regular at 1min steps.
ttMin = retime(tt,"minutely","makima")
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")
Just need to unmaskl the gap ends, and then overwrite the gaps.
ttMin.Mask(tt.Time) = false
ttMin.X(ttMin.Mask) = NaN