Heart rate signal using simulink
Show older comments
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
William Rose
on 7 Oct 2023
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.
Rakan Khair
on 9 Oct 2023
Accepted Answer
More Answers (0)
Categories
Find more on LEGO MINDSTORMS EV3 Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!