data = load('Transfer1.txt');
freq = data(:,1); amp = data(:,2); phase = data(:,3);

figure(1);
plot(freq,amp);
grid on
xlabel('Frequency [Hz]');
ylabel('Amplitude');

figure(2);
plot(freq,phase);
grid on
xlabel('Frequency [Hz]');
ylabel('Phase');


figure(3);
semilogx(freq,20*log10(amp))
grid on
title('Bode plot of amplitudes');
xlabel('Frequency [Hz]');
ylabel('dB');


factor = 1000;  %% factor=1 will fail, due to ill conditioning
factor = 1;  %% factor=1 will fail, due to ill conditioning
freq  = freq/factor;
omega = freq*2*pi;
u = omega.^2;

n = length(omega);
F = ones(n,3);
F(:,2) = u;   F(:,3) = u.^2;


[a,yVar,residual,aVar] = LinearRegression(F,1./(amp.^2));
parameters = a'
StandardDev = sqrt(aVar)'
parameters_scaled = [a(1) a(2)*factor^2 a(3)*factor^4]
StandardDev_scaled = [sqrt(aVar(1)) sqrt(aVar(2))*factor^2 sqrt(aVar(3))*factor^4]

y = F*a;
figure(4)
clf()
plot(u,1./(amp.^2),u,y)
grid on
