function [peaks_loc, peaks_amp, max_loc] = findpeaks_my(data, threshold, deltay);
%FINDPEAKS_MY Find up peaks in real data 
%
%    [peaks_loc, peaks_amp, max_loc] = findpeaks_my(data, threshold, deltay);
%          finds up peaks in real data greater than threshold 
%          and beween two minima lower than peaks - deltay.
%    with
%          peaks_loc = peaks location
%          peaks_amp = peaks amplitude
%          max_loc   = location of all local maxima
%          threshold = peaks lower than threshold are rejected 
%          deltay    = minimum value between peaks and adjacent minima
%
% july 2010 - fmy

%% finding all local max
yy = data;
max_loc = [];
for k1 = 3:length(yy)
  if yy(k1-1)>yy(k1-2) & yy(k1-1)>yy(k1)
    max_loc = [max_loc, k1-1];
  end;
end;    

%% rejection of max not well defined: delta < deltay
kfm = max_loc; lkfm = length(kfm); kcd = 0;
l_min = min(yy(1:max_loc(1))); % left min
for kk = 1:lkfm - 1
  r_min = min(yy(max_loc(kk):max_loc(kk+1))); % right min 
  cdt1 = yy(max_loc(kk)) - l_min > deltay;
  cdt2 = yy(max_loc(kk)) - r_min > deltay;
  l_min = r_min;
  if not(cdt1&cdt2)
    kfm(kk-kcd) = [];
    kcd = kcd + 1; 
  end;
end;    

%% processing of last max
r_min = min(yy(max_loc(lkfm):end));
cdt1 = yy(max_loc(lkfm)) - l_min > deltay;
cdt2 = yy(max_loc(lkfm)) - r_min > deltay;
if not(cdt1&cdt2)
  kfm = kfm(1:end-1); 
end;

%% peaks > threshold
peaks_loc = kfm; 
peaks_amp = data(kfm); 
kf = find(peaks_amp > threshold);
peaks_loc = kfm(kf);
peaks_amp = peaks_amp(kf);

