forked from BlackrockNeurotech/NPMK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openCCF.m
571 lines (495 loc) · 15.7 KB
/
openCCF.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
function [infoPackets, version] = openCCF(filename)
% openCCF
%
% This script reads a .ccf file and outputs it in a structure.
%
% filename: Name of the file to be opened. If the fname is omitted
% the user will be prompted to select a file.
% DEFAULT: Will open Open File UI.
%
% Channels 1-128 are neural channels
% Channels 129-144 are external analog inputs
% Channels 145-148 are external analog outpus
% Channels
%
% Kian Torab
% kian@blackrockmicro.com
% Blackrock Microsystems
% Version 2.1.0.0
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Version History
%
% 1.0.0.0:
% - Initial release.
%
% 1.2.3.0:
% - Minor bug fix that led to a crash in certain cases.
%
% 1.2.4.0:
% - Minor bug fix regarding passing a filename variable to the function.
%
% 2.0.0.0:
% - Implemented XML CCF file format.
%
% 2.1.0.0:
% - Fixed a bug in loading nTrode groups with a base of 0.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
infoPackets.openCCFVersion = '2.1.0.0';
%% Parameters
% Which versions of .ccf's are supported
supportedVersions = {'3.6', '3.7', '3.8', '3.9'};
% Number of cbPKT_CHANINFO packets to read in the file
nValidPKT_CHANINFO = 160;
if( nargin ~= 1 )
[filename, pathname] = getFile('*.ccf');
if ~filename
disp('No file was selected.');
return;
end
fullfilename = [pathname filename];
elseif (nargin == 1)
fullfilename = filename;
else
if strcmpi(filename, 'ver')
disp(['Version is ' infoPackets.openCCFVersion]);
infoPackets = [];
return;
end
end
%% Open file
[fid, message] = fopen( fullfilename, 'rb', 'l');
if( fid == -1 ),
warning(['Unable to open file: ' fullfilename '. Error message ' message]);
return;
end;
%% Read header incl. version
head = fread(fid, 16, '*char*1' )';
if strcmp(head(1:5), 'cbCCF')
version = deblank(strtrim(head(6:end))); % Strip possible leading space and trailing nulls
elseif strcmp(head(1:5), '<?xml')
version = '3.9';
else
warning('Not a well-formed .ccf file');
fclose(fid);
return;
end
% Save version info in the structure.
infoPackets.Version = version;
% Verify to see if the file version is supported
if ~ismember(version, supportedVersions)
warning(sprintf('Unsupported file version: %s', version));
fclose(fid);
return;
end
if strcmpi(version, '3.9')
infoPackets = parseCCF(fullfilename);
for nTrodeIDX = 1:length(infoPackets.Children(7).Children)
for trodeIDX = 1:length(infoPackets.Children(7).Children(nTrodeIDX).Children(9).Children)
tempTrodes(trodeIDX) = ...
str2double(infoPackets.Children(7).Children(nTrodeIDX).Children(9).Children(trodeIDX).Children.Data) + 1;
end
if ~all(tempTrodes == 1)
infoPackets.NTrodeInfo.NTrodeID(nTrodeIDX) = nTrodeIDX;
infoPackets.NTrodeInfo.NTrodeMembers{nTrodeIDX} = tempTrodes;
end
end
return;
end
%% Read packets
for p = 1:nValidPKT_CHANINFO
if p == 128
% disp('w');
end
infoPackets.ChanInfo(p) = readInfoPacket(fid, version);
end
infoPackets.AdaptiveFilterInfo = readAdapFilterPacket(fid);
infoPackets.SortingInfo = readSortingPacket(fid);
infoPackets.SystemInfo = []; %readSystemInfoPacket(fid);
infoPackets.NTrodeInfo = readNTrodeInfoPacket(infoPackets);
%% Close file
fclose(fid);
function p = readInfoPacket(fid, version)
% Read a cbPKT_CHANINFO packet
p = [];
cbLEN_STR_LABEL = 16;
cbMAXUNITS = 5;
cbMAXHOOPS = 4;
p.SystemClockTimestamp = fread(fid, 1, '*uint32');
p.Child = fread(fid, 1, '*uint16');
p.AnaInPacketType = fread(fid, 1, '*uint8');
p.SystemDataLength = fread(fid, 1, '*uint8');
p.AnaInChannelID = fread(fid, 1, '*uint32');
p.AnaInChannelProcessor = fread(fid, 1, '*uint32');
p.AnaInBankID = fread(fid, 1, '*uint32');
p.AnaInPinID = fread(fid, 1, '*uint32');
h = fread(fid, 1, '*uint32');
p.CapabilitiesGeneralChannel = setFlagsCBCHAN(h);
h = fread(fid, 1, '*uint32');
p.CapabilitiesDigOutChannel = setFlagsCBDOUT(h);
h = fread(fid, 1, '*uint32');
p.CapabilitiesDigInChannel = setFlagsCBDINP(h);
h = fread(fid, 1, '*uint32');
p.CapabilitiesAnaOutChannel = setFlagsCBAOUT(h);
h = fread(fid, 1, '*uint32');
p.CapabilitiesAnaInChannel = setFlagsCBAINP(h);
p.CapabilitiesSpikeProcessing = fread(fid, 1, '*uint32');
p.InputPhysicalChannelScalingInfo = readCbScaling(fid);
p.InputPhysicalChannelFilterDefinition = readFiltDesc(fid);
p.OutputPhysicalChannelScalingInfo = readCbScaling(fid);
p.OutputPhysicalChannelFilterDefinition = readFiltDesc(fid);
p.ChannelLabel = fread(fid, cbLEN_STR_LABEL, '*char*1')';
p.ChannelUserFlags = fread(fid, 1, '*uint32');
p.ChannelPosition = fread(fid, 4, '*int32')';
p.InputPhysicalChannelScalingInfoCustom = readCbScaling(fid);
p.OuputPhysicalChannelScalingInfoCustom = readCbScaling(fid);
p.doutopts = fread(fid, 1, '*uint32');
p.dinpopts = fread(fid, 1, '*uint32');
p.aoutopts = fread(fid, 1, '*uint32');
p.eopchar = fread(fid, 1, '*uint32');
% Below two lines are actually part of a union, not sure how to figure out
% which option is actually being used
p.AnaInMonitorChannelAddress = fread(fid, 1, '*uint32');
p.AnaInOutputValue = fread(fid, 1, '*int32');
p.AnaInOptions = fread(fid, 1, '*uint32');
p.AnaInLNCAdaptationRate = fread(fid, 1, '*uint32');
p.AnaInContinuousStreamFilterID = fread(fid, 1, '*uint32');
p.AnalogInputContinuousStreamSampleGroup = fread(fid, 1, '*uint32');
p.AnaInContinuousStreamDisplayFactorMin = fread(fid, 1, '*int32');
p.AnaInContinuousStreamDisplayFactorMax = fread(fid, 1, '*int32');
p.AnaInSpikeStreamFilterID = fread(fid, 1, '*uint32');
p.AnaInSpikeStreamDisplayFactorMax = fread(fid, 1, '*int32');
p.AnaInLNCDisplayFactorMax = fread(fid, 1, '*int32');
p.AnaInSpikeProcessingOptions = fread(fid, 1, '*uint32');
p.AnaInSpikeStreamThresholdLevel = fread(fid, 1, '*int32');
p.AnaInSpikeStreamThresholdLimit = fread(fid, 1, '*int32');
p.AnaInNTrodeGroupID = fread(fid, 1, '*uint32');
p.AnaInAmplitudeRejectionValuePositive = fread(fid, 1, '*int16');
p.AnaInAmplitudeRejectionValueNegative = fread(fid, 1, '*int16');
p.AnaInDigitalReferencingChannel = fread(fid, 1, '*uint32');
p.AnaInManualMappingUnit = readCbManualUnitMappings(fid, cbMAXUNITS, version);
p.AnaInSpikeHoopsSorting = readCbHoops(fid, cbMAXUNITS, cbMAXHOOPS);
function p = readAdapFilterPacket(fid)
p = [];
p.Time = fread(fid, 1, '*uint32');
p.Child = fread(fid, 1, '*uint16');
p.Type = fread(fid, 1, '*uint8');
p.DataLength = fread(fid, 1, '*uint8');
p.Chan = fread(fid, 1, '*uint32');
p.Mode = fread(fid, 1, '*uint32');
p.LearningRate = fread(fid, 1, '*float32');
p.RefChan1 = fread(fid, 1, '*uint32');
p.RefChan2 = fread(fid, 1, '*uint32');
function p = readSortingPacket(fid)
p = [];
p.Time = fread(fid, 1, '*uint32');
p.Child = fread(fid, 1, '*uint16');
p.Type = fread(fid, 1, '*uint8');
p.DataLength = fread(fid, 1, '*uint8');
p.NumMaxSimultChans = fread(fid, 1, '*uint32');
p.RefractoryPeriodInSamples = fread(fid, 1, '*uint32');
function p = readSystemInfoPacket(fid)
p = [];
function p = readNTrodeInfoPacket(infoPackets)
p = [];
NTrodeGroups = unique([infoPackets.ChanInfo.AnaInNTrodeGroupID]); NTrodeGroups(1) = [];
NTrodeGroupsCount = length(NTrodeGroups);
for ntrodeIDX = 1:NTrodeGroupsCount
p.NTrodeID(ntrodeIDX) = NTrodeGroups(ntrodeIDX);
p.NTrodeMembers{ntrodeIDX} = find([infoPackets.ChanInfo.AnaInNTrodeGroupID] == ntrodeIDX);
end
%% Sub-struct readers
function sc = readCbScaling(fid)
% Read a cbSCALING struct
cbLEN_STR_UNIT = 8;
sc.AnalogChannelScaleMin = fread(fid, 1, '*int16');
sc.AnalogChannelScaleMax = fread(fid, 1, '*int16');
sc.AnalogChannelScaleMin = fread(fid, 1, '*int32');
sc.AnalogChannelScaleMax = fread(fid, 1, '*int32');
sc.AnalogChannelGain = fread(fid, 1, '*int32');
sc.AnalogChannelUnit = fread(fid, cbLEN_STR_UNIT, '*char*1')';
function fd = readFiltDesc(fid)
% Read a cbFILTDESC struct
cbLEN_STR_FILT_LABEL = 16;
fd.FilterLabel = fread(fid, cbLEN_STR_FILT_LABEL, '*char*1')';
fd.FilterHighPassFrequency = fread(fid, 1, '*uint32');
fd.FilterHighPassOrder = fread(fid, 1, '*uint32');
fd.FilterHighPassType = fread(fid, 1, '*uint32');
fd.FilterLowPassFrequency = fread(fid, 1, '*uint32');
fd.FilterLowPassOrder = fread(fid, 1, '*uint32');
fd.FilterLowPassType = fread(fid, 1, '*uint32');
function ums = readCbManualUnitMappings(fid, maxUnits, version)
% Loop to read all the cbMANUALUNITMAPPING packets. Note that this requires
% knowing the file version: v3.6 is different from 3.7 and 3.8
% Loop growing is inefficient, but it's just not that much data.
if strcmp(version, '3.6')
for i = 1:maxUnits
ums(i) = readCbManualUnitMapping3_6(fid);
end
else
for i = 1:maxUnits
ums(i) = readCbManualUnitMapping(fid);
end
end
function um = readCbManualUnitMapping(fid)
% Read a cbMANUALUNITMAPPING packet for a v3.7 or 3.8 file
um.nOverride = fread(fid, 1, '*int16');
um.afOrigin = fread(fid, 3, '*int16')';
um.afShape = fread(fid, [3 3], '*int16')';
um.aPhi = fread(fid, 1, '*int16')';
um.bValid = fread(fid, 1, '*uint32')';
function um = readCbManualUnitMapping3_6(fid)
% Read a cbMANUALUNITMAPPING packet for a v3.6 file
um.nOverride = fread(fid, 1, '*uint32');
um.afOrigin = fread(fid, 3, '*float32')';
um.afShape = fread(fid, [3 3], '*float32')';
um.aPhi = fread(fid, 1, '*float32')';
um.bValid = fread(fid, 1, '*uint32')';
function hs = readCbHoops(fid, maxUnits, maxHoops)
% Loop to read the cbHOOP packets
% Loop growing is inefficient, but it's just not that much data.
for u = 1:maxUnits
for h = 1:maxHoops
hs(u, h) = readCbHoop(fid);
end
end
function h = readCbHoop(fid)
% Read a cbHOOP packet
h.valid = fread(fid, 1, '*uint16')';
h.time = fread(fid, 1, '*int16')';
h.min = fread(fid, 1, '*int16')';
h.max = fread(fid, 1, '*int16')';
function h = setFlagsCBCHAN(flags)
binaryValue = dec2bin(flags,12);
% define constants
h.ChanExists = 0; % Channel id is allocated
h.ChanConnected = 0; % Channel is connected and mapped and ready to use
h.ChanIsolated = 0; % Channel is electrically isolated
h.ChanType = '';
if strcmpi(binaryValue(1), '1')
h.ChanType = 'DigitalOut';
end
if strcmpi(binaryValue(2), '1')
h.ChanType = 'DigitalInput';
end
if strcmpi(binaryValue(3), '1')
h.ChanType = 'AnalogOutput';
end
if strcmpi(binaryValue(4), '1')
h.ChanType = 'AnalogInput';
end
if strcmpi(binaryValue(10), '1')
h.ChanIsolated = 1;
end
if strcmpi(binaryValue(11), '1')
h.ChanConnected = 1;
end
if strcmpi(binaryValue(12), '1')
h.ChanExists = 1;
end
function h = setFlagsCBDOUT(flags)
binaryValue = dec2bin(flags,30);
h.BaudRate = 0;
h.NumberOfBits = 0;
h.CanBeManuallyConfigured = 0;
h.TrackMostRecentlySelected = 0;
h.CanOutputFrequency = 0;
h.ChanMonitorUnit0 = 0;
h.ChanMonitorUnit1 = 0;
h.ChanMonitorUnit2 = 0;
h.ChanMonitorUnit3 = 0;
h.ChanMonitorUnit4 = 0;
h.ChanMonitorUnit5 = 0;
h.ChanMonitorUnitAll = 0;
if strcmpi(binaryValue(1:6), '111111')
h.ChanMonitorUnitAll = 1;
else
if strcmpi(binaryValue(1), '1')
h.ChanMonitorUnit5 = 1;
end
if strcmpi(binaryValue(2), '1')
h.ChanMonitorUnit4 = 1;
end
if strcmpi(binaryValue(3), '1')
h.ChanMonitorUnit3 = 1;
end
if strcmpi(binaryValue(4), '1')
h.ChanMonitorUnit2 = 1;
end
if strcmpi(binaryValue(5), '1')
h.ChanMonitorUnit1 = 1;
end
if strcmpi(binaryValue(6), '1')
h.ChanMonitorUnit0 = 1;
end
end
if strcmpi(binaryValue(12), '1')
h.CanOutputFrequency = 1;
end
if strcmpi(binaryValue(13), '1')
h.TrackMostRecentlySelected = 1;
end
if strcmpi(binaryValue(18), '1')
h.CanBeManuallyConfigured = 1;
end
if strcmpi(binaryValue(19), '1')
h.NumberOfBits = 32;
end
if strcmpi(binaryValue(20), '1')
h.NumberOfBits = 16;
end
if strcmpi(binaryValue(21), '1')
h.NumberOfBits = 8;
end
if strcmpi(binaryValue(22), '1')
h.NumberOfBits = 1;
end
if strcmpi(binaryValue(25), '1')
h.BaudRate = 115200;
end
if strcmpi(binaryValue(26), '1')
h.BaudRate = 57600;
end
if strcmpi(binaryValue(27), '1')
h.BaudRate = 38400;
end
if strcmpi(binaryValue(28), '1')
h.BaudRate = 19200;
end
if strcmpi(binaryValue(29), '1')
h.BaudRate = 9600;
end
if strcmpi(binaryValue(30), '1')
h.BaudRate = 2400;
end
function h = setFlagsCBDINP(flags)
binaryValue = dec2bin(flags,22);
h.BaudRate = 0;
h.NumberOfBits = 0;
h.PortReadType = 0;
h.CapturePacketMethod = 0;
h.PortControlsEvent = 0;
if strcmpi(binaryValue(1), '1')
h.PortReadType = '8-bit strobe/8-bit falling edge';
end
if strcmpi(binaryValue(2), '1')
h.PortReadType = '8-bit strobe/8-bit rising edge';
end
if strcmpi(binaryValue(3), '1')
h.PortReadType = '8-bit strobe/8-bit any edge';
end
if strcmpi(binaryValue(4), '1')
h.PortReadType = '8-bit any bit falling edge';
end
if strcmpi(binaryValue(5), '1')
h.PortReadType = '8-bit any bit rising edge';
end
if strcmpi(binaryValue(6), '1')
h.PortControlsEvent = 1;
end
if strcmpi(binaryValue(7), '1')
h.CapturePacketMethod = 'Logic input';
end
if strcmpi(binaryValue(8), '1')
h.CapturePacketMethod = 'Character';
end
if strcmpi(binaryValue(9), '1')
h.CapturePacketMethod = 'Word strob';
end
if strcmpi(binaryValue(10), '1')
h.CapturePacketMethod = 'Any bit change';
end
if strcmpi(binaryValue(11), '1')
h.NumberOfBits = 32;
end
if strcmpi(binaryValue(12), '1')
h.NumberOfBits = 16;
end
if strcmpi(binaryValue(13), '1')
h.NumberOfBits = 8;
end
if strcmpi(binaryValue(14), '1')
h.NumberOfBits = 1;
end
if strcmpi(binaryValue(17), '1')
h.BaudRate = 115200;
end
if strcmpi(binaryValue(18), '1')
h.BaudRate = 57600;
end
if strcmpi(binaryValue(19), '1')
h.BaudRate = 38400;
end
if strcmpi(binaryValue(20), '1')
h.BaudRate = 19200;
end
if strcmpi(binaryValue(21), '1')
h.BaudRate = 9600;
end
if strcmpi(binaryValue(22), '1')
h.BaudRate = 2400;
end
function h = setFlagsCBAOUT(flags)
binaryValue = dec2bin(flags,22);
h.Audio = 0;
h.AnalogScale = 0;
h.ChanFunction = '';
if strcmpi(binaryValue(1), '1')
h.ChanFunction = 'Waveform generator';
end
if strcmpi(binaryValue(2), '1')
h.ChanFunction = 'Monitors spikes';
end
if strcmpi(binaryValue(3), '1')
h.ChanFunction = 'Monitors SMP';
end
if strcmpi(binaryValue(4), '1')
h.ChanFunction = 'Monitors LNC';
end
if strcmpi(binaryValue(5), '1')
h.ChanFunction = 'Monitors raw';
end
if strcmpi(binaryValue(6), '1')
h.AnalogScale = 'Waveform generator';
end
if strcmpi(binaryValue(7), '1')
h.AnalogScale = 'Waveform generator';
end
if strcmpi(binaryValue(8), '1')
h.AnalogScale = 'Waveform generator';
end
if strcmpi(binaryValue(9), '1')
h.Audio = 1;
end
function h = setFlagsCBAINP(flags)
binaryValue = dec2bin(flags,22);
h.Audio = 0;
h.AnalogScale = 0;
h.ChanFunction = '';
if strcmpi(binaryValue(1), '1')
h.ChanFunction = 'Waveform generator';
end
if strcmpi(binaryValue(2), '1')
h.ChanFunction = 'Monitors spikes';
end
if strcmpi(binaryValue(3), '1')
h.ChanFunction = 'Monitors SMP';
end
if strcmpi(binaryValue(4), '1')
h.ChanFunction = 'Monitors LNC';
end
if strcmpi(binaryValue(5), '1')
h.ChanFunction = 'Monitors raw';
end
if strcmpi(binaryValue(6), '1')
h.AnalogScale = 'Waveform generator';
end
if strcmpi(binaryValue(7), '1')
h.AnalogScale = 'Waveform generator';
end
if strcmpi(binaryValue(8), '1')
h.AnalogScale = 'Waveform generator';
end
if strcmpi(binaryValue(9), '1')
h.Audio = 1;
end