Trouble simulating an RC low and high pass filter with trapoziodal aproximation
Hi,
I have been working to recreate the example code found in Andrew Simper’s 2020 lecture at Cytomic but have run into a roadblock. I can’t seem to get a reasonable capacitor approximation and am not sure why. You can see the relevant code on page 45 of the PDF here (lecture). I’ve attached my code below as well.
If you have any suggestions, please let me know. Thank you for your time.
%% circuit variables
Is=1e-12; % Saturation current (in Amperes)
n = 2; % Ideality factor (typically between 1 and 2)
temp = 300; % Temperature in Kelvin
k = 1.380649e-23; % Boltzmann constant (J/K)
q = 1.602176634e-19; % Electron charge (C)
% Calculate thermal voltage
vt=(k*temp)/q; % Thermal voltage (in Volts)
R1=2.2e3;
R2=6.8e3;
C1=0.47e-6;
C2=0.01e-6;
%% simulation loop
fs=48e3; %sample rate
Fd=1000; %input frequnecy
T=(1/1000)*10;
time=0:1/fs:T; %create time vector
vs=2.*sawtooth(Fd*2*pi*time); %create sawtooth input
%figure,plot(time,vs), title(‘input wavefrom (voltage v time’)
gr1=1/R1;%deinfe gr1 as the reciprocal of r1 for easier eq formation
gr2=1/R2;
m=1/2; %define division integral aproximation
h=1/fs; %define hight of the traps
gc1=C1/(h*m);
gc2=C2/(h*m);
ic1eq=0;
ic2eq=0;
%initalize vectors
v1=zeros(1,length(vs));
v2=zeros(1,length(vs));
v3=zeros(1,length(vs));
va1=0;
va2=0;
va3=0;
for k=1:length(vs)
va1=vs(k);
v1(k)=va1;
va2=(gr1*va1+gc1*va3-ic1eq)/(gr1+gc1);
v2(k)=va2;
va3=(gc1*va2+ic1eq+ic2eq)/(gc2+gc1+gr2);
%va3=(gc1*ic2eq+gr1*(ic1eq+ic2eq+gc1*va1))/(gr1*(gc2+gr2)+gc1*(gc2+gr1+gr2));
v3(k)=va3;
ic1eq = ic1eq + (1/m)*(gc1*(va3-va2)-ic1eq);
ic2eq = ic2eq + (1/m)*(gc2*(va3)-ic2eq);
end
figure, plot(time,v3)
title(‘input wave form vs output’), xlabel(‘time’), ylabel(‘voltage’)
hold on
plot(time,v1), legend(‘v3′,’v1’)Hi,
I have been working to recreate the example code found in Andrew Simper’s 2020 lecture at Cytomic but have run into a roadblock. I can’t seem to get a reasonable capacitor approximation and am not sure why. You can see the relevant code on page 45 of the PDF here (lecture). I’ve attached my code below as well.
If you have any suggestions, please let me know. Thank you for your time.
%% circuit variables
Is=1e-12; % Saturation current (in Amperes)
n = 2; % Ideality factor (typically between 1 and 2)
temp = 300; % Temperature in Kelvin
k = 1.380649e-23; % Boltzmann constant (J/K)
q = 1.602176634e-19; % Electron charge (C)
% Calculate thermal voltage
vt=(k*temp)/q; % Thermal voltage (in Volts)
R1=2.2e3;
R2=6.8e3;
C1=0.47e-6;
C2=0.01e-6;
%% simulation loop
fs=48e3; %sample rate
Fd=1000; %input frequnecy
T=(1/1000)*10;
time=0:1/fs:T; %create time vector
vs=2.*sawtooth(Fd*2*pi*time); %create sawtooth input
%figure,plot(time,vs), title(‘input wavefrom (voltage v time’)
gr1=1/R1;%deinfe gr1 as the reciprocal of r1 for easier eq formation
gr2=1/R2;
m=1/2; %define division integral aproximation
h=1/fs; %define hight of the traps
gc1=C1/(h*m);
gc2=C2/(h*m);
ic1eq=0;
ic2eq=0;
%initalize vectors
v1=zeros(1,length(vs));
v2=zeros(1,length(vs));
v3=zeros(1,length(vs));
va1=0;
va2=0;
va3=0;
for k=1:length(vs)
va1=vs(k);
v1(k)=va1;
va2=(gr1*va1+gc1*va3-ic1eq)/(gr1+gc1);
v2(k)=va2;
va3=(gc1*va2+ic1eq+ic2eq)/(gc2+gc1+gr2);
%va3=(gc1*ic2eq+gr1*(ic1eq+ic2eq+gc1*va1))/(gr1*(gc2+gr2)+gc1*(gc2+gr1+gr2));
v3(k)=va3;
ic1eq = ic1eq + (1/m)*(gc1*(va3-va2)-ic1eq);
ic2eq = ic2eq + (1/m)*(gc2*(va3)-ic2eq);
end
figure, plot(time,v3)
title(‘input wave form vs output’), xlabel(‘time’), ylabel(‘voltage’)
hold on
plot(time,v1), legend(‘v3′,’v1’) Hi,
I have been working to recreate the example code found in Andrew Simper’s 2020 lecture at Cytomic but have run into a roadblock. I can’t seem to get a reasonable capacitor approximation and am not sure why. You can see the relevant code on page 45 of the PDF here (lecture). I’ve attached my code below as well.
If you have any suggestions, please let me know. Thank you for your time.
%% circuit variables
Is=1e-12; % Saturation current (in Amperes)
n = 2; % Ideality factor (typically between 1 and 2)
temp = 300; % Temperature in Kelvin
k = 1.380649e-23; % Boltzmann constant (J/K)
q = 1.602176634e-19; % Electron charge (C)
% Calculate thermal voltage
vt=(k*temp)/q; % Thermal voltage (in Volts)
R1=2.2e3;
R2=6.8e3;
C1=0.47e-6;
C2=0.01e-6;
%% simulation loop
fs=48e3; %sample rate
Fd=1000; %input frequnecy
T=(1/1000)*10;
time=0:1/fs:T; %create time vector
vs=2.*sawtooth(Fd*2*pi*time); %create sawtooth input
%figure,plot(time,vs), title(‘input wavefrom (voltage v time’)
gr1=1/R1;%deinfe gr1 as the reciprocal of r1 for easier eq formation
gr2=1/R2;
m=1/2; %define division integral aproximation
h=1/fs; %define hight of the traps
gc1=C1/(h*m);
gc2=C2/(h*m);
ic1eq=0;
ic2eq=0;
%initalize vectors
v1=zeros(1,length(vs));
v2=zeros(1,length(vs));
v3=zeros(1,length(vs));
va1=0;
va2=0;
va3=0;
for k=1:length(vs)
va1=vs(k);
v1(k)=va1;
va2=(gr1*va1+gc1*va3-ic1eq)/(gr1+gc1);
v2(k)=va2;
va3=(gc1*va2+ic1eq+ic2eq)/(gc2+gc1+gr2);
%va3=(gc1*ic2eq+gr1*(ic1eq+ic2eq+gc1*va1))/(gr1*(gc2+gr2)+gc1*(gc2+gr1+gr2));
v3(k)=va3;
ic1eq = ic1eq + (1/m)*(gc1*(va3-va2)-ic1eq);
ic2eq = ic2eq + (1/m)*(gc2*(va3)-ic2eq);
end
figure, plot(time,v3)
title(‘input wave form vs output’), xlabel(‘time’), ylabel(‘voltage’)
hold on
plot(time,v1), legend(‘v3′,’v1’) signal processing, filters MATLAB Answers — New Questions