Plot data and keep the maximum values

조회 수: 7 (최근 30일)
Panagiotis Artemiou
Panagiotis Artemiou 2023년 6월 5일
댓글: Mathieu NOE 2023년 6월 5일
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
Nathan Hardenberg 2023년 6월 5일
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)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Multirate Signal Processing에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by