function [S1_norm, ecgSig, mfil2] = getECGsig_normalized(signal, fsOrg)

fs = 300;
if(fsOrg~=300)
    ecgSig = resample(signal(:,1), fs, fsOrg);
else
    ecgSig = signal(:,1);
end

Sstd = ecgSig;
mfil1=medfilt1(Sstd,fs/5);  %medfilt1 is an inbuilt function for median filter.
mfil2 = medfilt1(mfil1,(fs/5)*3);

S1 = Sstd-mfil2;
S1_norm = zeros(size(S1));
ecgSig_raw = zeros(size(S1));

k = 1;

for i = 301:600:length(S1)-300
    sf(k) = max(abs(S1(i-300:i+300)));
    
    if(sf(k)<0.01)
        S1_norm(i-300:i+300) = S1(i-300:i+300);
    else
        S1_norm(i-300:i+300) = S1(i-300:i+300)/sf(k);
    end
end

S1_norm(i+300:end) = S1(i+300:end)/max(abs(S1(i+300:end)));

end