Heart rate signal using simulink

Hope you are all doing well
I am building heart rate system using simulink with arduino Uno , when I use codes in matlab I get results that look like normal ECG but when using simulink with live data the signal appear as attached with the filter design .
Please advice
here is the code
a=arduino
refVoltage = 5.0;
resolutionBits = 10;
maxCount = 2^resolutionBits - 1;
fs = 500 ; % Set the desired sample rate in Hz (e.g., 1000 Hz)
duration = 100; % Duration of data collection in seconds
t=[0 0];
figure
hold on
for x=1:inf
tend=t(1,end)
tic
for i=1:(duration);
input = readVoltage(a, 'A0');
sensorReading(1,i) = input*maxCount/refVoltage;
t(1,i)=toc+tend;
pause(1/fs); % Pause to control sample rate
end
fn=50; %NOTCH f
Q = 50;
bw = fn / Q;
notch_filter = designfilt('bandstopiir', 'FilterOrder', 2, 'HalfPowerFrequency1', fn - bw/2, 'HalfPowerFrequency2', fn + bw/2, 'DesignMethod', 'butter', 'SampleRate', fs);
srfiltnotch = filtfilt(notch_filter, sensorReading);
fcutoff=5
[fa,fb]=butter(7,fcutoff/(fs/2),'high');
srfilhigh=filtfilt(fa,fb,srfiltnotch);
fhcutoff=150
[faa,fbb]=butter(7,fhcutoff/(fs/2),'low');
srfillow=filtfilt(faa,fbb,srfilhigh);
%% create Gaussian kernel
% full-width half-maximum: the key Gaussian parameter
fwhm = 25; % in ms
k = 20;
gtime = 1000*(-k:k)/fs;
% create Gaussian window
gauswin = exp( -(4*log(2)*gtime.^2) / fwhm^2 );
% compute empirical FWHM
pstPeakHalf = k+dsearchn(gauswin(k+1:end)',.5);
prePeakHalf = dsearchn(gauswin(1:k)',.5);
empFWHM = gtime(pstPeakHalf) - gtime(prePeakHalf);
gauswin = gauswin / sum(gauswin);
for i=k+1:(duration)-k-1
% each point is the weighted average of k surrounding points
filtsigG(i) = sum( srfillow(i-k:i+k).*gauswin );
end
if x>=2
filtsigGx(i*(x-1)+1:i*x)=filtsigG;
plot(t,filtsigGx(1:length(t)),"r")
else
filtsigGx=filtsigG;
end
[peaks, locations] = findpeaks(filtsigGx, 'MinPeakHeight',25);
% Calculate time intervals between R-peaks (in seconds)
rr_intervals = diff(locations) / fs; % sampling_rate is the ECG data sampling rate
% Calculate heart rate in beats per minute (BPM)
heart_rate = 60 / mean(rr_intervals)
filtsigGx=cat(2,filtsigGx,zeros(1,i));
end

2 Comments

I am not a SImulink person so I can;t help there. I notice that your Simulink time domain plot has values on the order of 10^32, and the Simulink spectrum values are on the order of 10^63 W. Those values are obviously wrong, so I would look into that.
Is your Simulink script interpreting integers as reals, or vice versa? Is it misinterpreting the byte order of the incoming numbers? I.e., for 16 bit integer from Arduino, does it have the high, low bytes correct? I would check.
I think there is somthing worng with the ECG board :( , it have noise

Sign in to comment.

 Accepted Answer

You replied that you are afraid that the ECG board (Arduino board) has noise. Let's collect some data to see if that is true.
You sample from pin A0. Confirm that that is correct.
Your code as written only collects "duration" samples, but your comment in the code says you intend to collect for "duration" seconds. Use the following mofidified fragment of your code to collect the correct number of samples:
a=arduino
fs = 500; % sample rate (Hz)
duration = 100; % duration of data (s)
N=duration*fs
rawData=zeros(N,1); % allocate vector
for i=1:nSamp
rawData(i) = readVoltage(a, 'A0');
pause(1/fs); % pause to control sample rate
end
save("rawdata.mat","rawdata")
The code above saves vector rawdata to file "rawdata.mat". Please run the code above, report errors if any, and if there are no errors, attach rawdata.mat to your reply.

8 Comments

Thank for taking fom your time to aid me.
Attached is the rawdata file and the ecg board. the ECG board led is blinking even when there is no sensor .
the only error I had was that nSamp is not variable
I see the photo you posted of your experimental setup. The photo shows an Arduino Uno board, a smaller board connected to the Uno board, and a 2-line display that is not connected to anything. It appears that the Uno board gets power from a USB cable. The Arduino Uno board is connected to a smaller board via three wires. I think the wires are ground (green wire), +5V out from the Uno board (purple wire), and analog in to the Uno board (white wire). What is the smaller board? Where are the ECG cables?
The ECG needs to be amplified before it goes to pin A0. Does the small board do this? Why are there not any ECG cables going to the small board?
The signal recorded in file rawdata.mat, from pin A0, appears to be noise. There is no usable ECG signal that I can detect. See plot of the signal below. The top plot is 100 seconds; the bottom plot is a 5-second-long segment.
x=load('rawdata');
ecgRaw=x.rawData;
N=length(ecgRaw);
Fs=500;
t=[0:N-1]/Fs;
subplot(211); plot(t,ecgRaw,'-r'); grid on;
xlabel('Times (s)'); ylabel('ecg (V)'); title('Raw ECG')
subplot(212); plot(t,ecgRaw,'-r'); grid on;
xlabel('Times (s)'); ylabel('ecg (V)'); xlim([10,15]);
It looks like noise to me. I don't think I can help, since I cannot examine your experimental setup.
Thank you for your reply
I was trying to use this setup as in the link I think I will try to change the setup as it appear either the sensor not working or the board https://embedded-lab.com/blog/pc-based-heart-rate-monitor-using-arduino-and-easy-pulse-sensor/
@Rakan Khair, The Easy Pulse board and finger tip detector record the blood pulse. The system uses infrared light to sense blood flow changes in the fingertip. This is not the electrocardiogram (ECG), which is an electrical signal measured between two or more points on the surface of the skin.
I am not sure why your system is not detecting the pulse. Check all cables. Try a different USB cable, if you have one, between the computer and the Arduino.
Rakan Khair
Rakan Khair on 11 Oct 2023
Edited: Rakan Khair on 11 Oct 2023
Thank you for you help , much appreciated .
Rgearding the usage of the easy pulse , I suspected that I had a misunderstanding , thanks for clearing that up
The issue is with board I think ,I am buying a different one
@Rakan Khair, you're welcome. Good luck with your work.
Rakan Khair
Rakan Khair on 20 Oct 2023
Edited: Rakan Khair on 20 Oct 2023
@William Rose the easy pulse board needed to be connected to external power source to work propely.
thank you for your help it works good now :)
@Rakan Khair, I am glad to hear that your system is working now! Good luck with your research.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!