-
Notifications
You must be signed in to change notification settings - Fork 5
/
tsEvaFillSeries.m
44 lines (38 loc) · 1.31 KB
/
tsEvaFillSeries.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function [ filledTimeStamps, filledSeries, dt ] = tsEvaFillSeries( timeStamps, series )
indxs = ~isnan(series);
timeStamps = timeStamps(indxs);
series = series(indxs);
% ensuring monotonic time vector and no dubplicates. In case of dubplicate time stamps the highest value is considered
[newTs,~,idx] = unique(sort(timeStamps));
newSeries = accumarray(idx,series,[],@max);
mint = min(newTs);
maxt = max(newTs);
dt = min(diff(newTs));
if dt >= 350 && dt <= 370
% this is an annual series
mindtVec = datevec(mint);
mindtY = mindtVec(1);
maxdtVec = datevec(maxt);
maxdtY = maxdtVec(1);
years = (mindtY:maxdtY)';
dtvec = [years, ones(size(years)), ones(size(years))];
filledTimeStamps = datenum(dtvec);
elseif dt >= 28 && dt <= 31
% this is a monthly series
mindtVec = datevec(mint);
mindtY = mindtVec(1);
maxdtVec = datevec(maxt);
maxdtY = maxdtVec(1);
years = (mindtY:maxdtY);
months = 1:12;
[ymtx, mmtx] = meshgrid(years, months);
ys = ymtx(:);
ms = mmtx(:);
dtvec = [ys, ms, ones(size(ys))];
filledTimeStamps = datenum(dtvec);
else
filledTimeStamps = (mint:dt:maxt)';
end
filledSeries = interp1(newTs, newSeries, filledTimeStamps, 'nearest');
filledSeries = tsRemoveConstantSubseries(filledSeries, 4);
end