Integration via trapezoidal rule in various sections of the same array

2 次查看(过去 30 天)
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.
  2 个评论
darova
darova 2019-10-22
You can manually divide/split your data in specific areas. Use findpeaks to find peaks
FW
FW 2019-10-22
编辑:FW 2019-10-22
Yes, sure but the key question is what should be the syntax? For example findpeaks gives the peak position [peaks, index] and peaks have a bseline width. If my signal is S, how should integrate if the peaks are very close, say the the time index for peak 1 is 150:200, and the other peak is adjacent with some overlap so its index is 200:350. How would one apply trapz for selected ranges? Thanks.

请先登录,再进行评论。

采纳的回答

Jon
Jon 2019-10-22
编辑:Jon 2019-10-22
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
end
  2 个评论
FW
FW 2019-10-22
Thanks, I will try this approach. These indices in the range will belong to the Y values (or Signal values) not the indices corresponding to time values, is this correct? I use the following syntax to find peaks [ peaks index] =findpeaks(Y); With the help of index values I find the values of corresponding time ranges.
Jon
Jon 2019-10-22
编辑:Jon 2019-10-22
Suppose you have a vector of time values, let's call it t, and a vector of corresponding y values, let's call it Y. Suppose using findpeaks or some other function you determine a range of interest in your vector y starts at index values which we will call r1 and r2 respectively. Then you can find the corresponding y values using Y(r1:r2) and the corresponding time values using t(r1:r2).
Note if you use the function findpeaks then you should call it using the syntax
% get location and width of peaks
[pks,locs,w] = findpeaks(Y);
% calculate half width of peaks as an integer count of indices
halfWidth = round(w/2);
% calculate start and end indices for each peak
r1 = locs - halfWidth % vector of starting indices
r2 = locs + halfWidth % vector of ending indices
% put the ranges into a matrix for further use
ranges = [r1(:) r2(:)] % colon makes sure that vector is a column

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by