fp=fopen('G:\A1001D1.DAT','r'); % Open file and initialize file pointer , Fs=250Hz, V=5uV/bit
Sh=6000;
RecLen=1500;
d1=fread(fp,Sh,'int16'); % Read 6000 discrets from the file
d=fread(fp,RecLen,'int16'); % Read 1500 discrets from the file
d=d*0.005; % convert digits to millivolts
ECG1=zeros(size(d));
ECG1(5:RecLen-6)=(d(1:RecLen-10)+d(2:RecLen-9)+d(3:RecLen-8)+d(4:RecLen-7)+d(5:RecLen-6)+d(6:RecLen-5)+d(7:RecLen-4)+d(8:RecLen-3)+d(9:RecLen-2))/9;
ECG2=zeros(size(d));
ECG2(5:RecLen-6)=(d(1:RecLen-10)+2*d(2:RecLen-9)+3*d(3:RecLen-8)+4*d(4:RecLen-7)+5*d(5:RecLen-6)+4*d(6:RecLen-5)+3*d(7:RecLen-4)+2*d(8:RecLen-3)+d(9:RecLen-2))/25;
Y2=zeros(size(d));
Y1=ECG1;
Yx=d-Y1;
Y2(5:RecLen-6)=(Yx(1:RecLen-10)+Yx(2:RecLen-9)+Yx(3:RecLen-8)+Yx(4:RecLen-7)+Yx(5:RecLen-6)+Yx(6:RecLen-5)+Yx(7:RecLen-4)+Yx(8:RecLen-3)+Yx(9:RecLen-2))/9;
ECG3=ECG2+Y2;
Time=0.004:0.004:RecLen*0.004; % Time vector
figure(1)
subplot(6,1,1), plot(Time, d), title('ECG Record'), xlabel('[Sec]'), ylabel('[mV]'); % Original ECG
subplot(6,1,2), plot(Time, ECG1), title('Filter I - Moving Average I'), xlabel('[Sec]'), ylabel('[mV]'); % Filter 1
subplot(6,1,3), plot(Time, Yx), title('X-Filter I'), xlabel('[Sec]'), ylabel('[mV]'); % Filter 2
subplot(6,1,4), plot(Time, Y2), title('Filter(X-Filter I)'), xlabel('[Sec]'), ylabel('[mV]'); % Filter 2
subplot(6,1,5), plot(Time, ECG2), title('Filter II - Moving Average High Order'), xlabel('[Sec]'), ylabel('[mV]'); % Filter 2
subplot(6,1,6), plot(Time, ECG3), title('Filter III'), xlabel('[Sec]'), ylabel('[mV]'); % Filter 3

figure(2)
subplot(3,1,1), plot(Time, d-ECG1), title('MA Error'), xlabel('[Sec]'), ylabel('[mV]'); % Original ECG
subplot(3,1,2), plot(Time, d-ECG2), title('High Order MA Error'), xlabel('[Sec]'), ylabel('[mV]'); % Filter 1
subplot(3,1,3), plot(Time, d-ECG3), title('Filter III Error'), xlabel('[Sec]'), ylabel('[mV]'); % Filter 2

