# plx_ad

## PURPOSE

plx_ad(filename, channel): Read a/d data from a .plx file

## SYNOPSIS

function [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)

## DESCRIPTION

     plx_ad(filename, channel): Read a/d data from a .plx file

     [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)

     INPUT:
       filename - if empty string, will use File Open dialog
       channel - 0 - based channel number

               a/d data come in fragments. Each fragment has a timestamp
               and a number of a/d data points. The timestamp corresponds to
               the time of recording of the first a/d value in this fragment.
               All the data values stored in the vector ad.
     OUTPUT:
       n - total number of data points
       ts - array of fragment timestamps (one timestamp for fragment, in seconds)
       fn - number of data points in each fragment
       ad - array of raw a/d values

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)
    0002 % plx_ad(filename, channel): Read a/d data from a .plx file
    0003 %
    0004 % [adfreq, n, ts, fn, ad] = plx_ad(filename, ch)
    0005 %
    0006 % INPUT:
    0007 %   filename - if empty string, will use File Open dialog
    0008 %   channel - 0 - based channel number
    0009 %
    0010 %           a/d data come in fragments. Each fragment has a timestamp
    0011 %           and a number of a/d data points. The timestamp corresponds to
    0012 %           the time of recording of the first a/d value in this fragment.
    0013 %           All the data values stored in the vector ad.
    0014 % OUTPUT:
    0015 %   n - total number of data points
    0016 %   ts - array of fragment timestamps (one timestamp for fragment, in seconds)
    0017 %   fn - number of data points in each fragment
    0018 %   ad - array of raw a/d values
    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 % calculate file size
    0043 fseek(fid, 0, 'eof');
    0044 fsize = ftell(fid);
    0045 fseek(fid, 0, 'bof');
    0046
    0047
    0048 disp(strcat('file = ', filename));
    0049
    0050 % read file header
    0051 header = fread(fid, 64, 'int32');
    0052 freq = header(35);  % frequency
    0053 ndsp = header(36);  % number of dsp channels
    0054 nevents = header(37); % number of external events
    0055 nslow = header(38);  % number of slow channels
    0056 npw = header(39);  % number of points in wave
    0057 npr = header(40);  % number of points before threshold
    0058 tscounts = fread(fid, [5, 130], 'int32');
    0059 wfcounts = fread(fid, [5, 130], 'int32');
    0060 evcounts = fread(fid, [1, 512], 'int32');
    0061
    0062 % A/D counts are stored in evcounts (301, 302, etc.)
    0063 count = 0;
    0064 if evcounts(301+ch) > 0
    0065     count = evcounts(301+ch);
    0066     ad = 1:count;
    0067 end
    0068
    0069 % skip DSP and Event headers
    0070 fseek(fid, 1020*ndsp + 296*nevents, 'cof');
    0071
    0072 % read one A/D header and get the frequency
    0073 adheader = fread(fid, 74, 'int32');
    0074 adfreq = adheader(10);
    0075
    0076 % skip all other a/d headers
    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);