Plot data and keep the maximum values

4 次查看(过去 30 天)
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
KSSV 2023-6-5
编辑:KSSV 2023-6-5
Read about envelope
T = readtable('https://in.mathworks.com/matlabcentral/answers/uploaded_files/1402969/data.txt') ;
x = T.(1) ;
y = T.(2) ;
[u,l] = envelope(y,10,'peak') ;
plot(x,y,'r',x,u,'b')

更多回答(3 个)

Pramil
Pramil 2023-6-5
编辑:Pramil 2023-6-5
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
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
Mathieu NOE 2023-6-5
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 :
data = readmatrix('data.txt');
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.
%
% See also DIFF INTERP1
% 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
  1 个评论
Mathieu NOE
Mathieu NOE 2023-6-5
Finally, maybe this is the best solution, without too much hassle :
data = readmatrix('data.txt');
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)

请先登录,再进行评论。

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by