clc; clear all; close all;
fvz = 1000; load s0076lrem.mat;
signal = val(1,:); N=length(signal);

signal=signal*-1;

maximum=max(signal);
minimum=min(signal);
if abs(maximum)<abs(minimum)
    signal=signal*-1;
end



signal_original=signal;

[QRS RR_interval TP TPprum]= QRSdetekce(signal,fvz);

hor=max(signal);dol=min(signal);

b=fir1(1000,[2*2/fvz 30*2/fvz],'bandpass');
signal = filter(b,1,signal);clear b;
signal=signal(501:end);

signal = medfilt1(signal,11);

cislo_QRS=2;







poz = [QRS(2,cislo_QRS)-300, QRS(2,cislo_QRS)+500];
vyrez = signal(poz(1):poz(2));

plot(vyrez);hold on;plot(zeros(length(vyrez)),'r');

[m i] = max(vyrez);plot(i,m,'go');hold on;
[n j] = min(vyrez(1:i));plot(j,n,'go');hold on;
[o k] = max(vyrez(i+50:end));plot(k+i+50,o,'go');hold on;

xx=j-10; yy=j+10;
fzac = aprox(xx,yy,vyrez);
z = (xx:1:yy);figure(1);plot(z,fzac,'black');hold on;

zz=k+i+50;xkonec=zz;ykonec=zz+100;
fkon = aprox(xkonec,ykonec,vyrez);
zkon = (xkonec:1:ykonec);figure(1);plot(zkon,fkon,'black');hold on;

bzac = bod(fzac(1),fzac(3),z(1),z(3)); %nalezeni potaceniho bodu
bkon = bod(fkon(end-1),fkon(1),zkon(end-1),zkon(1));%nalezeni koncoveho bodu

delka = bkon-bzac








zacQ = [bzac bzac];konT = [bkon bkon];osa = [hor dol]; %vykresleni hranice

plot(zacQ,osa,'g');hold on;plot(konT,osa,'g');hold on;
plot(signal_original(poz(1):poz(2)),'black');hold on;




