# Finding max peak between two datetime values

3 views (last 30 days)
Joel on 27 Apr 2023
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)

Mathieu NOE on 2 May 2023
hello
try 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)
for ci = 1:numel(t0_pos1)
idx = (t>=t0_pos1(ci)&t<=t0_neg1(ci));
[max_of_cluster(ci),ix] = max(EFFEKT(idx));
tmp = t(idx);
tmax(ci) = tmp(ix);
%plot
figure(ci)
ind = find(idx);
a = 2; % add some pre and post samples to better visualize the raw data curve
ind = ind(1)-a:ind(end)+a;
plot(t(ind),EFFEKT(ind),t0_pos1(ci),Abonnemang,'dr',t0_neg1(ci),Abonnemang,'dk',tmax(ci),max_of_cluster(ci),'dm');
title(['Cluster # ' num2str(ci)]);
end

Antoni Garcia-Herreros on 2 May 2023
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)
ans = datetime
27-Jan-2021 05:24:37
t0_neg1(10)
ans = datetime
27-Jan-2021 19:37: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 Descriptive Statistics in Help Center and File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!