%% plotting sqz vs. frequency

%% load DARM 

DARM = load('sqz_data_Feb19.txt');
DARM_f = DARM(:,1); %freq
DARM_ref = DARM(:,2); %reference
DARM_sqz = DARM(:,4); %SQZ

%%
sqz_ratio = DARM_sqz./DARM_ref;
sqz_ratio_dB = 20*log10(sqz_ratio);

%% interpolate -- NOPE
% x = DARM_f;
% v = sqz_ratio_dB;
% xq = 1:50:length(DARM_f);
% vq1 = interp1(x,v,xq,'pchip');

%% smooth?
s = smooth(DARM_f, sqz_ratio_dB, 0.5, 'rloess');
%%
% Fit a line through the data
% p = polyfit(DARM_f, s, 3);
% % Get the fitted values
% fit = polyval(p, DARM_f);

%% smooth?
%s = smooth(DARM_f, sqz_ratio_dB, 0.1, 'rloess')

%%

% p1 = 3.347e-14; 
% p2 = -4.902e-10;  
% p3 = 2.395e-06; 
% p4 = -0.004344;
% p5 = 0.2552;
% fit = p1.*DARM_f.^4 + p2.*DARM_f.^3 + p3.*DARM_f.^2 + p4.*DARM_f + p5;

%% plot shit
figure(400)
set(gcf,'color',[0.5, 0.5, 0.5]) 
fig = gcf;
fig.InvertHardcopy = 'off';

p1 = semilogx(DARM_f, sqz_ratio_dB, 'LineWidth', 2, 'Color', [0.6350, 0.0780, 0.1840]);
p1.Color(4) = 0.5
xlabel('Frequency, Hz'), ylabel('sqz (dB)');
hold on
semilogx(DARM_f, s, '--', 'LineWidth', 2, 'Color', [1, 0, 0]);
%semilogx(DARM_f, fit,  'LineWidth', 2, 'Color', [1, 0, 0]);


set(gca,'FontSize',18, 'Color', 'k');
hTitle = title('IFO SQZ vs. Frequency');
grid on;
xlim([10,10e3])
ylim([-10,10])
ax = gca;
ax.XColor = 'k'; 
ax.YColor = 'k'; 
ax.MinorGridColor = 'w';
ax.GridColor = 'w';


legend({'\color{white} 20*log(sqz/ref)','\color{white} smoothed'}, 'FontSize',14, 'Location', 'Northeast')

hold off