Saving an array of complex numbers within a loop

2 visualizaciones (últimos 30 días)
David Schwartzman
David Schwartzman el 13 de Ag. de 2019
Respondida: Guillaume el 13 de Ag. de 2019
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 on
fprintf('\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');

Respuesta aceptada

Guillaume
Guillaume el 13 de Ag. de 2019
Note: you should extract your constants definition out of the loops. Also, there's no point in having two names for the same constant. Considering you're never using L anywhere other than to copy it in NFFT, just call it NFFT to start with.
The simplest (but not necessarily the most efficient) is to store all your ffts in a 2D cell array:
%before the loops:
allffts = cell(numel(subject), numel(condition)); %preallocation
for i = 1:numel(subject) %prefer numel to length
for j = 1:numel(condition)
%... loop code calculating the fft Y (very poor variable name by the way, especially considering you also have a lowercase y variable!)
allffts{i, j} = Y;
end
end
%calculate the average of the ffts:
meanfft = mean(vertcat(allffts{:}), 1);
%do whatever plotting you need to do

Más respuestas (0)

Categorías

Más información sobre Parametric Spectral Estimation en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by