How to do interpolation only if data interval is > than 3h?
조회 수: 1 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
Peter Perkins
2022년 3월 24일
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
댓글 수: 2
Peter Perkins
2022년 3월 25일
Those warnings/errors seem pretty straight-forward. The warning is probably harmless, depending on what you are doing. The error is because your data are not sorted by time.
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!