-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSignalsProjectInitial.m
306 lines (271 loc) · 6.86 KB
/
SignalsProjectInitial.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
%% Notes
% Alpha 8 - 12
% Beta 12 - 30
% Gamma 30 - 100+
% Delta 0 - 4
% Theta 4 - 7
%%
clear all
close all
name = input('Enter the EEG file name (without .mat extension) :','s');
EEGSignal = load(name);
% p0 = necg;
% %%
% for i = 0:9
% while i <= 9
% i = num2str(i)
% j = 0;
% j = num2str(j);
% temp = strcat('EEG_subject0',j,i);
% EEG = load(temp);
%
% end
% end
% if i = 9
% for a = 0:9
% load('EEG_subject0',j,i);
% end
% end
% end
% end
%%
%find the waves associated with the EEG - the bands
%do filtering such as butterworth accordingly.
% do the processing indvidually
%
%% Loading the ECG file %%
%EEGSignal.EEG(i).ch
% EEGSignal = load('EEG_subject001.mat');
fs = 256;
T = 1/fs;
slen1 = length(EEGSignal);
tEEG = [1:slen1]/fs;
nEEGSamp = length(EEGSignal.EEG);
nEEGch = length (EEGSignal.EEG(1).ch);
tEEG = (1:length(EEGSignal.EEG(1).ch))*T;
%%
% %%
for i = 1:nEEGSamp
slen1 = length(EEGSignal.EEG(i).ch);
tEEG = [1:slen1]/fs;
plot(tEEG, EEGSignal.EEG(i).ch);
i= num2str(i);
name = strcat('Original Signal Channel',' ', i);
title([name]);
xlabel('time (s)');
ylabel('Amplitude');
figure;
end
% % Plotting Original EEG Signals
% for i = 1:5
% subplot(5,1,i);
% plot(tECG, EEGSignal.EEG(i).ch);
% i= num2str(i);
% name = strcat('Original Signal',' ', i);
% title([name]);
% xlabel('time (s)');
% ylabel('Amplitude');
% end
% j = 1;
% for i = 6:10
% if j <= 5
% subplot(5,1,j);
% plot(tECG, EEGSignal.EEG(i).ch);
% i= num2str(i);
% name = strcat('Original Signal',' ', i);
% title([name]);
% xlabel('time (s)');
% ylabel('Amplitude');
% j = j +1;
% end
% end
% figure;
% j = 1;
% for i = 11:20
% if j <= 5
% subplot(5,1,j);
% plot(tECG, EEGSignal.EEG(i).ch);
% i= num2str(i);
% name = strcat('Original Signal',' ', i);
% title([name]);
% xlabel('time (s)');
% ylabel('Amplitude');
% j = j +1;
% end
% end
% figure;
% j = 1;
% for i = 21:23
% if j <= 3
% subplot(3,1,j);
% plot(tECG, EEGSignal.EEG(i).ch);
% i= num2str(i);
% name = strcat('Original Signal',' ', i);
% title([name]);
% xlabel('time (s)');
% ylabel('Amplitude');
% j = j +1;
% end
% end
% figure;
%% Notch filter to remove 60 Hz (and lowpass filter) %%
[a1] = [1 -0.1960342807 1];
[b1] = [-0.1768195611];
M = 128;
freqz(a1, b1, M, fs);
title('Notch Filter Frequency Response');
% Output of the notch filter %
% notch_filter = struct([]);
for i = 1:nEEGSamp
figure;
NF = abs(EEGSignal.EEG(i).ch);
notch_filter(:,i) = filtfilt(a1, b1, NF);
plot(tEEG, notch_filter(:, i))
I = num2str(i);
title(['Notch Filter Frequency Response of',' ', I]);
xlabel('Time in seconds');
ylabel('ECG');
axis tight;
end
% FR = fft(NF);
% plot(tEEG, FR);
% [b,a] = butter(6,fc/(fs/2));
% freqz(b,a,128,fs);
% subplot(2,1,1);
% temp1 = EEGSignal.EEG(2).ch;
% plot(temp1(1500:1700));
% temp2 = notch_filter(1500:1700,2);
% subplot(2,1,2);
% plot(temp2);
%% The difference equations of the filters used below are given in the text book (pages 250 - 256) %%
% Delta Waves
% Freq Range % Delta 0 - 4
%%% Low pass filter %%% % Hz
% Alpha 8 - 12
% Beta 12 - 30
% Gamma 30 - 100+
% Delta 0.5 - 4
% Theta 4 - 7
%Delta
lowEnd = 0.5; % Hz
highEnd = 4; % Hz
filterOrder = 4; % Filter order (e.g., 2 for a second-order Butterworth filter). Try other values too
[b, a] = butter(filterOrder, [lowEnd highEnd]/(fs/2)); % Generate filter coefficients
DeltaW = filtfilt(b, a, notch_filter);
DFW = fft(DeltaW);
plot(tEEG, DFW(:,1));
plot(tEEG, DeltaW(:,2));
title('Delta Waves');
figure;
FR = fft(DeltaW);
FR = FR/slen1;
plot(tEEG, FR);
wnum = input('Enter the EEG file name (without .mat extension) :','s');
DW_Reshape = reshape(DeltaW, [], 4608);
%Alpha
lowEnd = 8; % Hz
highEnd = 12; % Hz
filterOrder = 2; % Filter order (e.g., 2 for a second-order Butterworth filter). Try other values too
[b, a] = butter(filterOrder, [lowEnd highEnd]/(fs/2)); % Generate filter coefficients
Alpha = filtfilt(b, a, notch_filter);
plot(tEEG, Alpha(:,2));
title('Alpha Waves');
figure;
%Beta
lowEnd = 12; % Hz
highEnd = 30; % Hz
filterOrder = 2; % Filter order (e.g., 2 for a second-order Butterworth filter). Try other values too
[b, a] = butter(filterOrder, [lowEnd highEnd]/(fs/2)); % Generate filter coefficients
Beta = filtfilt(b, a, notch_filter);
plot(tEEG, Beta(:,2));
title('Beta Waves');
figure;
%Theta
lowEnd = 5; % Hz
highEnd = 7; % Hz
filterOrder = 2; % Filter order (e.g., 2 for a second-order Butterworth filter). Try other values too
[b, a] = butter(filterOrder, [lowEnd highEnd]/(fs/2)); % Generate filter coefficients
Theta = filtfilt(b, a, notch_filter);
plot(tEEG, Theta(:,2));
title('Theta Waves');
% freqz(bLP, aLP, M, fs);
% title 'Low-pass Filter Frequency Response';
% figure;
% lpeeg = filter (bLP, aLP, notch_filter);
% plot (t, lpeeg);
% title 'Low-pass Filter EEG signal';
for i = 1:nEEGSamp
LP = EEGSignal.EEG(i).ch;
LP_filter(:,i) = filter(a1, b1, LP);
plot(tEEG, LP_filter(:, i))
I = num2str(i);
title(['Low Pass Filter Frequency Response of',' ', I]);
xlabel('Time in seconds');
ylabel('EEG');
axis tight;
figure;
end
%%
hpemg = filter(b, a, lpeeg);
figure
plot(tEEG, hpemg)
xlabel('Time in seconds');
ylabel('ECG');
title ('High-Pass Filter ECG Signal');
%%
%%% High pass filter (Allpass-lowpass) %%%
[b] = [-1/32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1/32];
[a] = [1 -1];
figure;
freqz(b, a, M, fs);
title ('High-Pass Filter Frequency Response');
figure;
zplane(b,a);
xlabel ('Real');
ylabel ('Imaginary');
title ('High-Pass Filter Poles and Zeros');
hpemg = filter(b, a, lpeeg);
figure
plot(tEEG, hpemg)
xlabel('Time in seconds');
ylabel('ECG');
title ('High-Pass Filter ECG Signal');
%% Band Pass
a4 = conv(a3,a2);
b4 = conv(b3, b2);
figure;
freqz(a4, b4, M, fs);
title('Band Pass Filter Frequency Response');
figure;
zplane(a4, b4);
title('Pole-Zero diagram of Band Pass Filter');
bandpass_filter = filter(a4, b4, notch_filter);
% Plot of the ECG after Notch filtering
slen1 = length(ecg);
tEEG = [1:slen1]/fs;
figure;
plot(tEEG, bandpass_filter)
title(['Band Pass Filter of ' name]);
xlabel('Time in seconds');
ylabel('ECG');
axis tight;
%%
%%% Moving window integral %%%
a6 = ones(1,31);
b6 = [30];
figure;
freqz(a6, b6, M, fs);
title('Moving Window Integral Filter Frequency Response');
figure;
zplane(a6, b6);
title('Pole-Zero diagram of Moving Window Integral Filter');
moving_window = filter(a6, b6, square);
% Plot of the ECG after Notch filtering
slen1 = length(eeg);
tEEG = [1:slen1]/fs;
figure;
plot(tEEG, moving_window)
title(['Moving Window Integration Filter of ' EEGSignal]);
xlabel('Time in seconds');
ylabel('ECG');
axis tight;