trapz function only for limited x range

喜你入骨 提交于 2020-01-06 08:37:14

问题


I want to integrate time series data (of a river wave) which has certain peaks, but I only need the integral for the highest peak.

I have a predefined area which the integral should satisfy, and I change a y-axis threshold to achieve this area.

This previous question describes the original problem in more detail: Integration returning required y value for predefined area. With help of Wolfie, code was developed to answer that.

The problem is now that I also integrate over other peaks, when I don't want to.

The code so far:

x = Q(:,1); %
y = Q(:,2); %

target = 17.4e+06;  % Target area = Polder Volume

yi = max( y );      % Initialise yi to be max possible y
dy = 10;            % Step change in yi

Ai = 0;             % Area first iteration
thresh = 10000;     % Threshold for stopping loop
while target - Ai > thresh && yi >= min(y)
    yi = yi - dy;
    ix = y >= yi;
    % Approximate integral above the line
    Ai = trapz( x(ix), y(ix) - yi );
end
figure(e); clf; hold on

plot( x, y );
patch( x(ix), y(ix), [1,0.5,0.5], 'facealpha', 0.5 );
plot( x, ones(size(x))*yi, '--', 'linewidth', 2 )
xlim( [min(x),max(x)] ) 

Results in:

If applied to Data with several peaks.

In summary:

How can I make sure only the biggest peak is cut off by the integral? How can I prevent Ai from including to other peaks and still come close to the target?

来源:https://stackoverflow.com/questions/56025930/trapz-function-only-for-limited-x-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!