I have problomes with my matlab code that dose not read ecg signal and max 30100 spo2 sensor and the oled display dose not work

4 views (last 30 days)
clc
close
clear
% Enter your MATLAB code below
readChannelID = 2378975 ; %%your channel ID here
readAPIKey = '1N2NDFU116BEG5D6'; %%your read API key here
y = thingSpeakRead(readChannelID,'Field',1,'NumPoints',500,'ReadKey',readAPIKey);
a = arduino();
e=0;
fs = 250; % find the sampling rate or frequency
T = 1/fs ; % sampling rate or frequency
w = 120; % 2 min 0r 120 second
y = 500;
t = (0 : y-1) /fs ; % sampling period
%Compute the spectrum of the ECG and provide remarks on the spectral features of the ECG
%DFT to describe the signal in the frequency
Y = fft(y);
NFFT = 2 ^ 2 * (y);
Y = fft(y, NFFT) / y;
f = (fs / 2 * linspace(0, 1, NFFT / 2+1)); % Vector containing frequencies in Hz
d = 2 * abs(Y(1:NFFT / 2 + 1)); % Vector containing corresponding amplitudes
figure;
plot (f, d);
title ('plot Frequency Response of ECG signal');
xlabel ('frequency (Hz)');
ylabel ('|y(f)|');
grid on;
for k=1:500
b = readVoltage(a,'A0');
e = [e,b];
plot(e);
grid on;
drawnow;
end
%ecg7500=load('BEAT_12-2-2016 17.53.13.txt');
%for k=1:500
%ecg(k)=ecg7500(k);
%plot(ecg);
% drawnow;
% end
%R wave calculation
wt = modwt(e,5);
wtrec = zeros(size(wt));
wtrec(4:5,:) = wt(4:5,:);
y = imodwt(wtrec,'sym4');
tm = 0:1:499;
y = abs(y).^2;
[pk_Fs, locs_Fs] = findpeaks(y,tm,'MinPeakHeight',0.35,'MinPeakDistance',0.35);
figure,
plot(y,tm);
hold on
plot(locs,qrspeaks,'ro')
xlabel('Seconds')
title('R Peaks Localized by Wavelet Transform with Automatic Annotations')
grid on
distanceBetweenFirstAndLastPeaks = locs(length(locs))-locs(1);
averageDistanceBetweenPeaks = distanceBetweenFirstAndLastPeaks/length(locs);
averageHeartRate = averageDistanceBetweenPeaks*10/3;
disp('Average Heart Rate = ');
disp(averageHeartRate);
%THINGSPEAK SETUP------------------------------
channelID = 2378975; %%add your channel ID here
writeKey = 'X6BHN8LI6444FEXI'; %%add your write API key here
UserApikey = '1Y5V66D0O8HPKNVW'; %%add youe User API key here
url = sprintf('https://api.thingspeak.com/channels/%s/feeds.json?api_key=%s',num2str(channelID),UserApikey);
s = webwrite(url, weboptions('RequestMethod','delete'));
%Creating timestamp----------------------------
tStamps = (datetime('now')-seconds(499):seconds(1):datetime('now'))';
dataTable = timetable(tStamps,ecg ,y');
thingSpeakWrite(channelID,dataTable,'WriteKey',writeKey);
% Enter your MATLAB code below
readChannelID = 2378975 ; %%channel Id here
readAPIKey = '1N2NDFU116BEG5D6'; %%read API key here
f_s=250;
ecg = thingSpeakRead(readChannelID,'Field',1,'NumPoints',500,'ReadKey',readAPIKey);
N1=length(ecg);
t1=(0:N1-1)/f_s;
plot(t1,ecg,'r');
title('Raw ECG Data') ;
xlabel('time');
ylabel('amplitude');
% Enter your MATLAB code below
readChannelID = 2378975; %%your channel ID here
readAPIKey = '1N2NDFU116BEG5D6'; %%your read API here
y = thingSpeakRead(readChannelID,'Field',2,'NumPoints',500,'ReadKey',readAPIKey);
N = length(y);
ls = size(y);
fs = 250;% find the sampling rate or frequency
t = (0 : N-1) / fs;% sampling period
T = 1/fs;% sampling rate or frequency
plot(t,y); title('R Peaks Localized by Wavelet Transform: Automatic Annotations')
% Enter your MATLAB code below
readChannelID = 2378975; %%channel ID here
readAPIKey = '1N2NDFU116BEG5D6'; %%your read API key here
y = thingSpeakRead(readChannelID,'Field',1,'NumPoints',500,'ReadKey',readAPIKey);
fs = 250; % find the sampling rate or frequency
T = 1/fs;% sampling rate or frequency
w = 120; % 2 min 0r 120 second
N = 500;
t = (0 : N-1) / fs;% sampling period
%Compute
the spectrum of the ECG and provide remarks on the spectral features of the ECG
%DFT to describe the signal in the frequency
NFFT = 2 ^ nextpow2(N);
Y = fft(y, NFFT) / N;
f = (fs / 2 * linspace(0, 1, NFFT / 2+1))'; % Vector containing
frequencies in Hz
amp = ( 2 * abs(Y(1: NFFT / 2+1))); % Vector containing corresponding
amplitudes
figure;
plot (f, amp);
title ('plot Frequency Response of EC signal');
xlabel ('frequency (Hz)');
ylabel ('|y(f)|');
grid on;
% Enter your MATLAB code below
readChannelID = 2378975; %%your channel ID here
readAPIKey = '1N2NDFU116BEG5D6'; %%your read API key here
y = thingSpeakRead(readChannelID,'Field',1,'NumPoints',500,'ReadKey',readAPIKey);
fs = 250; % find the sampling rate or frequency
T = 1/fs; % sampling rate or frequency
w = 120; % 2 min 0r 120 second
N = 500;
t = (0 : N-1) / fs;% sampling period
%Compute the spectrum of the ECG and provide remarks on the spectral features of the ECG
%DFT to describe the signal in the frequency
NFFT = 2 ^ nextpow2(N);
Y = fft(y, NFFT) / N;
f = (fs / 2 * linspace(0, 1, NFFT / 2+1))'; % Vector containing frequencies in Hz
amp = ( 2 * abs(Y(1: NFFT / 2+1))); % Vector containing corresponding amplitudes
figure;
plot (f, amp);
title ('plot Frequency Response of ECG signal');
xlabel ('frequency (Hz)');
ylabel ('|y(f)|');
grid on;
% Enter your MATLAB code below
readChannelID = 2378975; %%channel ID here
readAPIKey = '1N2NDFU116BEG5D6'; %%your read API key here
y = thingSpeakRead(readChannelID,'Field',1,'NumPoints',500,'ReadKey',readAPIKey);
fs = 250; % find the sampling rate or frequency
T = 1/fs;% sampling rate or frequency
w = 120; % 2 min 0r 120 second
N = 500;
t = (0 : N-1) / fs;% sampling period
%Compute the spectrum of the ECG and provide remarks on the spectral features of the ECG
%DFT to describe the signal in the frequency
NFFT = 2 ^ nextpow2(N);
Y = fft(y, NFFT) / N;
f = (fs / 2 * linspace(0, 1, NFFT / 2+1))'; % Vector containing frequencies in Hz
amp = ( 2 * abs(Y(1:NFFT / 2+1))) / NFFT; % Vector containing corresponding amplitudes
figure;
plot (f, amp);
title ('plot Frequency Response of ECG signal');
xlabel ('frequency (Hz)');
ylabel ('|y(f)|');
grid on;
% Enter your MATLAB code below
readChannelID = 2378975; %%channel Id here
readAPIKey = '1N2NDFU116BEG5D6'; %%read API key here
f_s=250;
ecg = thingSpeakRead(readChannelID,'Field',1,'NumPoints',500,'ReadKey',readAPIKey);
N1=length(ecg);
t1=(0:N1-1)/f_s;
plot(t1,ecg,'r'); title('Raw ECG Data')
xlabel('time')
ylabel('amplitude')
%arduino set and variables
d=arduino();
ecg=0;
for k=1:500
b=readVoltage(d,'A0');
e=[ecg,b];
plot(e);
grid on;
drawnow;
end
% ecg7500=load('BEAT_12-2-2016 17.53.13.txt');
% for k=1:500
% ecg(k)=ecg7500(k);
% plot(ecg);
% drawnow;
% end
%R wave calculation
wt = modwt(ecg,5);
wtrec = zeros(size(wt));
wtrec(4:5,:) = wt(4:5,:);
y = imodwt(wtrec,'sym4');
tm = 0:1:499;
y = abs(y).^2;
[qrspeaks,locs] = findpeaks(y,tm,'MinPeakHeight',0.35,'MinPeakDistance',0.150);
figure,
plot(tm,y);
hold on
plot(locs,qrspeaks,'ro')
xlabel('Seconds')
title('R Peaks Localized by Wavelet Transform with Automatic Annotations')
grid on
distanceBetweenFirstAndLastPeaks = locs(length(locs))-locs(1);
averageDistanceBetweenPeaks = distanceBetweenFirstAndLastPeaks/length(locs);
averageHeartRate = averageDistanceBetweenPeaks*10/3;
disp('Average Heart Rate = ');
disp(averageHeartRate);
%THINGSPEAK SETUP------------------------------
channelID = '2378975'; %%add your channel ID here
writeKey = 'X6BHN8LI6444FEXI'; %%add your write API key here
UserApikey = '1Y5V66D0O8HPKNVW'; %%add youe User API key here
url = sprintf('https://api.thingspeak.com/channels/%s/feeds.json?api_key=%s',num2str(channelID),UserApikey);
h = webwrite(url, weboptions('RequestMethod','delete'));
%Creating timestamp----------------------------
tStamps = (datetime('now')-seconds(499):seconds(1):datetime('now'))';
dataTable = timetable(tStamps, ecg',y');
thingSpeakWrite(channelID,dataTable,'WriteKey',writeKey);
%ARDUINO SET and TIME AXIS-----------------------------
d = arduino(); %'COM4', 'Uno', 'ForceBuildOn', true
ecg=0;
f_s=250;
N=180;
t=(0:N-1)/f_s;
%loop to take data-------------------------------------
for k=1:180
b=readVoltage(d,'A0');
e=[ecg,b];
plot(e);
grid on;
drawnow;
end
%NOTCH FILTER ECG------------------------------
w=50/(250/2);
bw=w;
[num,den]=iirnotch(w,bw); % notch filter implementation
ecg_notch=filter(num,den,ecg);
%PLOT FILTERED ECG-----------------------------
figure, %%1
N1=length(ecg_notch);
t1=(0:N1-1)/f_s;
plot(t1,ecg_notch,'r');
title('Filtered ECG signal ');
xlabel('time');
ylabel('amplitude');
%THINGSPEAK SETUP------------------------------
channelID = '2378975'; %%your channel ID here
writeKey = 'X6BHN8LI6444FEXI'; %%your write API key here
UserApikey = '1Y5V66D0O8HPKNVW'; %%your User API key here
url = sprintf('https://api.thingspeak.com/channels/%s/feeds.json?api_key=%s',num2str(channelID),UserApikey);
s = webwrite(url, weboptions('RequestMethod','delete'));
%Creating timestamp----------------------------
tStamps = (datetime('now')-hours(179):hours(1):datetime('now'))';
%adding data with timestamp--------------------
for k=1:180
s(k) = round(ecg_notch(k));
g(k) = ecg_notch(k);
end
dataTable = timetable(tStamps,s');
thingSpeakWrite(channelID,dataTable,'WriteKey',writeKey);
%XLS DATA AND PLOT-----------------------------
t180 = 0:1:179;
xlsdata = [t180;g];
figure, %%2------------------------------------
plot(t180,ecg(1:180),'r'); title('Data plotting for 0 to 0.804 time frame')
xlabel('time')
ylabel('amplitude')
hold on
plot(t180,g,'g');
legend('ORIGINAL ECG SIGNAL',' Flitered ECG SIGNAL')
hold off
[Status, Message] = writecell('ECG_FILTERED_THINGSPEAK.xlsx', xlsdata');
  2 Comments
Christopher Stapels
Christopher Stapels on 13 Feb 2024
Can you simplify the code you shared and describe your question in more detail? Its a little difficult to understand what you need help with.
William Rose
William Rose on 27 Feb 2024
@Hasan, I agree exactly with @Christopher Stapels. Please provide the simplest possible code that illustrates the problem you have. For example, if the problem is with importing data, then none of the data analysis code is necessary.
You call thingSeakRead() seven times. Each time you use the same readChannelID and readAPIKey. Do you expect a different result each time? IN the code below, I read from the channel two times, with a 4 second apuse between reads. The plot shows that I get the same data each time.
Before doing analysis such as FFT, wavelet transform, or peak detection, plot the raw data to make sure it is what you expect. The plot below shows that the data is just noise, with 2-3 bits resolution, and there are only 360 points, not 500.
readChannelID = 2378975 ; %%your channel ID here
readAPIKey = '1N2NDFU116BEG5D6'; %%your read API key here
y1 = thingSpeakRead(readChannelID,'Field',1,'NumPoints',500,'ReadKey',readAPIKey);
Warning: 'Unable to read 500 points as requested. Only 360 points available in the channel.
N1=length(y1);
pause(4); % wait 4 seconds
y2 = thingSpeakRead(readChannelID,'Field',1,'NumPoints',500,'ReadKey',readAPIKey);
Warning: 'Unable to read 500 points as requested. Only 360 points available in the channel.
N2=length(y2);
fs = 250; % sampling rate (Hz)
t1 = (0:N1-1)/fs ; % time vector
t2 = (0:N2-1)/fs ; % time vector
plot(t1,y1,'r.',t2,y2,'-b');
grid on; xlabel('Time (s)'); legend('y1','y2')
OK

Sign in to comment.

Answers (0)

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!