0001 function [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 if(nargin ~= 2)
0021 disp('2 input arguments are required')
0022 return
0023 end
0024
0025 i = 0;
0026 n = 0;
0027 ts = 0;
0028 fn = 0;
0029 ad = 0;
0030
0031 if(isempty(filename))
0032 [fname, pathname] = uigetfile('*.plx', 'Select a plx file');
0033 filename = strcat(pathname, fname);
0034 end
0035
0036 fid = fopen(filename, 'r');
0037 if(fid == -1)
0038 disp('cannot open file');
0039 return
0040 end
0041
0042
0043 fseek(fid, 0, 'eof');
0044 fsize = ftell(fid);
0045 fseek(fid, 0, 'bof');
0046
0047
0048 disp(strcat('file = ', filename));
0049
0050
0051 header = fread(fid, 64, 'int32');
0052 freq = header(35);
0053 ndsp = header(36);
0054 nevents = header(37);
0055 nslow = header(38);
0056 npw = header(39);
0057 npr = header(40);
0058 tscounts = fread(fid, [5, 130], 'int32');
0059 wfcounts = fread(fid, [5, 130], 'int32');
0060 evcounts = fread(fid, [1, 512], 'int32');
0061
0062
0063 count = 0;
0064 if evcounts(301+ch) > 0
0065 count = evcounts(301+ch);
0066 ad = 1:count;
0067 end
0068
0069
0070 fseek(fid, 1020*ndsp + 296*nevents, 'cof');
0071
0072
0073 adheader = fread(fid, 74, 'int32');
0074 adfreq = adheader(10);
0075
0076
0077 fseek(fid, 296*(nslow-1), 'cof');
0078
0079 record = 0;
0080
0081 wf = zeros(1, npw);
0082 adpos = 1;
0083
0084 while feof(fid) == 0
0085 type = fread(fid, 1, 'int16');
0086 upperbyte = fread(fid, 1, 'int16');
0087 timestamp = fread(fid, 1, 'int32');
0088 channel = fread(fid, 1, 'int16');
0089 unit = fread(fid, 1, 'int16');
0090 nwf = fread(fid, 1, 'int16');
0091 nwords = fread(fid, 1, 'int16');
0092 if nwords > 0
0093 wf = fread(fid, [1 nwords], 'int16');
0094 end
0095 if nwords > 0
0096 if type == 5
0097 if channel == ch
0098 i = i + 1;
0099 n = n + nwords;
0100 ts(i) = timestamp/freq;
0101 fn(i) = nwords;
0102 if count > 0
0103 if adpos+nwords-1 <= count
0104 ad(adpos:adpos+nwords-1) = wf(1:nwords);
0105 adpos = adpos + nwords;
0106 else
0107 for i=1:nwords
0108 ad(adpos) = wf(i); adpos = adpos + 1;
0109 end
0110 end
0111 else
0112 ad = [ad wf(1, 1:nwords)];
0113 end
0114 end
0115 end
0116 end
0117
0118 record = record + 1;
0119 if mod(record, 1000) == 0
0120 disp(sprintf('records %d points %d (%.1f%%)', record, n, 100*ftell(fid)/fsize));
0121 end
0122
0123 if feof(fid) == 1
0124 break
0125 end
0126
0127 end
0128
0129 if adpos-1 < count
0130 ad = ad(1:adpos-1);
0131 end
0132
0133 disp(strcat('number of data points = ', num2str(n)));
0134
0135 fclose(fid);