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 2023-6-5
编辑:KSSV 2023-6-5
Read about envelope
T = readtable('') ;
x = T.(1) ;
y = T.(2) ;
[u,l] = envelope(y,10,'peak') ;

更多回答(3 个)

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.

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;

Mathieu NOE
Mathieu NOE 2023-6-5
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);
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, <>, 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.');
if (nargin < 2)||(nargin > 3),
error('Please see help for INPUT DATA.');
elseif (nargin == 2)
interpMethod = 'linear';
% 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);
  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);



Help CenterFile Exchange 中查找有关 Multirate Signal Processing 的更多信息





Community Treasure Hunt

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

Start Hunting!

Translated by