Jose Lopez
Jose Lopez

Reputation: 11

Instrumental Amplifier with Monte Carlo in NGSPICE

I want to try to do the Monte Carlo in this circuit with an instrumental amplifier, but the loop does not work for me, I try to export in a txt file, but the 100 iterations are accumulated in the first one. Everything is explained in the following code, just add the TL-081.cir amplifier, or any other amplifier, first describe the circuit and then create a function for the Monte-Carlo.

* Amplificador Instrumental
.include tl081.cir

* Alimentación
vcc vcc 0 ac 0V dc 15V
vee 0 vee ac 0V dc 15V

* Arousals
*Vs in 0 ac 1V dc 0

Vc inc 0 ac 0V dc 0
Vd ind 0 ac 1V dc 0
Ed1 2 inc ind 0 {1/2}
Ed2 5 inc ind 0 {-1/2}

* Filter
R1 4 8 10k
R2 8 out 10k
R3 3 9 10k
R4 9 0 10k
R5 1 3 10k
R6 6 4 10k
R7 1 6 82

X1A 2 1 vcc vee 3 TL081
X1B 5 6 vcc vee 4 TL081
X1C 9 8 vcc vee out TL081

* Analisis

.control

** CRun at Nominal Value
*ac dec 20 1 1MegHz
*plot v(out)
*meas ac Ad find v(out) at=100

define ngauss( nom, var, n)(nom + nom *(var/2)*sgauss(vector(n)))
let Nmc = 100 
let run = 1

** Vectors of Random Component Values
let R1=ngauss(10k,0.05,Nmc)
let R2=ngauss(10k,0.05,Nmc)
let R3=ngauss(10k,0.05,Nmc)
let R4=ngauss(10k,0.05,Nmc)
let R5=ngauss(10k,0.05,Nmc)
let R6=ngauss(10k,0.05,Nmc)
let R7=ngauss(82,0.05,Nmc)

** Network functions
let AAd = ngauss(0,1,Nmc)
let AAc = vector(Nmc)

** Open the file to write
write Montecarlo_results.txt AAd AAc    

dowhile run < Nmc ;
alter R1=R1[run]
alter R2=R2[run]
alter R3=R3[run]
alter R4=R4[run]
alter R5=R5[run]
alter R6=R6[run]
alter R7=R7[run]

*echo **Differential Mode
alter Vc ac=0V
alter Vd ac=1V

ac dec 25 1Hz 10kHz
let Adf = abs(v(out)/v(2,5));
meas ac AAd[run] find Adf at=100Hz

*echo **Common Mode
alter Vc ac=1V 
alter Vd ac=0V

ac dec 25 1Hz 10kHz
let Acf = abs(v(out)/(v(2)+v(5))/2); 
meas ac AAc[run] find Acf at=100Hz

let run = run + 1

** Write the results to the text file 
write Montecarlo_results.txt AAd[run] AAc[run]
print AAd[run] AAc[run]
end;

.endc
.end

Upvotes: 0

Views: 17

Answers (0)

Related Questions