-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximize_gain_singlecoil.ngjs
executable file
·60 lines (54 loc) · 1.7 KB
/
maximize_gain_singlecoil.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
53
54
55
56
57
58
59
60
#!/usr/bin/env ngspicejs
// Maximize gain of common emitter, manually tuned to 217, then optimized to 224
// linter: ngspicejs-lint
"use strict";
battery('U1', 14, 0, 9);
ammeter('A1', 14, 15);
var r1 = resistor('R1', 15, 6, '4k7');
var r2 = resistor('R2', 16, 6, '100k');
resistor('R3', 9, 0, '50k');
capacitor('C1', 11, 16, '4u7');
capacitor('C2', 6, 9, '4u7');
npn('T1', 6, 16, 0, 'BC547');
//sinewave('U2', 11, 0).v('1m').f(196);
pickup_singlecoil('L1', 11, 0).v('1m').f(196).damping(0);
//pickup_humbucker('L1', 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(A1)'].avg().toEng());
//ac().fstop('20k').run().chart('V(9)');
var values = series_e6(10, '10M');
var best = {r1: 0, r2: 0, gain: 0};
echo('Maximizing gain... ', values.length * values.length, 'attempts');
var csv = [['R1', 'R2', 'Gain']];
values.forEach(function (a) {
values.forEach(function (b) {
r1.r(a);
r2.r(b);
//var g = tran().run().gain(11, 9);
t = tran().run();
var g = t.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);
}
//echo('R1=' + a.toEng(), 'R2=' + b.toEng(), ' Gain=' + g);
csv.push([a,b,g]);
});
});
echo_json(best);
//file_write_csv('scatter.csv', csv);
r1 = csv.column(0, 1);
r2 = csv.column(1, 1);
var gain = csv.column(2, 1);
chart_scatter(r1, r2, 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_singlecoil.gif');