function [RPeaks_pruned, ann_res_pruned, res] = getECG_Rpeaks(ecgSig)

load Params_MITBIH_20.mat

ecgSig1 = [zeros(150,1); ecgSig; zeros(150,1)];

preds1 = checkQuality(opttheta,ecgSig1,numClasses,imageDim, filterDim,numFilters,poolDim);

res = preds1'-1;

len = 50;
th = 30;
[Rpeaks, ann_res] = LocatePeaks_ECG(ecgSig, res, len, th);

clear v remPeaks RR_width falseRid
RR_width = [];

si = find(ann_res-100>0);
ei = find(ann_res+100< length(ecgSig));

for idx = si(1):ei(end)%3:length(ann_res)-2
    
    ss1 = ecgSig(ann_res(idx):-1:ann_res(idx)-100)/ecgSig(ann_res(idx));
    ss2 = ecgSig(ann_res(idx):1:ann_res(idx)+100)/ecgSig(ann_res(idx));
    
    startId = find(ss1<0.5);
    endId = find(ss2<0.5);
    if(length(startId)>1 && length(endId>1))
    RR_width(idx) = startId(1)+endId(1);
    end
end

falseRid1 = [];
if(length(RR_width)>0)
falseRid1 = find(RR_width>=20);
end

rrD = diff(ann_res);
falseRid2 = find(rrD<120);
falseRid2 = falseRid2+1;

falseRid = intersect(falseRid1, falseRid2);

remStartPeak = 1:si(1)-1;
remEndPeak = ei(end)+1:length(ann_res);

remPeaks = zeros(size(Rpeaks));
remPeaks(ann_res(falseRid)) = 1;

ann_res_pruned = ann_res;
ann_res_pruned([remStartPeak falseRid remEndPeak]) = [];
RPeaks_pruned = zeros(size(Rpeaks));
RPeaks_pruned(ann_res_pruned) = 1;
end
