如何在matlab中使用trapz计算峰下面积?

时间:2017-11-06 15:20:17

标签: matlab matlab-figure matlab-guide

我是matlab的新手,并且在计算峰值下面积时存在问题。以下是我用来查找峰值的代码:

%% Peak flow analysis 2 (Figure 3)
flowtable = finalCSVnew(:,[1,7:8]);                                         % create table containing DateAndTime, Durchflusslm, and SummeaktuellerTagm data

peakflowEvent = flowtable{:,2} > aa ;                                        % determine the threshold of flow(m3/h) for peakflowEvent

% use false as logical vector to determine transition. With function diff, 
% transitions from false (0) to true (1) will be 1 and transitions from true
% to false will be -1. This will be 1 at the start of a dry period and -1 after the end
peakTransitions = diff([false; peakflowEvent; false]);                             
eventStarts = find(peakTransitions == 1);
eventEnds = find(peakTransitions == -1) -1;

% define the peak flow of each event through the flow data (peakflow) and
% the time when peak flow is happened (peakflowtime)
[peakflow, peakflowlocrel] = arrayfun(@(s, e) max(flowtable.Durchflusslm(s:e)), eventStarts, eventEnds);
peakflowlocabs = peakflowlocrel + eventStarts - 1;
peakflowtime=flowtable.DateAndTime(peakflowlocabs);

% create result table containing start and end time for peak flow event, the duration
% between start and end time, and peak flow
peakflowanalysis2 = table(flowtable.DateAndTime(eventStarts), flowtable.DateAndTime(eventEnds), ...
                 flowtable.DateAndTime(eventEnds) - flowtable.DateAndTime(eventStarts), ...
                 peakflow, peakflowtime, ...
                 'VariableNames', {'Start', 'End', 'Duration','PeakFlow','PeakFlowTime'});

numPeakflow2 = height(peakflowanalysis2);                                   % calculate the number of max flow

% plot flow and peak flow
figure(3)
plot(flowtable.DateAndTime,flowtable.Durchflusslm,flowtable.DateAndTime(peakflowlocabs),peakflow,'v','MarkerFaceColor','red',...
    'MarkerSize',5)
xlabel('Date and Time');                                                    % define Date and Time as x-axis
ylabel('Flow [m3/h]');                                                      % define Flow as y-axis
title('Peak Flow Events (2)');                                              % define the title of the plot
legend('Flow','Peak Flow','Location','Northeast','Orientation','Vertical')
grid on                                                                     % show grid on plot
datacursormode on                                                           % enable to display data value interactively in the plot

% clear temporary variables
clearvars peakflowEvent peakTransitions eventStarts eventEnds peakflowlocrel peakflowlocabs peakflow peakflowtime 

它产生两个表和一个数字。包含峰值的图形也代表每个事件。峰数=事件数 figure 该表包含原始数据: flowtable 下表包含具有峰值的过滤数据 peakflowanalysis2 error

我想知道如何计算每个事件(11个事件)的峰值下面积,而不是总和。我读到了trapz(),但我很困惑如何将它应用于多个峰值。请你帮助我好吗?非常感谢你的帮助。

1 个答案:

答案 0 :(得分:0)

首先,不要在表中存储大型数据集,它们非常慢。我使用大量的水文数据,并且总是使用数字数组作为数据,并使用数字数组(作为datenum)来表示时间序列。

要获得峰值以下的区域,请使用trapz并在每个洪峰上循环:

time_start=datenum(Peakflowanalysis.Start,'dd.mm.yyyy HH:MM:SS');
time_end=datenum(Peakflowanalysis.Ende,'dd.mm.yyyy HH:MM:SS');
time_array=datenum(flowtable.DateandTime,'dd.mm.yyyy HH:MM:SS');

for i=1:height(peakflow)
ind1=find(time_array==time_start)
ind2=find(time_array==time_end)
peak_sum(i)=trapz(flowdata(Start(ind1):Ende(ind2)));% trapz method
peak_sum(i,2)=sum(flowdata(Start(ind1):Ende(ind2)));%sum method
end

之前,您需要将开始和结束转换为您的流数据的索引。

也尝试使用sum方法。您将看到它与您的trapz结果非常相似,具体取决于数据的时间分辨率。