%% Find Heart Rate Using Peak Finder Panel with ECG Input Signal
% Use Peak Finder panel of the Time Scope to measure a heart rate.

%% Create and Display ECG Signal
% Create the electrocardiogram (ECG) signal. The custom |ecg| function 
% helps generate the heartbeat signal.
% 
% <include>ecg.m</include> 

x1 = 3.5*ecg(2700).';
y1 = sgolayfilt(kron(ones(1,13),x1),0,21);
n = (1:30000)';
del = round(2700*rand(1));
mhb = y1(n + del);
ts = 0.00025;

%%
% Create a |timescope| object and call the object to display the signal. 
% To autoscale the axes and enable changes to property values and input 
% characteristics, call |release|.

scope = timescope(1,1/ts);
scope(mhb);
release(scope)

%% Find Heart Rate
% Use the Peak Finder measurements to measure the time between heart beats.
%
% # On the *Measurements* tab, select *Peak Finder*. 
% # For the *Num Peaks* property, enter |10|. 

%%
% In the *Peaks* pane at the bottom of the window, the Time Scope displays a list of ten peak amplitude 
% values and the times at which they occur.

%%
% <<../TS_PeakFinder.png>>

%%
% The list of peak values shows a constant time difference of 0.675 second 
% between each heartbeat. Based on the following equation, the heart 
% rate of this ECG signal is about 89 beats per minute.
% 
% $$\frac{60 \textrm{ s/min}}{0.675 \textrm{ s/beat}} = 88.89 \textrm{ bpm}$$
% 

%%
% Close the Time Scope window and remove the variables you created from the workspace.

clear scope x1 y1 n del mhb ts

%% 
% Copyright 2012 The MathWorks, Inc.