Error when using findpeaks for different limits

3 views (last 30 days)
Hi
I have a script identifying peaks in a curve (EFFEKT). The following part of the script smoothens out the peaks; if there are several peaks close to each other (not going below the limit (Abonnemang) in between), it displays only the biggest.
Abonnemang=24200;
for x=1:99999 %IF THERE IS SEVERAL PEAKS IN A ROW, TAKE THE BIGGEST
[max_val,locs] = findpeaks(EFFEKT,'MinPeakHeight',Abonnemang,'MinPeakDistance',x); % find the peak and its x axis location
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(EFFEKT,t,Abonnemang,'linear'); % positive (pos) and negative (neg) slope crossing points
peak_width = t0_neg1- t0_pos1;
if length(max_val)==length(t0_pos1)
break
end
end
dt = datetime(Q,1,1:1:length(Data{:,1}));
et = hours(0:23)';
t = repmat(dt,length(et),1) + repmat(et,1,length(dt));
t=t(:);
figure(3)
plot(t,EFFEKT)
hold on
plot(t,AB)
ylim([-10000 40000])
ylabel('kW')
title('Effektkurva Konsumtion 2021')
My limit was initially set to 24200. I want to check the peaks for the limit 23200, 22200, 21200 and 20200.
24200, 23200 and 22200 works fine, but when I set Abonnemang=21200 I get the following error:
Error using findpeaks
Expected MinPeakDistance to be a scalar with value < 8759.
Error in findpeaks>parse_inputs (line 330)
validateattributes(Pd,{'numeric'},{'real','scalar','nonempty','nonnegative','<',x(M)-x(1)},'findpeaks','MinPeakDistance');
Error in findpeaks (line 135)
= parse_inputs(isInMATLAB,Yin,varargin{:});
Error in FLEX (line 377)
[max_val,locs] = findpeaks(EFFEKT,'MinPeakHeight',Abonnemang,'MinPeakDistance',x); % find the peak and its x axis location
>>
  1 Comment
Image Analyst
Image Analyst on 21 Apr 2023
You forgot to attach your data. If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
If you just want peaks, or indexes, where your signal is above some threshold, like 24200 then why not just threshold
itsAPeak = signal > threshold;

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 21 Apr 2023
The 'MinPeakDistance' value must be less than the number of elements in the vector being used as the first argument to findpeaks. Setting the upper limit on the ‘x’ argument to not exceed that value should avoid that error. Beyond that, ‘t’ does not exist and neither does ‘crossing_v7’ (although I suspect is is a relatively straightforward intersection function using interp1) so this is as far as we can go.
load('EFFEKT.mat')
whos
Name Size Bytes Class Attributes EFFEKT 8760x1 70080 double ans 1x35 70 char cmdout 1x33 66 char
% Abonnemang=24200;
Abonnemang=21200;
for x=1:numel(EFFEKT)-1 %IF THERE IS SEVERAL PEAKS IN A ROW, TAKE THE BIGGEST
[max_val,locs] = findpeaks(EFFEKT,'MinPeakHeight',Abonnemang,'MinPeakDistance',x); % find the peak and its x axis location
[t0_pos1,s0_pos1,t0_neg1,s0_neg1]= crossing_V7(EFFEKT,t,Abonnemang,'linear'); % positive (pos) and negative (neg) slope crossing points
peak_width = t0_neg1- t0_pos1;
if length(max_val)==length(t0_pos1)
break
end
end
Unrecognized function or variable 't'.
dt = datetime(Q,1,1:1:length(Data{:,1}));
et = hours(0:23)';
t = repmat(dt,length(et),1) + repmat(et,1,length(dt));
t=t(:);
figure(3)
plot(t,EFFEKT)
hold on
plot(t,AB)
ylim([-10000 40000])
ylabel('kW')
title('Effektkurva Konsumtion 2021')
.
  2 Comments
Joel
Joel on 23 Apr 2023
Thank you
Apologies for being so unclear.
I reposted this question with the function file and structured the code correctly.
Would really appreciate if you want to look into it
Star Strider
Star Strider on 23 Apr 2023
As always, my pleasure!
I answered the follow-up as well.

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!