- /
-
ECG Signal
on 17 Oct 2024
- 44
- 119
- 0
- 0
- 1440
Cite your audio source here (if applicable): Sound Effect from <a href="https://pixabay.com/sound-effects/?utm_source=link-attribution&utm_medium=referral&utm_campaign=music&utm_content=6149">Pixabay</a>
drawframe(80);
Write your drawframe function below
function drawframe(f)
% Simulates an EKG on an old oscilloscope with phosphor persistence.
% Generate EKG signal 'x' for 4 seconds
x = 0.4*repmat([zeros(1500,1); ecg(5000); zeros(2000,1)], [6,1]);
total_frames = 100; total_time = 6;
t = linspace(0, total_time, length(x));
% Current index 'n' for frame 'f'
n = round((f / total_frames) * length(x));
n = max(2, min(n, length(x)));
% Set figure properties
set(gcf, 'Color', '#ffe9f0'); clf; hold on;
% Persistence effect
persistence_length = round(length(x) * 0.8);
start_idx = max(1, n - persistence_length);
num_segments = 50;
indices = round(linspace(start_idx, n, num_segments));
for i = 1:length(indices)-1
idx1 = indices(i); idx2 = indices(i+1);
plot(t(idx1:idx2), x(idx1:idx2), 'Color', 'r', 'LineWidth', 2.5);
end
% Latest segment in bright green
plot(t(n-1:n), x(n-1:n), 'Color', 'r', 'LineWidth', 1);
% Gaussian diffuse dot at last point
dot_size = 50; sigma = dot_size / 2;
[xg, yg] = meshgrid(linspace(t(n)-dot_size/1000, t(n)+dot_size/1000, dot_size), ...
linspace(x(n)-dot_size/1000, x(n)+dot_size/1000, dot_size));
gauss = exp(-((xg - t(n)).^2 + (yg - x(n)).^2) / (2 * (sigma/1000)^2));
gauss = gauss / max(gauss(:));
dot_image = cat(3, zeros(size(gauss)), gauss, zeros(size(gauss)));
imagesc([t(n)-dot_size/1000, t(n)+dot_size/1000], [x(n)-dot_size/1000, x(n)+dot_size/1000], dot_image);
set(gca, 'YDir', 'normal');
% Final plot settings
xlim([0 total_time]); ylim([-0.7 1.5]); axis off; hold off;
end
function x = ecg(L)
% EKG Electrocardiogram signal generator.
% Author(s): R. Losada
% Copyright 1988-2002 The MathWorks, Inc.
x = zeros(L, 1);
a0 = [0,1,20,1,0,-34,118,-59,0,2,31,2,0,0,0];
d0 = [0,27,59,91,131,141,163,185,195,275,307,339,357,390,440];
a = a0 / max(a0); d = round(d0 * L / d0(15)); d(15) = L;
for i = 1:14
m = d(i):d(i+1)-1;
slope = (a(i+1)-a(i)) / (d(i+1)-d(i));
x(m+1) = a(i) + slope * (m - d(i));
end
x = sgolayfilt(x, 0, 15);
end