how to calculate duration of peaks
Show older comments
I have an envelope signal attached y.mat and time array t.mat
I want to calculate the duration in second between the peaks (mentionned in red color in the figure below)

How I can reach this task
Accepted Answer
More Answers (2)
Image Analyst
on 9 Jul 2022
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
st = load('t.mat')
t = st.ty;
sy = load('y.mat')
sh1 = sy.sh1;
y = sh1;
plot(t, y, 'b-', 'LineWidth', 2);
grid on;
hold on;
xlabel('t', 'FontSize', fontSize);
ylabel('y', 'FontSize', fontSize);
[peakValues, indexesOfPeaks] = findpeaks(y, 'MinPeakProminence', 0.25); % Desired Peak & Index
% Plot peaks
plot(t(indexesOfPeaks), peakValues, 'rv')
numPeaks = numel(peakValues)
% Fall down the left sides
leftIndexes = zeros(numPeaks, 1);
tLeft = zeros(numPeaks, 1);
for k = 1 : numPeaks
for k2 = indexesOfPeaks(k) : -1 : 1
thisValue = y(k2);
if thisValue < 0.1 && thisValue > y(k2+1)
leftIndexes(k) = k2;
tLeft(k) = t(leftIndexes(k));
% Put a green line at the start of the peak.
xline(tLeft(k), 'Color', 'g', 'LineWidth', 2);
break;
end
end
end
% Fall down the right sides
rightIndexes = zeros(numPeaks, 1);
tRight = zeros(numPeaks, 1);
for k = 1 : numPeaks
for k2 = indexesOfPeaks(k) : length(y)
thisValue = y(k2);
if thisValue < 0.1 && thisValue > y(k2-1)
rightIndexes(k) = k2;
tRight(k) = t(rightIndexes(k));
% Put a red line at the end of the peak.
xline(tRight(k), 'Color', 'r', 'LineWidth', 2);
break;
end
end
end
% Get the widths
peakWidths = tRight - tLeft

The left side is tLeft and is indicated by the green line.
The right side is tRight and is indicated by the red line.
The widths are given by peakWidths.
peakWidths =
0.146280579131303
0.157264103844234
0.155516724912631
0.159510733899151
0.170993509735397
0.159011482775836
0.177983025461807
0.179980029955067
0.150773839241138
0.159760359460809
2 Comments
Abdelhakim Souidi
on 11 Jul 2022
Image Analyst
on 11 Jul 2022
@Abdelhakim Souidi I don't use it that much. All I'd do is look at the documentation and the examples there and try to apply it to your data, something you can do as well as me.
Image Analyst
on 9 Jul 2022
Edited: Image Analyst
on 9 Jul 2022
0 votes
7 Comments
Abdelhakim Souidi
on 9 Jul 2022
Abdelhakim Souidi
on 9 Jul 2022
Image Analyst
on 9 Jul 2022
Looks like Star's code worked, so just use that.
Abdelhakim Souidi
on 9 Jul 2022
Image Analyst
on 9 Jul 2022
Please give an example of someplace in your data where Star's code identified either the start of the peak, the peak location, or the end of the peak as being at the wrong place.
Another option would be to just find the peaks with findpeaks and then "fall down" each side of the peak until you get to a point where the data starts to increase again.
Abdelhakim Souidi
on 9 Jul 2022
Image Analyst
on 11 Jun 2023
Try adjusting the 'MinPeakDistance' to make sure your valleys are not too close together.
Categories
Find more on Descriptive Statistics 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!


