-
Notifications
You must be signed in to change notification settings - Fork 2
/
erfosc_freq.m
52 lines (46 loc) · 1.76 KB
/
erfosc_freq.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 [freq_onset, freq_shift, P] = erfosc_freq(data_onset, data_shift, latency, subject, foi, smo)
% Frequency analysis using fieldtrip mtmfft
%
% INPUT
% data_onset (struct): fieldtrip data structure, time locked to stimulus
% onset.
% data_shift (struct): fieldtrip data structure, time locked to stimulus
% change.
% latency: scalar or string, can be 'all', 'prestim', 'poststim', or
% [beg end], specify time range in seconds.
% subject (int): subject ID, ranging from 1 to 33, excluding 10.
% foi: vector 1 x numfoi, frequencies of interest (default: subject-
% specific gamma peak frequency)
% smo: number, the amount of spectral smoothing through multi-tapering.
% Note that 4 Hz smoothing means plus-minus 4 Hz, i.e. a 8 Hz
% smoothing box. (default: subject specific gamma bandwidth)
%
% OUTPUT
% freq_onset: frequency estimate, timing relative to stimulus onset
% freq_shift: frequency estimate, timing relative to stimulus change
% P: projection matrix to go from fourier to power
if nargin<5 || isempty(foi)
foi = [1 1].*subject.gammapeak(end);
end
if nargin<6 || isempty(smo)
smo = diff(subject.gammaband(end,:))./2;
end
cfg = [];
cfg.latency = latency;
data_onset = ft_selectdata(cfg, data_onset);
data_shift = ft_selectdata(cfg, data_shift);
cfg = [];
cfg.method = 'mtmfft';
cfg.output = 'fourier';
cfg.foilim = foi;
cfg.tapsmofrq = smo;
cfg.pad = 1;
freq_onset = ft_freqanalysis(cfg, data_onset);
freq_shift = ft_freqanalysis(cfg, data_shift);
% projection matrix to get from fourier to power
nrpt = numel(freq_onset.cumtapcnt);
ntap = freq_onset.cumtapcnt(1);
ix = reshape(repmat(1:nrpt,[ntap 1]),[],1);
iy = 1:(nrpt*ntap);
iz = ones(nrpt*ntap,1)./ntap;
P = sparse(iy,ix,iz,nrpt*ntap,nrpt);