# Plot data and keep the maximum values

3 views (last 30 days)
Panagiotis Artemiou on 5 Jun 2023
Commented: Mathieu NOE on 5 Jun 2023
Hello I have a set of data in a matrix, that have, two columns, the first is a distance, and the second is a shear stress. The plot that comes out when i plot these is this:
Is there a way that I can keep a simple curve of that plot that looks something like this:
I am also attaching the set of data. Thank you.

KSSV on 5 Jun 2023
Edited: KSSV on 5 Jun 2023
x = T.(1) ;
y = T.(2) ;
[u,l] = envelope(y,10,'peak') ;
plot(x,y,'r',x,u,'b')

Pramil on 5 Jun 2023
Edited: Pramil on 5 Jun 2023
you can try and fit a regression line to the scatter plot of your data to obtain a simple curve that approximates the trend in your data. https://www.mathworks.com/help/matlab/ref/polyfit.html

Nathan Hardenberg on 5 Jun 2023
Maybe using the islocalmax-function is appropriate. But looking at your desired line this might get to many points:
x = 1:30;
y = rand([1,30]);
maxPoints = islocalmax(y);
figure(1);clf; hold on;
plot(x,y)
plot(x(maxPoints),y(maxPoints))

Mathieu NOE on 5 Jun 2023
hello
several approaches are possible to draw an envelope of your data - like those examples
you will notice that none of those codes does really match the shape of your expected envelop at the rising portion of your data (at the end)
this will require a liitle bit more work if you really want this shape
so far my suggestions :
t = data(:,1);
x = data(:,2);
% remove duplicates
[t,ia,ic] = unique(t);
x = x(ia);
% obtain the envelope data
%--------------------------------------------
[up1,down1] = envelope(x,17,'peak'); % option 1 with regular (TMW) envelope function
[up2,down2] = envelope2(t,x,'linear'); % option 2 with envelope2 (see function provided below)
[env] = env_secant(t, x, 10, 'top'); % option 3 with env_secant (see function attached)
tf = islocalmax(x,'MinProminence',1e-4,'MinSeparation',5); % option 3 with islocalmax (you can also try with find peaks)
tt = t(tf);
xt = x(tf);
plot(t,x,t,up1,t,up2,tt,xt,t,env)
legend('signal','envelope','envelope2','islocalmax','env secant');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [up,down] = envelope2(x,y,interpMethod)
%ENVELOPE gets the data of upper and down envelope of the known input (x,y).
%
% Input parameters:
% x the abscissa of the given data
% y the ordinate of the given data
% interpMethod the interpolation method
%
% Output parameters:
% up the upper envelope, which has the same length as x.
% down the down envelope, which has the same length as x.
%
% Designed by: Lei Wang, <WangLeiBox@hotmail.com>, 11-Mar-2003.
% Last Revision: 21-Mar-2003.
% Dept. Mechanical & Aerospace Engineering, NC State University.
% \$Revision: 1.1 \$ \$Date: 3/21/2003 10:33 AM \$
if length(x) ~= length(y)
error('Two input data should have the same length.');
end
if (nargin < 2)||(nargin > 3),
error('Please see help for INPUT DATA.');
elseif (nargin == 2)
interpMethod = 'linear';
end
% Find the extreme maxim values
% and the corresponding indexes
%----------------------------------------------------
extrMaxIndex = find(diff(sign(diff(y)))==-2)+1;
extrMaxValue = y(extrMaxIndex);
% Find the extreme minim values
% and the corresponding indexes
%----------------------------------------------------
extrMinIndex = find(diff(sign(diff(y)))==+2)+1;
extrMinValue = y(extrMinIndex);
up = extrMaxValue;
up_x = x(extrMaxIndex);
down = extrMinValue;
down_x = x(extrMinIndex);
% Interpolation of the upper/down envelope data
%----------------------------------------------------
up = interp1(up_x,up,x,interpMethod);
down = interp1(down_x,down,x,interpMethod);
end
Mathieu NOE on 5 Jun 2023
Finally, maybe this is the best solution, without too much hassle :
t = data(:,1);
x = data(:,2);
% remove duplicates
[t,ia,ic] = unique(t);
x = x(ia);
% "detrend" the signal by removing the smoothed data (slightly amplified to
% remove the small peaks from selection)
xm = 1.15*smoothdata(x,'movmedian',30);
xd = x - xm;
id = xd<0;
xd(id) = 0;
tf = islocalmax(xd,'MinSeparation',10); % option 3 with islocalmax (you can also try with find peaks)
tt = t(tf);
xt = x(tf);
plot(t,x,tt,xt)

### Categories

Find more on Multirate Signal Processing in Help Center and File Exchange

R2020b

### Community Treasure Hunt

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

Start Hunting!