Area Under Curve and above X axis only, and between x axis limits.

15 次查看(过去 30 天)
I need to calculate the area under the curve. The curve may go down the x axis, but I dont' want to calculate that area. And then it may come up the axis again. Also, I need to calculate area in the limits of 1Hz to 6Hz, as shown in the image attached.
1.png
Please help out. I went through the trapz() function, but I don't know, how I can measure the area in this case. Thanks in advance.
  2 个评论
Looky
Looky 2019-12-18
编辑:Looky 2019-12-18
The easiest solution would be to make a copy of your data and just set everything below zero to zero. The use trapz:
y=yourYData(yourXData>1&yourXData<6);
x=yourXData(yourXData>1&yourXData<6);
y(y<0)=0;
area=trapz(x, y )

请先登录,再进行评论。

采纳的回答

Allen
Allen 2019-12-18
If you are trying to determine the area under the curve between the frequencies of 1 Hz to 6 Hz for all y-values greater than 0, then Looky's suggestion will do the trick. However, if you are trying to determine the area under the curve between 1 Hz to 6 Hz and for all y-values greater than some threshold value other than zero, you will need to some additional adjustments before running trapz(). From the image you provided it looks like 1 V/H is your target threshold.
% Assign the y-value threshold to a variable. Makes is clearer to see how and where it is used,
% as well as makes is easy to change if needed.
thres = 1;
% Extract x- and y-data for frequencies in the desired range of 1-6 Hz.
x = yourXData(yourXData>1 & yourXData<6); % x-values in the range
y = yourYData(yourXData>1 & yourXData<6)-thres; % y-values in range & with a threshold adjustment
% Set all values that are <0 (these were originally all values less than the threshold) to zero.
y(y<0) = 0;
% Use trapz function to calculate the area under the curve.
area = trapz(x, y )
  2 个评论
Bhanu Pratap Singh
Bhanu Pratap Singh 2019-12-20
Yes, I need the threshold to be 1. I think I can do just the following, to make all values less than the threshold to be zero. Thanks a lot for helping me out.
% Set all values that are < 1 (these were originally all values less than the threshold) to zero.
y(y<1) = 0;
Looky
Looky 2019-12-20
Hey Bhanu,
no, this is not the same! Just take @Allen's Solution, it has a variable threshold via the thres variable, that is what you want.
Your solution will give you the area under the curve but enclosed with x-axis and not your threshhold at 1 !

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differentiation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by