-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathpspm_get_cnt.m
87 lines (77 loc) · 2.7 KB
/
pspm_get_cnt.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
function [sts, import, sourceinfo] = pspm_get_cnt(datafile, import)
% ● Description
% pspm_get_cnt is the main function for import of NeuroScan cnt files
% ● Format
% [sts, import, sourceinfo] = pspm_get_cnt(datafile, import);
% This function uses fieldtrip fileio functions
% ● Arguments
% datafile:
% ┌─────import:
% ├────.typeno:
% ├───.channel:
% ├────────.sr:
% ├──────.data:
% ├────.marker:
% └.markerinfo:
% ├───.value:
% └────.name:
% ● History
% Introduced in PsPM 3.0
% Written in 2008-2015 by Dominik R Bach (Wellcome Trust Centre for Neuroimaging)
% Maintained in 2022 by Teddy Chao
%% Initialise
global settings
if isempty(settings)
pspm_init;
end
sts = -1;
addpath(pspm_path('Import','fieldtrip','fileio'));
sourceinfo = [];
% get external file, using fieldtrip
% -------------------------------------------------------------------------
% data storage is assumed to be 16 bit by default, see also
% http://fieldtrip.fcdonders.nl/faq/i_have_problems_reading_in_neuroscan_.cnt_files._how_can_i_fix_this
if isfield(import{1}, 'bit') && import{1}.bit == 32
headerformat = 'ns_cnt32';
else
headerformat = 'ns_cnt16';
end;
hdr = ft_read_header(datafile, 'headerformat', headerformat);
indata = ft_read_data(datafile, 'headerformat', headerformat, 'dataformat', headerformat);
try mrk = ft_read_event(datafile, 'headerformat', headerformat, 'dataformat', headerformat, 'eventformat', headerformat); catch, mrk = []; end;
% extract individual channels
% -------------------------------------------------------------------------
for k = 1:numel(import)
if strcmpi(settings.channeltypes(import{k}.typeno).data, 'wave')
if import{k}.channel > 0
channel = import{k}.channel;
else
channel = pspm_find_channel(hdr.label, import{k}.type);
if channel < 1, return; end;
end;
sourceinfo.channel{k, 1} = sprintf('Channel %02.0f: %s', channel, hdr.label{channel});
% sample rate ---
import{k}.sr = hdr.Fs;
% get data ---
import{k}.data = indata(channel, :);
else % event channels
% time unit
import{k}.sr = 1./hdr.Fs;
if ~isempty(mrk)
import{k}.data = [mrk(:).sample];
import{k}.marker = 'timestamps';
import{k}.markerinfo.value = [mrk(:).value];
import{k}.markerinfo.name = {mrk(:).type};
else
import{k}.data = [];
import{k}.markerinfo.value = [];
import{k}.markerinfo.name = [];
end;
end;
end;
% clear path and return
% -------------------------------------------------------------------------
rmpath(pspm_path('Import','fieldtrip','fileio'));
%% return values
sts = 1;
return