# plx_waves

## PURPOSE

plx_waves(filename, channel, unit): Read waveform data from a .plx file

## SYNOPSIS

function [n, npw, ts, wave] = plx_waves(filename, ch, u)

## DESCRIPTION

     plx_waves(filename, channel, unit): Read waveform data from a .plx file

     [n, npw, ts, wave] = plx_waves(filename, channel, unit)

     INPUT:
       filename - if empty string, will use File Open dialog
       channel - 1-based channel number
       unit  - unit number (0- invalid, 1-4 valid)
     OUTPUT:
       n - number of waveforms
       npw - number of points in each waveform
       ts - array of timestamps (in seconds)
       wave - array of waveforms [npw, n], raw a/d values

## CROSS-REFERENCE INFORMATION

This function calls:

This function is called by:

## SOURCE CODE

    0001 function [n, npw, ts, wave] = plx_waves(filename, ch, u)
    0002 % plx_waves(filename, channel, unit): Read waveform data from a .plx file
    0003 %
    0004 % [n, npw, ts, wave] = plx_waves(filename, channel, unit)
    0005 %
    0006 % INPUT:
    0007 %   filename - if empty string, will use File Open dialog
    0008 %   channel - 1-based channel number
    0009 %   unit  - unit number (0- invalid, 1-4 valid)
    0010 % OUTPUT:
    0011 %   n - number of waveforms
    0012 %   npw - number of points in each waveform
    0013 %   ts - array of timestamps (in seconds)
    0014 %   wave - array of waveforms [npw, n], raw a/d values
    0015
    0016 if(nargin ~= 3)
    0017    disp('3 input arguments are required')
    0018    return
    0019 end
    0020
    0021 n = 0;
    0022 npw = 0;
    0023 ts = 0;
    0024 wave = 0;
    0025
    0026 if(isempty(filename))
    0027    [fname, pathname] = uigetfile('*.plx', 'Select a plx file');
    0028     filename = strcat(pathname, fname);
    0029 end
    0030
    0031 fid = fopen(filename, 'r');
    0032 if(fid == -1)
    0033     disp('cannot open file');
    0034    return
    0035 end
    0036
    0037 disp(strcat('file = ', filename));
    0038
    0039 % read file header
    0040 header = fread(fid, 64, 'int32');
    0041 freq = header(35);  % frequency
    0042 ndsp = header(36);  % number of dsp channels
    0043 nevents = header(37); % number of external events
    0044 nslow = header(38);  % number of slow channels
    0045 npw = header(39);  % number of points in wave
    0046 npr = header(40);  % number of points before threshold
    0047 tscounts = fread(fid, [5, 130], 'int32');
    0048 wfcounts = fread(fid, [5, 130], 'int32');
    0049 evcounts = fread(fid, [1, 512], 'int32');
    0050
    0051 % skip variable headers
    0052 fseek(fid, 1020*ndsp + 296*nevents + 296*nslow, 'cof');
    0053
    0054 record = 0;
    0055 wave = zeros(npw, 1);
    0056 wf = zeros(npw, 1);
    0057
    0058 % read data records
    0059 while feof(fid) == 0
    0060    type = fread(fid, 1, 'int16');
    0061     upperbyte = fread(fid, 1, 'int16');
    0062     timestamp = fread(fid, 1, 'int32');
    0063     channel = fread(fid, 1, 'int16');
    0064    unit = fread(fid, 1, 'int16');
    0065    nwf = fread(fid, 1, 'int16');
    0066    nwords = fread(fid, 1, 'int16');
    0067    toread = nwords;
    0068    if toread > 0
    0069       wf = fread(fid, [toread, 1], 'int16');
    0070    end
    0071    if toread > 0
    0072        if type == 1
    0073          if channel == ch
    0074             if unit == u
    0075             n = n + 1;
    0076                ts(n) = timestamp/freq;
    0077                wave(:, n) = wf;
    0078             end
    0079            end
    0080        end
    0081    end
    0082
    0083    record = record + 1;
    0084    if feof(fid) == 1
    0085       break
    0086    end
    0087
    0088 end
    0089 disp(strcat('number of waveforms = ', num2str(n)));
    0090
    0091 fclose(fid);