plx_ad.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  2. "http://www.w3.org/TR/REC-html40/loose.dtd">
  3. <html>
  4. <head>
  5. <title>Description of plx_ad</title>
  6. <meta name="keywords" content="plx_ad">
  7. <meta name="description" content="plx_ad(filename, channel): Read a/d data from a .plx file">
  8. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  9. <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  10. <meta name="robots" content="index, follow">
  11. <link type="text/css" rel="stylesheet" href="../../../m2html.css">
  12. <script type="text/javascript">
  13. if (top.frames.length == 0) { top.location = "../../../index.html"; };
  14. </script>
  15. </head>
  16. <body>
  17. <a name="_top"></a>
  18. <!-- ../../menu.html chronux_2_10 --><!-- ../menu.html dataio --><!-- menu.html ReadingPLXandDDTfilesinMatlab -->
  19. <h1>plx_ad
  20. </h1>
  21. <h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  22. <div class="box"><strong>plx_ad(filename, channel): Read a/d data from a .plx file</strong></div>
  23. <h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  24. <div class="box"><strong>function [adfreq, n, ts, fn, ad] = plx_ad(filename, ch) </strong></div>
  25. <h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  26. <div class="fragment"><pre class="comment"> plx_ad(filename, channel): Read a/d data from a .plx file
  27. [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)
  28. INPUT:
  29. filename - if empty string, will use File Open dialog
  30. channel - 0 - based channel number
  31. a/d data come in fragments. Each fragment has a timestamp
  32. and a number of a/d data points. The timestamp corresponds to
  33. the time of recording of the first a/d value in this fragment.
  34. All the data values stored in the vector ad.
  35. OUTPUT:
  36. n - total number of data points
  37. ts - array of fragment timestamps (one timestamp for fragment, in seconds)
  38. fn - number of data points in each fragment
  39. ad - array of raw a/d values</pre></div>
  40. <!-- crossreference -->
  41. <h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  42. This function calls:
  43. <ul style="list-style-image:url(../../../matlabicon.gif)">
  44. </ul>
  45. This function is called by:
  46. <ul style="list-style-image:url(../../../matlabicon.gif)">
  47. </ul>
  48. <!-- crossreference -->
  49. <h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../../up.png"></a></h2>
  50. <div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)</a>
  51. 0002 <span class="comment">% plx_ad(filename, channel): Read a/d data from a .plx file</span>
  52. 0003 <span class="comment">%</span>
  53. 0004 <span class="comment">% [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)</span>
  54. 0005 <span class="comment">%</span>
  55. 0006 <span class="comment">% INPUT:</span>
  56. 0007 <span class="comment">% filename - if empty string, will use File Open dialog</span>
  57. 0008 <span class="comment">% channel - 0 - based channel number</span>
  58. 0009 <span class="comment">%</span>
  59. 0010 <span class="comment">% a/d data come in fragments. Each fragment has a timestamp</span>
  60. 0011 <span class="comment">% and a number of a/d data points. The timestamp corresponds to</span>
  61. 0012 <span class="comment">% the time of recording of the first a/d value in this fragment.</span>
  62. 0013 <span class="comment">% All the data values stored in the vector ad.</span>
  63. 0014 <span class="comment">% OUTPUT:</span>
  64. 0015 <span class="comment">% n - total number of data points</span>
  65. 0016 <span class="comment">% ts - array of fragment timestamps (one timestamp for fragment, in seconds)</span>
  66. 0017 <span class="comment">% fn - number of data points in each fragment</span>
  67. 0018 <span class="comment">% ad - array of raw a/d values</span>
  68. 0019
  69. 0020 <span class="keyword">if</span>(nargin ~= 2)
  70. 0021 disp(<span class="string">'2 input arguments are required'</span>)
  71. 0022 <span class="keyword">return</span>
  72. 0023 <span class="keyword">end</span>
  73. 0024
  74. 0025 i = 0;
  75. 0026 n = 0;
  76. 0027 ts = 0;
  77. 0028 fn = 0;
  78. 0029 ad = 0;
  79. 0030
  80. 0031 <span class="keyword">if</span>(isempty(filename))
  81. 0032 [fname, pathname] = uigetfile(<span class="string">'*.plx'</span>, <span class="string">'Select a plx file'</span>);
  82. 0033 filename = strcat(pathname, fname);
  83. 0034 <span class="keyword">end</span>
  84. 0035
  85. 0036 fid = fopen(filename, <span class="string">'r'</span>);
  86. 0037 <span class="keyword">if</span>(fid == -1)
  87. 0038 disp(<span class="string">'cannot open file'</span>);
  88. 0039 <span class="keyword">return</span>
  89. 0040 <span class="keyword">end</span>
  90. 0041
  91. 0042 <span class="comment">% calculate file size</span>
  92. 0043 fseek(fid, 0, <span class="string">'eof'</span>);
  93. 0044 fsize = ftell(fid);
  94. 0045 fseek(fid, 0, <span class="string">'bof'</span>);
  95. 0046
  96. 0047
  97. 0048 disp(strcat(<span class="string">'file = '</span>, filename));
  98. 0049
  99. 0050 <span class="comment">% read file header</span>
  100. 0051 header = fread(fid, 64, <span class="string">'int32'</span>);
  101. 0052 freq = header(35); <span class="comment">% frequency</span>
  102. 0053 ndsp = header(36); <span class="comment">% number of dsp channels</span>
  103. 0054 nevents = header(37); <span class="comment">% number of external events</span>
  104. 0055 nslow = header(38); <span class="comment">% number of slow channels</span>
  105. 0056 npw = header(39); <span class="comment">% number of points in wave</span>
  106. 0057 npr = header(40); <span class="comment">% number of points before threshold</span>
  107. 0058 tscounts = fread(fid, [5, 130], <span class="string">'int32'</span>);
  108. 0059 wfcounts = fread(fid, [5, 130], <span class="string">'int32'</span>);
  109. 0060 evcounts = fread(fid, [1, 512], <span class="string">'int32'</span>);
  110. 0061
  111. 0062 <span class="comment">% A/D counts are stored in evcounts (301, 302, etc.)</span>
  112. 0063 count = 0;
  113. 0064 <span class="keyword">if</span> evcounts(301+ch) &gt; 0
  114. 0065 count = evcounts(301+ch);
  115. 0066 ad = 1:count;
  116. 0067 <span class="keyword">end</span>
  117. 0068
  118. 0069 <span class="comment">% skip DSP and Event headers</span>
  119. 0070 fseek(fid, 1020*ndsp + 296*nevents, <span class="string">'cof'</span>);
  120. 0071
  121. 0072 <span class="comment">% read one A/D header and get the frequency</span>
  122. 0073 adheader = fread(fid, 74, <span class="string">'int32'</span>);
  123. 0074 adfreq = adheader(10);
  124. 0075
  125. 0076 <span class="comment">% skip all other a/d headers</span>
  126. 0077 fseek(fid, 296*(nslow-1), <span class="string">'cof'</span>);
  127. 0078
  128. 0079 record = 0;
  129. 0080
  130. 0081 wf = zeros(1, npw);
  131. 0082 adpos = 1;
  132. 0083
  133. 0084 <span class="keyword">while</span> feof(fid) == 0
  134. 0085 type = fread(fid, 1, <span class="string">'int16'</span>);
  135. 0086 upperbyte = fread(fid, 1, <span class="string">'int16'</span>);
  136. 0087 timestamp = fread(fid, 1, <span class="string">'int32'</span>);
  137. 0088 channel = fread(fid, 1, <span class="string">'int16'</span>);
  138. 0089 unit = fread(fid, 1, <span class="string">'int16'</span>);
  139. 0090 nwf = fread(fid, 1, <span class="string">'int16'</span>);
  140. 0091 nwords = fread(fid, 1, <span class="string">'int16'</span>);
  141. 0092 <span class="keyword">if</span> nwords &gt; 0
  142. 0093 wf = fread(fid, [1 nwords], <span class="string">'int16'</span>);
  143. 0094 <span class="keyword">end</span>
  144. 0095 <span class="keyword">if</span> nwords &gt; 0
  145. 0096 <span class="keyword">if</span> type == 5
  146. 0097 <span class="keyword">if</span> channel == ch
  147. 0098 i = i + 1;
  148. 0099 n = n + nwords;
  149. 0100 ts(i) = timestamp/freq;
  150. 0101 fn(i) = nwords;
  151. 0102 <span class="keyword">if</span> count &gt; 0
  152. 0103 <span class="keyword">if</span> adpos+nwords-1 &lt;= count
  153. 0104 ad(adpos:adpos+nwords-1) = wf(1:nwords);
  154. 0105 adpos = adpos + nwords;
  155. 0106 <span class="keyword">else</span>
  156. 0107 <span class="keyword">for</span> i=1:nwords
  157. 0108 ad(adpos) = wf(i); adpos = adpos + 1;
  158. 0109 <span class="keyword">end</span>
  159. 0110 <span class="keyword">end</span>
  160. 0111 <span class="keyword">else</span>
  161. 0112 ad = [ad wf(1, 1:nwords)];
  162. 0113 <span class="keyword">end</span>
  163. 0114 <span class="keyword">end</span>
  164. 0115 <span class="keyword">end</span>
  165. 0116 <span class="keyword">end</span>
  166. 0117
  167. 0118 record = record + 1;
  168. 0119 <span class="keyword">if</span> mod(record, 1000) == 0
  169. 0120 disp(sprintf(<span class="string">'records %d points %d (%.1f%%)'</span>, record, n, 100*ftell(fid)/fsize));
  170. 0121 <span class="keyword">end</span>
  171. 0122
  172. 0123 <span class="keyword">if</span> feof(fid) == 1
  173. 0124 <span class="keyword">break</span>
  174. 0125 <span class="keyword">end</span>
  175. 0126
  176. 0127 <span class="keyword">end</span>
  177. 0128
  178. 0129 <span class="keyword">if</span> adpos-1 &lt; count
  179. 0130 ad = ad(1:adpos-1);
  180. 0131 <span class="keyword">end</span>
  181. 0132
  182. 0133 disp(strcat(<span class="string">'number of data points = '</span>, num2str(n)));
  183. 0134
  184. 0135 fclose(fid);</pre></div>
  185. <hr><address>Generated on Fri 12-Aug-2011 11:36:15 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
  186. </body>
  187. </html>