· 4 years ago · Jun 13, 2021, 01:54 PM
1clc;
2clear all;
3close all;
4
5%ARDUINO SET and TIME AXIS-----------------------------
6a = arduino(); %'COM6', 'Nano3', 'ForceBuildOn', true
7ecg=0;
8f_s=250;
9N=180;
10t=[0:N-1]/f_s;
11
12%loop to take data-------------------------------------
13for k=1:180
14 b=readVoltage(a,'A0');
15 ecg=[ecg,b];
16 plot(ecg);
17 grid on;
18 drawnow;
19end
20
21%NOTCH FILTER ECG------------------------------
22w=50/(250/2);
23bw=w;
24[num,den]=iirnotch(w,bw); % notch filter implementation
25ecg_notch=filter(num,den,ecg);
26
27%PLOT FILTERED ECG-----------------------------
28figure, %%1
29N1=length(ecg_notch);
30t1=[0:N1-1]/f_s;
31plot(t1,ecg_notch,'r'); title('Filtered ECG signal ')
32xlabel('time')
33ylabel('amplitude')
34
35%THINGSPEAK SETUP------------------------------
36channelID = ; %%your channel ID here
37writeKey = ''; %%your write API key here
38UserApikey = ''; %%your User API key here
39url = sprintf('https://api.thingspeak.com/channels/%s/feeds.json?api_key=%s',num2str(channelID),UserApikey);
40response = webwrite(url, weboptions('RequestMethod','delete'));
41%Creating timestamp----------------------------
42tStamps = [datetime('now')-hours(179):hours(1):datetime('now')]';
43%adding data with timestamp--------------------
44for k=1:180
45 ecg_180r(k) = round(ecg_notch(k));
46 ecg_180(k) = ecg_notch(k);
47end
48dataTable = timetable(tStamps,ecg_180r');
49thingSpeakWrite(channelID,dataTable,'WriteKey',writeKey);
50
51%XLS DATA AND PLOT-----------------------------
52t180 = 0:1:179;
53xlsdata = [t180;ecg_180];
54figure, %%2------------------------------------
55plot(t180,ecg(1:180),'r'); title('Data plotting for 0 to 0.804 time frame')
56xlabel('time')
57ylabel('amplitude')
58hold on
59plot(t180,ecg_180,'g');
60legend('ORIGINAL ECG SIGNAL',' Flitered ECG SIGNAL')
61hold off
62[Status, Message] = xlswrite('ECG_FILTERED_THINGSPEAK.xlsx', xlsdata');