Fs=500;   % Sampling frequency
HR=60;    % Heart Rate
wind=0.3/sqrt(60/HR); % number of seconds after T-peak to search for lowest point ...
% Note that it is adjusted by heart rate according to Bazett's formaula - 'Hacky''

[s, ipeaks] = ecgsynNparam(Fs,64,0,60);

t=find(ipeaks==5);

% find T offsets
for(i=1:length(t)); 
 tmp=s(t:t+round(wind*Fs)-1); 
 toff(i)=t(i)+min(find(tmp==min(tmp))); 
end

% Q points
q  = find(ipeaks==2);

% QT intervals
qt = toff(2:length(toff))'-q(1:length(toff)-1);

%%%%%' Plotting
subplot(2,1,1);
plot(s),hold on;
plot(q,s(q),'g+');
plot(t,s(t),'r*');


% plot T-offsets
plot(toff,s(toff),'>r');
legend('ecg','Q','T','Toffset');
title('Ideal ECG with QT intervals');
xlabel('seconds');
ylabel('Amplitude (mV/10');

% histogram of QT intervals
subplot(2,1,2);hist(qt/Fs);
title('Histogram of QT intervals');
xlabel('seconds');
ylabel('N');
