function [Rpeaks, ann_res] = LocatePeaks_ECG(filData, res, len, th)

ecgSig = filData;

data = filData;
l = len;
temp = ones(l,1);

r1 = [zeros(1, l/2), res', zeros(1, l/2)];
tempres1 = conv(r1, temp, 'valid');
el = length(tempres1);
tempres = tempres1(1:el-1);

finalres = zeros(size(res));
idx = find(tempres>th);

%finalres(idx+10) = 1;
finalres(idx) = 1;
%finalres(idx+50) = 1;

r = finalres - [finalres(2:end); 0];

id1 = find(r<0);
id2 = find(r>0);
ann_res = [];
for i = 1:length(id1)
    
    [~, idx] = max(abs(ecgSig(id1(i):id2(i))));
    ann_res(i) = id1(i)+idx;
end

tempId = find(ann_res>length(data));
ann_res(tempId) = [];
Rpeaks =  zeros(size(res));
Rpeaks(ann_res) = 1;

end


