MATLAB: How to find a minimum only before a peak has been detected

findpeakslocalminimalocalminimumpeaks

I have the following graph in which I used function findpeaks and MinPeakHeight to find the peaks that I'm interested in.
[pks,locs]=findpeaks(filter_sgo,'MinPeakHeight',limiar,'MinPeakDistance',minpkdist);
Now I want to find the local minimums but only before the peaks that I have already detected (red points of the picture). I know I have to invert the signal and use findpeaks again, but I can't figure out how to only detect before a peak.
Thanks a lot.

Best Answer

What you are looking for seems to be the beginning of the peak, which is not necessarily a valley. A simple method to approximate those values would be to count backwards from each peak, and find the location when the signal intercepts a predefined threshold. Here's a simple example. You may recognize the peaks from the documentation on findpeaks.
x = linspace(0,1,1000);
Pos = [1 2 3 5 7 8]/10;
Hgt = [3 4 4 2 2 3];
Wdt = [2 6 3 3 4 6]/100;
for n = 1:length(Pos)
Gauss(n,:) = Hgt(n)*exp(-((x - Pos(n))/Wdt(n)).^2);
end
PeakSig = sum(Gauss);
%%example starts here
[pks,locs] = findpeaks(PeakSig);
locs=[1 locs];
thres=0.5; %set threshold
win=diff(locs)
mins=nan(size(win))
for i=1:numel(win);
ind=find(PeakSig(locs(i):locs(i+1))<thres);
if ~isempty(ind)
mins(i)=max(ind)+locs(i);
end
end
mins(isnan(mins))=[];
figure;hold on
findpeaks(PeakSig)
plot(mins,PeakSig(mins),'rx')