function [z,y] = hrdet(x,fs)
 % z: computed the heart rate signals 
% z=hrdet(s(1:4000,2));
%mcc -x hrdet
if nargin < 2
    fs = 496;
end

tlg = 0.3; 
ts = 1/fs;
wlg = floor(tlg * fs/2);
hrth =1;

y=x;
n =length(x);
j=0;
z = zeros(size(x));

%  find(x(i) - min(x(i-wlg:i+wlg)) > hrth);

i = (1+wlg);
while i<(n-wlg)+1
    if x(i) - min(x((i-wlg):(i+wlg))) > hrth
        y(i)=1;
        z(i) = 1/((i-j)*ts);
        if z(i) <0
            z(i)
        end
        j = i;
        i = i + wlg;
    else
        y(i)=0;
        i = i + 1;
    end
end



% mcc -t -L C myfun
