-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximize_gain_3V3.ngjs
executable file
·53 lines (48 loc) · 1.46 KB
/
maximize_gain_3V3.ngjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env ngspicejs
// Maximize gain of common emitter with 3.3V battery, manually tuned to 22.1x, then optimized to 22.8x
// linter: ngspicejs-lint
"use strict";
// Common emitter
battery('U1', 15, 0, 9);
var r1 = resistor('R1', 15, 16, '68k');
var r2 = resistor('R2', 16, 0, '6k8');
resistor('R3', 15, 6, '10k');
resistor('R4', 13, 0, '330');
resistor('R5', 9, 0, '50k');
capacitor('C1', 11, 16, '4u7');
capacitor('C2', 6, 9, '4u7');
npn('T1', 6, 16, 13, 'BC547');
sinewave('U2', 11, 0).v('1m').f(196);
var t = tran().run().chart('V(9)');
echo('Gain', t.gain(11, 9).toFixed(3));
echo('Current', t.data['I(U1)'].avg().toEng());
// Try R1 R2 from 10R to 10M
var values = series_e6(10, '10M');
var best = {r1: 0, r2: 0, gain: t.gain(11, 9).toFixed(3)};
echo('Maximizing gain... ', values.length * values.length, 'attempts');
var x = [], y = [], gain = [];
values.forEach(function (a) {
values.forEach(function (b) {
r1.r(a);
r2.r(b);
var g = tran().run().gain(11, 9);
if (g > best.gain) {
best.r1 = a;
best.r2 = b;
best.gain = g;
echo('R1=' + a.toEng(), 'R2=' + b.toEng(), ' Gain=' + g.toFixed(1));
}
x.push(a);
y.push(b);
gain.push(g);
});
});
chart_scatter(x, y, gain)
.log_x(true)
.log_y(true)
.size(20)
.title('Gain of common emitter depending on R1, R2')
.label_x('R1')
.label_y('R2')
.show()
.gif('maximize_gain_3V3.gif');