0001 function [factor_mag, factor_ph, factor_eig] = eeg_factor(avgcsdm,good_chan,freq,output); 0002 ch_pair_indices 0003 if size(avgcsdm,2) == 8385 0004 NChan = 129; 0005 else 0006 error('number of channels is unknown'); 0007 end; 0008 0009 if any(good_chan) > 129 0010 error('channel out of range'); 0011 end; 0012 0013 if freq > size(avgcsdm,1) 0014 error('frequency out of range'); 0015 end; 0016 csdm_data = avgcsdm; 0017 csdm_mat = zeros(NChan,NChan); 0018 csdm = zeros(size(good_chan,2),size(good_chan,2)); 0019 for ichan =1:NChan 0020 for jchan = ichan:NChan; 0021 csdm_mat(ichan,jchan) = csdm_data(freq,chpair(ichan,jchan)); 0022 csdm_mat(jchan,ichan) = conj(csdm_mat(ichan,jchan)); 0023 end; 0024 end; 0025 0026 csdm = csdm_mat(good_chan,good_chan); 0027 0028 [vec_csd, val_csd] = eig(csdm,'nobalance'); 0029 val_csd = real(val_csd); 0030 factor_eig = diag(val_csd); 0031 vec_csd = vec_csd'; 0032 factor_mag = zeros(size(vec_csd,1),129); 0033 factor_mag(:,good_chan) = abs(vec_csd); 0034 phase_data = vec_csd; 0035 if output == 'cos' 0036 phase_data = real(phase_data)./abs(phase_data); 0037 else 0038 real_sign = sign(real(phase_data)); 0039 phase_data = angle(phase_data); 0040 ang_sign = sign(phase_data); 0041 for iph = 1:size(phase_data,1) 0042 for jph = size(phase_data,2) 0043 if real_sign(iph,jph) < 0 0044 phase_data(iph,jph) = phase_data(iph,jph) + pi*real_sign(iph,jph)*ang_sign(iph,jph); 0045 end; 0046 end; 0047 end; 0048 0049 0050 0051 end 0052 phase_data(find(isnan(phase_data))) = zeros(size(find(isnan(phase_data)))); 0053 0054 if ~isreal(phase_data) 0055 error('Phase matrix is not real') 0056 end 0057 0058 factor_ph = zeros(size(phase_data,1),129); 0059 factor_ph(:,good_chan) = phase_data; 0060 0061 0062