Heart rate signal using simulink

13 Ansichten (letzte 30 Tage)
Rakan Khair
Rakan Khair am 7 Okt. 2023
Kommentiert: William Rose am 20 Okt. 2023
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 Kommentare
William Rose
William Rose am 7 Okt. 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
Rakan Khair am 9 Okt. 2023
I think there is somthing worng with the ECG board :( , it have noise

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

William Rose
William Rose am 9 Okt. 2023
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 Kommentare
Rakan Khair
Rakan Khair am 20 Okt. 2023
Bearbeitet: Rakan Khair am 20 Okt. 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 :)
William Rose
William Rose am 20 Okt. 2023
@Rakan Khair, I am glad to hear that your system is working now! Good luck with your research.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by