-
Notifications
You must be signed in to change notification settings - Fork 0
/
untitled2.m
90 lines (77 loc) · 2.69 KB
/
untitled2.m
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
fs = double(fs);
lower_cutoff = 0.5/(fs / 2);
upper_cutoff = 50/(fs / 2);
if upper_cutoff >=1
upper_cutoff = 0.99;
end
if lower_cutoff >= 1
lower_cutoff = 0.001;
end
% Normalize the cutoff frequencies to the Nyquist frequency
Wn = [lower_cutoff, upper_cutoff];
[b, a] = butter(3, Wn);
% Initialize
new_peak_loc = NaN;
last_processed_sample = 0;
if ~isempty(liveBeatStart)
last_processed_sample = liveBeatStart(end);
end
if last_processed_sample > 101
new_data_start = last_processed_sample - 100;
else
new_data_start = last_processed_sample + 1;
end
if new_data_start > length(ecg_avail)
return;
end
% Extract new data for analysis
new_data = ecg_avail(new_data_start:end,:);
average_ecg = mean(new_data,2);
%filteredECG = filter(b, a, ecg);
filteredbnECG = filter(b, a, average_ecg);
window_size = 5; % Adjust this value based on your requirements
% Apply the moving average filter
smoothed_signal = movmean(filteredbnECG, window_size);
filteredECG = (smoothed_signal - min(smoothed_signal)) / (max(smoothed_signal) - min(smoothed_signal));
mad_threshold = 1.4 * mad(filteredECG, 1);
% outliers based on MAD threshold
outliers = abs(filteredECG - median(filteredECG)) > 3 * mad_threshold;
heightECG = filteredECG;
heightECG(outliers) = NaN;
% find the max value considering outliers
max_ecg = max(heightECG, [], 'omitnan');
height = max_ecg;
%distance = round(fs / (2));
% Find peaks
if length(filteredECG) < 3
peaks = [0];
locs = [0];
else
[peaks, locs] = findpeaks(filteredECG, 'MinPeakHeight', height);
end
if ~isempty(peaks)
% Find peaks above the threshold
peaks_above_threshold = find(peaks >= height);
if ~isempty(peaks_above_threshold)
% Adjust and store locations
stored_locations = locs(peaks_above_threshold);
stored_locations = stored_locations + new_data_start - 1;
else
stored_locations = [];
end
% Loop through stored locations
index_last_processed = find(stored_locations == last_processed_sample);
if ~isempty(index_last_processed)
for i = index_last_processed:length(stored_locations)
if last_processed_sample == stored_locations(i)
continue;
else
% Set new_peak_loc and break the loop
new_peak_loc = stored_locations(i);
break;
end
end
else
new_peak_loc = stored_locations(1);
end
end