Hi,
I have written the following script that computes an FFT on some EEG data and plots and saves various conditions for each subject
My question is how to save the FFT data for each subject then average them all together and produce a 'Grand Average' FFT plot of all subjects?
Many thanks for your help
%Subject info, folder needs to be same name
subject = {'02', '03', '04','05','06','07','08','09'}; condition = {'pre_low_lum_CleanICA', '40_low_lum_CleanICA', 'post_low_lum_CleanICA','pre_high_lum_CleanICA', '40_high_lum_CleanICA', 'post_high_lum_CleanICA'};%Axis windows
axtimes = [1 45 0 1.2]; figure;for i = 1:length(subject); for j = 1:length(condition);%% use sprintf to create file path for each file
% subjectpath = sprintf('%s\\%s\\%s.set', pathAll, subject{i}, condition{j});
% subjectpath_save = strrep(subjectpath, '.cnt', '.set');
%Load the file from prepocessed .SET file
fname = sprintf('%s_%s.set',subject{i}, condition{j}); datapath = sprintf('%s%s',homedir, subject{i}) fprintf('\n\n\n**** %s : Loading ****\n\n\n', fname); EEG = pop_loadset('filename',fname,'filepath',datapath);%% ----------------------------- FFT Parameters -----------------------------
L = 307200;%x= EEG.pnts-307200
%specifry channel here 31 = Oz CHECK CHANNEL NUMBER IS CORRECT
y = EEG.data(31, :);Fs = EEG.srate;%FFT
% Reshape subplot
index = reshape(1:6, 2, 3).'; subplot(3, 2, index(j)); NFFT = L; Y = fft(y,NFFT)/L; f = Fs/2*linspace(0,1,NFFT/2+1); plot(f,2*abs(Y(1:NFFT/2+1))); axis([axtimes]) ; xlabel('Frequency (Hz)') ylabel('|Y(f)|') cond = condition{j}(1:end-9); title(sprintf('FFT: Subject %s, Condition = %s', subject{i}, cond),'interpreter', 'none'); hold onfprintf('\n\n\n**** %s: FINISHED ****\n\n\n', fname); end%Save current figure
fname = sprintf('%sFFT_%s.png',savepath, subject{i});saveas(gcf,fname);% Close current figure and run for next subject
close all end fprintf('\n\n\n**** Script FINISHED ****\n\n\n');
Best Answer