www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/UsePeakFinderToFindHeartRateFromECGInputExample.m

    %% Use Peak Finder to Find Heart Rate from ECG Input
% This example shows how to use the Time Scope Peak Finder panel.
%%
% At the MATLAB(R) command prompt, type <matlab:ex_timescope_heartbeat 
% ex_timescope_heartbeat> to open the example model. The following 
% Simulink(R) model appears. 
%
% <<../timescope_example5_model_heart.png>>
%
% In this example, Simulink(R) imports the variable |mhb|, from the 
% MATLAB(R) workspace. This variable is created when the model loads 
% because the commands that construct it are in the model Preload function.
% To view these commands, in the Simulink(R) menu, select |File > Model 
% Properties > Model Properties|. The Model Properties dialog box appears. 
% Click the |Callbacks| tab. The following lines of MATLAB(R) code appear.
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;
%%
% This example uses the Savitzky-Golay filter |sgolayfilt| in the Signal
% Processing Toolbox(R). For more information, type <matlab:sgolaydemo 
% sgolademo> to run an example.
%%
% Run your model to see the time domain output. To show the |Peak Finder|
% panel, in the Time Scope menu, select Tools > Measurements > Peak
% Finder|. To expand the |Settings| section, click the arrow next to
% the label. Enter 10 for |Max Num of Peaks| and press the Enter key. 
% The Time Scope Peaks section now displays a list of 10 peak amplitude 
% values, and the times at which they occur, as shown in the following 
% figure. 
%
% <<../timescope_example5_display_heart.png>>
%
%%
% As you can see from the list of peak values, there is a constant time 
% difference of 0.675 seconds between each heartbeat. Therefore, the heart 
% rate of the ECG signal is 88.89 beats per min (bpm).