Finding max peak between two datetime values
Show older comments
Hi
I have a cluster of peaks that I want to make into one, but this is an issue. I have a function crossing_V7 that identifies when a peak starts (goes crosses the limit Abonnemang) and ends. Now I want a vector of the same length that has the height of every max peak in every cluster.
clear all
clc
load EFFEKT %EFFEKT contains a value for every our of the year
Abonnemang=21200;
dt = datetime(2021,1,1:1:365);
et = hours(0:23)';
t = repmat(dt,length(et),1) + repmat(et,1,length(dt));
t=t(:);
%(1:744) means I only look at January to simplify
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(EFFEKT(1:744),t(1:744),Abonnemang,'linear'); % positive (pos) and negative (neg) slope crossing points
t0_pos1(10)
t0_neg1(10)

Accepted Answer
More Answers (1)
Hello Joel,
You could try something like this:
clear all
clc
load EFFEKT %EFFEKT contains a value for every our of the year
Abonnemang=21200;
dt = datetime(2021,1,1:1:365);
et = hours(0:23)';
t = repmat(dt,length(et),1) + repmat(et,1,length(dt));
t=t(:);
%(1:744) means I only look at January to simplify
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(EFFEKT(1:744),t(1:744),Abonnemang,'linear'); % positive (pos) and negative (neg) slope crossing points
t0_pos1(10)
t0_neg1(10)
MAXVEC=zeros(size(s0_neg1)); % Array to store the maximum values for each segment
POSMAX(size(MAXVEC,2))=datetime; % Variable to store the position of the maximum for each segment
for i=1:size(s0_neg1,2) % Loop through the segments
jmin=find(t>t0_pos1(i),1); % Find the points inside the segment
jmax=find(t>t0_neg1(i),1)-1;
tj=[t0_pos1(i);t(jmin:jmax);t0_neg1(i)]; % tj are the timepoints conforming the segment
sj=[s0_pos1(i);EFFEKT(jmin:jmax);s0_neg1(i)];% sj are the y-values conforming the segment
[MAXVEC(i),pos]=max(sj);
POSMAX(i)=tj(pos);
end
%% Plot
imax=744;
plot(t(1:imax),EFFEKT(1:imax))
hold on
plot(t(1:imax),Abonnemang*ones(1,imax))
plot(t0_pos1,s0_pos1,'r*')
plot(t0_neg1,s0_neg1,'r*')
plot(POSMAX,MAXVEC,'g*','MarkerSize',10)
ylim([0.95*Abonnemang max(EFFEKT(1:imax))])
xlim([t0_pos1(1) t0_neg1(end) ])
l=legend({'EFFEKT','Abonnemang','','','Maximum'});
l.Position = [0.37, 0.64, 0.22, 0.12];
Hope this helps!
Categories
Find more on Logical 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!