% GPIB to R&S FSU ESA| Grab current PHASE NOISE data set
clear all;close all;instrreset;
clc;
% tic
obj1 = gpib('NI', 0, 29);
obj1.Timeout=300;
obj1.InputBufferSize=400000;
fopen(obj1);
% cfreq=query(obj1,'SENS:FREQ:CENT?');
% pnts=query(obj1,'SENS:SWE:POIN?');
% fstrt=query(obj1,'SENS:FREQ:STAR?');
% fstp=query(obj1,'SENS:FREQ:STOP?');
% fprintf(obj1,'UNIT:POW DBM');
fprintf(obj1,'FORM ASCII');
[pow]=query(obj1,'TRACE? TRACE1');
% fstrt=str2num(fstrt);
% fstp=str2num(fstp);
d=str2num(pow);
kk=1;
for ii=1:2:length(d)
    freq(kk)=d(ii);
    p(kk)=d(ii+1);
    kk=kk+1;
end
% freq = linspace(fstrt, fstp, length(d));
% figure, plot(freq,d,'b');
H1=figure;
semilogx(freq,p,'b.');
xlabel('Freq [Hz]');
% xlim([fstrt, fstp]);
ylabel('Phase Noise [dBc/Hz]');
axis tight;
fclose(obj1);
delete(obj1);
% toc;
%%
filename='FreqDiv4_E8257D_5Vreg_20G_n10dBm_10Hz-100MHz_PM';
   save([filename '_' datestr(now,30) '.txt'],'freq','d', '-ASCII','-double');
    saveas(H1, [filename '_' datestr(now,30) '.png']); 
    saveas(H1, [filename '_' datestr(now,30) '.fig']); 
    