使用一个范围内的trapz MATLAB计算设定水平之上和之下的区域

时间:2017-01-12 17:43:46

标签: matlab area curve

Data Plot

        %% Data
% Imports the array data (x and y) to the workspace by loading Excel data
% that has been imported and converted to *.mat format.
    load('900day_r') % y data.
    load('x_degreesb'); % x axis data 

%% Remove non linear trends
    opol = 0;% Degree of filtering on original profile
    [p,s,mu] = polyfit(x_degreesb,x900day_r,opol);
    f_y = polyval(p,x_degreesb,[],mu);
    x900day_r = x900day_r - f_y;

    max_x = max(x900day_r);% Find maximum in array
    x900day_r = x900day_r/max_x;% Normalize height to max
    min_x = min(x900day_r);% Find minimum in array
    x900day_r = x900day_r - min_x;% Shift profile (lowest value in array = 0)

%% Find Peaks & Valleys 
    [pks, locs] = findpeaks(x900day_r); % returns peaks & locations  
    x900day_r_Inv = max(x900day_r)-x900day_r; % invert y data   
    vlys = max(vlys)-vlys; % invert data for valley markers 

%% Plot Profile
% Plot profile and markers for peaks
    plot(x_degreesb,x900day_r,'b',x_degreesb(locs),pks+0.04,'v','markersize',5,'markerfacecolor','b','linewidth',1); 
    hold on
% Plot profile and markers for valleys
    plot(x_degreesb(min_locs),vlys-0.04,'^','markersize',5,'markerfacecolor','b','linewidth',1); 

% Plot characteristics
    axis('tight') % Makes the graph fill the figure.
    grid on % Turns the grid on (major not minor).

% Sets up the figure (fig) for display
    fig = gca; 
    set(fig,'fontname','Bodoni 72','fontsize',20);

% Set y limits to auto. Set x limits and x tick marks
    ylim('auto'); % Sets the y limits 
    xlim([0, 360]); % Sets the x limits from 0 to 360
    set (fig, 'XTick', [0, 45, 90, 135, 180, 225, 270, 315, 360]) % Sets x axis tick marks

% Set fig for presentation appearance
    x = xlabel('$Location On Cylinder Perimiter {[degrees]}$'); % x label.
    y = ylabel('$Curve Height {[mm]}$'); % y label.
    t = title('$900 Days Cylinder$ r'); % Title (presentation fraction).

% Set vertical lines at quadrant boundaries
    hold on
    x1 = 90;
    x2 = 180;
    x3 = 270;
    x4 = 360;
    y1 = get(gca, 'ylim');
    plot ([x1 x1],y1,'k')%Caudal Medial(0:90) 
    plot ([x2 x2],y1,'k')%Cranial Medial(90:180)
    plot ([x3 x3],y1,'k')%Cranial Lateral(180:270)
    plot ([x4 x4],y1,'k')%Cadual Lateral(270:360)
    hold on

% Interpretation of text characters for presentation
    set(t,'Interpreter','Latex');
    set(x,'Interpreter','Latex');
    set(y,'Interpreter','Latex');

%% Isolate Cranial Medial & Lateral Section of Profile
    x = x_degreesb;% Quadrant data
    y = x900day_r;% Profile data

    indx = (x >= 90) & (x <= 270);% Index section
    [pks, locs, widths, proms] = findpeaks(y(indx));% Find peaks in section

    Rmax = max(pks);% Find maximum peak    
    Rmin = min(y(indx));% Find minimum in section   
    CL = (Rmax + Rmin)/2;% Center line of sectioned profile       

%% Plot Center Line 
    hold on
    x1 = 90;
    x2 = 270;
    y1 = CL;
    plot ([x1 x2],[y1 y1],'r','linewidth',1.5);

%% Plot Rmax 
    hold on
    x1 = 90;
    x2 = 270;
    y1 = Rmax;
    plot ([x1 x2],[y1 y1],'k','linewidth',1.5);

%% Plot Rmin 
    hold on
    x1 = 90;
    x2 = 270;
    y1 = Rmin;
    plot ([x1 x2],[y1 y1],'k','linewidth', 1.5);

%% Highlight Region of Interest
%subplot(2,1,2)
    x = x_degreesb;% Quadrant data
    y = x900day_r;% Profile data
    indx = (x >= 90) & (x <= 270);% Index section

    plot(x(indx),y(indx),'k','linewidth',2)% Plot 90:270 curve in black
    level = CL;% set the centerline as the level for area shading
% Shade the area of the curve in the indexed section grey [.7 .7 .7] above the level CL
    area(x(indx), max(y(indx), level),level, 'EdgeColor', 'none', 'FaceColor',[.7 .7 .7],'showbaseline', 'off'); 
% Shade the area of the curve in the indexed section dark grey below the level CL
    area(x(indx), min(y(indx), level),level,  'EdgeColor', 'none', 'FaceColor',[.5 .5 .5],'showbaseline','off');

有没有人知道如何使用MATLAB找到90:270之间特定范围的上方(浅灰色)和低于(深灰色)中心线(红色)的区域?我一直在尝试使用trapz,设置水平(数据图中的红线图片),但似乎无法获得trapz来计算突出显示的区域。我发布了代码,但没有发布数据,因为它构成了一条相当大的数据集。任何帮助将不胜感激!

RP

@Some Guy:感谢您的快速回复。这是一个可以运行的示例脚本。您可以通过更改级别值来更改蓝色区域与灰色区域的比率。在水平设置为2的pic 1中,它们应该相等。在图2中,蓝色应该大于灰色,水平设置为1.6。多数民众赞成我想做的事。有什么想法吗?

Graph 1 Graph 2

%% Example 
x = 0:.01:4*pi;% x data
y = sin(x)+2;% y data
level = 1.6;% level
plot(x, y)
hold on
x_interest = 0:.01:x(length(y));
y_interest = sin(x_interest)+2;
xlim ([0 x(length(y))])

% Shaded area above level
area(x_interest, max(y_interest, level), level, ...
    'EdgeColor', 'none', 'FaceColor', [.6 .7 .8], ...
    'ShowBaseLine', 'off');

% Shaded area below level
area(x_interest, min(y_interest, level), level, ...
    'EdgeColor', 'none', 'FaceColor', [.5 .5 .5], ...
    'ShowBaseLine', 'off');

y_above = max(y_interest - level,0); % Take only the part of curve above y_split
y_below = max(-y_above,0); % Take the part below y_split
A_above = trapz(y_above)
A_below = trapz(y_below)

1 个答案:

答案 0 :(得分:1)

如果我在向量y和标量y_split中有数据我想要分割,我会这样做:

y_above = y - y_split;
y_below = max(-y_above,0); % Take the part below y_split
y_above = max(y_above,0); % Take the part above y_split 
A_above = trapz(y_above);
A_below = trapz(y_below);

您可以绘制y_abovey_below,以确保您按照自己的意愿进行整合。

编辑:使用OP示例脚本,level = 2区域为:

A_above =

  399.9997


A_below =

  399.9976

level = 1.6

A_above =

  683.5241


A_below =

  181.1221