Skip to content

Commit e904e18

Browse files
committed
S1 test, single point SNR ests OK
1 parent 633b44e commit e904e18

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

est_snr.py

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,46 +46,47 @@ def snr_est_test(model, snr_target, h, Nw):
4646

4747
Nc = model.Nc
4848
Pc = np.array(model.pilot_gain*model.P)
49-
print(np.sum(Pc))
5049

5150
# matrix of transmitted pilots for time window
52-
# time steps across rows, carrioer across cols
51+
# time steps across rows, carrier across cols
5352
Pcn = np.zeros((Nw,Nc), dtype=np.complex64)
5453
for n in np.arange(Nw):
5554
Pcn[n,:]= Pc
5655

5756
# sequence of noise samples
58-
Es = np.dot(Pc,np.conj(Pc))/Nc # energy per symbol
57+
Es = np.sum(Pc**2)/Nc # energy per symbol
5958
No = Es/snr_target # noise per symbol
6059
sigma = np.sqrt(No)/(2**0.5)
6160
n = sigma*(np.random.normal(size=(Nw,Nc)) + 1j*np.random.normal(size=(Nw,Nc)))
62-
61+
print(Es,No,sigma,np.var(n),np.var(Pcn), np.sum(np.abs(Pcn)**2))
6362
# matrix of received pilots plus noise samples
6463
Pcn_hat = h*Pcn + n
6564

6665
# phase corrected received pilots
6766
Rcn_hat = np.abs(h)*Pcn + n
6867

69-
# remove pilot modulation to map to one point
70-
Z=Rcn_hat/Pcn
68+
# calculate S1 two ways to test expression
69+
70+
S1 = np.sum(np.abs(Pcn_hat)**2)
71+
S1_first = np.sum(np.abs(h*Pcn)**2)
72+
S1_second = np.sum(2*(h*Pcn*n).real)
73+
#S1_second = np.sum(h*Pcn*np.conj(n) + np.conj(h*Pcn)*n)
74+
S1_third = np.sum(np.abs(n)**2)
75+
S1_sum = S1_first + S1_second + S1_third
76+
print(f"S1_first: {S1_first:5.2f} S1_second: {S1_second:5.2f} S1_third: {S1_third:5.2f}")
77+
print(f"S1: {S1:5.2f} S1_sum: {S1_sum:5.2f}")
7178

7279
# calculate SNR est
73-
S1 = np.abs(np.sum(Z))
74-
S2 = np.sum(Z.imag*np.conj(Z.imag))
75-
print(np.sum(Z))
76-
print(f"S1: {S1:f} S2: {S2}")
80+
S2 = np.sum(np.abs(Rcn_hat.imag)**2)
81+
print(S1, S2)
82+
#print(f"S1: {S1:f} S2: {S2:f}")
7783

78-
snr_est = S1/(2*S2)
84+
snr_est = S1/(2*S2) - 1
7985

8086
# actual snr as check, should be same as snr_target
81-
snr_check = np.sum(Pcn*np.conj(Pcn))/np.sum(n*np.conj(n))
82-
print(f"S: {np.sum(Pcn*np.conj(Pcn)).real:f} N: {np.sum(n*np.conj(n)).real}")
87+
snr_check = np.sum(np.abs(h*Pcn)**2)/np.sum(np.abs(n)**2)
88+
print(f"S: {np.sum(np.abs(h*Pcn)**2):f} N: {np.sum(np.abs(n)**2)}")
8389
print(f"snr:target {snr_target:5.2f} snr_check: {snr_check.real:5.2f} snr_est: {snr_est:5.2f}")
84-
plt.figure(1)
85-
plt.plot(Rcn_hat.real, Rcn_hat.imag,'b+')
86-
plt.plot(Z.real, Z.imag,'r+')
87-
plt.show()
88-
quit()
8990

9091
return snr_est,snr_check
9192

@@ -99,7 +100,7 @@ def snr_est_test(model, snr_target, h, Nw):
99100
def single(snrdB, h, Nw):
100101
snr_est, snr_check = snr_est_test(model, 10**(snrdB/10), h, Nw)
101102
#print(f"snrdB: {snrdB:5.2f} snrdB_check: {10*np.log10(snr_check):5.2f} snrdB_est: {10*np.log10(snr_est):5.2f}")
102-
print(f"snrdB: {snrdB:5.2f} snrdB_check: {10*np.log10(snr_check):5.2f}")
103+
print(f"snrdB: {snrdB:5.2f} snrdB_check: {10*np.log10(snr_check):5.2f} snrdB_est: {10*np.log10(snr_est):5.2f}")
103104

104105
# run over a sequence of timesteps
105106
def sequence(Ntimesteps, EsNodB, h):
@@ -161,8 +162,11 @@ def sweep(Ntimesteps, h):
161162
if len(args.h_file):
162163
h = np.fromfile(args.h_file,dtype=np.float32)
163164
h = h.reshape((-1,model.Nc))
165+
print(h.shape)
164166
# sample once every modem frame
165-
h = h[:model.Ns+1:]
167+
h = h[::model.Ns+1,:]
168+
h = h[:Nw,:]
169+
print(h.shape)
166170
else:
167171
h = 0.5*np.ones((Nw,model.Nc))
168172
print(h.shape)

0 commit comments

Comments
 (0)