function  [GS13_noise_floor] = GS13_noise_calc_2007(ff,amp);
% GS13_noise_calc_2007  calculates the noise of a GS13
%
% calculation of the noise for an GS13
% with a simple non-inverting LT1012 amplifier readout
% BTL Aug 15 2006
%
% modified Sept 16, 2007 to include different amplifiers
% call as 
% noise = GS13_noise_calc_2007(freq,'lt1001');
%   to calculate with an LT1001 instead of the default LT1012
%   options are the amps defined in opampnoise.m
%   'lt1001', 'lt1007', 'lt1012', 'lt1792', 'opa27', 'ad743', '6013'

%clear; close all;

%freq = logspace(log10(.05),2,300);
rows = size(ff,1);
cols = size(ff,2);
if cols > rows
	ff = ff';
end

%ff = freq_bin;
w = 2*pi*ff;

if nargin == 1
    amp = 'lt1012';
end
disp(['   Using an ',amp,' op-amp'])

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%             L-4C info                %

GC = 2180;   % V*s/m, from the manual
w0 = 2*pi*1;

Rc = 9100;   % coil resistance
Rd = 845000; % damping resistance, helpful to test circuit, adds noise, not necessary

Q   = 5;
num = (Rd/(Rc+Rd)) * GC*[1 0 0];
den = [1 w0/Q w0^2];
GS13_model_velocity = tf(num,den);  % this is the velocity response, in dspace*sec/meter

figure
bode(tf(num,den))
title('GS13 velocity response in V/m/sec')


GS13_position_response = GS13_model_velocity * tf([1 0],[1])

figure
bode(GS13_position_response)
title('GS13 displacement response in V/m')

GS13_resp_FR = squeeze(freqresp(GS13_position_response,w));    %V/m

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%               noise calcs             %%
kT = 1.38E-23 * 300;

R_sensor = (Rc*Rd)/(Rc+Rd);			% resistance of the sensor
Lc = Rc/(2*pi*20);  % BOGUS I don't know what the value is for the coil inductance

% resistance of the 2 gain setting resistors in parallel
% is the load on the negative terminal of the opamp
R_readout = 100;				

%%%%%%%%%%%%%%%%%%%%%%%%

%      calc the impedance of the coil
% the dynamic part is:

m = 5;
k = w0^2 * m;

Z_dyn      = GC^2/m * tf([0 1 0],[1 w0/Q w0^2]);
Z_dyn_FR   = squeeze(freqresp(Z_dyn,2*pi*ff));

Z_R_L    = tf([Lc , R_sensor],1);

Z_total    = Z_dyn + Z_R_L + R_readout;
Z_total_FR = squeeze(freqresp(Z_total,2*pi*ff));


figure
loglog(ff,Rc* ones(size(ff)), ...
	ff, abs(Z_dyn_FR),...
	ff,abs(Z_total_FR))
title('Impedance of the GS-13 coil')
legend('coil resistance','dynamics','total')
grid on

%% with the LT1012

[nn, vn_lt1012, cn_lt1012, jn_crap] = opampnoise(1,0,ff, amp);
% cn_lt1012 is the current noise * (1+0) ohms resistance
%[nn, vn_lt1012, cn_lt1012, jn_crap] = opampnoise(1,0,ff, 'ad743');
% the ad743 isabout 1.4 better above 4 Hz or so. 


cur_noise = Z_total_FR .* cn_lt1012;
cur_9k    =    9200    .* cn_lt1012;

john_noise = sqrt(4*kT*real(Z_total_FR));
john_9k    = sqrt(4*kT * 9200);

volt_noise = vn_lt1012;

total_noise = abs(sqrt(volt_noise.^2 + cur_noise.^2 + john_noise.^2));
total_9k    = abs(sqrt(volt_noise.^2 + cur_9k.^2    + john_9k.^2   ));


figure
ll=loglog(ff, [ volt_noise, abs(cur_noise), john_noise, total_noise, total_9k]);
set(ll(1),'LineWidth',2);
set(ll(2),'LineWidth',2);
set(ll(3),'LineWidth',2);
set(ll(4),'LineWidth',2,'Color','k');
set(ll(5),'LineWidth',3,'Color','k','LineStyle','--');

title(['Noise components for the GS-13 readout with an ',amp])
legend([amp,' volt noise'],[amp,' cur noise * impedance'],'johnson noise', ...
	'total noise','total 9200 ohm')
axis([.03 100 1e-9 1e-6])
grid on
xlabel('freq (Hz)')
ylabel('ASD of voltage noise (V/\surdHz)')
IDplot
FillPage('w')

GS13_noise_floor = abs(total_noise ./ GS13_resp_FR);


figure
ll=loglog(ff, GS13_noise_floor);
set(ll(1),'LineWidth',2);
axis([.03 100 1e-14 1e-6])
grid on
title(['Noise floor of GS13 with ',amp,' readout'])
xlabel('freq (Hz)')
ylabel('ASD of noise floor (m/\surdHz)')
IDplot
FillPage('w')



	

