-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimEvoked.m
115 lines (83 loc) · 3.5 KB
/
simEvoked.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
%% File for simulating Evoked potential data
%
% Copyright (C) 2023 Harvey Huang
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
%
% Code initially simulated CCEP data
% DH edited to remove stimulation artifact
%
%% Start by creating simulated parameters and channels
set(0, 'DefaultFigureRenderer', 'painters');
srate = 500;
tt = -0.5:1/srate:1-1/srate;
nchs = 50; % number of simulated channels
ntrs = 12; % number of trials
chs = arrayfun(@(x) sprintf('ch%d', x), 1:nchs, 'UniformOutput', false)';
%% A) Create data, add stim artifacts and EP
seed = 10; % controls the seed for all subsequent steps.
rng(seed);
% stores the data
V0 = zeros(length(tt), nchs);
Aart = 50 + rand(nchs, 1)*5; % slightly different artifact amplitudes for each channel
chsResp = 1:20;
A = 100;
sig = genRandSig(tt, length(chsResp), A);
V1 = V0;
V1(:, chsResp) = V0(:, chsResp) + sig;
figure('Position', [200, 200, 400, 800]); plotTrials(tt, V1, 80, [], [], 'LineWidth', 1.5);
xlabel('Time (s)'); ylabel('Channels');
%% B) Add a systematic noise across all chs and trials, e.g. from reference.
rng(seed);
Aglobal = 0; % same amplitude of noise on all channels
sigCommon = genRandSig(tt, 1, Aglobal);
V2 = V1 + sigCommon;
figure('Position', [200, 200, 400, 800]); plotTrials(tt, V2, 80, [], [], 'LineWidth', 1.5);
xlabel('Time (s)'); ylabel('Channels');
%% C) Add noise common to all channels (line noise and some brown noise from reference)
rng(seed);
% C) Add common noise to all channels at each trial
V3 = repmat(V2', 1, 1, ntrs); % ch x time points x trial
phLN = rand(ntrs, 3)*2*pi; % LN phases
LN = zeros(length(tt), ntrs);
for ii = 1:ntrs
LN(:, ii) = 8*sin(2*pi*60*tt - phLN(ii, 1)) + 2*sin(2*pi*120*tt - phLN(ii, 2)) + 1*sin(2*pi*180*tt - phLN(ii, 3));
end
% brown noise shared across channels
BN = cumsum(0.5*randn(2*length(tt), ntrs));
BN = ieeg_highpass(BN, srate, true);
BN = BN((0.5*length(tt)+1) : 1.5*length(tt), :);
noiseCommon = LN + BN;
V3 = V3 + shiftdim(noiseCommon, -1);
figure('Position', [200, 200, 800, 800]);
subplot(1, 2, 1); plotTrials(tt, V3(:, :, 1)', 80, [], [], 'LineWidth', 1.5);
title('V at one trial');
xlabel('Time (s)'); ylabel('Channels');
subplot(1, 2, 2); plotTrials(tt, mean(V3, 3)', 80, [], [], 'LineWidth', 1.5);
title('V across trials');
xlabel('Time (s)'); ylabel('Channels');
%% D) add random brown noise across all channels
rng(seed);
noiseRand = cumsum(0.5*randn(nchs, 2*length(tt), ntrs), 2); % give double the number of time points so we can highpass it
for ii = 1:nchs
noiseRand(ii, :, :) = ieeg_highpass(squeeze(noiseRand(ii, :, :)), srate, true);
end
noiseRand = noiseRand(:, (0.5*length(tt)+1) : 1.5*length(tt), :);
V4 = V3 + noiseRand;
figure('Position', [200, 200, 800, 800]);
subplot(1, 2, 1); plotTrials(tt, V4(:, :, 1)', 80, [], [], 'LineWidth', 1.5);
title('V at one trial');
xlabel('Time (s)'); ylabel('Channels');
subplot(1, 2, 2); plotTrials(tt, mean(V4, 3)', 80, [], [], 'LineWidth', 1.5);
title('V across trials');
xlabel('Time (s)'); ylabel('Channels');