How do i insert seconds in a timeseries data when the frequency of the data is inconsistent?

4 views (last 30 days)
I have a timeseries data as:
And i need to insert seconds on this data in way that it depends upon the number of datapoints the minutues has. For example, 9/5/2021 11:30:00 has 5 datapoints so every data will be increased by {60/5= 12 seconds), 11:31:00 has 3 data points so, every data is increased by {60/3=20 seconds).
Input_data=
9/5/2021 11:30:00
9/5/2021 11:30:00
9/5/2021 11:30:00
9/5/2021 11:30:00
9/5/2021 11:30:00
9/5/2021 11:31:00
9/5/2021 11:31:00
9/5/2021 11:31:00
9/5/2021 11:32:00
9/5/2021 11:32:00
9/5/2021 11:32:00
9/5/2021 11:32:00
Final_output=
9/5/2021 11:30:00
9/5/2021 11:30:12
9/5/2021 11:30:24
9/5/2021 11:30:36
9/5/2021 11:30:48
9/5/2021 11:31:00
9/5/2021 11:31:20
9/5/2021 11:31:40
9/5/2021 11:32:00
9/5/2021 11:32:15
9/5/2021 11:32:30
9/5/2021 11:32:45

Accepted Answer

Star Strider
Star Strider on 8 Jun 2021
Try this —
Input_data = ['9/5/2021 11:30:00'
'9/5/2021 11:30:00'
'9/5/2021 11:30:00'
'9/5/2021 11:30:00'
'9/5/2021 11:30:00'
'9/5/2021 11:31:00'
'9/5/2021 11:31:00'
'9/5/2021 11:31:00'
'9/5/2021 11:32:00'
'9/5/2021 11:32:00'
'9/5/2021 11:32:00'
'9/5/2021 11:32:00'];
TV = datetime(Input_data, 'InputFormat','MM/dd/yyyy HH:mm:ss'); % Convert To 'datetime' Array
[TVu,~,Idx] = unique(TV,'stable'); % Unique Values In Original Order
Counts = accumarray(Idx,1); % Count Unique Occurrences
for k = 1:numel(Counts)
ss{k,:} = (0:Counts(k)-1).'*60/Counts(k); % Create 'seconds' Column Vector
end
cs = seconds(cell2mat(ss)); % Convert Numeric Values To 'duration' 'seconds'
TV = TV + cs % Add 'seconds' To Original Vector To Produce Desired Result
TV = 12×1 datetime array
05-Sep-2021 11:30:00 05-Sep-2021 11:30:12 05-Sep-2021 11:30:24 05-Sep-2021 11:30:36 05-Sep-2021 11:30:48 05-Sep-2021 11:31:00 05-Sep-2021 11:31:20 05-Sep-2021 11:31:40 05-Sep-2021 11:32:00 05-Sep-2021 11:32:15 05-Sep-2021 11:32:30 05-Sep-2021 11:32:45
I am not certain that this is robust enough to work with any ‘Input_data’ vector, however it works with the example provided.
(To understand how it works, remove the ending semicolons to see the interim results.)
.

More Answers (1)

dpb
dpb on 8 Jun 2021
indx=[0;find(minutes(diff(TT.Time)));height(TT)]; % find change locations indices
N=diff(indx); % number observations/group
secs=60./N; % number seconds differential in group
dsecs=arrayfun(@(s,n)s*[(0:n-1).'],secs,N,'UniformOutput',false); % compute the vector of addends
dsecs=vertcat(dsecs{:});
TT.Time=TT.Time+dsecs; % fixup the time field
Applied to your sample, this results in--
>> TT =
12×1 timetable
Time x
____________________ ____
05-Sep-2021 11:30:00 0.18
05-Sep-2021 11:30:12 0.34
05-Sep-2021 11:30:24 0.21
05-Sep-2021 11:30:36 0.51
05-Sep-2021 11:30:48 0.91
05-Sep-2021 11:31:00 0.63
05-Sep-2021 11:31:20 0.10
05-Sep-2021 11:31:40 0.39
05-Sep-2021 11:32:00 0.05
05-Sep-2021 11:32:15 0.50
05-Sep-2021 11:32:30 0.43
05-Sep-2021 11:32:45 1.00
>>
where I just created a dummy variable to have a non-empty TT.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!