How to get realtime data(ecg sensor) from arduino and filter and process it and plot the output ?

16 次查看(过去 30 天)
I have been trying to get realtime ecg data from arduino using matlab.
I am able to visualise the real time data using this code.
Here the issue is I cannot get the filtered output at the same time as I am not aware of the fs.
Would like to know how we can process the signal realtime and plot the output.
I am attaching the result I am getting with this code (0 to 200)
Would be helpful if I could get a better code as well.
The output is taken from arduino pin A0
clear
clc
close all
%get some user settings
%ledPin = 'D13'; %For blinking the led we should access this pin
%deltaT_blink = 0.5;
port = 'COM5';
board = 'Uno';
a = arduino(port,board); % we are creating an arduino object with this specific board and this specific port
%write a small for loop that flashes the LED on and off
% for k = 1:20
% %turn the arduino LED on
% a.writeDigitalPin(ledPin,1);
% pause(deltaT_blink*5);
% %turn the arduino LED off
% a.writeDigitalPin(ledPin,0);
% pause(deltaT_blink*2);
%
% end
init_time = 1;
x =0;
s=0;
f = 0;
tic
while(init_time<1000)
b = readVoltage(a,"A0");
x = [x,b];
% subplot(2,1,1)
plot(x)
xlim tight
% fs = 16; % Sample rate in Hz
% fc = [5 15]
% Passband frequency range in Hz
% [b1, a1] = butter(4, fc/(fs/2), 'bandpass');
% ecg_filtered = filter(b1, a1, x);
% subplot(2,1,2)
% plot(ecg_filtered)
% f1 = fft(x);
% s1 = spectrogram(x);
% subplot(3,1,2)
% f = [f,f1];
% plot(f)
% subplot(3,1,3)
% s = [s,s1];
% plot(s)
grid on
init_time = init_time+1;
drawnow
end
toc

回答(1 个)

Dhruv
Dhruv 2023-5-16
Based on my understanding, it seems that you are attempting to process or filter a real-time signal obtained from Arduino. However, you are facing difficulties in achieving the same due to sampling rate values.
In the above code, uncomment the relevant code block to enable filtering. But if you're not aware of the sampling frequency (fs) of the ECG signal, try estimating it using the time elapsed between consecutive samples. Updating the code by including an estimation of the sampling frequency (fs):
current_time = toc;
prev_time = 0;
if init_time > 1
elapsed_time = current_time - prev_time;
fs = 1 / elapsed_time;
[b, a] = butter(4, fc/(fs/2),'bandpass');
end
prev_time = current_time;
The updated code has the variable (fs) to estimate the sampling frequency. After the first sample, the elapsed time between consecutive samples is computed using tic and toc functions. From the elapsed time, we can estimate the sampling frequency as “1 / elapsed_time”.
The filter coefficients are then updated based on the estimated sampling frequency. This allows you to dynamically adjust the filter in real-time as the sampling frequency is estimated.

类别

Help CenterFile Exchange 中查找有关 Signal Processing Toolbox 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by