st1=7;
st2=8;

%erfile=[st1:2:st1+4];
%fbfile=[st2:2:st2+4];

erfile=st1;
fbfile=st2;

fil1file='mass_fil1';
ol_mass='mass1';
fil2file='pzt_fil1';
olfile='ol2';

calib=(8.6+10)/2*4*pi/1.064e-6;

%%
[fr,er1]=spe_marge(erfile);
[fr,fb1]=spe_marge(fbfile);
%[fr,er2]=spe_marge(er2file);
%[fr,fb2]=spe_marge(fb2file);

[fr2,fb2]=spe_marge(7);
fb2w=fb2.*abs((1+1i.*(fr2./1.5915)).^2.*(1+1i.*(fr2./0.0399))./(fr2./0.0399))./110;

%l1=fr>10;
%l2=fr2<=10;
%fb1=[er2(l2);fb1(l1)];

%loglog(fr,er1,fr,fb1,fr,er2,fr,fb2); grid on
loglog(fr,fb1,fr2,fb2w); grid on

%%
figure(4)
dat=read_svk(fil1file,1);

fr0=0.0159;
fr1=1.5915;
fr2=31.4535;
fr3=723.4316;
fr4=1.539e3;

fil_mod=(1+i.*(dat(:,2)/fr1)).*(1+i.*(dat(:,2)/fr2))...
      ./(1+i.*(dat(:,2)/fr3))./(1+i.*(dat(:,2)/fr4))./(1+i.*(dat(:,2)/fr0));

ga=dat(:,3)./abs(fil_mod);

l=and(dat(:,5)>0.9999,dat(:,2)<500);
mga1=mean(ga(l));
filg1=mga1.*abs(fil_mod);

subplot(3,1,1)
hold on
loglog(dat(:,2),abs(filg1),'r')
hold off
title(['DC gain: ',num2str(mga1)])

filmod1=mga1.*(1+i.*fr/fr1).*(1+i.*fr/fr2)./(1+i.*fr/fr3)./(1+i.*fr/fr4)./(i.*fr/fr0);


%%

erh1=fb1./abs(filmod1);
erh2=fb2w./abs(filmod1);

figure(2)
%loglog(fr,er1,fr,er2,fr,erh1,fr,erh2)
loglog(fr,erh1,fr,erh2)
grid on
xlabel('Frequency  [Hz]')
ylabel('Voltage noise [V/Hz^{1/2}]')


%% Calibration factor

%[cf,fr]=c_factor(fr,filfile,olfile,calib);
[cf,fr]=c_factor2(fr,fil1file,ol_mass,fil2file,olfile,calib);

%%

%d0=erh1.*cf;
d1=fb2.*cf;

figure(11)
loglog(fr,erh1/calib,fr,erh2/calib,fr,d1)
%loglog(fr,disp,fr,disp_er,fr,d0,fr,d1)
grid on
xlabel('Frequency  [Hz]')
ylabel('Displacement noise [m/Hz^{1/2}]')

%%

d_dat=[fr,d1];
save disp100904_mi1.dat d_dat -ascii

