clear all;
close all;
clc;

%% Parameters
Adc = 1e4;
wc = 2*pi*10e3;

Rin = 10e9;
Cin = 1.2e-12;
Rfb = 333e3;
Cfb = 170e-15;
rout = 2.5e6;
Cl = 1.2e-12;


%% Impedances
s = tf('s');

Zin = 1/(1/Rin+s*Cin);
Zfb = 1/(1/Rfb+s*Cfb);
Zout = rout;
Zl = 1/(s*Cl);

Zin = zpk(Zin);
Zfb = zpk(Zfb);
Zout = zpk(Zout);
Zl = zpk(Zl);

Za = 1/(1/Zout+1/Zl);
Za = zpk(Za);
Zb = 1/(1/(Zin+Zfb)+1/Zl);
Zb = zpk(Zb);

%% Open loop transfer function
A = Adc/(1+s/wc);
A = zpk(A);
A = minreal(A);

%% Loop parameters

ain = Za/(Za+Zin+Zfb);
ain = zpk(ain);
ain = minreal(ain);

a1 = Zb/(Zb+Zout);
a1 = zpk(a1);
a1 = minreal(a1);

bin = (Za+Zfb)/(Za+Zfb+Zin);
bin = zpk(bin);
bin = minreal(bin);

b1 = Zb/(Zb+Zout)*Zin/(Zfb+Zin);
b1 = zpk(b1);
b1 = minreal(b1);

Ab = -A;


%% Voltage transfer function

H_Amp = ain+(bin*Ab*a1)/(1-Ab*b1);
H_Amp = zpk(H_Amp);
H_Amp = minreal(H_Amp);

figure(1);
title("H_amp");
bode(H_Amp);


%% Tia transfer function

H_tia = (ain*(1-b1*Ab)+bin*Ab*a1)*Zin/(1-b1*Ab-bin);
H_tia = zpk(H_tia);
H_tia = minreal(H_tia);

figure(2);
title("H_tia");
bode(H_tia);




