Extracting 2 peak values and their respective location from a set of data points

1 visualización (últimos 30 días)
I am aiming to extract two data points from a waveform within a for loop of n number of repeititions.
I would ideally like it where I can put the range for which I want the two peaks and their locations to be extracted from, i.e. first peak to be extracted from 20-30 and the second peak to be extracted from 60-80. I have included the code I have so far but I feel there must be a much more easier way of doing it:
%% Find Peak Controls
Thold=0.65; %This adjusts the cutoff for the find peaks function 0.65
width=23; %This determines the minimum width between peaks 30
for i=1:nwalks
[Maxpeaks]=findpeaks(Results.Knee_Add_L_Torque(i).waveforms(1:100),'SortStr','descend');
Threshold=Maxpeaks(1,1)*Thold; %Determines the threshold for the findpeaks function
[Results.Knee_Add_L_Torque(i).MaxPeaks, Results.Knee_Add_L_Torque(i).Peaklocationpercent]=findpeaks(Results.Knee_Add_L_Torque(i).waveforms,'MinPeakHeight',Threshold,'MinPeakDistance',width);
[Results.FirstEKAMMaxpeaks(i), Results.FirstEKAMLocation(i)]=findpeaks(Results.Knee_Add_L_Torque(i).waveforms(23:35),'SortStr','descend');
[Results.SecondEKAMMaxpeaks(i), Results.SecondEKAMLocation(i)]=findpeaks(Results.Knee_Add_L_Torque(i).waveforms(70:89),'SortStr','descend');
end
  2 comentarios
Mehmed Saad
Mehmed Saad el 15 de Mayo de 2020
Editada: Mehmed Saad el 15 de Mayo de 2020
you want to do it without for loop?
Jake Bowd
Jake Bowd el 15 de Mayo de 2020
No, I will be using a for loop.
My question is how can i set 2 boundaries to extract data from?
for Results.Knee_Add_L_Torque(i).waveforms I would like to extract the peak between 23-35 and also between 70-89.

Iniciar sesión para comentar.

Respuesta aceptada

Mehmed Saad
Mehmed Saad el 15 de Mayo de 2020
Editada: Mehmed Saad el 15 de Mayo de 2020
I know a workaround(There might be some direct method)
s = sin(2*pi*10*(0:0.01:1-0.01));
findpeaks(s)
Now replace all elements which are not required for peakdetection with NaN. ( i.e. 1:22, 36:69 and 90:100)
ind = 1:100;
s(~(ind>22&ind<36 | ind>69&ind<90)) = NaN;
findpeaks(s)

Edit:

You can also use arrayfun (or cellfun) to directly do that
[peak_s,ind_s]=arrayfun(@(x,y) findpeaks(s(x:y)),[23 70],[35 89],'uni',0);
[Results.FirstEKAMMaxpeaks(i), Results.SecondEKAMMaxpeaks(i)] = deal(peak_s{:});
[Results.FirstEKAMLocation(i), Results.SecondEKAMLocation(i)] = deal(ind_s{:});
where s is equal to your variable Results.Knee_Add_L_Torque(i).waveforms(1:100). You can replace s with Results.Knee_Add_L_Torque(i).waveforms
  2 comentarios
Jake Bowd
Jake Bowd el 15 de Mayo de 2020
Thank you for your response.
I have tried this approach and I am receiving an error message of 'cannot call or index into a temporary array'.
I have checked it and it would appear the second section is still trying to call on two peaks, rather than just one.
[peak_Results.Knee_Add_L_Torque(i).waveforms(1:100),ind_Results.Knee_Add_L_Torque(i).waveforms(1:100)]=arrayfun(@(x,y) findpeaks(Results.Knee_Add_L_Torque(i).waveforms(1:100)(x:y)),[23 35],[70 89],'uni',0);
Mehmed Saad
Mehmed Saad el 15 de Mayo de 2020
Try this and yes arrayfun will call findpeaks twice
[peak_s,ind_s]=arrayfun(@(x,y) findpeaks(Results.Knee_Add_L_Torque(i).waveforms(1:100)(x:y)),[23 35],[70 89],'uni',0);
[Results.FirstEKAMMaxpeaks(i), Results.SecondEKAMMaxpeaks(i)] = deal(peak_s{:});
[Results.FirstEKAMLocation(i), Results.SecondEKAMLocation(i)] = deal(ind_s{:});
For a single call use the first approach

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 15 de Mayo de 2020
What if you just set the signal outside the ranges of interest to the minimum value and then found the remaining peaks, which would be inside the ranges of interest.
signal = Results.Knee_Add_L_Torque(i).waveforms(1:100);
minValue = min(signal);
signal(1 : 19) = minValue;
signal(31 : 59) = minValue;
signal(81 : end) = minValue;
[Maxpeaks]=findpeaks(signal,'SortStr','descend');

Community Treasure Hunt

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

Start Hunting!

Translated by