PageRenderTime 27ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/matlab/fieldtrip-20101107/fileio/private/read_plexon_nex.m

http://github.com/yuval-harpaz/ft_BIU
MATLAB | 192 lines | 113 code | 19 blank | 60 comment | 20 complexity | ddd5e1bafbd237ca874db87ef640f25f MD5 | raw file
Possible License(s): BSD-3-Clause, GPL-3.0, GPL-2.0, AGPL-1.0, LGPL-2.0
  1. function [varargout] = read_plexon_nex(filename, varargin)
  2. % READ_PLEXON_NEX reads header or data from a Plexon *.nex file, which
  3. % is a file containing action-potential (spike) timestamps and waveforms
  4. % (spike channels), event timestamps (event channels), and continuous
  5. % variable data (continuous A/D channels).
  6. %
  7. % LFP and spike waveform data that is returned by this function is
  8. % expressed in microVolt.
  9. %
  10. % Use as
  11. % [hdr] = read_plexon_nex(filename)
  12. % [dat] = read_plexon_nex(filename, ...)
  13. % [dat1, dat2, dat3, hdr] = read_plexon_nex(filename, ...)
  14. %
  15. % Optional arguments should be specified in key-value pairs and can be
  16. % header structure with header information
  17. % feedback 0 or 1
  18. % tsonly 0 or 1, read only the timestamps and not the waveforms
  19. % channel number, or list of numbers (that will result in multiple outputs)
  20. % begsample number (for continuous only)
  21. % endsample number (for continuous only)
  22. %
  23. % See also READ_PLEXON_PLX, READ_PLEXON_DDT
  24. % Copyright (C) 2007, Robert Oostenveld
  25. %
  26. % This file is part of FieldTrip, see http://www.ru.nl/neuroimaging/fieldtrip
  27. % for the documentation and details.
  28. %
  29. % FieldTrip is free software: you can redistribute it and/or modify
  30. % it under the terms of the GNU General Public License as published by
  31. % the Free Software Foundation, either version 3 of the License, or
  32. % (at your option) any later version.
  33. %
  34. % FieldTrip is distributed in the hope that it will be useful,
  35. % but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. % GNU General Public License for more details.
  38. %
  39. % You should have received a copy of the GNU General Public License
  40. % along with FieldTrip. If not, see <http://www.gnu.org/licenses/>.
  41. %
  42. % $Id: read_plexon_nex.m 945 2010-04-21 17:41:20Z roboos $
  43. % parse the optional input arguments
  44. hdr = keyval('header', varargin);
  45. channel = keyval('channel', varargin);
  46. feedback = keyval('feedback', varargin);
  47. tsonly = keyval('tsonly', varargin);
  48. begsample = keyval('begsample', varargin);
  49. endsample = keyval('endsample', varargin);
  50. % set the defaults
  51. if isempty(feedback)
  52. feedback=0;
  53. end
  54. if isempty(tsonly)
  55. tsonly=0;
  56. end
  57. if isempty(begsample)
  58. begsample=1;
  59. end
  60. if isempty(endsample)
  61. endsample=Inf;
  62. end
  63. % start with empty return values and empty data
  64. varargout = {};
  65. % read header info from file, use Matlabs for automatic byte-ordering
  66. fid = fopen(filename, 'r', 'ieee-le');
  67. fseek(fid, 0, 'eof');
  68. siz = ftell(fid);
  69. fseek(fid, 0, 'bof');
  70. if isempty(hdr)
  71. if feedback, fprintf('reading header from %s\n', filename); end
  72. % a NEX file consists of a file header, followed by a number of variable headers
  73. % sizeof(NexFileHeader) = 544
  74. % sizeof(NexVarHeader) = 208
  75. hdr.FileHeader = NexFileHeader(fid);
  76. if hdr.FileHeader.NumVars<1
  77. error('no channels present in file');
  78. end
  79. hdr.VarHeader = NexVarHeader(fid, hdr.FileHeader.NumVars);
  80. end
  81. for i=1:length(channel)
  82. chan = channel(i);
  83. vh = hdr.VarHeader(chan);
  84. clear buf
  85. fseek(fid, vh.DataOffset, 'bof');
  86. switch vh.Type
  87. case 0
  88. % Neurons, only timestamps
  89. buf.ts = fread(fid, [1 vh.Count], 'int32=>int32');
  90. case 1
  91. % Events, only timestamps
  92. buf.ts = fread(fid, [1 vh.Count], 'int32=>int32');
  93. case 2
  94. % Interval variables
  95. buf.begs = fread(fid, [1 vh.Count], 'int32=>int32');
  96. buf.ends = fread(fid, [1 vh.Count], 'int32=>int32');
  97. case 3
  98. % Waveform variables
  99. buf.ts = fread(fid, [1 vh.Count], 'int32=>int32');
  100. if ~tsonly
  101. buf.dat = fread(fid, [vh.NPointsWave vh.Count], 'int16');
  102. % convert the AD values to miliVolt, subsequently convert from miliVolt to microVolt
  103. buf.dat = buf.dat * (vh.ADtoMV * 1000);
  104. end
  105. case 4
  106. % Population vector
  107. error('population vectors are not supported');
  108. case 5
  109. % Continuously recorded variables
  110. buf.ts = fread(fid, [1 vh.Count], 'int32=>int32');
  111. buf.indx = fread(fid, [1 vh.Count], 'int32=>int32');
  112. if vh.Count>1 && (begsample~=1 || endsample~=inf)
  113. error('reading selected samples from multiple AD segments is not supported');
  114. end
  115. if ~tsonly
  116. numsample = min(endsample - begsample + 1, vh.NPointsWave);
  117. fseek(fid, (begsample-1)*2, 'cof');
  118. buf.dat = fread(fid, [1 numsample], 'int16');
  119. % convert the AD values to miliVolt, subsequently convert from miliVolt to microVolt
  120. buf.dat = buf.dat * (vh.ADtoMV * 1000);
  121. end
  122. case 6
  123. % Markers
  124. ts = fread(fid, [1 vh.Count], 'int32=>int32');
  125. for j=1:vh.NMarkers
  126. buf.MarkerNames{j,1} = fread(fid, [1 64], 'uint8=>char');
  127. for k=1:vh.Count
  128. buf.MarkerValues{j,k} = fread(fid, [1 vh.MarkerLength], 'uint8=>char');
  129. end
  130. end
  131. otherwise
  132. error('incorrect channel type');
  133. end % switch channel type
  134. % return the data of this channel
  135. varargout{i} = buf;
  136. end % for channel
  137. % always return the header as last
  138. varargout{end+1} = hdr;
  139. fclose(fid);
  140. return
  141. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  142. function hdr = NexFileHeader(fid);
  143. hdr.NexFileHeader = fread(fid,4,'uint8=>char')'; % string NEX1
  144. hdr.Version = fread(fid,1,'int32');
  145. hdr.Comment = fread(fid,256,'uint8=>char')';
  146. hdr.Frequency = fread(fid,1,'double'); % timestamped freq. - tics per second
  147. hdr.Beg = fread(fid,1,'int32'); % usually 0
  148. hdr.End = fread(fid,1,'int32'); % maximum timestamp + 1
  149. hdr.NumVars = fread(fid,1,'int32'); % number of variables in the first batch
  150. hdr.NextFileHeader = fread(fid,1,'int32'); % position of the next file header in the file, not implemented yet
  151. Padding = fread(fid,256,'uint8=>char')'; % future expansion
  152. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  153. function hdr = NexVarHeader(fid, numvar);
  154. for varlop=1:numvar
  155. hdr(varlop).Type = fread(fid,1,'int32'); % 0 - neuron, 1 event, 2- interval, 3 - waveform, 4 - pop. vector, 5 - continuously recorded
  156. hdr(varlop).Version = fread(fid,1,'int32'); % 100
  157. hdr(varlop).Name = fread(fid,64,'uint8=>char')'; % variable name
  158. hdr(varlop).DataOffset = fread(fid,1,'int32'); % where the data array for this variable is located in the file
  159. hdr(varlop).Count = fread(fid,1,'int32'); % number of events, intervals, waveforms or weights
  160. hdr(varlop).WireNumber = fread(fid,1,'int32'); % neuron only, not used now
  161. hdr(varlop).UnitNumber = fread(fid,1,'int32'); % neuron only, not used now
  162. hdr(varlop).Gain = fread(fid,1,'int32'); % neuron only, not used now
  163. hdr(varlop).Filter = fread(fid,1,'int32'); % neuron only, not used now
  164. hdr(varlop).XPos = fread(fid,1,'double'); % neuron only, electrode position in (0,100) range, used in 3D
  165. hdr(varlop).YPos = fread(fid,1,'double'); % neuron only, electrode position in (0,100) range, used in 3D
  166. hdr(varlop).WFrequency = fread(fid,1,'double'); % waveform and continuous vars only, w/f sampling frequency
  167. hdr(varlop).ADtoMV = fread(fid,1,'double'); % waveform continuous vars only, coeff. to convert from A/D values to Millivolts
  168. hdr(varlop).NPointsWave = fread(fid,1,'int32'); % waveform only, number of points in each wave
  169. hdr(varlop).NMarkers = fread(fid,1,'int32'); % how many values are associated with each marker
  170. hdr(varlop).MarkerLength = fread(fid,1,'int32'); % how many characters are in each marker value
  171. Padding = fread(fid,68,'uint8=>char')';
  172. end