Extracting 2 peak values and their respective location from a set of data points
3 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Mehmed Saad
2020-5-15
编辑:Mehmed Saad
2020-5-15
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 个评论
Mehmed Saad
2020-5-15
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
更多回答(1 个)
Image Analyst
2020-5-15
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');
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!