close all;

sample = clean;



fSize = 16;

for i = 1:2

    subplot(2,1,i);

    set(gca,'FontSize',fSize);

end



% Define input

t = sample(:,1);

x = sample(:,2);

Tdelay = 0.512;

Tmiddle = mean(t);

%----------------------------------------------------------------------

% Time domain

%----------------------------------------------------------------------

% Define filter

f = [5 30]; % Bandpass markers [Hz] 

f = f/(1/2*fs); % Normalized freq (Matlab convention)

N = 256; % Order of filter

b = fir1(N,f);



% Apply filter

y = filter(b,[1],x);

subplot(211);

plot(t,x,'--'); 

xlim([min(t) max(t)]);

hold on;

% subplot(3,2,[3 4]);

plot(t-Tdelay,y,'r','Linewidth',2);

xlim([min(t) max(t)]);

% xlim([103 106]);

xlim([Tmiddle-2 Tmiddle+2]);

xlabel('Time (s)');

ylabel('EKG (V)');

grid on;



%----------------------------------------------------------------------

% Frequency domain

%----------------------------------------------------------------------

[P  f] = pwelch(x,[],[],[],fs);

[Pf f] = pwelch(y,[],[],[],fs);

fRange = [min(f) max(f)];

P_dB  = 10*log10(P);

Pf_dB = 10*log10(Pf);

subplot(212);

plot(f,P_dB,'--'); 

xlim(fRange);

hold on;

% subplot(224);

plot(f,Pf_dB,'r','Linewidth',2);

xlim(fRange);

xlabel('Frequency (Hz)');

ylabel('PSD (dB/Hz)');

legend('Original','Filtered','Location','Best');

grid on;

