-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpspm_remove_epochs.m
82 lines (81 loc) · 2.64 KB
/
pspm_remove_epochs.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
function [sts] = pspm_remove_epochs(datafile, channel, epochfile, options)
% ● Description
% pspm_remove_epochs sets epochs of data to NaN
% ● Format
% [sts] = pspm_remove_epochs(datafile, channel, epochfile, options)
% ● Arguments
% datafile: a filename or a cell of filenames
% channel: defines which channels should be affected by epoch removal. This
% argument is passed to pspm_load_data(). Therefore, valid values
% are defined therein.
% epochfile: a filename which defines the epoch to be set to NaN. The epochs
% must be in seconds. This parameter is passed to pspm_get_timing().
% timeunits: timeunit of the epochfile.
% ┌─options: [struct]
% └.channel_action:
% ['add'/'replace'] Defines whether the new channels should be
% added or the corresponding channel should be replaced.
% (Default: 'add')
% ● History
% Introduced in PsPM 4.0
% Written in 2016 by Tobias Moser (University of Zurich)
global settings
if isempty(settings)
pspm_init;
end
sts = -1;
% input checks for options only as these are not directly passed to
% other functions.
if nargin < 3
warning('ID:invalid_input', 'Not enough input arguments');
return;
end
if ~exist('options', 'var')
options = struct();
end
options = pspm_options(options, 'remove_epochs');
if options.invalid
return
end
[lsts, ~, data] = pspm_load_data(datafile, channel);
if lsts == -1
warning('ID:invalid_input', 'Could not load data properly.');
return;
end
[lsts, ep] = pspm_get_timing('epochs', epochfile, 'seconds');
if lsts == -1
warning('ID:invalid_input', 'Could not load epochs properly.');
return;
end
n_ep = size(ep, 1);
n_data = numel(data);
for i_data = 1:n_data
channel = data{i_data};
sr = channel.header.sr;
channeltype = channel.header.chantype;
for i_ep = 1:n_ep
epoch = ep(i_ep, :);
if strcmpi(settings.channeltypes(strcmpi({settings.channeltypes.type}, ...
channeltype)).data, 'events')
% remove markers within the period
channel.data(channel.data >= epoch(1) & channel.data <= epoch(2)) = [];
else
% find start and stop sample and ensure they're not
% going over the edges
smp_start = max(1, round(sr*epoch(1)));
smp_stop = min(numel(channel.data), round(sr*epoch(2)));
% set period to NaN
channel.data(smp_start:smp_stop) = NaN;
end
% write back to data struct
data{i_data}.data = channel.data;
end
end
% save data to file
[lsts] = pspm_write_channel(datafile, data, options.channel_action);
if ~lsts
warning('ID:invalid_input', 'Could not write channel to file.');
return;
end
sts = 1;
return