[569f14]: / ab_detection.m

Download this file

128 lines (90 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
function [a_wave, I_a, b_wave, I_b] = ab_detection(d2y, Ts)
L = length(d2y);
% %Cancellation of b wave
% d2y_ab = d2y;
%
% d2y(d2y<0) = 0;
for i = 1:L
C(i) = max(0,d2y(i));
end
%Squaring
Z = (C).^2;
%Generating blocks of interest
%Calculating Moving Averages
%1st Moving Average
W1_time = 0.200; % [s]
W1 = round(W1_time/Ts); %Index
% round to nearest odd integer.
idx = mod(W1,2)<1;
W1 = floor(W1);
W1(idx) = W1(idx)+1;
% MA_peak = zeros(1,L);
% for i = 1 + (W1-1)/2 : L - (W1-1)/2
% MA_peak(i) = 1/W1 * (sum(Z(i-(W1-1)/2:i+(W1-1)/2)));
% end
MA_peak = movmean(Z,W1);
%2nd Moving Average
W2_time = 0.667; % [s]
W2 = W2_time/Ts; %Index
% round to nearest odd integer.
idx = mod(W2,2)<1;
W2 = floor(W2);
W2(idx) = W2(idx)+1;
% MA_beat = zeros(1,L);
% for j = 1 + (W2-1)/2 : L - (W2-1)/2
% MA_beat(j) = 1/W2 * (sum(Z(j-(W2-1)/2:j+(W2-1)/2)));
% end
MA_beat = movmean(Z,W2);
%Thresholding
avg_Z = nanmean(Z);
alpha = 0.02 * avg_Z;
THR_1 = MA_beat + alpha;
BlocksOfInterest = zeros(1,L);
for k = 1:length(MA_peak)
if MA_peak(k) > THR_1(k)
BlocksOfInterest(k) = 0.1;
else
BlocksOfInterest(k) = 0;
end
end
Blocks = (BlocksOfInterest~=0);
THR_2 = W1;
% THR_2 = 20;
%Counting the number of blocks of interest
f_ones = find(diff([0,Blocks,0]==1));
p = f_ones(1:2:end-1); % Start indices
cons_Blocks = f_ones(2:2:end) - p; % Consecutive onesí counts
B = length(cons_Blocks);
% a_wave = zeros(1,B);
% I_a = zeros(1,B);
index = zeros(2,B);
for m = 1 : B
if cons_Blocks(m) > THR_2 || cons_Blocks(m) == THR_2
index_start = f_ones(m+m-1); %block start
index_end = f_ones(m+m) - 1; %block end
[~, I_a(m)] = max(Z(index_start:index_end)); %the blocks where there is an a wave
I_a(m) = I_a(m) + index_start;
a_wave(m) = d2y(I_a(m));
% try
% a_wave(m) = d2y_ab(I_a(m));
% catch
% a_wave(m) = d2y_ab(I_a(m) - 1);
% end
end
end
%Finding the b wave
I_a(I_a==0) = [];
for m = 1:length(I_a) - 1
if I_a(m) ~= 0 && I_a(m) ~= 1
[b_wave(m), I_b(m)] = min(d2y(I_a(m):I_a(m+1)));
I_b(m) = I_b(m) + I_a(m);
else
I_a(m) = 1;
[b_wave(m), I_b(m)] = min(d2y_ab);
end
end
%Don't know the reason why the point are shifted one sample away, both PPGf
%and PPGw
I_a = I_a - 1;
I_b = I_b - 1;
end