【问题标题】:How to calculate area under peaks by using trapz in matlab?如何在matlab中使用trapz计算峰下面积?
【发布时间】:2017-11-06 15:20:16
【问题描述】:

我是 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 

它产生两张表和一张图。包含峰值并代表每个事件的图形。峰数=事件数 该表包含原始数据: 下表包含过滤后的数据,它们有峰值

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

【问题讨论】:

    标签: matlab matlab-figure matlab-guide


    【解决方案1】:

    首先,不要将大型数据集存储在表中,它们非常慢。我处理大量水文数据,并且总是对数据使用数值数组,对时间序列使用数值数组(作为 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
    

    之前,您需要将 start 和 end 转换为 flowdata 的索引。

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

    【讨论】:

    • 感谢您的回复 Squeezie。很抱歉,我仍然不明白将开始和结束转换为我的流数据。你能解释一下吗?
    • Trapz 需要一个向量作为输入。由于您只有一个大向量,因此您需要为每个洪水事件的开始和结束提供索引。似乎您仅将开始日期和结束日期作为字符串获取当偶数开始和结束时,您需要在向量中找到索引。它适用于字符串,但比使用 datenum 并将字符串转换为数字数据时要慢。
    • 感谢您的回复。我在上面应用了您的方法,但是出现错误“使用 datetime/datenum 输入参数过多时出错。”
    • 您是否检查过我是否正确写入了所有变量?此外,您可能需要将表中的日期字符串转换为 cellarray 和/或使用 cellfun 运行它。
    • 是的,我根据数据稍微修改了变量。我上传了上面关于我得到的错误的第四张图片。所以我需要使用 cellfun 将日期时间转换为 cellarray?不使用日期数字?
    猜你喜欢
    • 2021-12-05
    • 1970-01-01
    • 2018-10-28
    • 2019-02-11
    • 2012-01-29
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 2015-08-10
    相关资源
    最近更新 更多