-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathosl_hilbenv.m
119 lines (96 loc) · 3.45 KB
/
osl_hilbenv.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
function Denv = osl_hilbenv(S)
% Computes the Hilbert envelope of MEEG data
% Dnew = osl_hilbenv(S)
%
% S.D - MEEG object
% S.winsize - window size (seconds)
% S.prefix - filename prefix for new MEEG object
% S.freqbands - cell array of frequency bands to use [Hz]
% i.e. {[f1_low f1_high],[f2_low f2_high]}
% S.logtrans - apply log transform [0/1] (default 0)
% S.demean - remove mean from envelope (default 0)
% S.downsample- downsample envelope (default 1)
% S.remove_edge_effects- remove_edge_effects (default 1)
%
% Adam Baker 2014
% Check SPM File Specification:
try
if isa(S.D,'meeg')
D = S.D;
else
S.D = char(S.D);
[pathstr,filestr] = fileparts(S.D);
S.D = fullfile(pathstr,[filestr '.mat']); % force .mat suffix
D = spm_eeg_load(S.D);
end;
D.check;
catch
error('SPM file specification not recognised or incorrect');
end
S.prefix = ft_getopt(S,'prefix','h');
S.logtrans = ft_getopt(S,'logtrans',0);
S.demean = ft_getopt(S,'demean',0);
S.downsample = ft_getopt(S,'downsample',1);
S.remove_edge_effects = ft_getopt(S,'remove_edge_effects',1);
S.winsize = ft_getopt(S,'winsize');
if isempty(S.winsize)
S.winsize = 0; % No smoothing
end
S.freqbands = ft_getopt(S,'freqbands',{});
if ~iscell(S.freqbands)
error('S.freqbands must be a cell array');
end
% Set up new MEEG object to hold downsampled envelope
[~,t_env] = hilbenv(D.time,D.time,round(S.winsize*D.fsample),S.downsample,S.remove_edge_effects);
if numel(S.freqbands) < 2
% Create Nchannels x Nsamples x Ntrials object
Denv = clone(montage(D,'switch',0),prefix(D.fnamedat,S.prefix),[D.nchannels,length(t_env),D.ntrials]);
Denv = timeonset(Denv,t_env(1));
else
% Create Nchannels x Nfrequencies x Nsamples x Ntrials TF object
Denv = clone(montage(D,'switch',0),prefix(D.fnamedat,S.prefix),[D.nchannels,numel(S.freqbands),length(t_env),D.ntrials]);
Denv = timeonset(Denv,t_env(1));
end
if S.downsample
fsampleNew = 1./(diff(t_env([1,end]))/length(t_env));
else
fsampleNew = D.fsample;
end
Denv = fsample(Denv,fsampleNew);
% Loop over blocks and voxels and apply envelope averaging
blks = osl_memblocks(size(D),1,50*2^20);
ft_progress('init','etf')
for iblk = 1:size(blks,1)
ft_progress(iblk/size(blks,1));
for trl = 1:D.ntrials
if ~isempty(S.freqbands)
% Filter and compute envelope for each band
for f = 1:numel(S.freqbands)
dat_blk = bandpass(D(blks(iblk,1):blks(iblk,2),:,trl),S.freqbands{f},D.fsample);
env = hilbenv(dat_blk,D.time,round(S.winsize*D.fsample),S.downsample,S.remove_edge_effects);
env = permute(env,[1 3 2]);
if S.logtrans
env = log10(env);
end
if S.demean
env = bsxfun(@minus,env,mean(env,2));
end
Denv(blks(iblk,1):blks(iblk,2),f,:,trl) = env;
end
else
% Compute wideband envelope
dat_blk = D(blks(iblk,1):blks(iblk,2),:,trl);
env = hilbenv(dat_blk,D.time,round(S.winsize*D.fsample),S.downsample,S.remove_edge_effects);
if S.logtrans
env = log10(env);
end
if S.demean
env = bsxfun(@minus,env,mean(env,2));
end
Denv(blks(iblk,1):blks(iblk,2),:,trl) = env;
end
end
end
ft_progress('close');
Denv.save;
end