%MATLAB function that reads in a Tektronix file and writes a MATLAB variable.

%Written by Craig Skoch January 10, 2003

%Originally worked with TDS5000/6000/7000 Series Scopes

%Modified by Craig Skoch August 19, 2005 to allow this program to work with
%Tektronix 5034B Scope  (Should work for all TDS5000B Series)

%Modified by Matt Borg November 21, 2006 to allow this program to work with
%Tektronix DPO7054 Scope (Should work for all DPO7000 Series)
%% The big changes are that there needs to be a shift of 4 bytes after
%% each of the following positions: 302, 458, 590, 722

%Command line should look like: [v,t]=tekread('tektronix filename')
%Currently doesn't work with FastFrame Data

function [A,t]=tekread(tekfile)

%tek_ptr=fopen(tekfile);
tek_ptr=fopen(tekfile,'r','b');%Open for read-only with big-endian format

fseek(tek_ptr,2,'bof');
vers=fread(tek_ptr,8,'char')';%Versioning number

%Account for 2 extra bytes in header for TDS5034B Scope at position 154
%TDS5034B Scope needs file reopened using little-endian format

if vers==':WFM#001'%Version Number for TDS7104 Scope
    shiftbytes=0;
elseif vers==':WFM#002'| vers==':WFM#003'%Version Number for TDS5034B Scope and DPO7054, respectively
    shiftbytes=2;
    tek_ptr=fopen(tekfile);
 end

fseek(tek_ptr,0,'bof');
dec2hex(fread(tek_ptr,1,'uint16'),4);%Byte order verification

fseek(tek_ptr,2,'bof');
vers=fread(tek_ptr,8,'char')';%Versioning number

fseek(tek_ptr,10,'bof');
digbytecount=fread(tek_ptr,1,'char');%Num digits in byte count

fseek(tek_ptr,11,'bof');
fread(tek_ptr,1,'*int32');%Num of bytes to the end of file

fseek(tek_ptr,15,'bof');
bytesperpnt=fread(tek_ptr,1,'char');%Number of bytes per point
begbuff=fread(tek_ptr,1,'int32');%Byte offset to beginning of curve buffer

%fseek(tek_ptr,40,'bof')
%fread(tek_ptr,32,'char')%Waveform Label

fseek(tek_ptr,76,'bof');
hdrsize=fread(tek_ptr,1,'int16');%Size of Waveform Header

fseek(tek_ptr,122,'bof');
datatype=fread(tek_ptr,1,'int8');%Data Type

fseek(tek_ptr,166+shiftbytes,'bof');
Vscale=fread(tek_ptr,1,'double');%Voltage Scale

fseek(tek_ptr,174+shiftbytes,'bof');
Voff=fread(tek_ptr,1,'double');%Voltage Offset

fseek(tek_ptr,222+shiftbytes,'bof');
fread(tek_ptr,1,'double');%Voltage Resolution


if vers==':WFM#003';shiftbytes=shiftbytes+4;end
%% Insert code here if you want to look at data that is stored between 302
%% and 458
if vers==':WFM#003';shiftbytes=shiftbytes+4;end


fseek(tek_ptr,478+shiftbytes,'bof');
tscale=fread(tek_ptr,1,'double');%Time Scale

fseek(tek_ptr,486+shiftbytes,'bof');
toff=fread(tek_ptr,1,'double');%Time Offset

fseek(tek_ptr,494+shiftbytes,'bof');
numpts=fread(tek_ptr,1,'int32');%Record Length

if vers==':WFM#003';shiftbytes=shiftbytes+4;end
%% Insert code here if you want to look at data that is stored between 590
%% and 722
if vers==':WFM#003';shiftbytes=shiftbytes+4;end

fseek(tek_ptr,804+shiftbytes,'bof');
buffoffset=fread(tek_ptr,1,'int32');%Bytes of Buffer

fseek(tek_ptr,808+shiftbytes,'bof');
enddata=fread(tek_ptr,1,'int32');%Bytes from Beg Buffer to End of Data

numpts=(enddata-buffoffset)/bytesperpnt;

fseek(tek_ptr,buffoffset+begbuff,'bof');

if bytesperpnt==1
    A=fread(tek_ptr,numpts,'int8');
elseif bytesperpnt==2
    A=fread(tek_ptr,numpts,'int16');
end

t=(toff+tscale:tscale:toff+tscale*numpts)';
A=A*Vscale+Voff;

fclose(tek_ptr);