MATLAB: Integration via trapezoidal rule in various sections of the same array

numerical integrationtrapezoidal rule

If the signal S consists of several peaks as a function of time, is there a way to assign integration limits for the trapezoidal rule? Say, I would like to integrate peak 1 from 5 to 7 min, then there is another peak 2 which needs to be integrated from 7.5 to 9.5 minutes. The documentation for trapezoidal rule does not mention anything about limits. Assume we do not know the functional form of the peaks. Thanks.

Best Answer

  • Assuming you can find the indices of your ranges by some means, e.g. findpeaks, then make an array of the ranges, for example
    ranges = [103 128;
    182 196;
    297 315]
    then loop through the ranges using the trapezoidal rule for example
    numRanges = size(ranges,1)
    F = zeros(numRanges,1); %preallocate array to hold integrals for each range
    % loop through ranges
    for k = 1:size(ranges,1)
    F(k) = trapz(Y(ranges(k,:))) % assuming your original signal is in vector, Y