ImaGIN_spm_eeg_rdata_spike2_mono.m
3.13 KB
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
function D = ImaGIN_spm_eeg_rdata_spike2_mono(S)
% Converts EEG data from CED Spike2 to SPM format (.mat/.dat)
% -=============================================================================
% This function is part of the ImaGIN software:
% https://f-tract.eu/
%
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE AUTHORS
% DO NOT ASSUME ANY LIABILITY OR RESPONSIBILITY FOR ITS USE IN ANY CONTEXT.
%
% Copyright (c) 2000-2018 Inserm U1216
% =============================================================================-
%
% Authors: Olivier David
%% ===== INPUTS =====
% Prepare SPM window
Finter = spm_figure('GetWin','Interactive');
spm_figure('Clear',Finter)
% Parse inputs
try
Fdata = S.Fdata;
catch
Fdata = spm_select(1, '\.smr$', 'Select Spike2 file');
end
try
FileOut = S.FileOut;
catch
FileOut = spm_input('Name of converted file', '+1', 's');
end
try
S.channel;
catch
S.channel = str2num(spm_input('Index of channels to read', '+1', 's'));
end
try
S.coarse;
catch
S.coarse = str2num(spm_input('Reading downsampling factor ', '+1', 's',1));
end
% Wait icon
spm('Pointer', 'Watch');
drawnow;
%% ===== READ FILE =====
% Read file header
fid = fopen(Fdata, 'r');
ChanList = SONChanList(fid);
fclose(fid);
% By default: read all channels
if isempty(S.channel)
S.channel = [ChanList.number];
end
% Read data
[Data, iGoodChannels, Evt] = ImaGIN_read_smr(Fdata, S.channel, S.coarse);
%% ===== SAVE SPM FILE =====
% Separate path and file name
[fPath, fBase] = fileparts(FileOut);
% D structure
D.type = 'continuous';
D.path = fPath;
D.fname = [fBase '.mat'];
D.Nchannels = size(Data.data,1);
D.Nsamples = length(Data.time);
D.Fsample = 1 ./ (Data.time(2) - Data.time(1));
D.time = Data.time;
% Make channel names unique
chNames = Data.name;
for i = 1:length(chNames)
if (nnz(strcmpi(chNames{i}, Data.name)) > 1)
chNames{i} = sprintf('%s%02d', chNames{i}, i);
end
end
% Channel names
for i = 1:length(chNames)
% Channels
D.channels(i).label = chNames{i};
% Sensors
D.sensors.eeg.label{i} = D.channels(i).label;
D.sensors.eeg.chantype{i} = 'eeg';
D.sensors.eeg.chanunit{i} = 'm';
D.sensors.eeg.chanpos(i,1:3) = [NaN, NaN, NaN];
D.sensors.eeg.elecpos(i,1:3) = [NaN, NaN, NaN];
end
D.sensors.eeg.unit = 'm';
% Trials
D.trials.label = 'Undefined';
D.trials.events = [];
D.trials.onset = 1 ./ D.Fsample;
% Physically initialise .dat file
D.data = file_array([FileOut '.dat'], [D.Nchannels, D.Nsamples], 'float32-le');
D.data(:,:) = full(Data.data);
% Convert to SPM MEEG object
Dmeeg = meeg(D);
% Add events
if ~isempty(Evt)
Dmeeg = events(Dmeeg, 1, Evt);
end
% Get full file
Dfile = fullfile(Dmeeg);
% Convert back to struct to be able to add the 'spike' field
D = struct(Dmeeg);
% Spikes
if isfield(Data,'spike')
D.spike = Data.spike;
end
% Save file
save(Dfile, 'D');
% Stop processing
spm('Pointer','Arrow');