MATLAB: How to find the two lowest peaks in many different graphs

datapeaks

Hi,
I am currently analysing a lots of data from a load cell used during testing of forces on an object. The load cell has a frequency of 1kHz and at most 20,000 samples. I am interested in finding the two lowest peaks, I have written a code that finds those peaks at some of the tests, but sadly not all. I need some help to make this code more general and work for all tests. The two lowest peaks are 99% of the time very clear.
Here is my current code:
F_table = readtable(FName);
F = table2array(F_table);
plot(F)
t_length = numel(F);
t = [1:t_length]';
% Finds the minimum force
F_t = [t(:),F(:)];
[Fmin,Fminpos] = min(F);
select = F((Fminpos-1200):(Fminpos+50));
[Maxima2,MaxIdx2] = findpeaks(select,'MinPeakDistance',50);
FInv = 1.01*max(select) - select;
[Minima2,MinIdx2] = findpeaks(FInv,'MinPeakDistance',50);
Minima_array2 = [MinIdx2(:),select(MinIdx2)];
Minimum2 = Fmin
Minimum2_pos = Fminpos;
% Finds the first slamming force
Min = Minima_array2(:,2)
[Maxima,MaxIdx] = findpeaks(Min,'MinPeakDistance',5);
FInv = 1.01*max(Min) - Min;
[Minima,MinIdx] = findpeaks(FInv,'MinPeakDistance',5);
Minima_array = [MinIdx(:),Min(MinIdx)]
Minima_array = sort(Minima_array,'ascend');
if numel(Minima_array)>2
Minimum = Minima_array(2,2)
else
Minimum = Minima_array(1,2)
end
Minimum_pos = find(F==Minimum)
Fs = weight - Minimum;
Fs = Fs*7^3; % scaled
Cs = Fs/(0.5*rho_w*area*v^2)
% Finds the second slamming force
select = F((Minimum_pos):(Fminpos));
[Maxima2,MaxIdx2] = findpeaks(select,'MinPeakDistance',5);
Maxima_array = [MaxIdx2(:),select(MaxIdx2)];
F_max = max(Maxima_array);
Maximum_pos = find(F==F_max(2));
It loads the txt file with the numbers and finds the two lowest peaks. Sometimes at least..
Here's some photos of some of the graphs: 1.
2.
The two red circles are the two lowest peaks I am interested in, the rest is "noise".
3.
Here would be the smallest value and the first decrease in force.
All help is highly appreciated and thanks in advance!
Best regards,

Best Answer

If y is your signal
p=findpeaks(y)
a=sort(p)
out=a(1:2)