Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

LeCroy binary files!

261 views
Skip to first unread message

Alexis Lussier Desbiens

unread,
Aug 10, 2002, 7:38:45 PM8/10/02
to
Hi,

Does someone know a way to import binaries files from LeCroy oscilloscope in Matlab?

Alexis

Ben

unread,
Aug 10, 2002, 8:12:40 PM8/10/02
to
help fread


 FREAD Read binary data from file.
    [A, COUNT] = FREAD(FID,SIZE,PRECISION) reads binary data from the
    specified file and writes it into matrix A. Optional output
    argument COUNT returns the number of elements successfully read.
    
    FID is an integer file identifier obtained from FOPEN.
    
    The SIZE argument is optional; if not specified, the entire
    file is read and the file pointer is at the end of the file (see
    FEOF for details); if specified, valid entries are:
        N read N elements into a column vector.
        inf read to the end of the file.
        [M,N] read elements to fill an M-by-N matrix, in column
order.
               N can be inf, but M can't.
    
    The PRECISION argument is a string that specifies the format
    of the data to be read. It commonly contains a datatype specifier
    like 'int' or 'float' followed by an integer giving the size in
    bits. Any of the following strings, either the MATLAB version,
    or their C or Fortran equivalent, may be used. If not specified,
    the default is 'uchar'.
    
        MATLAB C or Fortran Description
        'uchar' 'unsigned char' unsigned character, 8 bits.
        'schar' 'signed char' signed character, 8 bits.
        'int8' 'integer*1' integer, 8 bits.
        'int16' 'integer*2' integer, 16 bits.
        'int32' 'integer*4' integer, 32 bits.
        'int64' 'integer*8' integer, 64 bits.
        'uint8' 'integer*1' unsigned integer, 8 bits.
        'uint16' 'integer*2' unsigned integer, 16 bits.
        'uint32' 'integer*4' unsigned integer, 32 bits.
        'uint64' 'integer*8' unsigned integer, 64 bits.
        'single' 'real*4' floating point, 32 bits.
        'float32' 'real*4' floating point, 32 bits.
        'double' 'real*8' floating point, 64 bits.
        'float64' 'real*8' floating point, 64 bits.
 
    For example,
 
        type fread.m
 
    displays the complete M-file containing this FREAD help entry. To
    simulate this command one could enter instead:
 
        fid = fopen('fread.m','r');
        F = fread(fid);
        s = char(F')
 
    The FOPEN command opens the M-file on the MATLAB path with name,
    'fread.m' for reading. The FREAD command assumes the default SIZE
of
    inf and the default PRECISION of 'uchar'. It reads the entire file
    converting the unsigned characters into a column vector of class
    'double' (double precision floating point). To display the result
    as readable text the 'double' column vector is transposed to a
    row vector and converted to class 'char' (character) using the
CHAR
    function.
 
    The following platform dependent formats are also supported but
    they are not guaranteed to be the same size on all platforms.
 
        MATLAB C or Fortran Description
        'char' 'char*1' character, 8 bits (signed or
unsigned).
        'short' 'short' integer, 16 bits.
        'int' 'int' integer, 32 bits.
        'long' 'long' integer, 32 or 64 bits.
        'ushort' 'unsigned short' unsigned integer, 16 bits.
        'uint' 'unsigned int' unsigned integer, 32 bits.
        'ulong' 'unsigned long' unsigned integer, 32 bits or 64
bits.
        'float' 'float' floating point, 32 bits.
 
    The following formats map to an input stream of bits rather than
    bytes.
 
        'bitN' signed integer, N bits
(1<=N<=64).
        'ubitN' unsigned integer, N bits
(1<=N<=64).
 
    If the input stream is bytes and FREAD reaches the end of file
    (see FEOF) in the middle of reading the number of bytes required
    for an element, the partial result is ignored. However, if the
    input stream is bits, then the partial result is ret

John Seney

unread,
Aug 12, 2002, 6:12:36 AM8/12/02
to
In article <7e19b38b.02081...@posting.google.com>,

% LCREAD read binary waveform file created by a LeCroy Oscilloscope
% W=LCREAD(FILENAME) loads the waveform file into the workspace
variable W.
% FILENAME can either be a variable or a string constant enclosed by
quotes.
%
% The return value W is a record containing four elements:
% W.INFO Waveform information, in readable formats. For example
Oscilloscope ID, sampling time and settings
% W.DESC Waveform information used for further calculations. For
example Sampling rate
% W.Y Values sampled by the oscilloscope
% W.X Array of time values corresponding to W.Y. Time '0' marks
the trigger event
%
% The routine was tested with files generated by a LC564A, Template
LECROY_2_2
%
% See also LCPLOT LCPLOTEXT
%
%------------------------------------------------------
% (c)2001 Hochschule f¸r Technik+Architektur Luzern
% Fachstelle Elektronik
% 6048 Horw, Switzerland

function wave=ReadLecroy(fn);
%------------------------------------------------------------------------
---------------
% Seek offset in the header block
%------------------------------------------------------------------------
---------------
fid=fopen(fn,'r');
if fid==-1
disp (sprintf('ERROR: file not found: %s', fn));
return
end;

data=fread(fid,50);
WAVEDESC=findstr('WAVEDESC', char(data(1:50)'))-1;
% subtract 1 because:
% - 1st byte in the File = Index [0]
% - 1st byte in the Matlab-Array = Index[1]

%------------------------------------------------------------------------
---------------
% Define the addresses of the various informations in the file
% These addresses are valid for the template LECROY_2_2 and are subject
to change in
% future revisions of the LeCroy firmware
%------------------------------------------------------------------------
---------------
TESTED_TEMPLATE = 'LECROY_2_3';

%Addresses (WAVEDESC + address as stated in the LECROY template)
aTEMPLATE_NAME = WAVEDESC+ 16;
aCOMM_TYPE = WAVEDESC+ 32;
aCOMM_ORDER = WAVEDESC+ 34;
aWAVE_DESCRIPTOR = WAVEDESC+ 36; % length of the descriptor block
aUSER_TEXT = WAVEDESC+ 40; % length of the usertext block
aWAVE_ARRAY_1 = WAVEDESC+ 60; % length (in Byte) of the sample
array
aINSTRUMENT_NAME = WAVEDESC+ 76;
aINSTRUMENT_NUMBER= WAVEDESC+ 92;
aTRACE_LABEL = WAVEDESC+ 96;
aWAVE_ARRAY_COUNT = WAVEDESC+ 116;

aVERTICAL_GAIN = WAVEDESC+ 156;
aVERTICAL_OFFSET = WAVEDESC+ 160;
aNOMINAL_BITS = WAVEDESC+ 172;

aHORIZ_INTERVAL = WAVEDESC+ 176;
aHORIZ_OFFSET = WAVEDESC+ 180;

aVERTUNIT = WAVEDESC+ 196;
aHORUNIT = WAVEDESC+ 244;
aTRIGGER_TIME = WAVEDESC+ 296;
aRECORD_TYPE = WAVEDESC+ 316;
aPROCESSING_DONE = WAVEDESC+ 318;
aTIMEBASE = WAVEDESC+ 324;
aVERT_COUPLING = WAVEDESC+ 326;
aPROBE_ATT = WAVEDESC+ 328;
aFIXED_VERT_GAIN = WAVEDESC+ 332;
aBANDWIDTH_LIMIT = WAVEDESC+ 334;
aVERTICAL_VERNIER = WAVEDESC+ 336;
aACQ_VERT_OFFSET = WAVEDESC+ 340;
aWAVE_SOURCE = WAVEDESC+ 344;

%------------------------------------------------------------------------
---------------
% determine the number storage format HIFIRST / LOFIRST (big endian /
little endian)
%------------------------------------------------------------------------
---------------

fseek(fid,aCOMM_ORDER,'bof');
COMM_ORDER=fread(fid,1,'int16');

fclose(fid);
% reopen the data file using the correct HIFIRST/LOFIRST format
if COMM_ORDER==0
fid=fopen(fn,'r','ieee-be'); % HIFIRST
else
fid=fopen(fn,'r','ieee-le'); % LOFIRST
end;

%------------------------------------------------------------------------
---------------
% Get the waveform information
%------------------------------------------------------------------------
---------------
% Check the template revision
TEMPLATE_NAME = ReadString(fid, aTEMPLATE_NAME);
if ~strcmp( TEMPLATE_NAME, TESTED_TEMPLATE)
disp (sprintf ('WARNING!\n %s %s %s\n %s %s %s',...
'This function has been written for the LeCroy-Template',...
TESTED_TEMPLATE, '.',...
'The current file contains information created with the
template',...
TEMPLATE_NAME, '.'));
end

% Instrument
wave.info.INSTRUMENT_NAME = ReadString(fid, aINSTRUMENT_NAME);
wave.info.INSTRUMENT_NUMBER = ReadLong (fid, aINSTRUMENT_NUMBER);
wave.info.Filename = fn;

% Channel
wave.info.TRIGGER_TIME = ReadTimestamp(fid, aTRIGGER_TIME);

tmp=['channel 1';'channel 2';'channel 3';'channel 4';'unknown '];
wave.info.WAVE_SOURCE = tmp (1+ ReadWord(fid, aWAVE_SOURCE),:);

tmp=['DC_50_Ohms'; 'ground ';'DC 1MOhm ';'ground ';'AC 1MOhm '];
wave.info.VERT_COUPLING = deblank (tmp (1+ ReadWord(fid,
aVERT_COUPLING),:));

tmp=['off'; 'on '];
wave.info.BANDWIDTH_LIMIT = deblank (tmp (1+ ReadWord(fid,
aBANDWIDTH_LIMIT),:));

tmp=[
'single_sweep '; 'interleaved '; 'histogram ';
'graph '; 'filter_coefficient'; 'complex ';
'extrema '; 'sequence_obsolete '; 'centered_RIS ';
'peak_detect '];
wave.info.RECORD_TYPE = deblank (tmp (1+ ReadWord(fid,
aRECORD_TYPE),:));

tmp=[
'no_processing'; 'fir_filter '; 'interpolated '; 'sparsed
';
'autoscaled '; 'no_result '; 'rolling '; 'cumulative
'];
wave.info.PROCESSING_DONE = deblank (tmp (1+ ReadWord(fid,
aPROCESSING_DONE),:));


% Vertical settings
FIXED_VERT_GAIN = ReadFixed_vert_gain(fid, aFIXED_VERT_GAIN);
PROBE_ATT = ReadFloat (fid, aPROBE_ATT);
VERTICAL_GAIN = ReadFloat (fid, aVERTICAL_GAIN);
VERTICAL_OFFSET = ReadFloat (fid, aVERTICAL_OFFSET);
wave.info.NOMINAL_BITS = ReadWord (fid, aNOMINAL_BITS);
wave.info.Gain_with_Probe = strcat
(Float_to_Eng(FIXED_VERT_GAIN*PROBE_ATT), 'V/div');

% Horizontal settings
HORIZ_INTERVAL = ReadFloat(fid, aHORIZ_INTERVAL);
HORIZ_OFFSET = ReadDouble(fid, aHORIZ_OFFSET);
wave.info.TIMEBASE = strcat (Float_to_Eng
(ReadTimebase(fid,aTIMEBASE)), 's/div');
wave.info.SampleRate = strcat (Float_to_Eng(1/HORIZ_INTERVAL) , 'S/sec');
wave.desc.Ts = HORIZ_INTERVAL;
wave.desc.fs = 1/HORIZ_INTERVAL;

%------------------------------------------------------------------------
---------------
% Read samples array (Plain binary ADC values)
%------------------------------------------------------------------------
---------------
COMM_TYPE = ReadWord(fid, aCOMM_TYPE);
WAVE_DESCRIPTOR = ReadLong(fid, aWAVE_DESCRIPTOR);
USER_TEXT = ReadLong(fid, aUSER_TEXT);
WAVE_ARRAY_1 = ReadLong(fid, aWAVE_ARRAY_1);

fseek(fid, WAVEDESC + WAVE_DESCRIPTOR + USER_TEXT, 'bof');
if COMM_TYPE == 0 % byte
wave.y=fread(fid,WAVE_ARRAY_1, 'int8');
else % word
wave.y=fread(fid,WAVE_ARRAY_1, 'int16');
end;

%------------------------------------------------------------------------
---------------
% Transform the ADC values to voltages and create corresponding array of
time
%------------------------------------------------------------------------
---------------
wave.y = VERTICAL_GAIN * wave.y - VERTICAL_OFFSET;
wave.x = [1:WAVE_ARRAY_1]'*HORIZ_INTERVAL + HORIZ_OFFSET;

%------------------------------------------------------------------------
---------------
% close the waveform file
%------------------------------------------------------------------------
---------------
fclose(fid);

%========================================================================
===============
% Support functions
%========================================================================
===============

%------------------------------------------------------------------------
---------------
% Read 8Bit signed Byte
%------------------------------------------------------------------------
---------------
function b=ReadByte(fid, Addr)
fseek(fid,Addr,'bof');
b=fread(fid,1,'int8');

%------------------------------------------------------------------------
---------------
% Read 16Bit signed Word
%------------------------------------------------------------------------
---------------
function w=ReadWord(fid, Addr)
fseek(fid,Addr,'bof');
w=fread(fid,1,'int16');

%------------------------------------------------------------------------
---------------
% Read 32Bit signed Long
%------------------------------------------------------------------------
---------------
function l=ReadLong(fid, Addr)
fseek(fid,Addr,'bof');
l=fread(fid,1,'int32');

%------------------------------------------------------------------------
---------------
% Read 32Bit IEEE Float
%------------------------------------------------------------------------
---------------
function f=ReadFloat(fid, Addr)
fseek(fid,Addr,'bof');
f=fread(fid,1,'float32');

%------------------------------------------------------------------------
---------------
% Read 64Bit IEEE Double
%------------------------------------------------------------------------
---------------
function d=ReadDouble(fid, Addr)
fseek(fid,Addr,'bof');
d=fread(fid,1,'float64');

%------------------------------------------------------------------------
---------------
% Read string (up to 16 characters)
%------------------------------------------------------------------------
---------------
function s=ReadString(fid, Addr)
fseek(fid,Addr,'bof');
s=fgets(fid,16);

%------------------------------------------------------------------------
---------------
% Read timestamp
%------------------------------------------------------------------------
---------------
function t=ReadTimestamp(fid, Addr)
fseek(fid,Addr,'bof');
seconds = fread(fid,1,'float64');
minutes = fread(fid,1,'int8');
hours = fread(fid,1,'int8');
days = fread(fid,1,'int8');
months = fread(fid,1,'int8');
year = fread(fid,1,'int16');

t=sprintf('%i.%i.%i, %i:%i:%2.0f', days, months, year, hours,
minutes, seconds);
%------------------------------------------------------------------------
---------------
% Timebase aus dem File lesen
%------------------------------------------------------------------------
---------------
function t=ReadTimebase(fid, Addr)
fseek(fid,Addr,'bof');
e=fread(fid,1,'int16');

tmp=[1 2 5];
mant = tmp( 1+ mod(e,3));
ex = floor (e / 3)-12;

t=mant*10^ex;
%------------------------------------------------------------------------
---------------
% fixed Vertical Gain aus dem File lesen
%------------------------------------------------------------------------
---------------
function t=ReadFixed_vert_gain(fid, Addr)
fseek(fid,Addr,'bof');
e=fread(fid,1,'int16');

tmp=[1 2 5];
mant = tmp( 1+ mod(e,3));
ex = floor (e / 3)-6;

t= mant*10^ex;
%------------------------------------------------------------------------
---------------
% Transform a Float to the Engineering-Format (returns a string)
%------------------------------------------------------------------------
---------------
function s=Float_to_Eng (f)
ex= floor(log10(f));
exeng=ex-mod(ex,3);
if exeng<-18 exeng=-18; end
if exeng>18 exeng=18; end;
mant=f/10^exeng;

prefix=['afpnum kMGPE']; %prefixes (u=micro, m=milli, k=kilo, ...)
s=sprintf('%g%s',mant, prefix( (exeng+18)/3 +1));

JS
Scope FAQ at http://www.qsl.net/wd1v


0 new messages