# Inter-trial phase clustering

Hi Dear Mike
Thank you so much for your informative videos.
In this study, 32-Channel EEG signals with the sampling frequency of 256 Hz, were recorded from 18 healthy participants with no history of neurological disorders. The participants were in the range of 24 to 25 years old. The task was a BCI task with an interaction error (System does not respond as expected) 30% of the time.

I have a dataset (48,35,307)=(trials,channels,time points).

I wanted to apply the code " Convolution in many trials", but I get the wrong answer. Would you please help what did I do wrong?

I also wanted to attach the dataset and code, but I couldn’t.
please tell me how to do it.
sincerely yours
Sara Sepehri Shakib

Hi Sara. If you’re following the book code, then it might be a matter of data organization. I use eeglab’s convention for organizing the data matrices, which is channels,time,trials. So the solution might be as simple as re-structuring your data to channels,time,trials instead of trials,channels,time.

Thank you so much for your answer. would you please tell me how can I re-structuring my data?
because my data is not like yours, it’s just a matrix and I can’t access each of the dimensions separately.

Easiest is to do this in a for-loop. Something like:

new = zeros(nchans,ntime,ntrials);
for triali=1:ntrials
new(:,:,triali) = old(triali,:,:);
end

I changed matrix dimension by this>>
VC_1_new=permute(VC_1,[2 3 1]);
but I get the wrong answer again

VC_1_new=permute(VC_1,[2 3 1]);
chan2use=17;% 17= Cz
pnts=size(VC_1_new,2);
trials=size(VC_1_new,3);
data = squeeze(VC_1_new( chan2use, :,:));
EEG_times=[1:307]/256-0.2;

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

% other wavelet parameters
range_cycles = [ 4 10 ];
srate=256;
s = logspace(log10(range_cycles(1)),log10(range_cycles(end)),num_frex) ./ (2pifrex);
wavtime = -2:1/srate:2;
half_wave = (length(wavtime)-1)/2;

tic; % start matlab timer

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

% initialize output time-frequency data
tf = zeros(length(frex),pnts,trials);

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

% create wavelet and get its FFT
% the wavelet doesn't change on each trial...
wavelet  = exp(2*1i*pi*frex(fi).*wavtime) .* exp(-wavtime.^2./(2*s(fi)^2));
waveletX = fft(wavelet,nConv);
waveletX = waveletX ./ max(waveletX);

% now loop over trials...
for triali=1:trials
dataX=fft(squeeze(VC_1_new(chan2use,:,triali)),nConv);

% dataX = fft(squeeze(EEG.data(strcmpi(channel2use,{EEG.chanlocs.labels}),:,triali)), nConv);

% run convolution
as = ifft(waveletX .* dataX);
as = as(half_wave+1:end-half_wave);

% put power data into big matrix
tf(fi,:,triali) = abs(as).^2;
end

end

tfTrialAve = mean(tf,3);

computationTime = toc; % end matlab timer for this cell

% plot results
figure(1), clf
contourf(EEG_times,frex,tfTrialAve,40,‘linecolor’,‘none’)
set(gca,‘clim’,[0 5],‘ydir’,‘normal’,‘xlim’,[-300 1000])

The plot is completely empty except for a vertical dashed line, which I don’t see in the code that you’re drawing. Did you intend to use hold on? Otherwise, I guess the TF map is all NaN’s, which means that there is something wrong with the data.