Am using matlab to detect peaks, how can I use it to find the area under the curve, average of data points between peak and valley, and slope?

6 visualizzazioni (ultimi 30 giorni)
Is there a way to do this automatically all at once? I have some data, which I find the peaks of like so:
Peakdata=TaskData(:,k);
[Maxima,MaxIdx] = findpeaks(Peakdata,'MinPeakHeight',mean(Peakdata),'MinPeakDistance',10);
Troughdata=1.01*max(Peakdata)-Peakdata;
[Minima,MinIdx] = findpeaks(Troughdata,'MinPeakHeight',mean(Troughdata),'MinPeakDistance',10);
Minima = Peakdata(MinIdx);
I have several different data sets which I do this to and I get a list of all the peaks and troughs in the data (looks kinda like an imperfect sine wave) . I also want to number all peaks and then calculate the average of the data between each peak/trough. As well as the ascent/descent rates (slope) to get from each peak and trough. Then if possible calculate the area under the curve between each peak and trough. So not the hole area like a parabola but half the parabola.

Risposte (2)

Image Analyst
Image Analyst il 5 Ago 2016
Modificato: Image Analyst il 5 Ago 2016
Did you try mean() and sum()? Like
for k = 1 : length(MinIdx)-1
peakAverages(k) = mean(Peakdata(MinIdx(k):MinIdx(k+1)));
peakAreas(k) = sum(Peakdata(MinIdx(k):MinIdx(k+1)));
end
I'm assuming you have mins on each side of a max - i.e. like no peak on the edge not enclosed in between with trough mins.
  3 Commenti
sal goodman
sal goodman il 12 Ago 2016
I am trying to get the code to work (even to just gather up the data points between each peak and valley) but running into an error:
The following error occurred converting from cell to double:
Error using double
Conversion to double from cell is not possible.
Error in Test (line 72)
peakAverages(:,j) = {Peakdata((MaxIdx(j):MinIdx(j+1)),:)};
And here is the code:
for i = 1:numel(tasknames) %for each test point do the math and find the peaks. tasknames lists the test points.
TaskData = cell2mat(task_data(i));
Peakdata=TaskData(:,4); %data is in the 4th column of my larger data matrix.
[Maxima,MaxIdx] = findpeaks(Peakdata,'MinPeakHeight',mean(Peakdata),'MinPeakDistance',10);
Maxima=Peakdata(MaxIdx);
Troughdata=1.01*max(Peakdata)-Peakdata;
[Minima,MinIdx] = findpeaks(Troughdata,'MinPeakHeight',mean(Troughdata),'MinPeakDistance',10);
Minima = Peakdata(MinIdx);
MinLength = length(MinIdx);
MaxLength = length(MaxIdx);
%if there is a trough first, I want to take the first peak value
%index and the second trough value index. Also since there are
%different numbers of peaks and troughs, I want to make sure that
%they match in length by always ending on a trough index.
%if there is a peak first, or MinIdx is greater than MaxIdx, do
%normal j = 1 until whichver list of peaks/trouhgs is shorter.
if MinIdx(1) < MaxIdx(1)
if MinLength > MaxLength
for j = 1 : length(MaxIdx)
peakAverages(:,j) = {Peakdata((MaxIdx(j):MinIdx(j+1)),:)};
end
elseif MinLength == MaxLength
for j = 1 : length(MinIdx)
peakAverages(:,j) = {Peakdata(MaxIdx(j):MinIdx(j+1),:)};
end
elseif MinLength < MaxLength
for j = 1 : length(MinIdx)
peakAverages(:,j) = {Peakdata(MaxIdx(j):MinIdx(j+1),:)};
end
end
else
if MinLength > MaxLength
for j = 1 : length(MaxIdx)
peakAverages(j,:) = {Peakdata(MaxIdx(j):MinIdx(j),:)};
end
elseif MinLength == MaxLength
for j = 1 : length(MinIdx)
peakAverages(:,j) = {Peakdata(MaxIdx(j):MinIdx(j),:)};
end
elseif MinLength < MaxLength
for j = 1 : length(MinIdx)
peakAverages(:,j) = {Peakdata(MaxIdx(j):MinIdx(j),:)};
end
end
end
end

Accedi per commentare.


Clara Sánchez del Valle
Clara Sánchez del Valle il 11 Dic 2020
I did it this way and it worked for me.
[max, maxIdx] = findpeaks(Data);
DataInv = 1.01*max(Data) - Data;
[min, minIdx] = findpeaks(DataInv);
maxminIdx = [maxIdx minIdx];
maxminIdx = sort(maxminIdx);
maxmin = Data(maxminIdx);

Community Treasure Hunt

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

Start Hunting!

Translated by