%% Link two plots together so that the x-axis always shows the same range.
figure(1); ah(1) = gca; figure(2); ah(2) = gca; linkaxes(ah, 'x')
%% Copy lines from one figure to another
L = findobj(2,'type','line'); % first argument is the figure to copy from
for i = 1:length(L)
copyobj(L(i),findobj(3,'type','axes')); %first arg is figure to copy to
end
% By hand: Copy/Paste works too
%% Make line invisible to "legend" command. Mark line first!
hasbehavior(gco, 'legend', false);
%% Do Allan Deviation
xdata = get(get(gca,'Children'),'xdata');
% xdata = (xdata - xdata(1))*24*3600; % in s from env data
ydata = get(get(gca,'Children'),'ydata');
ydata = ydata*200e-6/20/87.6; % relative deviation
delta_t = mean(diff(xdata));
[~,ADEV,~,~,~,TAU] = avar(ydata,delta_t);
figure
loglog(TAU,ADEV,'x-')
set(gca,'YLim',[1e-8,1e-7])
%% Linear Fit with Fit uncertainty
% Fit *column* vectors x,y with linear polynomial y = a*x + b
% Remember to remove mean(x) first, if you want he errors to be
% uncorrelated!
cf = fit(x,y,'poly1');
cf_coeff = coeffvalues(cf);
cf_confint = confint(cf);
a = cf_coeff(1);
b = cf_coeff(2);
a_uncert = (cf_confint(2,1) - cf_confint(1,1))/2;
b_uncert = (cf_confint(2,2) - cf_confint(1,2))/2;
%% Smoothing with moving average
smoothing_length = 10;
filtered_data = filter(ones(1,smoothing_length)/smoothing_length,1,data);
%% Smoothing with butterworth filter
cutoff_freq = 0.02; % in Hz - this is a 50 second filter
sample_freq = 1/(mean(diff(time_data))*24*3600);
[A,B] = butter(2,cutoff_freq/(sample_freq/2));
filtered_data = filtfilt(A,B,data);