-
Notifications
You must be signed in to change notification settings - Fork 5
/
calcftspec.m
52 lines (40 loc) · 1.26 KB
/
calcftspec.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
function calcftspec(basename)
loadpaths
load freqlist.mat
EEG = pop_loadset([filepath basename '.set']);
chanlocs = EEG.chanlocs;
load(sprintf('sortedlocs_%d.mat',length(chanlocs)));
EEG = convertoft(EEG);
cfg = [];
cfg.output = 'pow';
cfg.method = 'mtmfft';
cfg.foilim = [0.5 45];
% cfg.taper = 'rectwin';
cfg.taper = 'dpss';
cfg.tapsmofrq = 0.3;
cfg.pad='nextpow2';
EEG = ft_freqanalysis(cfg,EEG);
spectra = EEG.powspctrm;
freqs = EEG.freq;
[sortedchan,sortidx] = sort({chanlocs.labels});
if ~strcmp(chanlist,cell2mat(sortedchan))
error('Channel names do not match!');
end
spectra = spectra(sortidx,:);
chanlocs = chanlocs(sortidx);
bpower = zeros(size(freqlist,1),length(chanlocs));
for f = 1:size(freqlist,1)
[~, bstart] = min(abs(freqs-freqlist(f,1)));
[~, bstop] = min(abs(freqs-freqlist(f,2)));
[~,peakindex] = max(mean(spectra(:,bstart:bstop),1),[],2);
bpower(f,:) = spectra(:,bstart+peakindex-1);
end
for c = 1:size(bpower,2)
bpower(:,c) = bpower(:,c)./sum(bpower(:,c));
end
savefile = sprintf('%s%s_mohawk.mat',filepath,basename);
if exist(savefile,'file')
save(savefile, 'chanlocs', 'freqs', 'spectra', 'freqlist', 'bpower', '-append');
else
save(savefile, 'chanlocs', 'freqs', 'spectra', 'freqlist', 'bpower');
end