-
Notifications
You must be signed in to change notification settings - Fork 14
/
gen_data.m
162 lines (137 loc) · 4.39 KB
/
gen_data.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
function [data, source_signal, Asignal] = gen_data(T, D, N, K, snr, ssnr, samesignal, samenoise, pink, distr)
% generate data as linear mixture of K correlated components and background noise
%
% T: number of time samples
% D: number of sensors
% N: number of subjects/viewings
% K: number of correlated dimensions
% snr: snr in [0, 1]
% ssnr: spatially uncorrelated to correlated noise ratio in [0 1]
% samesignal: 1/0, indicates same or different signal correlation structure
% samenoise: 1/0, indicates same or different noise correlation structure
% distr: distribution of signal and noise sources
% either 'Gauss' (default), 'Chi2' or 'Bernoulli'
% in the latter case, observations are also dichotomized.
%
% Stefan Haufe, 2017
if length(K) > 1
target_ISC = K;
K = length(K);
else
target_ISC = (K:-1:1)/K;
end
alphas = sqrt(1+target_ISC).*sqrt(target_ISC)./sqrt(1-target_ISC.^2);
betas = ones(size(alphas));
ininf = isinf(alphas);
alphas(ininf) = 1;
betas(ininf) = 0;
if nargin < 6
ssnr = 0;
end
if nargin < 8
samesignal = 1;
samenoise = 1;
end
if nargin < 9
pink = 1;
end
if nargin < 10
distr = 'Gauss';
end
sensor_signal = zeros(T, D, N);
if K == 0
source_signal = [];
else
% time course of the correlated activity = signal
% at this point white or pink Gaussian noise
% assumed to be identical for all subject/viewings
% this may be relaxed
if pink
source_signal = mkpinknoise(T, K);
else
source_signal = randn(T, K);
end
% make signal chi2 or Bernoulli distributed
switch distr
case 'Chi2'
source_signal = source_signal.^2;
% case 'Bernoulli'
% source_signal = sign(source_signal);
end
source_signal = repmat(source_signal, 1, 1, N);
source_signal = zscore(source_signal);
ISC_noise = randn(size(source_signal));
if isequal(distr, 'Chi2')
ISC_noise = ISC_noise.^2;
end
ISC_noise = zscore(ISC_noise);
source_signal = source_signal.*repmat(alphas, T, 1, N) + ISC_noise.*repmat(betas, T, 1, N);
source_signal = zscore(source_signal);
% diag(corr(source_signal(:, :, 1), source_signal(:, :, 2)))
if samesignal
% spatial distribution of the correlated activity,
% also assumed to be the same for all subjects
% if this is not the case (e.g. due to anatomical differences,
% CCA is more appropriate than CorrCA
Asignal = randpsd(D, min(K, D));
for in = 1:N
sensor_signal(:, :, in) = source_signal(:, :, in)*Asignal;
end
else
% different signal correlation structure in each subject,
% representing, e.g., different subject anatomies
for in = 1:N
Asignal(:, :, in) = randpsd(D, min(K, D));
sensor_signal(:, :, in) = source_signal(:, :, in)*Asignal(:, :, in);
end
end
end
if samenoise
% sensor-space spatial correlation pattern of the noise
% Crucially, also assumed to be the same here for all subjects
% In a more realistic setting, this is different for all subjects,
% which would make CCA more appropriate than CorrCA
Anoise = randpsd(D, min(T, D));
else
% different noise spatial correlation structure in each subject
for in = 1:N
Anoise(:, :, in) = randpsd(D, min(T, D));
end
end
data = zeros(T, D, N);
for in = 1:N
% spatially correlated white or pink Gaussian noise in sensor space
if pink
source_noise(:, :, in) = mkpinknoise(T, min(T, D));
else
source_noise(:, :, in) = randn(T, min(T, D));
end
% make noise chi2 or Bernoulli distributed
switch distr
case 'Chi2'
source_noise = source_noise.^2;
case 'Bernoulli'
source_noise = sign(source_noise);
end
if samenoise
sensor_noise(:, :, in) = source_noise(:, :, in)*Anoise;
else
sensor_noise(:, :, in) = source_noise(:, :, in)*Anoise(:, :, in);
end
% spatially uncorrelated white sensor noise
meas_noise(:, :, in) = randn(T, min(T, D));
switch distr
case 'Chi2'
meas_noise = meas_noise.^2;
% case 'Bernoulli'
% meas_noise = sign(meas_noise);
end
end
sensor_signal = sensor_signal / norm(sensor_signal(:));
sensor_noise = sensor_noise / norm(sensor_noise(:));
meas_noise = meas_noise / norm(meas_noise(:));
noise = ssnr*meas_noise + (1-ssnr)*sensor_noise;
data = snr*sensor_signal + (1-snr)*noise;
if isequal(distr, 'Bernoulli')
data = sign(data);
end