-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathexample3_setup.py
More file actions
133 lines (100 loc) · 4.17 KB
/
example3_setup.py
File metadata and controls
133 lines (100 loc) · 4.17 KB
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
import numpy as np
import matplotlib.pyplot as plt
import time
from scipy.signal import cheby1, freqs
def simControl():
# user-supplied file paths
spicePath = r'C:\\Program Files\\LTC\\LTspiceXVII\\XVIIx64.exe' # This is the path to your LT Spice installation
filePath = r'C:\\Users\\radam\\Documents\\LTspiceXVII\\' # This is the path to the working LTSPICE folder (schems, netlists, simulation output files)
fileName = 'example3'
# simControlOPtInstNames = ['R1', 'C1', 'L1', 'R7']
# simControlMinVals = [10, 100e-12, 1e-6, 10]
# simControlMaxVals = [1e6, 1e-7, 0.1, 1e6]
# simControlInstTol = ['E96', 'E96', 'E96', 'E96']
simControlOPtInstNames = ['R1', 'C1', 'R7']
simControlMinVals = [10, 100e-12, 10]
simControlMaxVals = [1e6, 1e-7, 1e6]
simControlInstTol = ['E96', 'E24', 'E96'] # 1% R, 5% C
LTSPice_output_node = 'V(vout)'
# Set the match mode.
# 1 = amplitude only
# 2 = phase only
# 3 = amplitude and phase both
matchMode = 1
# max # of iterations in he particle-swarm global optimization phase (enter 0 to skip)
maxIter_ps = 20
# max # of iterations in the least-squares optimization phase (enter 0 to skip lsq)
maxIter_lsq = 70
# return a dict
simControlDict = {}
simControlDict['fileNameD'] = fileName
simControlDict['spicePathD'] = spicePath
simControlDict['filePathD'] = filePath
simControlDict['simControlOPtInstNamesD'] = simControlOPtInstNames
simControlDict['simControlMinValsD'] = simControlMinVals
simControlDict['simControlMaxValsD'] = simControlMaxVals
simControlDict['simControlInstTolD'] = simControlInstTol
simControlDict['LTSPice_output_nodeD'] = LTSPice_output_node
simControlDict['matchModeD'] = matchMode
simControlDict['maxIter_lsqD'] = maxIter_lsq
simControlDict['maxIter_psD'] = maxIter_ps
return simControlDict
def setTarget(freqx, match_mode):
# User-defined target response. The user-defined response must be calculated
# at the same frequencies used in the LTSpice sim
# freqx = freqs returned by initial LTSpice simulation
# match_mode = 1, 2, or 3 (ampl only, phase only, or both)
# Default values for the frequency-dependent weighting functions
print('found ',len(freqx),' freqs in simulation')
err_weights_ampl = np.ones(len(freqx))
err_weights_phase = np.ones(len(freqx))
target_ampl = np.ones(len(freqx))
target_phase = np.ones(len(freqx))
if match_mode == 3: # match ampl + phase; return error is concatenation, so need 2X length
target = np.ones(2*len(freqx))
err_weights = np.ones(2*len(freqx))
else:
target = np.ones(len(freqx))
err_weights = np.ones(len(freqx))
X = np.linspace(0, 80000, 21) # every 4KHz from datasheet plot
VdB = [0, 0.5, 1, 1.5, 2, 3, 4.8, 7, 11, 17, 22, 13, 8, 5, 6, 4.5, 3, 2.5, 3, 3, 4.5] # from datasheet plot, freq resp in dB
mic_dB = np.interp(freqx, X, VdB)
#mic_dB = mic_dB+26 # set 26 dB gain target at DC
plt.figure()
plt.semilogx(freqx, mic_dB)
plt.title('Mic dB response')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain (dB)')
plt.grid(True)
#(block=False)
mic_lin = 10**(mic_dB / 20)
plt.figure()
plt.semilogx(freqx, mic_lin)
plt.title('Mic linear response')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.grid(True)
#plt.show(block=False)
target = 30.0 / mic_lin
err_weights = np.sqrt(mic_lin) # greatest weights where the target is smallest
plt.figure()
plt.semilogx(freqx, target)
plt.title('Inverse mic response, linear')
plt.xlabel('Frequency (Hz)')
plt.ylabel('Gain')
plt.grid(True)
#plt.show(block=False)
# plot the target response and error weighting function
plt.figure()
plt.subplot(2, 1, 1)
plt.semilogx(freqx, target)
plt.title('target response')
plt.ylim([min(target)-0.5, max(target)+0.5])
plt.subplot(2, 1, 2)
plt.semilogx(freqx, err_weights)
plt.ylim([min(err_weights)-0.5, max(err_weights)+0.5])
plt.title('errWeights')
plt.tight_layout()
#(block=False)
# Continue with other examples or user-defined cases
return target, err_weights