-
Notifications
You must be signed in to change notification settings - Fork 1
/
eq.py
355 lines (281 loc) · 14.8 KB
/
eq.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
import numpy as np
from scipy.linalg import toeplitz
from numpy import linalg
# Matlab code (not the same but the skeletons are similar): http://bard.ece.cornell.edu/downloads/tutorials/fsedfe/fsedfe.html
class DFE():
'''
This is the implementation of DFE
'''
def __init__(self, sampled_pulse_response, n_taps_dfe, samples_per_symbol):
self.sampled_pulse_response = sampled_pulse_response
self.n_taps_dfe = n_taps_dfe
self.main_cursor = np.max(abs(self.sampled_pulse_response))
self.samples_per_symbol = samples_per_symbol
def coefficients(self):
'''
Parameters
----------
normalize_factor : float
This is usually the absolute summation of FFE tap weights before its normalization
Returns
-------
dfe_tap_weights : TYPE
DESCRIPTION.
'''
dfe_tap_weights = self.sampled_pulse_response[1:]
for i in range(len(dfe_tap_weights)):
if dfe_tap_weights[i] > 0.5:
dfe_tap_weights[i] = 0.5
elif dfe_tap_weights[i] < -0.5:
dfe_tap_weights[i] = -0.5
return dfe_tap_weights
def eqaulization(self, dfe_tap_weights, pulse_response):
pulse_in = pulse_response
pulse_out = np.copy(pulse_in)
max_idx = np.argmax(abs(pulse_out))
for i in range(1,self.n_taps_dfe+1):
pulse_out[int(max_idx+self.samples_per_symbol*i-self.samples_per_symbol/2):int(max_idx+self.samples_per_symbol*i+self.samples_per_symbol/2)] -= dfe_tap_weights[i-1]
return pulse_out
class FFE():
'''
This is the implementation of FFE, including MMSE to find optimal tap weights
'''
def __init__(self, sampled_pulse_response, n_taps_pre, n_taps_post, n_taps_dfe, samples_per_symbol, dfe_limit=np.array([0])):
self.sampled_pulse_response = sampled_pulse_response
self.n_taps_pre = n_taps_pre
self.n_taps_post= n_taps_post
self.n_taps_ffe = n_taps_pre + n_taps_post + 1
self.n_taps_dfe = n_taps_dfe
self.samples_per_symbol = samples_per_symbol
self.channel_precursor = np.argmax(abs(sampled_pulse_response))
self.channel_coefficients_len = len(sampled_pulse_response)
self.idx_max = self.channel_precursor
self.delay = self.n_taps_pre+self.channel_precursor
self.dfe_limit = dfe_limit
self.c = np.zeros((self.channel_coefficients_len+self.n_taps_ffe-1,1))
self.c[self.delay] = 1
self.c[self.delay+1:self.delay+1+len(dfe_limit)] = dfe_limit
tmp = np.ones(self.channel_coefficients_len+self.n_taps_ffe-1)
tmp[self.delay+1:self.delay+1+self.n_taps_dfe] = 0
self.W = np.diag(tmp)
# A: channel convolution matrix
self.A = np.zeros((self.channel_coefficients_len+self.n_taps_ffe-1, self.n_taps_ffe))
H = np.append(self.sampled_pulse_response, np.zeros(self.n_taps_ffe-1))
for i in range(self.n_taps_ffe):
self.A[:, i] = np.roll(H, i)
def mmse_Hossain(self, SNR, signal_power, optimize_delay=True, zf=False):
# Autocorrelation matrix: A.T @ A
# cross-correlation matrix: A.T @ c
SNR_linear = 10**(SNR/10)
noise_power = signal_power/SNR_linear
if optimize_delay==True:
# sweep all the possible number of ffe precurosrs
self.unbiased_SNR = -np.inf
for i in range(self.n_taps_ffe):
delay = i+self.channel_precursor
c = np.zeros((self.channel_coefficients_len+self.n_taps_ffe-1,1))
c[delay] = 1
tmp = np.ones(self.channel_coefficients_len+self.n_taps_ffe-1)
tmp[delay+1:delay+1+self.n_taps_dfe] = 0
W = np.diag(tmp)
if zf == False:
b = np.linalg.inv(self.A.T @ W @ self.A + np.eye(self.n_taps_ffe) * noise_power) @ (self.A.T @ c)
else:
b = np.linalg.inv(self.A.T @ W @ self.A) @ (self.A.T @ c)
# find MMSE
# cross-correlation
Rxy = signal_power * (self.A.T @ c)
# auto-correlation
# Ryy = signal_power * (self.A.T @ self.A + np.eye(self.n_taps_ffe) * noise_power)
if zf == False:
mmse = signal_power - b.T @ Rxy
unbiased_SNR = 10*np.log10(signal_power/mmse - 1)
else:
mmse = (signal_power - b.T @ Rxy) + (noise_power * linalg.norm(np.squeeze(b))**2)
unbiased_SNR = 10*np.log10(signal_power/mmse)
# print(f'mmse: {mmse} | signal_power: {signal_power} | b.T @ Rxy: {b.T @ Rxy} ')
# print(f'unbiased_SNR: {unbiased_SNR}')
if unbiased_SNR > self.unbiased_SNR:
self.mmse = mmse
self.unbiased_SNR = unbiased_SNR
self.delay = delay
self.c = c
self.W = W
self.n_taps_pre = i
self.n_taps_post = self.n_taps_ffe - i -1
#normalize tap weights
# b = b/np.sum(abs(b))
max_idx = np.argmax(abs(b))
b = b/b[max_idx]
ffe_tap_weights = np.squeeze(b)
else:
Rxy = signal_power * (self.A.T @ self.c)
if zf == False:
Ryy = signal_power * (self.A.T @ self.A) + np.eye(self.n_taps_ffe) * noise_power
b = np.linalg.inv(self.A.T @ self.W @ self.A + np.eye(self.n_taps_ffe) * noise_power) @ (self.A.T @ self.c)
self.mmse = signal_power - b.T @ Rxy
self.unbiased_SNR = 10*np.log10(signal_power/self.mmse - 1)
else:
Ryy = signal_power * (self.A.T @ self.A)
b = np.linalg.inv(self.A.T @ self.W @ self.A) @ (self.A.T @ self.c)
self.mmse = (signal_power - b.T @ Rxy) + (noise_power * linalg.norm(np.squeeze(b))**2)
self.unbiased_SNR = 10*np.log10(signal_power/self.mmse)
# print(self.unbiased_SNR )
#normalize tap weights
max_idx = np.argmax(abs(b))
b = b/b[max_idx]
# b = b/np.sum(abs(b))
ffe_tap_weights = np.squeeze(b)
return ffe_tap_weights
def mmse(self, SNR, signal_power, optimize_delay=True, zf=False):
# Autocorrelation matrix: A.T @ A
# cross-correlation matrix: A.T @ c
if optimize_delay==True:
# sweep all the possible number of ffe precurosrs
self.unbiased_SNR = -np.inf
for i in range(self.n_taps_ffe):
delay = i+self.channel_precursor
c = np.zeros((self.channel_coefficients_len+self.n_taps_ffe-1,1))
c[delay] = 1
tmp = np.ones(self.channel_coefficients_len+self.n_taps_ffe-1)
tmp[delay+1:delay+1+self.n_taps_dfe] = 0
W = np.diag(tmp)
if zf == False:
b = np.linalg.inv(self.A.T @ W @ self.A + np.eye(self.n_taps_ffe) * 10**(-(SNR/10))) @ (self.A.T @ c)
else:
b = np.linalg.inv(self.A.T @ W @ self.A) @ (self.A.T @ c)
# find MMSE
# cross-correlation
Rxy = signal_power * (self.A.T @ c)
# auto-correlation
# Ryy = signal_power * (self.A.T @ self.A + np.eye(self.n_taps_ffe) * 10**(-(SNR/10)))
if zf == False:
mmse = signal_power - b.T @ Rxy
unbiased_SNR = 10*np.log10(signal_power/mmse - 1)
else:
mmse = (signal_power - b.T @ Rxy) + (signal_power * 10**(-(SNR/10)) * linalg.norm(np.squeeze(b))**2)
unbiased_SNR = 10*np.log10(signal_power/mmse)
# print(f'mmse: {mmse} | signal_power: {signal_power} | b.T @ Rxy: {b.T @ Rxy} ')
# print(f'unbiased_SNR: {unbiased_SNR}')
if unbiased_SNR > self.unbiased_SNR:
self.mmse = mmse
self.unbiased_SNR = unbiased_SNR
self.delay = delay
self.c = c
self.W = W
self.n_taps_pre = i
self.n_taps_post = self.n_taps_ffe - i -1
#normalize tap weights
# b = b/np.sum(abs(b))
max_idx = np.argmax(abs(b))
b = b/b[max_idx]
ffe_tap_weights = np.squeeze(b)
else:
Rxy = signal_power * (self.A.T @ self.c)
if zf == False:
Ryy = signal_power * (self.A.T @ self.A) + np.eye(self.n_taps_ffe) * 10**(-(SNR/10))
b = np.linalg.inv(self.A.T @ self.W @ self.A + np.eye(self.n_taps_ffe) * 10**(-(SNR/10))) @ (self.A.T @ self.c)
self.mmse = signal_power - b.T @ Rxy
self.unbiased_SNR = 10*np.log10(signal_power/self.mmse - 1)
else:
Ryy = signal_power * (self.A.T @ self.A)
b = np.linalg.inv(self.A.T @ self.W @ self.A) @ (self.A.T @ self.c)
self.mmse = (signal_power - b.T @ Rxy) + (signal_power * 10**(-(SNR/10)) * linalg.norm(np.squeeze(b))**2)
self.unbiased_SNR = 10*np.log10(signal_power/self.mmse)
# print(self.unbiased_SNR )
#normalize tap weights
max_idx = np.argmax(abs(b))
b = b/b[max_idx]
# b = b/np.sum(abs(b))
ffe_tap_weights = np.squeeze(b)
return ffe_tap_weights
def convolution(self, tap_weights, h):
tap_filter = np.zeros((self.n_taps_ffe-1)*self.samples_per_symbol+1)
for i in range(self.n_taps_ffe):
tap_filter[i*self.samples_per_symbol] = tap_weights[i]
length = h.size
h_out = np.convolve(h, tap_filter)
h_out = h_out[self.n_taps_pre*self.samples_per_symbol:self.n_taps_pre*self.samples_per_symbol+length]
return h_out
def mmse_ffe_dfe(sampled_pulse_response, n_taps_ffe, n_taps_dfe, signal_power, noise_var, oversampling=1, delay=-1, zf=False):
noise_var_scaler = np.copy(noise_var)
noise_auto = np.append(np.array([noise_var]), np.zeros(n_taps_ffe-1))
size = len(sampled_pulse_response)
nu = int(np.ceil(size/oversampling) - 1) # channel memory so that it is FIR
sampled_pulse_response = np.append(sampled_pulse_response, np.zeros((nu+1)*oversampling-size))
# error check
if n_taps_ffe <= 0:
print(f'{n_taps_ffe} should be >0')
if n_taps_dfe <0:
print(f'{n_taps_dfe} should be >=0')
if delay > n_taps_ffe+nu -1:
print(f'delay must be <= {n_taps_ffe}+{len(sampled_pulse_response)}-2')
if delay < -1:
print('delay must be >= -1')
if delay == -1:
print('optimal delay will be searched')
delay = [i for i in range(n_taps_ffe+nu)]
if type(delay) == int:
delay = [delay]
# do some oversampling
sampled_pulse_response_temp = np.zeros((oversampling, nu+1))
sampled_pulse_response_temp[:oversampling,0] = np.append(sampled_pulse_response[0], np.zeros(oversampling-1))
for i in range(nu):
sampled_pulse_response_temp[:oversampling, i+1] = np.conj(np.flip(sampled_pulse_response[i*oversampling+1:i*oversampling+2]).T)
dfseSNR = -100
for d in delay:
print(d)
n_taps_dfe_used = min(n_taps_ffe+nu-1-d, n_taps_dfe)
# generate channel pulse matrix
P = np.zeros((n_taps_ffe*oversampling+n_taps_dfe_used, n_taps_ffe+nu))
for i in range(n_taps_ffe):
P[i*oversampling:i*oversampling+1, i:(i+nu+1)] = sampled_pulse_response_temp
P[n_taps_ffe*oversampling:n_taps_ffe*oversampling+n_taps_dfe_used, d+1:d+n_taps_dfe_used+1] = np.eye(n_taps_dfe_used)
# compute Rn, noise autocorrelation matrix
Rn = np.zeros((n_taps_ffe*oversampling+n_taps_dfe_used, n_taps_ffe*oversampling+n_taps_dfe_used))
if zf == False: # take noise into account when MMSE is used
Rn[:n_taps_ffe*oversampling, :n_taps_ffe*oversampling] = toeplitz(noise_auto)
# desire output
c = np.zeros((n_taps_ffe+nu, 1))
c[d,:] = 1
# MMSE
Ry = P @ P.T * signal_power + Rn
# print(Ry)
Rxy = P @ c * signal_power
#print(Rxy)
w_t_new = np.linalg.inv(Ry) @ Rxy
# new SNR
if zf == False:
sigma_dfse = np.squeeze(signal_power - np.real(w_t_new.T @ Rxy))
dfseSNR_new = 10*np.log10(signal_power/sigma_dfse-1)
else:
sigma_dfse = np.squeeze(signal_power - np.real(w_t_new.T @ Rxy)) + np.squeeze(noise_var_scaler * linalg.norm(np.squeeze(w_t_new)[:n_taps_ffe])**2)
dfseSNR_new = 10*np.log10(signal_power/sigma_dfse)
if dfseSNR_new >= dfseSNR:
w_t = w_t_new
dfseSNR = dfseSNR_new
delay_opt = d
n_taps_dfe_final = n_taps_dfe_used
if n_taps_dfe_final < n_taps_dfe:
print(f'For optimal DFE filter n_taps_dfe_final={n_taps_dfe_final} taps are used insteald of n_taps_dfe={n_taps_dfe} ')
w_t = np.squeeze(w_t)
return w_t, delay_opt, dfseSNR, n_taps_dfe_final #, Ry, P
if __name__ == '__main__':
# example 3.7.2 from Prof. Cioffi's textbook
zf = False
# sampled_pulse_response = np.array([0.9, 1])
sampled_pulse_response=np.array([-0.0113907,0.01509196,0.1249106,0.03442465,-0.016071648,0.01173258,-0.006703405,0.0031158951,-0.00355874,0.0004675312])
sampled_pulse_response = sampled_pulse_response/sum(abs(sampled_pulse_response))
signal_power = 1#np.mean(abs(sampled_pulse_response**2))
SNR = 10*np.log10(5.524) #5.524
delay = 1
ffe = FFE(sampled_pulse_response, n_taps_pre=3, n_taps_post=11, n_taps_dfe=0, samples_per_symbol=1)
tap_weights_ffe = ffe.mmse(SNR=SNR, signal_power=signal_power, optimize_delay=False, zf=zf)
delay_opt = ffe.n_taps_pre
unbiased_SNR = ffe.unbiased_SNR
result = np.convolve(sampled_pulse_response, tap_weights_ffe)
noise_var = 0.181
n_taps_dfe=1
n_taps_ffe=2
w_t, delay_opt_Cioffi, dfseSNR, n_taps_dfe_final = mmse_ffe_dfe(sampled_pulse_response, n_taps_ffe, n_taps_dfe, signal_power, noise_var, oversampling=1, delay=delay, zf=zf)
tap_weights_ffe_Cioffi = w_t[:n_taps_ffe]/sum(abs(w_t[:n_taps_ffe]))