Finding points in specific intervall and delete the ones not in this intervall

1 view (last 30 days)
Hello everyone,
i'm not working that long with matlab and i have a difficult question (at least for me).
I have ploted the following data and the peaks via findpeaks above 20 (x-axis:time, y-axis: absolute value)
What i want do to is i want to locate a peak value and if there are other peaks within 0.08s it's ok but if not delete this peak. For example at 101.42 there is only one peak (shall be droped) whereas the following two are at 102.09 and 102.14 (thats ok).
That (question.mat) is what i have to work with, and this is the line to find the peaks:
[max_YWerte, max_XWerte] = findpeaks(Betrag_Ableitung, t(index_1:index_2-1), "MinPeakHeight",20);
I hope one of you can help me!
Thank you in advance!

Accepted Answer

Voss
Voss on 26 Mar 2022
Edited: Voss on 26 Mar 2022
load('question.mat')
[max_YWerte, max_XWerte] = findpeaks(Betrag_Ableitung, t(index_1:index_2-1), "MinPeakHeight",20);
figure();
plot(t(index_1:index_2-1),Betrag_Ableitung);
hold on
plot(max_XWerte,max_YWerte,'g*');
% take the absolute difference of the time
% of each peak with the time of all peaks:
dt = abs(max_XWerte-max_XWerte.');
% set the difference between a given peak's time
% and itself to Inf (don't want to match a peak to itself):
dt(logical(eye(numel(max_XWerte)))) = Inf;
% dt(1:numel(max_XWerte)+1:end) = Inf; % another way to do the same thing
% any element of dt in a given row (row ii, say) being
% less than 0.08 is the same as another peak being
% within 0.08s of peak ii:
good_idx = any(dt < 0.08,2); % maybe use <= here instead (?)
% plot peaks in magenta that have any other peak within 0.08s:
plot(max_XWerte(good_idx),max_YWerte(good_idx),'mo');
xlim([100 108]);
% to remove the peaks that don't have another peak within 0.08s, you can:
% (1) keep only the peaks that have another peak within 0.08s:
max_XWerte = max_XWerte(good_idx);
max_YWerte = max_YWerte(good_idx);
% OR (2) remove peaks with no other peak within 0.08s:
max_XWerte(~good_idx) = [];
max_YWerte(~good_idx) = [];
% (but don't try to do both (1) and (2) together)
  3 Comments
Voss
Voss on 27 Mar 2022
You're welcome!
Take a look at the following, and see if it seems to work:
load('question.mat')
[max_YWerte, max_XWerte] = findpeaks(Betrag_Ableitung, t(index_1:index_2-1), 'MinPeakHeight',20);
figure();
plot(t(index_1:index_2-1),Betrag_Ableitung);
hold on
plot(max_XWerte,max_YWerte,'g*');
dt = abs(max_XWerte-max_XWerte.');
dt(logical(eye(numel(max_XWerte)))) = Inf;
% good_idx starts as a logical index saying whether each peak
% has another peak within 0.08s:
good_idx = any(dt <= 0.08,2);
% n_peaks_close counts the number of other peaks
% within 0.06s of each peak
n_peaks_close = sum(dt <= 0.06,2);
% because it is the number of *other* peaks (not counting itself),
% n_peaks_close == 2 means 3 peaks are within 0.06s of each other
idx_3_peaks = find(n_peaks_close == 2);
% by definition, idx_3_peaks will be in sets of 3 peaks,
% and in each set we want to remove the middle one
good_idx(idx_3_peaks(2:3:end)) = false;
% plot with red squares peaks that satisfy the conditions:
plot(max_XWerte(good_idx),max_YWerte(good_idx),'rs');
xlim([100 108]);
% out of curiosity, see if there are any sets of
% 4 or more peaks within 0.06s of each other:
idx_more_than_3_peaks = find(n_peaks_close > 2)
idx_more_than_3_peaks = 6×1
141 198 242 243 264 265

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!