-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathboss_DS1_transistor.m
283 lines (232 loc) · 7.4 KB
/
boss_DS1_transistor.m
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
% Boss DS-1 Transistor Stage
% Mac Porter
clear all
close all
clc
%% Parameters
% General
Fs = 44100; % Sample rate
len = .01; % Length of simulation (s)
inputType = 'audio'; % 'impulse', 'sine', or 'audio'
filename = 'TestGuitarPhraseMono.wav'; % Filename for audio
sinFreq = 1000; % Frequency for sine wave (Hz)
% Effect Parameters
inGain = 1; % Input gain (v)
Is = 6.734e-15; % Transistor saturation current (A)
Bf = 200; % Forward current gain
Br = 0.1; % Reverse current gain
battNoise = 0.05; % Amount of battery noise (0 to 1)
% Plotting
playSound = 'on';
plotTime = 'off'; % Plot time
plotFreq = 'off'; % Plot frequency
% For Newton solver
tol = 10e-7; % Error tolerance
maxIters = 100; % Number of allowed iterations
maxSubIters = 10; % Number of allowed sub-iterations
%% Derived Parameters
T = 1/Fs; % Sample period
% Input signal
if strcmp(inputType,'impulse')
N = floor(len*Fs);
t = (0:T:N*T-T);
in = [1e-6;zeros(N-1,1)];
elseif strcmp(inputType,'sine')
N = floor(len*Fs);
t = (0:T:N*T-T);
in = inGain*sin(2*pi*sinFreq*t);
elseif strcmp(inputType,'audio')
in = inGain*audioread(filename);
N = length(in);
t = (0:T:N*T-T);
end
if strcmp(inputType,'impulse')
Vcc = zeros(1,N);
else
Vcc = 9+battNoise*sin(120*pi*t); % Battery signal
end
fx = (0:N-1).*Fs/N; % Frequency vector
out = zeros(N,1); % Initialize output
%% Physical Parameters
R1 = 100e3;
R2 = 10e3;
R3 = 470e3;
R4 = 22;
R5 = 100e3;
C1 = .047e-6;
C2 = 250e-12;
C3 = .47e-6;
% Precomputed constants
invVt = 1/25.85e-3;
invBf = 1/Bf;
invBr = 1/Br;
IsVtBf = Is*invBf*invVt;
IsVtBr = Is*invBr*invVt;
%% System matrices
A = [-1/(R1*C1)-1/(R2*C1)-1/(R5*C1) -1/(R2*C1)-1/(R5*C1) -1/(R5*C1);
-1/(R2*C2)-1/(R5*C2) -1/(R2*C2)-1/(R3*C2)-1/(R5*C2) -1/(R5*C2);
-1/(R5*C3) -1/(R5*C3) -1/(R5*C3)];
B = [1/(R1*C1)+1/(R2*C1)+1/(R5*C1) -1/(R2*C1);
1/(R2*C2)+1/(R5*C2) -1/(R2*C2); 1/(R5*C3) 0];
C = [1/C1 1/C1; 0 1/C2; 0 0];
D = [-1 -1 -1];
E = [1 0];
F = [0 0];
G = [-1 0 0; 0 1 0];
H = [1 0; 0 0];
K = [-R4 -R4; 0 0];
%% DC Analysis - to find steady state values
% In a real time implementation, this could be precalculated
% DC system matrices
Ra = R1+R2+R3;
Adc = [-R1/Ra; -R3/Ra; 1-R2/Ra];
Bdc = [R1-R1*R1/Ra R1-(R1*R1+R1*R3)/Ra; -R1*R3/Ra R3-(R1*R3+R3*R3)/Ra;...
-R1*R2/Ra -(R1*R2+R2*R3)/Ra];
Cdc = [R1/Ra; -R3/Ra];
Ddc = [R1*R1/Ra-R1-R4 (R1*R1+R1*R3)/Ra-R1-R4; -R1*R3/Ra R3-(R1*R3+R3*R3)/Ra];
u = 9;
v = [0;0];
error = 1;
iters = 0;
% Damped Newton
while (error > tol) && (iters < maxIters)
% Precomputation
VbeVt = v(1)*invVt;
VbcVt = v(2)*invVt;
% Vector of transistor base and collector currents
i = [Is*(invBf*(exp(VbeVt)-1)+invBr*(exp(VbcVt)-1));...
Is*((exp(VbeVt)-1)-(1+Br)*invBr*(exp(VbcVt)-1))];
% Partial derivatives of transistor currents
iDer = [IsVtBf*exp(VbeVt) IsVtBr*exp(VbcVt);...
Is*invVt*exp(VbeVt) -IsVtBr*(1+Br)*exp(VbcVt)];
M = Cdc*u+Ddc*i-v; % Function to solve
J = Ddc*iDer-eye(2); % Jacobian matrix
step = J\M; % Newton step
vNew = v-step; % New transistor voltages
VbeVtNew = vNew(1)*invVt;
VbcVtNew = vNew(2)*invVt;
% New transistor currents
iNew = [Is*(invBf*(exp(VbeVtNew)-1)+invBr*(exp(VbcVtNew)-1));...
Is*((exp(VbeVtNew)-1)-(1+Br)*invBr*(exp(VbcVtNew)-1))];
MNew = Cdc*u+Ddc*iNew-vNew; % Updated function
% Apply damping if the step goes in the wrong direction, or if the
% function goes to inf
subStep = step;
subIters = 0;
while ((norm(MNew) > norm(M)) && (subIters < maxSubIters))...
|| (isnan(norm(MNew))) || (isinf(norm(MNew)))
subStep = subStep/2;
vNew = v-subStep;
VbeVtNew = vNew(1)*invVt;
VbcVtNew = vNew(2)*invVt;
iNew = [Is*(invBf*(exp(VbeVtNew)-1)+invBr*(exp(VbcVtNew)-1));...
Is*((exp(VbeVtNew)-1)-(1+Br)*invBr*(exp(VbcVtNew)-1))];
MNew = Cdc*u+Ddc*iNew-vNew;
subIters = subIters+1;
end
error = norm(vNew-v)/norm(v); % Relative error
iters = iters+1;
v = vNew; % Final transistor voltages
end
VbeVt = v(1)*invVt;
VbcVt = v(2)*invVt;
% Transistor currents
i = [Is*(invBf*(exp(VbeVt)-1)+invBr*(exp(VbcVt)-1));...
Is*((exp(VbeVt)-1)-(1+Br)*invBr*(exp(VbcVt)-1))];
% Steady state voltages across the capacitors
x = Adc*u+Bdc*i;
%% Simulation
if strcmp(inputType,'impulse')
x = [0;0;0];
i = [0;0];
v = [0;0];
uprev = [0;0];
else
uprev = [0;9];
end
xprev = x;
iprev = i;
% Matrix inversion for trapezoid discretization
Q = inv(2*Fs*eye(3)-A);
% Precomputation
GQCK = G*Q*C+K;
% Main loop
for n = 1:N
u = [in(n);Vcc(n)]; % Input
error = 1;
iters = 0;
% Constant term from discretization
r = G*Q*(2*Fs*eye(3)+A)*xprev+(G*Q*B)*uprev+(G*Q*B+H)*u+(G*Q*C)*iprev;
% Damped Newton
while (error > tol) && (iters < maxIters)
% Precomputation
VbeVt = v(1)*invVt;
VbcVt = v(2)*invVt;
% Vector of transistor base and collector currents
i = [Is*(invBf*(exp(VbeVt)-1)+invBr*(exp(VbcVt)-1));...
Is*((exp(VbeVt)-1)-(1+Br)*invBr*(exp(VbcVt)-1))];
% Partial derivatives of transistor currents
iDer = [IsVtBf*exp(VbeVt) IsVtBr*exp(VbcVt);...
Is*invVt*exp(VbeVt) -IsVtBr*(1+Br)*exp(VbcVt)];
M = r+(GQCK)*i-v; % Function to solve
J = (GQCK)*iDer-eye(2); % Jacobian matrix
step = J\M; % Newton step
vNew = v-step; % New transistor voltages
VbeVtNew = vNew(1)*invVt;
VbcVtNew = vNew(2)*invVt;
% New transistor currents
iNew = [Is*(invBf*(exp(VbeVtNew)-1)+invBr*(exp(VbcVtNew)-1));...
Is*((exp(VbeVtNew)-1)-(1+Br)*invBr*(exp(VbcVtNew)-1))];
MNew = r+(GQCK)*iNew-vNew; % Updated function
% Apply damping if the step goes in the wrong direction, or if the
% function goes to inf
subStep = step;
subIters = 0;
while ((norm(MNew) > norm(M)) && (subIters < maxSubIters))...
|| (isnan(norm(MNew))) || (isinf(norm(MNew)))
subStep = subStep/2;
vNew = v-subStep;
VbeVtNew = vNew(1)*invVt;
VbcVtNew = vNew(2)*invVt;
iNew = [Is*(invBf*(exp(VbeVtNew)-1)+invBr*(exp(VbcVtNew)-1));...
Is*((exp(VbeVtNew)-1)-(1+Br)*invBr*(exp(VbcVtNew)-1))];
MNew = r+(GQCK)*iNew-vNew;
subIters = subIters+1;
end
error = norm(vNew-v)/norm(v); % Relative error
iters = iters+1;
v = vNew; % Final transistor voltages
end
VbeVt = v(1)*invVt;
VbcVt = v(2)*invVt;
% Transistor currents
i = [Is*(invBf*(exp(VbeVt)-1)+invBr*(exp(VbcVt)-1));...
Is*((exp(VbeVt)-1)-(1+Br)*invBr*(exp(VbcVt)-1))];
% State update
x = Q*((2*Fs*eye(3)+A)*xprev+B*(u+uprev)+C*(i+iprev));
% Output
y = D*x+E*u+F*i;
out(n) = y;
% Update previous values
xprev = x;
uprev = u;
iprev = i;
end
if strcmp(playSound,'on')
soundsc(out,Fs);
end
%% Plots
if strcmp(plotTime,'on')
figure();
plot(t,out);
xlabel('Time (s)');
ylabel('Volts');
end
if strcmp(plotFreq,'on')
Y = 1e6*abs(fft(out));
figure();
semilogx(fx,20*log10(Y));
xlim([20 20000]);
xlabel('Freq (Hz)');
ylabel('dB');
end