This repository was archived by the owner on Jun 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeierabend_philipp_filtering.m
More file actions
66 lines (52 loc) · 2.74 KB
/
Copy pathfeierabend_philipp_filtering.m
File metadata and controls
66 lines (52 loc) · 2.74 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
function y_f = feierabend_philipp_filtering(y_s, y_t, plot_filtering)
disp('Step 2.3');
disp('-------------------------------------------------------------');
disp('Health Bot: Filtering the ECG signal...');
% Check if the signal is long enough (at least 25 data points)
if length(y_s) <= 24
disp('Signal too short, skipping filtering.');
y_f = y_s; % Return the original signal if it's too short
return; % Exit the function without applying the filter
end
% Calculate the sampling frequency based on the time vector y_t
fs = 1 / (y_t(2) - y_t(1)); % Calculate the sampling frequency
% Define the relevant frequencies for the ECG signal
f_low = 0.5; % Lower cutoff frequency (Hz)
% Adjust the upper cutoff frequency to be slightly below the Nyquist frequency
nyquist_freq = fs / 2; % Nyquist frequency (half of the sampling frequency)
f_high = min(40, nyquist_freq * 0.99); % Set f_high slightly below the Nyquist frequency
% Normalize the frequencies to the [0,1] range
f_low_norm = f_low / nyquist_freq; % Normalized lower cutoff frequency
f_high_norm = f_high / nyquist_freq; % Normalized upper cutoff frequency
% Design the bandpass filter with the normalized frequencies
[b, a] = butter(4, [f_low_norm f_high_norm], 'bandpass'); % Butterworth bandpass filter
% Apply the filter to the signal with zero-phase filtering
signal_filtered = filtfilt(b, a, y_s); % Zero-phase filtering
% Define the duration for plotting (30 seconds or the length of the signal)
plot_duration = min(30, y_t(end)); % Plot only the first 30 seconds or the entire signal if shorter
idx_plot = y_t <= plot_duration; % Indices for the plot
% Plot only if plot_filtering is true
if plot_filtering
% Plot the original signal and filtered signal for comparison
figure;
subplot(2,1,1); % First subplot for original signal
plot(y_t(idx_plot), y_s(idx_plot)); % Plot original signal for first 30 seconds
title('Sampled ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
subplot(2,1,2); % Second subplot for filtered signal
plot(y_t(idx_plot), signal_filtered(idx_plot)); % Plot filtered signal for first 30 seconds
title('Filtered ECG Signal with Band-Pass Filter');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;
% Dynamically adjust the xlim based on the time vector
xlim([min(y_t(idx_plot)) max(y_t(idx_plot))]);
disp('Health Bot: Yay! Your ECG signal is successfully filtered.');
else
disp('Health Bot: Plotting skipped.');
end
% Return the filtered signal
y_f = signal_filtered;
end