%--------------- ECG Signal manipulation ------------------------------
%
% Technical University of Sofia - MUSSPI lab course
% Created by Viktor Stoev
% October 2015
%----------------------------------------------------------------------

clear all;
close all;

%% Open the ECG signal and plot a portion of it

fid = fopen('ECG.txt','r');

heading = textscan(fid,'%s %s',2);                           % read heading of text file
fgetl(fid);                                                  % increment to next line
Data_matrix = textscan(fid,'%s %f','delimiter',',');         % read data

ECGf1 = Data_matrix{1,2};
time = (1:1:size(ECGf1,1)).';
%t = (0:1/250:1)';
x=[0:2*pi/5:(size(ECGf1)-1)/5*2*pi];
sinf = 2*sin(x)';
figure(1);
subplot(6,1,1);
%plot(sinf);
%plot(time(1:450,1),sinf(1:450,1));
%subplot(4,1,2);
plot(ECGf1);
title('ECG');
ECGf = ECGf1 + sinf;
%time = (1:1:size(ECGf,1)).';
%title('ECG');
subplot(6,1,2);
plot(ECGf);
title('ECG + 50hz noise');
size(sinf)
size(ECGf)
%xlabel('Time [s]');
%ylabel('Amplitude [mV]');

%% Run a high-pass filter through the signal

Fs = 60;
order = 2;
[b,a] = butter(order,30/Fs);

ECG_filtered = filter(b,a,ECGf);
subplot(6,1,3);


plot(ECG_filtered);
title('filter1')
%% Remove the offset

a=1;
b=[1/5 1/5 1/5 1/5 1/5];
ECG_filtered1 = filter(b,a,ECGf);

subplot(6,1,4);
plot(ECG_filtered1);
title('filter2');

difference_1 = ECG_filtered - ECGf1;

subplot(6,1,5);
plot(difference_1);
title('dif1');

difference_2 = ECG_filtered1 - ECGf1;
subplot(6,1,6);
plot(difference_2);
title('dif2');