-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolocarry_estimation.py
144 lines (101 loc) · 3.28 KB
/
solocarry_estimation.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
#!/usr/bin/env python
# A single file containing everything needed for the estimation
import numpy as np
import sys
import matplotlib.pyplot as plt
def generate_crlb(sigma_squared):
crlb_w = 12*sigma_squared / (A**2 * T**2 * N*(N**2 - 1))
crlb_phi = 12*sigma_squared*(n0**2 * N + 2*n0*P + Q) / (A**2 * N**2 * (N**2 - 1))
return crlb_w, crlb_phi
def generate_signal(sigma):
# Noise generation
wr = np.random.normal(0, sigma, N)
wi = np.random.normal(0, sigma, N)
w = wr + 1j*wi
# Pure signal generation
x = np.empty(N, dtype=np.complex_)
for n in range(N):
x[n] = A*np.exp(1j*(w0*(n+n0)*T + phi))
return x + w
def F(x_d, w):
Fw0 = 0
for n in range(N):
Fw0 += x_d[n]*np.exp(-1j*w*n*T) # Eq. (6)
Fw0 /= N
return Fw0
def fft_estimator(x_d, M):
Fw = np.fft.fft(x_d, M)
m_star = np.argmax(np.absolute(Fw))
w_hat = 2*np.pi*m_star / (M*T) # Eq. (8)
phi_hat = np.angle(np.exp(-1j*w_hat*n0*T)*F(x_d, w_hat)) # Eq. (7)
return w_hat, phi_hat, Fw
def plot(x_d, Fw, i):
Fw = np.fft.fftshift(Fw)
Fw = np.absolute(Fw)
Ff = np.fft.fftfreq(M, 1.0/Fs)
Ff = np.fft.fftshift(Ff)
if i == 0:
plt.figure(1)
plt.plot(np.arange(len(x_d)), np.real(x_d))
plt.title("First generated signal with GWN, real values")
plt.figure(2)
plt.plot(np.arange(len(x_d)), np.imag(x_d))
plt.title("First generated signal with GWN, imag values")
plt.figure(3)
plt.plot(Ff, Fw)
plt.title("Abs. of F-transform of first generated signal")
plt.figure(4)
plt.plot(Ff, Fw)
plt.title("F-transforms of all generated signals")
def print_status_bar(i, progress):
if i % (num_of_runs/status_bar_length) == 0:
progress += 1
sys.stdout.write('\r')
sys.stdout.write("Status: [" + "="*progress + " "*(status_bar_length-progress) + "]")
sys.stdout.flush()
return progress
# Constants
Fs = 10**6
T = 1.0/Fs
f0 = 10**5
w0 = 2*np.pi*f0
phi = np.pi / 8
A = 1.0
N = 513
P = N*(N-1)/2.0
Q = N*(N-1)*(2*N-1)/6.0
n0 = -P/N
status_bar_length = 50
num_of_runs = 500
# Generate multiple samples to calculate variance
SNR_dB = 30.0
SNR = 10**(SNR_dB/10.0)
K = 16
M = 2**K
sigma_squared = A**2 / (2*SNR)
w_estimates = np.empty(num_of_runs)
phi_estimates = np.empty(num_of_runs)
status_bar_progress = 0
do_plot = False
for i in range(num_of_runs):
x_d = generate_signal(np.sqrt(sigma_squared))
w_hat, phi_hat, Fw = fft_estimator(x_d, M)
w_estimates[i] = w_hat
phi_estimates[i] = phi_hat
if do_plot:
plot(x_d, Fw, i)
status_bar_progress = print_status_bar(i, status_bar_progress)
mean_w = np.mean(w_estimates)
mean_phi = np.mean(phi_estimates)
var_w = np.var(w_estimates)
var_phi = np.var(phi_estimates)
crlb_w, crlb_phi = generate_crlb(sigma_squared)
print("")
if var_w < crlb_w:
print("Variance for omega lower than CRLB!")
if var_phi < crlb_phi:
print("Variance for phi lower than CRLB!")
print("CONFIG | SNR [dB]: {}, M: 2^{}, true omega: {}, true phase: {}".format(SNR_dB, K, w0, phi))
print("OMEGA | estimated mean: {}, estimated variance: {}, crlb: {}".format(mean_w, var_w, crlb_w))
print("PHASE | estimated mean: {}, estimated variance: {}, crlb: {}".format(mean_phi, var_phi, crlb_phi))
plt.show()