PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/MatlabCode/branches/Greg's Branch/Slave/PlexonDataTransfer/Samples/ReadingPLXandDDTfiles/plx_ad.m

http://horwitzlab.googlecode.com/
MATLAB | 135 lines | 91 code | 21 blank | 23 comment | 12 complexity | cdf7b83dcb3ae2f4189687251b1ac093 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. function [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)
  2. % plx_ad(filename, channel): Read a/d data from a .plx file
  3. %
  4. % [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)
  5. %
  6. % INPUT:
  7. % filename - if empty string, will use File Open dialog
  8. % channel - 0 - based channel number
  9. %
  10. % a/d data come in fragments. Each fragment has a timestamp
  11. % and a number of a/d data points. The timestamp corresponds to
  12. % the time of recording of the first a/d value in this fragment.
  13. % All the data values stored in the vector ad.
  14. % OUTPUT:
  15. % n - total number of data points
  16. % ts - array of fragment timestamps (one timestamp for fragment, in seconds)
  17. % fn - number of data points in each fragment
  18. % ad - array of raw a/d values
  19. if(nargin ~= 2)
  20. disp('2 input arguments are required')
  21. return
  22. end
  23. i = 0;
  24. n = 0;
  25. ts = 0;
  26. fn = 0;
  27. ad = 0;
  28. if(length(filename) == 0)
  29. [fname, pathname] = uigetfile('*.plx', 'Select a plx file');
  30. filename = strcat(pathname, fname);
  31. end
  32. fid = fopen(filename, 'r');
  33. if(fid == -1)
  34. disp('cannot open file');
  35. return
  36. end
  37. % calculate file size
  38. fseek(fid, 0, 'eof');
  39. fsize = ftell(fid);
  40. fseek(fid, 0, 'bof');
  41. disp(strcat('file = ', filename));
  42. % read file header
  43. header = fread(fid, 64, 'int32');
  44. freq = header(35); % frequency
  45. ndsp = header(36); % number of dsp channels
  46. nevents = header(37); % number of external events
  47. nslow = header(38); % number of slow channels
  48. npw = header(39); % number of points in wave
  49. npr = header(40); % number of points before threshold
  50. tscounts = fread(fid, [5, 130], 'int32');
  51. wfcounts = fread(fid, [5, 130], 'int32');
  52. evcounts = fread(fid, [1, 512], 'int32');
  53. % A/D counts are stored in evcounts (301, 302, etc.)
  54. count = 0;
  55. if evcounts(301+ch) > 0
  56. count = evcounts(301+ch);
  57. ad = [1:count];
  58. end
  59. % skip DSP and Event headers
  60. fseek(fid, 1020*ndsp + 296*nevents, 'cof');
  61. % read one A/D header and get the frequency
  62. adheader = fread(fid, 74, 'int32');
  63. adfreq = adheader(10);
  64. % skip all other a/d headers
  65. fseek(fid, 296*(nslow-1), 'cof');
  66. record = 0;
  67. wf = zeros(1, npw);
  68. adpos = 1;
  69. while feof(fid) == 0
  70. type = fread(fid, 1, 'int16');
  71. upperbyte = fread(fid, 1, 'int16');
  72. timestamp = fread(fid, 1, 'int32');
  73. channel = fread(fid, 1, 'int16');
  74. unit = fread(fid, 1, 'int16');
  75. nwf = fread(fid, 1, 'int16');
  76. nwords = fread(fid, 1, 'int16');
  77. if nwords > 0
  78. wf = fread(fid, [1 nwords], 'int16');
  79. end
  80. if nwords > 0
  81. if type == 5
  82. if channel == ch
  83. i = i + 1;
  84. n = n + nwords;
  85. ts(i) = timestamp/freq;
  86. fn(i) = nwords;
  87. if count > 0
  88. if adpos+nwords-1 <= count
  89. ad(adpos:adpos+nwords-1) = wf(1:nwords);
  90. adpos = adpos + nwords;
  91. else
  92. for i=1:nwords
  93. ad(adpos) = wf(i); adpos = adpos + 1;
  94. end
  95. end
  96. else
  97. ad = [ad wf(1, 1:nwords)];
  98. end
  99. end
  100. end
  101. end
  102. record = record + 1;
  103. if mod(record, 1000) == 0
  104. disp(sprintf('records %d points %d (%.1f%%)', record, n, 100*ftell(fid)/fsize));
  105. end
  106. if feof(fid) == 1
  107. break
  108. end
  109. end
  110. if adpos-1 < count
  111. ad = ad(1:adpos-1);
  112. end
  113. disp(strcat('number of data points = ', num2str(n)));
  114. fclose(fid);