-
Notifications
You must be signed in to change notification settings - Fork 0
/
finetuneauto.py
139 lines (105 loc) · 3.52 KB
/
finetuneauto.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
import numpy as np
import matplotlib.pyplot as plt
import buggesmatteland as bml
import cmath
import statistics as st
import scipy.optimize
import sys
# ---------- Signal specifications ---------- #
A = 1.0
SNR_db = 30
SNR_linear = 10.0**(SNR_db/10)
SIGMA_SQUARED = (A**2)/(2*SNR_linear)
print("Running with SNR:", SNR_db, "dB")
T = 10**(-6)
N = 513
n_0 = -256
f_0 = 10**5
omega_0 = 2*np.pi*f_0
theta = np.pi/8
ITERATIONS = 100
k = 10
fft_length = 2**k
# ---------- CRLB Helpers ---------- #
P = (N*(N-1)) / 2
Q = (N*(N-1)*(2*N-1)) / 6
# ---------- CRLB ---------- #
CRLB_OMEGA = (12*(SIGMA_SQUARED)) / ((A**2)*(T**2)*N*((N**2)-1)) # In Radians^2
CRLB_THETA = 12*(SIGMA_SQUARED)*((n_0**2)*N + 2*n_0*P + Q) / ((A**2)*(N**2)*((N**2)-1))
# ---------- Computes MLE of the frecuency and phase ---------- #
def computeMLE():
# ---------- Signals ---------- #
# White complex Gaussian noise
wReal = np.random.normal(0, np.sqrt(SIGMA_SQUARED), size=N)
wImag = np.random.normal(0, np.sqrt(SIGMA_SQUARED), size=N)*1j
w = []
for i in range(N):
w.append(wReal[i] + wImag[i])
# Exponential signal
s = []
for n in range(N):
s.append(A*np.exp(np.complex(0,1)*((omega_0)*(n + n_0)*T + theta)))
# Total signal
x = []
for i in range(N):
x.append(s[i] + w[i])
# Fourier transform
FT_x = np.fft.fft(x,fft_length)
# Finding most dominant frequency in total signal
f_2,i = bml.findDominantFrequency(np.absolute(FT_x),T,fft_length)
t = np.angle((np.exp(-(np.complex(0,1)*2*np.pi*f_2*n_0*T)))*FT_x[i])
return f_2,t
# ---------- Function to be minimzed in part 1B) ---------- #
def functionToBeMinimized(f_variable):
# ---------- Generate a FFT to be used in 1B) ---------- #
# White complex Gaussian noise
gwReal = np.random.normal(0, np.sqrt(SIGMA_SQUARED), size=N)
gwImag = np.random.normal(0, np.sqrt(SIGMA_SQUARED), size=N)*1j
gw = []
for i in range(N):
gw.append(gwReal[i] + gwImag[i])
# Exponential signal
gs = []
for n in range(N):
gs.append(A*np.exp(np.complex(0,1)*((omega_0)*(n + n_0)*T + theta)))
# Total signal
gx = []
for i in range(N):
gx.append(gs[i] + gw[i])
gFFT = np.fft.fft(gx,2**10)
gf = bml.findDominantFrequency(np.absolute(gFFT),T,2**10)
f_var_sliced = f_variable[0]
# -------- Exponential signal without noise -------- #
s = []
for n in range(N):
s.append(A*np.exp(np.complex(0,1)*((2*np.pi*f_var_sliced)*(n + n_0)*T + theta)))
fftGuess = np.fft.fft(s,2**10)
mse = bml.meanSquareError(np.absolute(fftGuess),np.absolute(gFFT))
return mse
def main():
finetunes = []
errors = []
for i in range(ITERATIONS):
result = scipy.optimize.minimize(functionToBeMinimized,100000,method = "Nelder-Mead")
finetunes.append(result.x[0])
errors.append(f_0 - result.x[0])
print(i)
meanfinetunes = st.mean(finetunes)
meanerror = st.mean(errors)
errvar = st.variance(errors,meanerror)
print("Average finetuned frequency:",meanfinetunes)
print("Average finetuned error:",meanerror)
print("Finetuned error variance:",errvar)
# Plotting MSE
mse = []
t = [1,2]
for f in range(60000,140000,100):
t[0] = f
mse.append(functionToBeMinimized(t))
plt.figure(2)
plt.title("MSE")
plt.xlabel("Frequency [Hz]")
plt.ylabel("Mean Square Error")
plt.plot(np.arange(60000,140000,100),mse)
plt.savefig("Bilder/mse.png")
main()