-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerateVariableInfluence.py
439 lines (348 loc) · 18.7 KB
/
generateVariableInfluence.py
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
###########################################################################
#
# StatOpt Simulator
# by Jeremy Cosson-Martin, Jhoan Salinas of
# Ali Sheikholeslami's group
# Ported to Python 3 by Savo Bajic
# Department of Electrical and Computer Engineering
# University of Toronto
# Copyright Material
# For personal use only
#
###########################################################################
# This function creates all variable sources which influence the resultant
# signal distribution. This is to say, sources which may change based on
# settings. For example, the receiver output refered noise will depend on
# the receiver gain and equalization.
#
# Inputs:
# simSettings: structure containing simulation settings
# simResults: structure containing simulation results
#
###########################################################################
from userSettingsObjects import simulationSettings, nothing
from initializeSimulation import simulationStatus
import control.matlab as ml
import numpy as np
import scipy.stats as stats
def generateVariableInfluence(simSettings: simulationSettings, simResults: simulationStatus):
ml.use_matlab_defaults() # Needed to ensure compatibility with MATLAB expectations for control code
# Generate CTLE responses
generateCTLE(simSettings, simResults)
# Calculate RMS of RX FFE tap values
calculateFFERMS(simSettings, simResults)
# Generate transmitter noise distribution
generateTXNoise(simSettings, simResults)
# Generate channel noise distribution
generateChannelNoise(simSettings, simResults)
# Generate receiver noise distribution
generateRXNoise(simSettings, simResults)
# Combine influences
combineInfluences(simSettings, simResults)
class CTLE:
def __init__(self, tf, mag, ph, freq):
self.transferFunc = tf
self.magnitude = mag
self.phase = ph
self.frequency = freq
###########################################################################
# This function determines if the CTLE response for the given knob settings
# has already been calculated. If not it calculates the transfer function.
###########################################################################
def generateCTLE(simSettings: simulationSettings, simResults: simulationStatus):
# Import variables
addEqualization = simSettings.receiver.CTLE.addEqualization
zeroFreq = simSettings.receiver.CTLE.zeroFreq.value
zeroNumb = simSettings.receiver.CTLE.zeroNumb.value
pole1Freq = simSettings.receiver.CTLE.pole1Freq.value
pole1Numb = simSettings.receiver.CTLE.pole1Numb.value
pole2Freq = simSettings.receiver.CTLE.pole2Freq.value
pole2Numb = simSettings.receiver.CTLE.pole2Numb.value
channelFreqs = simResults.influenceSources.channel.thru.frequencies
zeroName = ('z' + str(zeroFreq/1e9)).replace('.', '_')
poleName = ('z' + str(pole1Freq/1e9)).replace('.', '_')
# Define CTLE transfer function
if addEqualization:
# Determine if TF already calculated
if 'RXCTLE' in simResults.influenceSources.__dict__:
CTLEs = simResults.influenceSources.RXCTLE
else:
setattr(simResults.influenceSources, 'RXCTLE', nothing())
CTLEs = []
calculated = True
if CTLEs == []:
calculated = False
elif not zeroName in CTLEs.__dict__:
calculated = False
elif not poleName in CTLEs.__dict__[zeroName].__dict__:
calculated = False
# Return if CTLE already calculated
if calculated: return
# Calculate new TF
else:
# Add first zero
wz = 2*np.pi*zeroFreq
listWz = -np.ones((zeroNumb,)) * wz
gain = (1/wz) ** zeroNumb
# Add first pole
wp1 = 2*np.pi*pole1Freq
listWp1 = -np.ones((pole1Numb,)) * wp1
gain = gain * ((wp1) ** pole1Numb)
# Add additional poles
wp2 = 2*np.pi*pole2Freq
listWp2 = -np.ones((pole2Numb,)) * wp2
gain = gain * ((wp2) ** pole2Numb)
# Combine pole lists
listWp = np.concatenate((listWp1, listWp2))
# Combine transfer functions
transferFunc = ml.zpk(listWz,listWp,gain)
else:
transferFunc = ml.tf([1],[1])
# Save results
magnitude, phase, w = ml.bode(transferFunc, 2*np.pi*channelFreqs, plot=False) # force frequencies to be same as channel
temp = CTLE(transferFunc, magnitude, phase, channelFreqs)
if 'RXCTLE' not in simResults.influenceSources.__dict__:
setattr(simResults.influenceSources, 'RXCTLE', nothing())
setattr(simResults.influenceSources.RXCTLE, zeroName, nothing())
setattr(simResults.influenceSources.RXCTLE.__dict__[zeroName], poleName, temp)
###########################################################################
# This function calculates the RMS value of the RX FFE tap settings. This
# value is required later for output-refering noise.
###########################################################################
def calculateFFERMS(simSettings: simulationSettings, simResults: simulationStatus):
# Import variables
taps = simSettings.receiver.FFE.taps
# Calculate RMS of taps (used for noise analysis)
FFESum = 0
for tap in taps.__dict__:
FFESum = FFESum + taps.__dict__[tap].value ** 2
tapRMS = np.sqrt(FFESum)
# Save results
setattr(simResults, 'pulseResponse', nothing())
setattr(simResults.pulseResponse, 'receiver', nothing())
setattr(simResults.pulseResponse.receiver, 'FFE', nothing())
setattr(simResults.pulseResponse.receiver.FFE, 'tapRMS', tapRMS)
###########################################################################
# This function creates a probability distribution for the transmitter
# noise. This function adds random aswell as deterministic noise. Since the
# noise is created at the transmitter, it is attenuated by the channel,
# amplified by the CTLE, and amplitied by the FFE. Thus the equivalent
# noise must first be calculated. To do so, it is assumed that the gaussian
# noise is white noise up to the bandwidth of the transmitter. The power of
# this white distribution is sent through the channel and CTLE power
# transfer function. The equivalent power at the receiver is calculated and
# a corresponding gaussian distribution is created.
###########################################################################
def generateTXNoise(simSettings: simulationSettings, simResults: simulationStatus):
# Import variables
yAxis = simSettings.general.yAxis.value
yAxisLength = simSettings.general.yAxisLength.value
yIncrement = simSettings.general.yIncrement.value
TXBandwidth = simSettings.transmitter.TXBandwidth.value
addNoise = simSettings.transmitter.noise.addNoise
stdDeviation = simSettings.transmitter.noise.stdDeviation.value
sineAmp = simSettings.transmitter.noise.amplitude.value
sineFreq = simSettings.transmitter.noise.frequency.value
supplyVoltage = simSettings.receiver.signalAmplitude.value
usePreAmp = simSettings.receiver.preAmp.addGain
gain = simSettings.receiver.preAmp.gain.value
useCTLE = simSettings.receiver.CTLE.addEqualization
zeroFreq = simSettings.receiver.CTLE.zeroFreq.value
pole1Freq = simSettings.receiver.CTLE.pole1Freq.value
useFFE = simSettings.receiver.FFE.addEqualization
FFERMS = simResults.pulseResponse.receiver.FFE.tapRMS
channelFreqs = simResults.influenceSources.channel.thru.frequencies
channelTF = simResults.influenceSources.channel.thru.transferFunction
zeroName = ('z' + str(zeroFreq/1e9)).replace('.', '_')
poleName = ('z' + str(pole1Freq/1e9)).replace('.', '_')
CTLEMagnitude = simResults.influenceSources.RXCTLE.__dict__[zeroName].__dict__[poleName].magnitude
# Add random noise
if addNoise and stdDeviation != 0:
# Create noise frequency distribution in transmitter
power = stdDeviation ** 2
maxIndex = int(np.interp(TXBandwidth, channelFreqs, np.arange(len(channelFreqs))))
if np.isnan(maxIndex):
maxIndex=len(channelFreqs)
powerDistribution = np.concatenate((np.ones((maxIndex,))*(power/TXBandwidth) , np.zeros((len(channelFreqs)-maxIndex,))))
# Calculate equivalent noise at receiver output
powerDistribution = powerDistribution * (abs(channelTF) ** 2)
if usePreAmp:
powerDistribution = powerDistribution * (gain ** 2)
if useCTLE:
powerDistribution = powerDistribution * (CTLEMagnitude ** 2)
if useFFE:
powerDistribution = powerDistribution * (FFERMS ** 2)
freqIncrement = channelFreqs[2]-channelFreqs[1]
outputPower = np.sum(powerDistribution)*freqIncrement
stdDeviationOutput = np.sqrt(outputPower)
# Create noise distribution
randNoise = stats.norm.pdf(yAxis, loc=0, scale=stdDeviationOutput)
randNoise = randNoise/np.sum(randNoise) # normalize PDF
else:
randNoise = 1 # perfect impulse
# Add deterministic noise
if addNoise and sineAmp != 0:
# Calculate equivalent noise at receiver output
freqIndex = np.interp(sineFreq, channelFreqs, np.arange(len(channelFreqs)))
sineAmp = sineAmp*abs(np.interp(freqIndex, np.arange(len(channelTF)),channelTF))
if usePreAmp:
sineAmp = sineAmp*gain
if useCTLE:
sineAmp = sineAmp*abs(np.interp(freqIndex, np.arange(len(channelTF)),CTLEMagnitude))
if useFFE:
sineAmp = sineAmp*FFERMS**2
# Generate sine distribution
sine = sineAmp*np.sin(2*np.pi*np.arange(0, 1, 0.0001))
sineNoise = np.histogram(sine, bins=yAxis)
sineNoise = sineNoise/np.sum(sineNoise) # normalize PDF
else:
sineNoise = 1 # perfect impulse
# Convolve both noise types
totalNoise = np.convolve(randNoise,sineNoise)
if len(totalNoise) < 2*supplyVoltage/yIncrement:
totalNoise = np.concatenate((np.zeros((round(yAxisLength/2),)), totalNoise, np.zeros((round(yAxisLength/2),))))
voltageScale = np.linspace(-(len(totalNoise)-1)/2*yIncrement, (len(totalNoise)-1)/2*yIncrement, len(totalNoise)+1)
# Save results
setattr(simResults.influenceSources, 'TXNoise', nothing())
simResults.influenceSources.TXNoise.random = randNoise
simResults.influenceSources.TXNoise.deterministic = sineNoise
simResults.influenceSources.TXNoise.totalNoise = totalNoise
simResults.influenceSources.TXNoise.voltageScale = voltageScale
###########################################################################
# This function creates a probability distribution for the channel noise.
# This function adds only random noise. Since the noise is defined at the
# output of the channel, it is amplified by the CTLE and FFE. Thus the
# equivalent noise must first be calculated. From there, a corresponding
# gaussian distribution is created.
###########################################################################
def generateChannelNoise(simSettings: simulationSettings, simResults: simulationStatus):
# Import variables
yAxis = simSettings.general.yAxis.value
yAxisLength = simSettings.general.yAxisLength.value
yIncrement = simSettings.general.yIncrement.value
addNoise = simSettings.channel.noise.addNoise
noiseDensity = simSettings.channel.noise.noiseDensity.value
usePreAmp = simSettings.receiver.preAmp.addGain
gain = simSettings.receiver.preAmp.gain.value
useCTLE = simSettings.receiver.CTLE.addEqualization
zeroFreq = simSettings.receiver.CTLE.zeroFreq.value
pole1Freq = simSettings.receiver.CTLE.pole1Freq.value
useFFE = simSettings.receiver.FFE.addEqualization
FFERMS = simResults.pulseResponse.receiver.FFE.tapRMS
zeroName = ('z' + str(zeroFreq/1e9)).replace('.', '_')
poleName = ('z' + str(pole1Freq/1e9)).replace('.', '_')
CTLE = simResults.influenceSources.RXCTLE.__dict__[zeroName].__dict__[poleName]
# Add random noise
if addNoise and noiseDensity != 0:
# Create noise frequency distribution before amplification
freqIncrement = CTLE.frequency[2]-CTLE.frequency[1]
powerDistribution = noiseDensity * freqIncrement * np.ones((len(CTLE.frequency),))
# Calculate equivalent noise at receiver output
if usePreAmp:
powerDistribution = powerDistribution * (gain ** 2)
if useCTLE:
powerDistribution = powerDistribution * (CTLE.magnitude ** 2)
if useFFE:
powerDistribution = powerDistribution * (FFERMS ** 2)
outputPower = np.sum(powerDistribution)
stdDeviationOutput = np.sqrt(outputPower)
# Create noise distribution
randNoise = stats.norm.pdf(yAxis, loc=0, scale=stdDeviationOutput)
randNoise = randNoise/np.sum(randNoise) # normalize PDF
else:
randNoise = np.concatenate((np.zeros((int(yAxisLength/2), )), [1], np.zeros((int(yAxisLength/2), )))) # perfect impulse
voltageScale = np.linspace(-(len(randNoise)-1)/2*yIncrement, (len(randNoise)-1)/2*yIncrement, len(randNoise)+1) # +1 needed for histograms
# Save results
setattr(simResults.influenceSources, 'CHNoise', nothing())
simResults.influenceSources.CHNoise.totalNoise = randNoise
simResults.influenceSources.CHNoise.voltageScale = voltageScale
###########################################################################
# This function creates a probability distribution for the receiver
# noise. This function adds random aswell as deterministic noise. It is
# assumed that the receiver noise is input refered and thus is affected by
# the pre-amp, CTLE and FFE.
###########################################################################
def generateRXNoise(simSettings: simulationSettings, simResults: simulationStatus):
# Import variables
yAxis = simSettings.general.yAxis.value
yAxisLength = simSettings.general.yAxisLength.value
yIncrement = simSettings.general.yIncrement.value
addNoise = simSettings.receiver.noise.addNoise
stdDeviation = simSettings.receiver.noise.stdDeviation.value
sineAmp = simSettings.transmitter.noise.amplitude.value
sineFreq = simSettings.transmitter.noise.frequency.value
supplyVoltage = simSettings.receiver.signalAmplitude.value
usePreAmp = simSettings.receiver.preAmp.addGain
gain = simSettings.receiver.preAmp.gain.value
useCTLE = simSettings.receiver.CTLE.addEqualization
zeroFreq = simSettings.receiver.CTLE.zeroFreq.value
pole1Freq = simSettings.receiver.CTLE.pole1Freq.value
useFFE = simSettings.receiver.FFE.addEqualization
FFERMS = simResults.pulseResponse.receiver.FFE.tapRMS
zeroName = ('z' + str(zeroFreq/1e9)).replace('.', '_')
poleName = ('z' + str(pole1Freq/1e9)).replace('.', '_')
CTLE = simResults.influenceSources.RXCTLE.__dict__[zeroName].__dict__[poleName]
# Add random noise
if addNoise and stdDeviation !=0:
# Create noise frequency distribution before amplification
power = stdDeviation ** 2
powerDistribution = np.ones((len(CTLE.frequency),))*(power/len(CTLE.frequency))
# Calculate output refered noise output
if useCTLE:
powerDistribution = powerDistribution * (CTLE.magnitude ** 2)
if usePreAmp:
powerDistribution = powerDistribution * (gain ** 2)
if useFFE:
powerDistribution = powerDistribution * (FFERMS ** 2)
outputPower = np.sum(powerDistribution)
stdDeviationOutput = np.sqrt(outputPower)
# Create noise distribution
randNoise = stats.norm.pdf(yAxis, loc=0, scale=stdDeviationOutput)
randNoise = randNoise/np.sum(randNoise) # normalize PDF
else:
randNoise = 1 # perfect impulse
# Add deterministic noise
if addNoise and sineAmp != 0:
# Calculate equivalent noise at receiver output
freqIndex = np.interp(sineFreq, CTLE.frequency, np.arange(len(CTLE.frequency)))
sineAmp = sineAmp*abs(np.interp(freqIndex, np.arange(len(CTLE.magnitude)), CTLE.magnitude))
if usePreAmp:
sineAmp = sineAmp * gain
if useCTLE:
sineAmp = sineAmp * abs(np.interp(freqIndex, np.arange(len(CTLE.magnitude)), CTLE.magnitude))
if useFFE:
sineAmp = sineAmp * (FFERMS ** 2)
# Generate sine distribution
sine = sineAmp*np.sin(2*np.pi*np.arange(0, 1, 0, 0.0001))
sineNoise = np.histogram(sine, bins=yAxis)
sineNoise = sineNoise/np.sum(sineNoise) # normalize PDF
else:
sineNoise = 1 # perfect impulse
# Convolve both noise types
totalNoise = np.convolve(randNoise,sineNoise)
if len(totalNoise) < 2*supplyVoltage/yIncrement:
totalNoise = np.concatenate((np.zeros((round(yAxisLength/2),)), totalNoise, np.zeros((round(yAxisLength/2),))))
voltageScale = np.linspace(-(len(totalNoise)-1)/2*yIncrement, (len(totalNoise)-1)/2*yIncrement, len(totalNoise)+1) # +1 needed for histograms
# Save results
setattr(simResults.influenceSources, 'RXNoise', nothing())
simResults.influenceSources.RXNoise.random = randNoise
simResults.influenceSources.RXNoise.deterministic = sineNoise
simResults.influenceSources.RXNoise.totalNoise = totalNoise
simResults.influenceSources.RXNoise.voltageScale = voltageScale
###########################################################################
# This function combines all sources of noise together.
###########################################################################
def combineInfluences(simSettings: simulationSettings, simResults: simulationStatus):
# Import variables
yIncrement = simSettings.general.yIncrement.value
TXNoise = simResults.influenceSources.TXNoise.totalNoise
CHNoise = simResults.influenceSources.CHNoise.totalNoise
RXNoise = simResults.influenceSources.RXNoise.totalNoise
# Combine noise
totalNoise = np.convolve(TXNoise,CHNoise)
totalNoise = np.convolve(totalNoise,RXNoise)
voltagescale = np.linspace(-(len(totalNoise)-1)/2*yIncrement, (len(totalNoise)-1)/2*yIncrement, len(totalNoise)+1) # +1 needed for histograms
# Save results
setattr(simResults.influenceSources, 'totalNoise', nothing())
simResults.influenceSources.totalNoise.histogram = totalNoise
simResults.influenceSources.totalNoise.voltageScale = voltagescale