Switch to unified view

a b/preprocessOfApneaECG/BioSigKit/Demo.m
1
%% =============== Demo ======================== %%
2
%% ============== Licensce ========================================== %%
3
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
4
% "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
5
% LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
6
% FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
% OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
8
% SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
9
% TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
10
% PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
11
% LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
12
% NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
13
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
% Author :
15
% Hooman Sedghamiz, Feb, 2018
16
% MSc. Biomedical Engineering, Linkoping University
17
% Email : Hooman.sedghamiz@gmail.com
18
%--------------- Only Hit the Run button -----------------%
19
20
%% ===================== DEMO FOR QRS DETECTORS ======================== %%
21
%---------------- Load a Sample ECG Signal from (\SampleSignals) directory -----------------%
22
slashchar = char('/'*isunix + '\'*(~isunix)); 
23
load([fileparts(which(mfilename)),slashchar,'SampleSignals',slashchar,...
24
    'ECG1.mat']);
25
addpath(genpath(fileparts(which(mfilename))));
26
load('ECG1.mat');
27
% ------------------ Call the BioSigKit Wrapper -------------------%
28
Analysis = RunBioSigKit(EKG1,250,0);          % Uses ECG1 as input,Fs=250
29
%-------------------- Call Pan Tompkins Algorithm ------------------- %
30
Analysis.MTEO_qrstAlg;                        % Runs MTEO algorithm
31
QRS = Analysis.Results.R;                     % Stores R peaks in QRS
32
%-------------------- Call MTEO QRS ------------------- %
33
Analysis.MTEO_qrstAlg;                        % Runs MTEO algorithm
34
QRS = Analysis.Results.R;                     % Stores R peaks in QRS
35
Qwave = Analysis.Results.Q;                   % Indice of Q peaks
36
Swave = Analysis.Results.S;                   % Indice of S peaks
37
Twave = Analysis.Results.T;                   % Indice of T peaks
38
Pwave = Analysis.Results.P;                   % Indice of P peaks
39
% ------------------------ Change Sample rate ------------------%
40
Analysis.Fs =360;
41
42
%--------------------------- Open GUI -----------------------------%
43
% Analysis = RunBioSigKit();                    % Opens GUI 
44
45
46
%% ==================  DEMO FOR OTHER SUBROUTINES =================== %%
47
% ----------------- Activity detection with hilber Transform -------------%
48
Analysis.PlotResult = 1;                           % Set the plot flag on
49
Analysis.Env_hilbert;                              % detect activities
50
51
v = repmat([.1*ones(200,1);ones(100,1)],[10 1]);   % generate true variance profile
52
Analysis.Sig = sqrt(v).*randn(size(v));            % Add white noise
53
Analysis.Env_hilbert;
54
Analysis.Sig = EKG1;
55
% ---------------- Compute mobility and complexity ------------------- %
56
[mobility,complexity] = Analysis.ComputeHjorthP;
57
fprintf(['Mobility = ',mat2str(mobility),'\n']);
58
fprintf(['Complexity = ',mat2str(complexity),'\n']);
59
%--------------------- Template matching -------------------------------%
60
template = Analysis.Sig(QRS(2)-60:QRS(2)+100);          % extract a heart-beat
61
[PsC_s,best_lag] = Analysis.TemplateMatch(template);    % Find it in the signal
62
fprintf(['PsuedoScore = ',mat2str(PsC_s),'\n']);
63
fprintf(['Location = ',mat2str(best_lag),'\n']);
64
figure,plot(Analysis.Sig);
65
hold on,plot((best_lag:best_lag+160),...
66
    Analysis.Sig(best_lag:best_lag+160));               % Highlight the template in Sig
67
title('PsuedoCorrelation Demo...');
68
69
% ------- Fetal ECG extraction in real time with neural PCA -------------%
70
load([fileparts(which(mfilename)),slashchar,'SampleSignals',slashchar,...
71
    'foetal_ecg.dat']);                                  % Load Foeatal ECG
72
Analysis.Sig = foetal_ecg(:,2:9)';                       % Load all the 8 channels
73
[~,PC] = Analysis.neural_pca(8,2);                       % get the PCs
74
figure;
75
for i = 1: 5
76
    subplot(5,1,i),plot(PC(i,:));
77
end
78
title('Foetal Beat detection with real-time neural-PCA...');
79
% ------------------------- Adaptive Filters ---------------------------%
80
Analysis.Sig = EKG1;
81
Analysis.Fs=250;
82
out = Analysis.adaptive_filter(2,[],250);                % Use adaptive line enhancer (type =2)
83
figure,plot(Analysis.Sig/max(Analysis.Sig));
84
hold on,plot(out/max(out));
85
title('Adaptive Line Enhancer with 1 Sec Delay');
86
87
% ------------------------- ECG derived Respiration --------------------%
88
Analysis.PlotResult = 0;                           % Set the plot flag of0
89
EDR = Analysis.EDR_comp;
90
figure,plot(Analysis.Sig/max(Analysis.Sig));
91
hold on,plot(EDR/max(EDR));
92
title('ECG Derived Resp Signal');
93
94
% ------- Single Channel Foetal ECG extraction with nonlinear filter -------------%
95
Analysis.Sig = foetal_ecg(:,2)';                                  % Load channel 2
96
Analysis.Fs=250;
97
output = Analysis.nonlinear_phase_filt(1, 50, 45, 1500);          % run the filter
98
figure,plot(Analysis.Sig);
99
hold on,plot(Analysis.Sig - output);
100
title('Foetal ECG extracted...');
101
102
% ----------------- ACC derived respiration ----------------------% 
103
load([fileparts(which(mfilename)),slashchar,'SampleSignals',slashchar,...
104
    'ACC.mat']);
105
Analysis.Sig = ACC';
106
Analysis.Fs = 200;
107
EDR = Analysis.ADR_comp;
108
figure,plot(Analysis.Sig/max(Analysis.Sig));
109
hold on,plot(EDR/max(EDR));
110
title('ECG Derived Resp Signal');
111
112
%---------------- ACC processing and Posture detection ---------------%
113
[output,state,EE,F,SMA] = Analysis.ACC_Act;
114
States ={'Steady','Slight Movement','High Activity'};
115
fprintf(['State is ',States{state+1},' and Energy Expenditure = ',mat2str(EE),'\n']);
116