Coding error - tf matrix remains empty

Hi all,

I have run into an issue with my MATLAB code. I feel a bit silly as it seems like a problem I should be able to fix easily. I am wondering if someone would be able to point me in the right direction. The code runs without any issues, but it does not appear to actually be doing anything (i.e. it does not generate any data). I am grateful for any suggestions/hints! thank you

for reference I would like to run this code on each subject so I can later do group-level analyses

%baseline selection
baseline_window = [ -400 -100];
baseidx = reshape( dsearchn(EEG.times’,baseline_window(:)), [],2);

% frequency parameters
min_freq = 3;
max_freq = 30;
num_frex = 40;
frex = linspace(min_freq,max_freq,num_frex);

% other wavelet parameters
range_cycles = [ 4 10 ];

s = logspace(log10(range_cycles(1)),log10(range_cycles(end)),num_frex) ./ (2pifrex);
wavtime = -2:1/EEG.srate:2;
half_wave = (length(wavtime)-1)/2;

% FFT parameters
nWave = length(wavtime);
nData = EEG.pnts * EEG.trials;
nConv = nWave + nData - 1;

% initialize output time-frequency data
tf = zeros(size(baseidx,1), EEG.nbchan, length(frex),EEG.pnts);

% loop over channels
for chani=1:EEG.nbchan

alldata = reshape( EEG.data(strcmpi(chani,{EEG.chanlocs.labels}),:,:) ,1,[]);
dataX   = fft( alldata ,nConv );

% loop over frequencies
for fi=1:length(frex)

 % create wavelet and get its FFT
 wavelet  = exp(2*1i*pi*frex(fi).*wavtime) .* exp(-wavtime.^2./(2*s(fi)^2));
 waveletX = fft(wavelet,nConv);
 waveletX = waveletX ./ max(waveletX);
 
% convolution
as = ifft(waveletX .* dataX);
as = as(half_wave+1:end-half_wave);

% and reshape back to time X trials
as = reshape( as, EEG.pnts, EEG.trials);

% compute power and average over trials
tf(1,chani,fi,:) = mean( abs(as).^2 ,2);

end
end

Hi mg4. In these situations, you need to trace back each individual step. Check the wavelets, check the FFT of the wavelet, check the spectrum of the data. You’ll notice that this is all zeros, because variable alldata is empty. That happens because of the line for getting the data from each channel. Your code is searching through for the channel label 1, 2, 3, … 64. But channels don’t have those labels. Those are the channel indices. So the line should look like this:
alldata = reshape( EEG.data(chani,:,:) ,1,[]);

Thank you so much! Not sure how I missed that…

I also had another quick question - I’m interested in looking at frequency band power in certain clusters of a dense array electrodes (ex. frontal electrodes). Does it make sense to calculate the power at each electrode and then take the average? Or is there a better way to go about it?

You have a few options here:

  1. Compute power at all electrodes and show topographical maps so people can qualitatively interpret the distribution.
  2. Average across electrodes, as you wrote. You just need to make sure to select electrodes in a way that doesn’t bias the result. In other words, you shouldn’t pick the electrodes that have the strongest power, but instead pick electrodes based on their location or some other criterion (e.g., previous findings).
  3. Statistically pool the data across electrodes by some spatial filtering method, like ICA, GED, or PCA.

Great, thanks again!