-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathft_write_spike.m
167 lines (144 loc) · 5.81 KB
/
ft_write_spike.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
function ft_write_spike(filename, spike, varargin)
% FT_WRITE_SPIKE writes animal electrophysiology spike timestamps and/or waveforms
% to file
%
% Use as
% ft_write_spike(filename, spike, ...)
%
% Additional options should be specified in key-value pairs and can be
% 'dataformat' string, see below
% 'fsample' sampling frequency of the waveforms
% 'chanindx' index of selected channels
% 'TimeStampPerSample' number of timestamps per sample
%
% The supported dataformats are
% neuralynx_nse
% neuralynx_nts
% plexon_nex
% matlab
%
% See also FT_READ_SPIKE
% Copyright (C) 2007-2012, Robert Oostenveld
%
% This file is part of FieldTrip, see http://www.fieldtriptoolbox.org
% for the documentation and details.
%
% FieldTrip is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% FieldTrip is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
%
% $Id$
% get the options
dataformat = ft_getopt(varargin, 'dataformat');
fsample = ft_getopt(varargin, 'fsample');
chanindx = ft_getopt(varargin, 'chanindx');
TimeStampPerSample = ft_getopt(varargin, 'TimeStampPerSample'); % FIXME rename the option TimeStampPerSample to ftimestamp, c.f. fsample
% optionally select channels
if ~isempty(chanindx)
spike.label = spike.label(chanindx);
spike.waveform = spike.waveform(chanindx);
spike.timestamp = spike.timestamp(chanindx);
end
nchans = length(spike.label);
switch dataformat
case 'matlab'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plain MATLAB file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[path, file, ext] = fileparts(filename);
filename = fullfile(path, [file, '.mat']);
save(filename, 'spike');
case 'neuralynx_nts'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% single channel Neuralynx NTS file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[path, file, ext] = fileparts(filename);
filename = fullfile(path, [file, '.nts']);
if nchans>1
ft_error('only supported for single-channel data');
end
label = spike.label{1};
timestamp = spike.timestamp{1};
clear spike
nts = [];
nts.TimeStamp = uint64(timestamp);
% construct the file header
nts.hdr.CheetahRev = '4.23.0';
% nts.hdr.NLX_Base_Class_Type = 'SEScAcqEnt';
nts.hdr.NLX_Base_Class_Name = label;
nts.hdr.RecordSize = 8;
% write the NSE structure to file
write_neuralynx_nts(filename, nts);
if 0
% the following code snippet can be used for testing
nts2 = read_neuralynx_nts(filename, 1, inf);
end
case 'neuralynx_nse'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% single channel Neuralynx NSE file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[path, file, ext] = fileparts(filename);
filename = fullfile(path, [file, '.nse']);
if nchans>1
ft_error('only supported for single-channel data');
end
label = spike.label{1};
waveform = spike.waveform{1};
timestamp = spike.timestamp{1};
unit = spike.unit{1};
nrecords = length(timestamp);
clear spike
% cut the data into record-size pieces around the spike segments
nrecords = length(timestamp);
nse = [];
nse.CellNumber = unit;
nse.TimeStamp = uint64(timestamp);
nse.ScNumber = zeros(1,nrecords);
nse.Param = zeros(8,nrecords);
nse.dat = waveform;
% construct the file header
nse.hdr.CheetahRev = '4.23.0';
nse.hdr.NLX_Base_Class_Type = 'SEScAcqEnt';
nse.hdr.NLX_Base_Class_Name = label;
nse.hdr.RecordSize = 48 + size(waveform,1)*2; % depends on the number of samples in each waveform, normal is 112
nse.hdr.SamplingFrequency = fsample;
% write the NSE structure to file
write_neuralynx_nse(filename, nse);
if 0
% the following code snippet can be used for testing
nse2 = read_neuralynx_nse(filename, 1, inf);
end
case 'plexon_nex'
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plexon NEX file
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[path, file, ext] = fileparts(filename);
filename = fullfile(path, [file, '.nex']);
if nchans>1
ft_error('only supported for single-channel data');
end
nex = [];
nex.hdr.FileHeader.Frequency = TimeStampPerSample*fsample;
nex.hdr.VarHeader.Type = 3;
nex.hdr.VarHeader.Name = spike.label{1};
nex.hdr.VarHeader.WFrequency = fsample;
nex.var.ts = spike.timestamp{1};
nex.var.dat = spike.waveform{1};
write_plexon_nex(filename, nex);
if 0
% the following code snippet can be used for testing
nex2 = [];
[nex2.var, nex2.hdr] = read_plexon_nex(filename, 'channel', 1);
end
otherwise
ft_error('not implemented');
end % switch dataformat