%{
GPIB Code for SR770 FFT Network Analyzer
OPTIONALLY grab current plot on trace 0 (should pause first)
Runs a new sweep with averaging of 100 traces
2 spans to see 1Hz to 250kHz, with 2 resolution bandwidths
Daryl Spencer, 2014
%}
clear all;close all;
instrreset;clc;tic
% file = 'OEO2Gen1v2_1Hz_4p4mAPD_0p86mAPD_Vfeedback_Locked';
file='50Ohm';filename=[file '_' datestr(now,30)];
cd('C:\Daryl_Main\Research\Prog, Instr,Parts,Trainings\FFT SR770')
span=[0.191;0.382;0.763;1.5;3.1;6.1;12.2;24.4;48.75;97.5;195;390;780;1560;3125;6250;12500;25e3;50e3;100e3];
tr='-1'; %Set trace: 0, 1, or -1(active trace)
obj1 = gpib('NI', 0, 10);%gpib('AGILENT', 0, 10);
obj1.Timeout=6;
obj1.InputBufferSize=20000;
obj1.OutputBufferSize=20000;
fopen(obj1);
query(obj1,'*IDN?');
fprintf(obj1,'AOFM 0'); %Turn off offset calibration
%% Query current trace data %%% 
% pts=str2num(query(obj1,['SPEC? ' tr])); % Query y data (full array)
% s=(str2num(query(obj1,'SPAN?'))+1)     % Query current span, add one as it starts from 0
% % freq=NaN(1,400);
% % f=linspace(span(s+1)/399,span(s+1),399);% j=linspace(0,399,400);
% for i=0:399 %Query x data (needs sweep) start at 0
%     freq(i+1)=str2num(query(obj1,(sprintf('BVAL? %s,%d',tr,i))));
% end
% H1=figure;semilogx(freq(2:end),20*log10(pts(2:end)),'r.-');
% title(sprintf('Current Data Plot \n"%s"',file),'Interpreter','None');
% xlabel('Frequency (Hz)');ylabel('PSD (dBV^2/Hz)');
% xlim([freq(1) freq(end)]);
% fprintf(1,sprintf('First data point is: %6.3fHz, %6.3e\n',freq(2),pts(2)));
% saveas(H1,[filename '.png']);
% saveas(H1,[filename '.fig']);
%% Run new trace and extract data %%
fprintf(obj1,'*RST');pause(3);
fprintf(obj1,'ICPL 0'); %Set coupling AC(i=0,0.16Hz BW), DC(i=1);
fprintf(obj1,'IGND 1'); %Set grounding float(i=0), GND(i=1);
fprintf(obj1,['XAXS ' tr ',1']); %Set xaxis to lin(i=0), log(i=1);
% freqaq=NaN(1,800);
fprintf(obj1,['MEAS ' tr ',1']); %Set measurement: Spectrum(i=0),PSD(i=1),Time(i=2),Octave(i=3)
fprintf(obj1,['UNIT' tr ',1' ';STRT']) %Set units:VoltsPk(i=0),VoltsRMS(i=1),dBV(i=2),or dBVrms(i=3)
fprintf(obj1,'MSGSI_pity_da_fool!!!!!!!');pause(2);

%%%% Take 390Hz/997mHzRBW span data
fprintf(obj1,'SPAN 11');
fprintf(obj1,'STRF 0');pause(1); %Set start freq to 0Hz
fprintf(obj1,['AVGO 0;STRT;' 'AUTS 0 ' tr]);pause(1); %Autoscale new trace
fprintf(obj1,'ARNG 1');pause(4); %Autorange new trace
fprintf(obj1,['AUTS 0 ' tr]);pause(1); %Autoscale new trace
fprintf(obj1,'AVGO 1;NAVG 100'); %Sets averaging to 100 traces
fprintf(obj1,'STRT');pause(3);   %Start data acquisition
ptsaq=str2num(query(obj1,['SPEC? ' tr])); % Query y data (full array)
for i=0:399 %Query x data (needs sweep) start at 0
    freqaq(i+1)=str2num(query(obj1,(sprintf('BVAL? %s,%d',tr,i))));
end
%%%% Take 250kHz/250HzRBW span data
fprintf(obj1,'SPAN 19');
s=(str2num(query(obj1,'SPAN?'))+1);     % Query current span, add one as it starts from 0
fprintf(obj1,['AVGO 0;STRT;' 'AUTS 0 ' tr]);pause(1); %Autoscale new trace
fprintf(obj1,'ARNG 1');pause(4); %Autorange new trace
fprintf(obj1,['AUTS 0 ' tr]);pause(1); %Autoscale new trace
fprintf(obj1,'AVGO 1;NAVG 100'); %Sets averaging to 100 traces
fprintf(obj1,'STRT');pause(3);   %Start data acquisition
ptsaq=[ptsaq str2num(query(obj1,['SPEC? ' tr]))]; % Query y data (full array)
for i=0:399 %Query x data (needs sweep) start at 0
    freqaq(400+i+1)=str2num(query(obj1,(sprintf('BVAL? %s,%d',tr,i))));
end

H2=figure;semilogx(freqaq(1:end),20*log10(ptsaq(1:end)),'r.-');
title(sprintf('New Data Plot \n"%s"',file),'Interpreter','None');
xlabel('Frequency (Hz)');ylabel('PSD (dBV^2/Hz)');
xlim([min(freqaq) max(freqaq)]);
set(gcf,'Units', 'Normalized', 'OuterPosition', [0.1 0.1 0.8 0.8]); 
grid on;

%% Close equipment
fprintf(obj1,'AOFM 1;AVGO 0;STRT'); %Turn on offset calibration
fclose(obj1);
%% Save traces
save(filename)
saveas(H2,[filename '.png']);
saveas(H2,[filename '.fig']);
toc;