forked from guetlove/FastICA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicaplot.m
397 lines (362 loc) · 12.9 KB
/
icaplot.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
function icaplot(mode, varargin);
%ICAPLOT - plot signals in various ways
%
% ICAPLOT is mainly for plottinf and comparing the mixed signals and
% separated ica-signals.
%
% ICAPLOT has many different modes. The first parameter of the function
% defines the mode. Other parameters and their order depends on the
% mode. The explanation for the more common parameters is in the end.
%
% Classic
% icaplot('classic', s1, n1, range, xrange, titlestr)
%
% Plots the signals in the same manner as the FASTICA and FASTICAG
% programs do. All the signals are plotted in their own axis.
%
% Complot
% icaplot('complot', s1, n1, range, xrange, titlestr)
%
% The signals are plotted on the same axis. This is good for
% visualization of the shape of the signals. The scale of the signals
% has been altered so that they all fit nicely.
%
% Histogram
% icaplot('histogram', s1, n1, range, bins, style)
%
% The histogram of the signals is plotted. The number of bins can be
% specified with 'bins'-parameter. The style for the histograms can
% be either 'bar' (default) of 'line'.
%
% Scatter
% icaplot('scatter', s1, n1, s2, n2, range, titlestr, s1label,
% s2label, markerstr)
%
% A scatterplot is plotted so that the signal 1 is the 'X'-variable
% and the signal 2 is the 'Y'-variable. The 'markerstr' can be used
% to specify the maker used in the plot. The format for 'markerstr'
% is the same as for Matlab's PLOT.
%
% Compare
% icaplot('compare', s1, n1, s2, n2, range, xrange, titlestr,
% s1label, s2label)
%
% This for for comparing two signals. The main used in this context
% would probably be to see how well the separated ICA-signals explain
% the observed mixed signals. The s2 signals are first scaled with
% REGRESS function.
%
% Compare - Sum
% icaplot('sum', s1, n1, s2, n2, range, xrange, titlestr, s1label,
% s2label)
%
% The same as Compare, but this time the signals in s2 (specified by
% n2) are summed together.
%
% Compare - Sumerror
% icaplot('sumerror', s1, n1, s2, n2, range, xrange, titlestr,
% s1label, s2label)
%
% The same as Compare - Sum, but also the 'error' between the signal
% 1 and the summed IC's is plotted.
%
%
% More common parameters
% The signals to be plotted are in matrices s1 and s2. The n1 and n2
% are used to tell the index of the signal or signals to be plotted
% from s1 or s2. If n1 or n2 has a value of 0, then all the signals
% from corresponding matrix will be plotted. The values for n1 and n2
% can also be vectors (like: [1 3 4]) In some casee if there are more
% than 1 signal to be plotted from s1 or s2 then the plot will
% contain as many subplots as are needed.
%
% The range of the signals to be plotted can be limited with
% 'range'-parameter. It's value is a vector ( 10000:15000 ). If range
% is 0, then the whole range will be plotted.
%
% The 'xrange' is used to specify only the labels used on the
% x-axis. The value of 'xrange' is a vector containing the x-values
% for the plots or [start end] for begin and end of the range
% ( 10000:15000 or [10 15] ). If xrange is 0, then value of range
% will be used for x-labels.
%
% You can give a title for the plot with 'titlestr'. Also the
% 's1label' and 's2label' are used to give more meaningfull label for
% the signals.
%
% Lastly, you can omit some of the arguments from the and. You will
% have to give values for the signal matrices (s1, s2) and the
% indexes (n1, n2)
% @(#)$Id: icaplot.m,v 1.2 2003/04/05 14:23:58 jarmo Exp $
switch mode
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 'dispsig' is to replace the old DISPSIG
% '' & 'classic' are just another names - '' quite short one :-)
case {'', 'classic', 'dispsig'}
% icaplot(mode, s1, n1, range, xrange, titlestr)
if length(varargin) < 1, error('Not enough arguments.'); end
if length(varargin) < 5, titlestr = '';else titlestr = varargin{5}; end
if length(varargin) < 4, xrange = 0;else xrange = varargin{4}; end
if length(varargin) < 3, range = 0;else range = varargin{3}; end
if length(varargin) < 2, n1 = 0;else n1 = varargin{2}; end
s1 = varargin{1};
range=chkrange(range, s1);
xrange=chkxrange(xrange, range);
n1=chkn(n1, s1);
clf;
numSignals = size(n1, 2);
for i = 1:numSignals,
subplot(numSignals, 1, i);
plot(xrange, s1(n1(i), range));
end
subplot(numSignals,1, 1);
if (~isempty(titlestr))
title(titlestr);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 'complot'
% icaplot(mode, s1, n1, range, xrange, titlestr)
if length(varargin) < 1, error('Not enough arguments.'); end
if length(varargin) < 5, titlestr = '';else titlestr = varargin{5}; end
if length(varargin) < 4, xrange = 0;else xrange = varargin{4}; end
if length(varargin) < 3, range = 0;else range = varargin{3}; end
if length(varargin) < 2, n1 = 0;else n1 = varargin{2}; end
s1 = remmean(varargin{1});
range=chkrange(range, s1);
xrange=chkxrange(xrange, range);
n1=chkn(n1, s1);
for i = 1:size(n1, 2)
S1(i, :) = s1(n1(i), range);
end
alpha = mean(max(S1')-min(S1'));
for i = 1:size(n1,2)
S2(i,:) = S1(i,:) - alpha*(i-1)*ones(size(S1(1,:)));
end
plot(xrange, S2');
axis([min(xrange) max(xrange) min(min(S2)) max(max(S2)) ]);
set(gca,'YTick',(-size(S1,1)+1)*alpha:alpha:0);
set(gca,'YTicklabel',fliplr(n1));
if (~isempty(titlestr))
title(titlestr);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 'histogram'
% icaplot(mode, s1, n1, range, bins, style)
if length(varargin) < 1, error('Not enough arguments.'); end
if length(varargin) < 5, style = 'bar';else style = varargin{5}; end
if length(varargin) < 4, bins = 10;else bins = varargin{4}; end
if length(varargin) < 3, range = 0;else range = varargin{3}; end
if length(varargin) < 2, n1 = 0;else n1 = varargin{2}; end
s1 = varargin{1};
range = chkrange(range, s1);
n1 = chkn(n1, s1);
numSignals = size(n1, 2);
rows = floor(sqrt(numSignals));
columns = ceil(sqrt(numSignals));
while (rows * columns < numSignals)
columns = columns + 1;
end
switch style
case {'', 'bar'}
for i = 1:numSignals,
subplot(rows, columns, i);
hist(s1(n1(i), range), bins);
title(int2str(n1(i)));
drawnow;
end
case 'line'
for i = 1:numSignals,
subplot(rows, columns, i);
[Y, X]=hist(s1(n1(i), range), bins);
plot(X, Y);
title(int2str(n1(i)));
drawnow;
end
otherwise
fprintf('Unknown style.\n')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case 'scatter'
% icaplot(mode, s1, n1, s2, n2, range, titlestr, xlabelstr, ylabelstr, markerstr)
if length(varargin) < 4, error('Not enough arguments.'); end
if length(varargin) < 9, markerstr = '.';else markerstr = varargin{9}; end
if length(varargin) < 8, ylabelstr = 'Signal 2';else ylabelstr = varargin{8}; end
if length(varargin) < 7, xlabelstr = 'Signal 1';else xlabelstr = varargin{7}; end
if length(varargin) < 6, titlestr = '';else titlestr = varargin{6}; end
if length(varargin) < 5, range = 0;else range = varargin{5}; end
n2 = varargin{4};
s2 = varargin{3};
n1 = varargin{2};
s1 = varargin{1};
range = chkrange(range, s1);
n1 = chkn(n1, s1);
n2 = chkn(n2, s2);
rows = size(n1, 2);
columns = size(n2, 2);
for r = 1:rows
for c = 1:columns
subplot(rows, columns, (r-1)*columns + c);
plot(s1(n1(r), range),s2(n2(c), range),markerstr);
if (~isempty(titlestr))
title(titlestr);
end
if (rows*columns == 1)
xlabel(xlabelstr);
ylabel(ylabelstr);
else
xlabel([xlabelstr ' (' int2str(n1(r)) ')']);
ylabel([ylabelstr ' (' int2str(n2(c)) ')']);
end
drawnow;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
case {'compare', 'sum', 'sumerror'}
% icaplot(mode, s1, n1, s2, n2, range, xrange, titlestr, s1label, s2label)
if length(varargin) < 4, error('Not enough arguments.'); end
if length(varargin) < 9, s2label = 'IC';else s2label = varargin{9}; end
if length(varargin) < 8, s1label = 'Mix';else s1label = varargin{8}; end
if length(varargin) < 7, titlestr = '';else titlestr = varargin{7}; end
if length(varargin) < 6, xrange = 0;else xrange = varargin{6}; end
if length(varargin) < 5, range = 0;else range = varargin{5}; end
s1 = varargin{1};
n1 = varargin{2};
s2 = varargin{3};
n2 = varargin{4};
range = chkrange(range, s1);
xrange = chkxrange(xrange, range);
n1 = chkn(n1, s1);
n2 = chkn(n2, s2);
numSignals = size(n1, 2);
if (numSignals > 1)
externalLegend = 1;
else
externalLegend = 0;
end
rows = floor(sqrt(numSignals+externalLegend));
columns = ceil(sqrt(numSignals+externalLegend));
while (rows * columns < (numSignals+externalLegend))
columns = columns + 1;
end
clf;
for j = 1:numSignals
subplot(rows, columns, j);
switch mode
case 'compare'
plotcompare(s1, n1(j), s2,n2, range, xrange);
[legendtext,legendstyle]=legendcompare(n1(j),n2,s1label,s2label,externalLegend);
case 'sum'
plotsum(s1, n1(j), s2,n2, range, xrange);
[legendtext,legendstyle]=legendsum(n1(j),n2,s1label,s2label,externalLegend);
case 'sumerror'
plotsumerror(s1, n1(j), s2,n2, range, xrange);
[legendtext,legendstyle]=legendsumerror(n1(j),n2,s1label,s2label,externalLegend);
end
if externalLegend
title([titlestr ' (' s1label ' ' int2str(n1(j)) ')']);
else
legend(char(legendtext));
if (~isempty(titlestr))
title(titlestr);
end
end
end
if (externalLegend)
subplot(rows, columns, numSignals+1);
legendsize = size(legendtext, 2);
hold on;
for i=1:legendsize
plot([0 1],[legendsize-i legendsize-i], char(legendstyle(i)));
text(1.5, legendsize-i, char(legendtext(i)));
end
hold off;
axis([0 6 -1 legendsize]);
axis off;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function plotcompare(s1, n1, s2, n2, range, xrange);
style=getStyles;
K = regress(s1(n1,:)',s2');
plot(xrange, s1(n1,range), char(style(1)));
hold on
for i=1:size(n2,2)
plotstyle=char(style(i+1));
plot(xrange, K(n2(i))*s2(n2(i),range), plotstyle);
end
hold off
function [legendText, legendStyle]=legendcompare(n1, n2, s1l, s2l, externalLegend);
style=getStyles;
if (externalLegend)
legendText(1)={[s1l ' (see the titles)']};
else
legendText(1)={[s1l ' ', int2str(n1)]};
end
legendStyle(1)=style(1);
for i=1:size(n2, 2)
legendText(i+1) = {[s2l ' ' int2str(n2(i))]};
legendStyle(i+1) = style(i+1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function plotsum(s1, n1, s2, n2, range, xrange);
K = diag(regress(s1(n1,:)',s2'));
sigsum = sum(K(:,n2)*s2(n2,:));
plot(xrange, s1(n1, range),'k-', ...
xrange, sigsum(range), 'b-');
function [legendText, legendStyle]=legendsum(n1, n2, s1l, s2l, externalLegend);
if (externalLegend)
legendText(1)={[s1l ' (see the titles)']};
else
legendText(1)={[s1l ' ', int2str(n1)]};
end
legendText(2)={['Sum of ' s2l ': ', int2str(n2)]};
legendStyle={'k-';'b-'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function plotsumerror(s1, n1, s2, n2, range, xrange);
K = diag(regress(s1(n1,:)',s2'));
sigsum = sum(K(:,n2)*s2(n2,:));
plot(xrange, s1(n1, range),'k-', ...
xrange, sigsum(range), 'b-', ...
xrange, s1(n1, range)-sigsum(range), 'r-');
function [legendText, legendStyle]=legendsumerror(n1, n2, s1l, s2l, externalLegend);
if (externalLegend)
legendText(1)={[s1l ' (see the titles)']};
else
legendText(1)={[s1l ' ', int2str(n1)]};
end
legendText(2)={['Sum of ' s2l ': ', int2str(n2)]};
legendText(3)={'"Error"'};
legendStyle={'k-';'b-';'r-'};
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function style=getStyles;
color = {'k','r','g','b','m','c','y'};
line = {'-',':','-.','--'};
for i = 0:size(line,2)-1
for j = 1:size(color, 2)
style(j + i*size(color, 2)) = strcat(color(j), line(i+1));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function range=chkrange(r, s)
if r == 0
range = 1:size(s, 2);
else
range = r;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function xrange=chkxrange(xr,r);
if xr == 0
xrange = r;
elseif size(xr, 2) == 2
xrange = xr(1):(xr(2)-xr(1))/(size(r,2)-1):xr(2);
elseif size(xr, 2)~=size(r, 2)
error('Xrange and range have different sizes.');
else
xrange = xr;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function n=chkn(n,s)
if n == 0
n = 1:size(s, 1);
end