MATLAB Answers

Continous Filtering of ECG

5 views (last 30 days)
Sibi Ramachandran
Sibi Ramachandran on 12 Feb 2019
Answered: Munish Raj on 25 Feb 2019
Hey guys I am hitting a roadblock in my code and I was wondering if I could get some help here. This is for a project I am working on currenty. In this project, I want to be able to recieve ECG, EMG, Forceand data from a Sparkfun Ad8232 Heart monitor, to an arduino mega board, through a HC-05 BT module and subsequently Matlab for signal processing. So far, I have developed a methodology to sort data from certain pins and create a live graph of said data. My main issue is with the ECG. I want to be able to implement filters much like in this answer to a question provided by @Star Strider, take a BPM measurement, and then display the corrected ecg samples on the same subplot. Any input would be greatly appreciated and I hope my comments make it clear the logic behind my code. I think the main issue is I do not know how to locate the sampling frequency of the arduino mega board, Best!
Fs = 175;
Fn = Fs/2;
i = 0; %counter
b = Bluetooth('HC-05',1); %Vector identifying the BT module
n = 1200; %Number of samples desired
window = 200; %Window of samples
y_min = 0; %Y-axis Min
y_max = 1000; %Y-axis Max
x = 1:n; %x-axis span
values = []; %Initialized matrix for analog reading from multiple sensors simultaneously
y1 = zeros(1,n); %Initializing sensor 1
y2 = zeros(1,n); %Initializing sensor 2
y3 = zeros(1,n); %Initializing sensor 3
y4 = zeros(1,n); %Initializing sensor 4
%Initialize plots for sensors 1-4
subplot(4,1,1)
h1 = plot(x,y1,'YDataSource','y1');
ylim([0 y_max]);
xlim([0 n]);
subplot(4,1,2)
h2 = plot(x,y2,'YDataSource','y2');
ylim([0 y_max]);
xlim([0 n]);
subplot(4,1,3)
h3 = plot(x,y3,'YDataSource','y3');
ylim([0 y_max]);
xlim([0 n]);
subplot(4,1,4)
h4 = plot(x,y4,'YDataSource','y4');
ylim([0 y_max]);
xlim([0 n]);
%The while loop checks incoming values from BT module in single bytes and
%outputs in order into initialized horizontal vector a string and stops as
%the $ character is reached. Values from the sensor correspond to a
%position in the vector. For example sensor one would corresponf to arduino
%pin A0 and vector position v(1)
ylim([0 y_max]);
subplot(4,1,4)
h4 = plot(x,y4,'YDataSource','y4');
ylim([0 y_max]);
%Begin reading from bluetooth module
fopen(b);
for i = 1:n
while i <= n
value = fscanf(b,'%c',1);
values = strcat(values,value);
if(value == '$')
break;
end
end
%Splits strings after each sensor value and erases ; and $ characters
v = strsplit(values,';');
v = erase(v,'$');
v = erase(v,';');
v = erase(v,' ');
value = 0; %Vector reset
values = []; %Vector reset
%Converts sensor string value back to a number
Voltage1 = str2double(v{1});
Voltage2 = str2double(v{2});
Voltage3 = str2double(v{3});
Voltage4 = str2double(v{4});
%y-values are inserted into subplots and refreshed
y1(i) = Voltage1;
while i <= n
if i >= window
k = y1([i-window i]);
Fn = Fs/2;
L = length(k);
fts = fft(k)/L;
refreshdata(h2,'caller')
%axis([x(i) x(i+window) y_min y_max]);
end
%heart beat calculations for this sample of data needs to be set
%Number of data points between peaks * Fs will give us the time between
%peaks
%Threshold voltage for peak to peak
%c = Number of Peaks *Time Between Peaks = Time for all Peaks in window
end
y2(i) = Voltage2;
%axis([x(i) x(i+window) y_min y_max]);
%This is for the moving window but it
%is commented out until I can figure it out
refreshdata(h2,'caller')
y3(i) = Voltage3;
% axis([x(i) x(i+window) y_min y_max]);
refreshdata(h3,'caller')
y4(i) = Voltage4;
% axis([x(i) x(i+window) y_min y_max]);
refreshdata(h4,'caller')
drawnow
i = i+1;
end
fclose(b);

Answers (1)

Munish Raj
Munish Raj on 25 Feb 2019
There is no in built method for finding the sampling frequency of the Arduino mega.
The Sampling frequency depends on a lot of factors
  • Clock frequency/Machine cycle of the mega
  • Time for serial communication interfaces
  • Time taken by other instructions in the loop.
Hence, the sampling frequency is defined in a microcontroller.
It is possible to find the time between two subsequent readings , thus calculating the frequency between two samples (sampling frequency)
This can be done by storing the time of the microcontroller before reading the analog value from the sensor and subtracting this time from the time the next sample is read.
This would give you
Ts(time between two samples)
and
Fs = 1/Ts
However, it is possible to achieve this task without having to use the sampling frequency in a user MATLAB function if you’re using simulink, as shown in this documentation link.

Community Treasure Hunt

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

Start Hunting!

Translated by