-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBME_51100_ProblemSet5_6.m
More file actions
60 lines (50 loc) · 1.71 KB
/
BME_51100_ProblemSet5_6.m
File metadata and controls
60 lines (50 loc) · 1.71 KB
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
load('EEG_SSPdata.mat');
nchans = size(dat, 1);
nsamps = size(dat, 2);
%Collect blink samples
tmin = -0.2;
tmax = +0.2;
imin = floor(tmin*fs);
imax = ceil(tmax*fs);
B = []; %To concatenate the samples across the blinks
for k = 1:numel(blink_samples)
start_ind = max([1, blink_samples(k) + imin]);
end_ind = min([blink_samples(k) + imax, nsamps]);
B = [B, dat(:, start_ind:end_ind)]; %B is of size 32x95178
end
%Performing PCA on the matrix B
C = cov(B');
[q, lambda] = eigs(C, 1);
%Constructing projection matrix
P = eye(nchans) - q*q';
%% Applying the Projection and plotting the channels A1 to A5 out
dat_cleaned = P*dat;
% Plot original and cleaned EEG data for channels A1-A5
plotEEG(dat, [1, 2, 3, 4, 5], "Original");
plotEEG(dat_cleaned, [1, 2, 3, 4, 5], "Cleaned");
%% Save the variable B and q matrix into a new file
save('gradedSSPoutput.mat', 'B', 'q', 'P');
%% Function for plotEEG
function plotEEG(data, channels, title_name)
% data: nchans x nsamps matrix of EEG data
% channels: vector of channel indices to plot
% Set up time axis
fs = 1000; %sampling rate
nsamps = size(data, 2);
t = linspace(0, nsamps/fs, nsamps);
% Plot original and cleaned data for selected channels
figure;
title(title_name + " Data");
for i = 1:length(channels)
subplot(length(channels), 1, i);
hold on;
plot(t, data(channels(i), :), 'b');
hold off;
title(sprintf('Channel %d', channels(i)));
ylabel('Voltage (uV)');
xlabel('Time (s)');
end
% Set the y-axis limits to the same range for both plots
ylims = get(gca, 'YLim');
set(gca, 'YLim', [min(ylims), max(ylims)]);
end