[422372]: / functions / miscfunc / compdsp.m

Download this file

274 lines (248 with data), 7.8 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
 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
% COMPDSP - Display standard info figures for a data decomposition
% Creates four figure windows showing: Component amplitudes,
% scalp maps, activations and activation spectra.
% Usage:
% >> compdsp(data,weights,locfile,[srate],[title],[compnums],[amps],[act]);
%
% Inputs:
% data = data matrix used to train the decomposition
% weights = the unmixing matrix (e.g., weights*sphere from RUNICA)
%
% Optional:
% locfile = 2-d electrode locations file (as in >> topoplot example)
% {default|[]: default locations file given in icadefs.m
% srate = sampling rate in Hz {default|[]: as in icadefs.m}
% title = optional figure title text
% {default|'': none}
% compnums = optional vector of component numbers to display
% {default|[] -> all}
% amps = all component amplitudes (from RUNICA)
% {default|[]->recompute}
% act = activations matrix (from RUNICA)
% {default|[]->recompute}
%
% Author: Scott Makeig, SCCN/INC/UCSD, La Jolla, 2000
% Copyright (C) 12/16/00 Scott Makeig, SCCN/INC/UCSD, scott@sccn.ucsd.edu
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
% 02-01-01 replaced STD with RMS -sm
% 02-10-01 made srate optional -sm
% 01-25-02 reformated help & license -ad
function compdsp(data,unmix,locfile,srate,titl,compnums,amps,act)
minHz = 2; % frequency display limits
maxHz = 40;
ACTS_PER_EEGPLOT = 32;
%
%%%%%%%%%%%%%%%%%%%%%% Read and test arguments %%%%%%%%%%%%%%%%%%%%%%%%
%
icadefs % read BACKCOLOR, DEFAULT_SRATE
if nargin<2
help compdsp
return
end
chans = size(data,1);
frames = size(data,2);
ncomps = size(unmix,1);
if ncomps < 1
error('Unmixing matrix must have at least one component');
end
if chans < 2
error('Data must have at least two channels');
end
if frames < 2
error('Data must have at least two frames');
end
if size(unmix,2) ~= chans
error('Sizes of unmix and data do not match');
end
if nargin<3
locfile=[];
end
if isempty(locfile)
locfile = DEFAULT_ELOC; % from icsdefs.m
end
if ~exist(locfile)
error('Cannot find electrode locations file');
end
if nargin<4
srate = 0; % from icadefs
end
if isempty(srate) || srate==0
srate = DEFAULT_SRATE; % from icadefs
end
if nargin<5
titl = ''; % default - no title text
end
if isempty(titl)
titl = '';
end
if nargin<6
compnums = 0; % default - all components
end
if isempty(compnums)
compnums = 0;
end
if nargin<7
amps = NaN; % default - recompute amps
end
if isempty(amps)
amps = NaN;
end
if ~isnan(amps) && length(amps) ~= ncomps
error('Supplied amps does not match the number of unmixed components');
end
if compnums(1) == 0
compnums = 1:ncomps;
end
if min(compnums) < 1
error('Compnums must be positive');
end
if min(compnums) > ncomps
error('Some compnum > number of components in unmix');
end
if nargin<8
act = NaN; % default - recompute act
end
%
%%%%%%%%%%%%%%%%%%%%%%%%%%% Create plots %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
if chans == ncomps
winv = inv(unmix);
elseif chans < ncomps
error('More components than channels?');
else
winv = pinv(unmix);
end
if isnan(act)
fprintf('Computing activations ...')
act = unmix(compnums,:)*data;
fprintf('\n');
elseif size(act,2) ~= frames
error('Supplied activations do not match data length');
elseif size(act,1) ~= ncomps && size(act,1) ~= length(compnums)
error('Number of supplied activations matrix does not match data or weights');
elseif size(act,1) == ncomps
act = act(compnums,:); % cut down matrix to desired components
end
%
%%%%%%%%%%%%%%%%%%%%% I. Plot component amps %%%%%%%%%%%%%%%%%%%%%%%%%%%
%
pos = [40,520,550,400]; figure('Position',pos);
if isnan(amps)
fprintf('Computing component rms amplitudes ');
amps = zeros(1,length(compnums));
for j=1:length(compnums)
amps(j) = rms(winv(:,compnums(j)))*rms(act(j,:)');
fprintf('.')
end
fprintf('\n');
else
amps = amps(compnums); % truncate passed amps to desired compnums
end
plot(compnums,amps,'k');
hold on
plot(compnums,amps,'r^');
xl=xlabel('Component numbers');
yl=ylabel('RMS Amplitudes');
tl=title([titl ' Amplitudes']);
ax = axis;
axis([min(compnums)-1 max(compnums)+1 0 ax(4)]);
%
%%%%%%%%%%%%%%%%%%%%%%%%% II. Plot component maps %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
pos = [40,40,550,400]; figure('Position',pos);
fprintf('Plotting component scalp maps ...') % compmaps() may make multiple figures
compmap(winv,locfile,compnums,[titl ' Scalp Maps'],0,compnums);
fprintf('\n');
%
%%%%%%%%%%%%%%%%%%%%%% III. EEGPLOT activations %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
if frames/srate < 10
dispsecs = ceil(frames/srate);
else
dispsecs = 10; % defaults - display 10s data per screen
end
range = 0.8*max(max(act')-min(act'));
stact=1;
lact=ACTS_PER_EEGPLOT;
if lact>size(act,1)
lact = size(act,1);
end
pos = [620,520,550,400]; figure('Position',pos);
while stact <= size(act,1)
% eegplot(data,srate,spacing,eloc_file,windowlength,title,posn)
eegplot(act(stact:lact,:),srate,range,compnums(stact:lact),...
dispsecs,[titl ' Activations'],pos);
pos = pos + [.02 .02 0 0];
stact = stact+ACTS_PER_EEGPLOT;
lact = lact +ACTS_PER_EEGPLOT;
if lact>size(act,1)
lact = size(act,1);
end
end
%
%%%%%%%%%%%%%%%%%%%%%%% IV. PLOTDATA spectra %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
pos = [620,40,550,400]; figure('Position',pos);
if frames > 2048
windw = 512;
elseif frames > 1024
windw = 256;
else
windw = 128;
end
fprintf('Computing component spectra ')
for j = 1:length(compnums)
% [Pxx,F] = PSD(X,NFFT,Fs,WINDOW,NOVERLAP)
[spec,freqs] = psd(act(j,:),1024,srate,windw,ceil(windw*0.5));
if ~exist('specs')
specs = zeros(length(compnums),length(freqs));
end
specs(j,:) = spec';
fprintf('.')
end
fprintf('\n');
specs = 10*log10(specs);
tmp = ceil(sqrt(length(compnums)));
tmp2 = ceil(length(compnums)/tmp);
for j=1:length(compnums)
sbplot(tmp2,tmp,j)
plot(freqs,specs(j,:))
set(gca,'box','off')
set(gca,'color',BACKCOLOR);
ax=axis;
axis([minHz,maxHz,ax(3),ax(4)]);
tl = title(int2str(compnums(j)));
end
xl=xlabel('Frequency (Hz)');
yl=ylabel('Power (dB)');
set(gca,'YAxisLocation','right');
txl=textsc([titl ' Activation Spectra'],'title');
axcopy % pop-up axes on mouse click
% plottopo(specs,[tmp2 tmp],0,[2,70 min(min(specs)) max(max(specs))],...
% [titl ' Activation Power Spectra']);
function rmsval = rms(column)
rmsval = sqrt(mean(column.*column)); % don't remove mean