%% ============================ MAIN =====================================
clc
clear all;
close all;

%% ____________________ Configuration VISA-PC ___________________________________
instrreset;
visaAddress = 'TCPIP0::192.168.1.10::inst0::INSTR';
instrument = instrfind('Type', 'visa-usb', 'RsrcName', visaAddress, 'Tag', '');
instrument = visa('tek', visaAddress);                           
buffer = 2e10;   
instrument.InputBufferSize = 1e8;
instrument.OutputBufferSize= 1e8;
fopen(instrument);                %open the instrument as a VISA resource
instrument.timeout = 1000;        %set instrument timeout
%% _________________ Identification and reset of the instrument ______________
instID = query(instrument,'*idn?');     %identify instrument
fprintf('%s\n',instID);                 %print instrument ID
fwrite(instrument,'*rst');              %default the instrument
%% __________________ Parameters _____________________________
voltsperdiv1 = 30e-3; 
voltsperdiv2 = 300e-3;
frames       = 500;   % number of frames (repetitions)
record       = 2560;  %size of each pulse equal to 51.2 ns 
samplerate   = 50e9;  %The used sample rate
Holdofftime  = 0.01;  %10ms
leveltrig    = 20e-3; %voltage level of the trigger 
%% __________________Amplitude ___________________
fprintf(instrument,'SELect:CH1 ON'); %activate Channel 1 
fprintf(instrument,'SELect:CH2 ON'); %activate Channel 2 
fprintf(instrument,'CH1:COUPLING DC'); %Set Channel 1 coupling to DC
fprintf(instrument,'CH2:COUPLING DC'); %Set Channel 2 coupling to DC
fprintf(instrument,'ch1:scale %f',voltsperdiv1); %Set Channel 1 voltage scale
fprintf(instrument,'ch2:scale %f',voltsperdiv2); %Set Channel 2 voltage scale
%% __________________ Parameters of sampling _____________________
fwrite(instrument,'acquire:state 0');  %stop the acquisitions to set peremeters 
fprintf(instrument,'HORizontal:MODE MANual');  
fprintf(instrument,' HORizontal:MODE:SAMPLERate %f', samplerate); %configure the sample rate 
fprintf(instrument,'HORizontal:MODE:RECOrdlength %i', record);  %configure the length of records
fprintf(instrument,'HORizontal:DELay:MODe On');
fprintf(instrument,'HORizontal:DELay:POSition 0'); 
fprintf(instrument,'HORizontal:DELay:TIMe 0');   
%% __________________ Parameters Fast Frame  ______________________________
fwrite(instrument,'acquire:mode average'); %use the average mode
fprintf(instrument,'ACQuire:NUMAVg 50') ; % number of pulses to average
fprintf(instrument,'HORIZONTAL:FASTFRAME:STATE 1'); %activate the fast frame option 
fprintf(instrument,'HORizontal:FASTframe:COUNt %i', frames);%set the number of frames to acquire 
fprintf(instrument,'HORizontal:FASTframe:SEQuence FIRst'); %sets single-sequence mode to stop after 500 frames
fprintf(instrument,'HORizontal:FASTframe:SUMFrame NONe'); 
%% ____________________________ Parameteres Trigger ____________________________
fprintf(instrument,'TRIGger:A:MODe NORMal'); %activate trigger 1
fprintf(instrument,'TRIGger:A:EDGE:SOUrce CH1'); %set the trigger source to CH&
fprintf(instrument,'TRIGger:A:EDGE:SLOpe: RISe');    % set slope at rise
fprintf(instrument,'TRIGger:A:LEVel:CH1 %f', leveltrig);  %set the trigger level 
fprintf(instrument,'TRIGger:A:HOLDoff:BY TIMe'); %use the holdoff by time
fprintf(instrument,'TRIGger:A:HOLDoff:TIMe %f',Holdofftime);% set holdoff to 10 ms 
%% __________________________ Configuration of Data transfer _______________
fwrite(instrument,'header 0');           %turn the header off
fwrite(instrument,'data:encdg fastest'); %set encoding type to fast binary
fwrite(instrument,'wfmoutpre:byt_n 2');  
fwrite(instrument, 'dese 209');  fwrite(instrument, '*ese 209'); fwrite(instrument, '*sre 0');
%% _______________________ Acquisition set _____________________________
fwrite(instrument,'ACQuire:STOPAfter SEQuence'); %sets single-sequence mode to stop after 500 frames
%% ______________________________ Acquisition _____________________________________
instrument.Timeout = 1000;                           
byt_nr             = str2double(query(instrument, 'wfmoutpre:byt_nr?'));
fwrite(instrument,'ACQuire:STOPAfter SEQuence'); 
fwrite(instrument, 'dese 209');
fwrite(instrument, '*ese 209');
fwrite(instrument, '*sre 0');
fwrite(instrument,'acquire:state 1'); %begin acquisitions 
op_complete        = str2num(query(instrument, '*opc?'));
%% ___________________________ Lecture reference signal from channel 1_____________________________ 
tic
fwrite(instrument,'data:source ch1') ; 
fwrite(instrument,'data:start 1');
fprintf(instrument,'data:stop %i', record);
fwrite(instrument,'DATa:FRAMESTARt 01 '); 
fprintf(instrument,'DATa:FRAMESTOP %i',frames ); %lecture of all the recorded frames
dy1                 = str2double(query(instrument, 'wfmoutpre:ymult?'));%the vertical scale factor per digitizing level for the waveform
yoff1               = str2double(query(instrument, 'wfmoutpre:yoff?'));
fwrite(instrument,'curve?');
Data_ref            = (binblockread(instrument,'int16')-yoff1)*dy1;
toc
 %% ___________________________ Lecture radar signal from channel 2 _______________________________ 
tic
fwrite(instrument,'data:source ch2') 
fwrite(instrument,'data:start 1');
fprintf(instrument,'data:stop %i', record); 
fwrite(instrument,'DATa:FRAMESTARt 01');
fprintf(instrument,'DATa:FRAMESTOP %i', frames ) ; %lecture of all the recorded frames
dy2                 = str2double(query(instrument, 'wfmoutpre:ymult?'));
yoff2               = str2double(query(instrument, 'wfmoutpre:yoff?'));
fwrite(instrument,'curve?');
Data_chI            = (binblockread(instrument,'int16')-yoff2)*dy2;
toc