Identify the maximum values within fixed time intervals of a time series data set

12 views (last 30 days)
Dear all fellow community members,
I have a matrix of dimensions 20x74932, signifying 20 variables within a time series sampled at 3000 Hz. Additionally, there is a corresponding time matrix sized 20x74932. The time matrix varies in accordance with the variables. In other words, only events are logged in the variables. For instance, for the first variable, a peak load occurs after 800 seconds, followed by the next peak load at 1550 seconds, then at 18000 seconds, and so forth.
For the second variable, the first peak load appears after 300 seconds, with the subsequent one at 1200 seconds, and so on.
My objective is to ascertain the highest peak load for each 20-minute interval throughout the entire time series. For example, if there are 2 peak loads within the first 0 to 20 minutes, I wish to retain only the highest. Subsequently, I want to keep the highest peak load from 20 minutes to 40 minutes and continue in this manner until the end of the time series.
Below is a hypothetical example for your reference:
Time=[4 5 6 7 8 9 10 25 26 27 28 29 46 47 48 49 50 58 59 60; 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 0 ; 1 2 3 4 5 6 7 8 9 10 11 12 0 0 0 0 0 0 0 0]; %Time vector, in minutes
x=[ 0 5 0 1 3 11 2 0 0 1 8 3 0 0 -5 12 2 -2 14 13; 0 0 0 0 0 0 0 15 0 1 -2 0 8 0 3 2 0 18 0 0; 0 8 -1 0 0 0 0 0 0 0 0 25 0 0 0 0 0 0 0 0]; %Variables of peak loads
As mentioned, my aim is to extract the highest peak load per 20-minute interval, which means I am seeking the following results;
0 min - 20min – 11 18 25
20min - 40min – 8 0 0
40min - 60min – 14 0 0
Any help is very appreciated! Thanks.
The example I provided was not 100% representative for my problem. I should have specified the time vector as a matrix, in order to demonstrate a varying time vector for each variable. I have therefore changed the time. This will also influence the results, which are also updated.
I really appreciate all your time and efforts, and apologize for providing an incorrect example to my actual problem.

Accepted Answer

Star Strider
Star Strider on 26 Jan 2024
Edited: Star Strider on 26 Jan 2024
Try this —
Time=[4 5 6 7 8 9 10 25 26 27 28 29 46 47 48 49 50 58 59 60]; %Time vector, in minutes
x=[ 0 5 0 1 3 11 2 0 0 1 8 3 0 0 -5 12 2 -2 14 13; 0 0 0 0 0 0 0 15 0 1 -2 0 8 0 3 2 0 18 0 0; 0 8 -1 0 0 0 0 0 0 0 0 25 0 0 0 0 0 0 0 0];
interval = ceil(Time/20)
interval = 1×20
1 1 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3
Rcell = accumarray(interval(:), (1:numel(interval)).', [], @(v){max(x(:,v).')});
Column_ = cell2mat(Rcell)
Column_ = 3×3
11 0 8 8 15 25 14 18 0
intv = 0:numel(Rcell);
rn = compose('% 2d min - %2d min — ',[intv(1:end-1); intv(2:end)].'*20);
Results = array2table(Column_, 'RowNames',rn)
Results = 3×3 table
Column_1 Column_2 Column_3 ________ ________ ________ 0 min - 20 min — 11 0 8 20 min - 40 min — 8 15 25 40 min - 60 min — 14 18 0
EDIT — (26 Jan 2024 at 15:20)
Added ‘Results’ table.
.
  8 Comments
Askeladden2
Askeladden2 on 30 Jan 2024
Hi again,
The new code is working perfectly. Filling the empty elements with zeros in 'Rcell' obviously did the trick using R2020a.
Thank you very much! I greatly appreciate your help.
Star Strider
Star Strider on 30 Jan 2024
As always, my pleasure!
That was the update required to use the offset vector in ‘Part Two’ earlier, where I added it originally.
My ‘Part Two’ code is much more robust that my original ‘Part One’ code.

Sign in to comment.

More Answers (2)

Catalytic
Catalytic on 26 Jan 2024
Edited: Catalytic on 26 Jan 2024
Time=[4 5 6 7 8 9 10 25 26 27 28 29 46 47 48 49 50 58 59 60]; %Time vector, in minutes
x=[ 0 5 0 1 3 11 2 0 0 1 8 3 0 0 -5 12 2 -2 14 13; 0 0 0 0 0 0 0 15 0 1 -2 0 8 0 3 2 0 18 0 0; 0 8 -1 0 0 0 0 0 0 0 0 25 0 0 0 0 0 0 0 0]; %Variables of peak loads
T=timetable(minutes(Time(:)), x');
% Retime timetable
newTimetable = retime(T,"regular","max","TimeStep",minutes(20),...
'IncludedEdge','right')
newTimetable = 3×1 timetable
Time Var1 ______ ______________ 20 min 11 0 8 40 min 8 15 25 60 min 14 18 0
  1 Comment
Askeladden2
Askeladden2 on 29 Jan 2024
Edited: Askeladden2 on 29 Jan 2024
Thank you very much for your response on an ingenious way to solve the problem. Must I have a constant time vector in this solution proposal of yours?
I was a bit hasty in my example, as I should have created a varying time vector for each variable. To simplify things, I am changing the time to the following matrix:
Time =[4 5 6 7 8 9 10 25 26 27 28 29 46 47 48 49 50 58 59 60; 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 0 0; 1 2 3 4 5 6 7 8 9 10 11 12 0 0 0 0 0 0 0 0];
NB! The zeros in rows 2 and 3 are added at the end to maintain equal size of the matrix.
This also influence the results;
0 min - 20min – 11 18 25
20min - 40min – 8 0 0
40min - 60min – 14 0 0
Thank you again for your time and effort!

Sign in to comment.


Morgan
Morgan on 26 Jan 2024
% INTERVAL TO SAMPLE
Interval = 20;
% TIME ARRAY
Time = [ 4 5 6 7 8 9 10 25 26 27 28 29 46 47 48 49 50 58 59 60 ];
% CALCULATE NUMBER OF INTERVALS
numIntervals = 1 + floor((Time(end) - Time(1))/Interval);
% CALCULATE INDICES FOR TIME ARRAY INTERVALS
startIdx = zeros(numIntervals,1);
endIdx = zeros(numIntervals,1);
for n = 1 : numIntervals
startIdx(n) = find(Time >= (n-1)*Interval, 1, 'first');
endIdx(n) = find(Time < n*Interval, 1, 'last');
end
% VARIABLES OF PEAK LOADS
x = [ 0 5 0 1 3 11 2 0 0 1 8 3 0 0 -5 12 2 -2 14 13 ; ...
0 0 0 0 0 0 0 15 0 1 -2 0 8 0 3 2 0 18 0 0 ; ...
0 8 -1 0 0 0 0 0 0 0 0 25 0 0 0 0 0 0 0 0 ];
% CALCULATE NUMBER OF VARIABLES
nvars = size(x,1);
% CALCULATE ARRAY A MAXIMUM PEAK LOAD VALUES
% AT EACH INTERVAL
maxValues = zeros(nvars,numIntervals);
for n = 1 : numIntervals
maxValues(:,n) = max(x(:,startIdx(n):endIdx(n)),[],2);
end
disp(maxValues)
11 8 14 0 15 18 8 25 0
  1 Comment
Askeladden2
Askeladden2 on 29 Jan 2024
Thank you for your response. I gave an incorrect example to my actual problem. I have updated the example in the original text, if you are still interested in helping me.
Thank you in advance.

Sign in to comment.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!