-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNumerical_computation_v2.py
175 lines (150 loc) · 6.27 KB
/
Numerical_computation_v2.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
import os # change dir
Wdir= os.getcwd()
import sys
sys.path.append(Wdir+'\functions')# add to path
import numpy as np
import scipy.io as sio # load save matlab files
import matplotlib.pyplot as plt # plotting
from matplotlib.gridspec import GridSpec # easy subplots
from functions.IAS_functions import f_est_linear,iaslinearapproxv2
#%% Basreline snr test
mat_contents = sio.loadmat('Dataz/'+'Numerical_signal', mdict=None, appendmat=True)
for k, v in mat_contents.items():
try:
if v.size==sum(v.shape)-1:
globals()[k]=v.flatten()
else:
globals()[k]=v
except:
globals()[k]=v
del k,v,mat_contents
Nw = 2**int(np.log2(.2*fs)) # window length
fmaxmin = np.r_[170,260]/fs
delta = 3/fs
K = np.r_[K]
savename = 'tmpd15'
savename1 = 'D15_ias_numerical_baseline_white'
savename2 = 'D15_ias_linear_approx_numerical_white'
savename3 = 'D15_ias_numerical_baseline_pink'
savename4 = 'D15_ias_linear_approx_numerical_pink'
f_alpha_est = np.zeros([x_f_0.shape[0],snrdb.size*2])
chirp = 0
for k in np.r_[:snrdb.size]:
tmp = iaslinearapproxv2(datanoisy_w[:,k],Nw,K,fmaxmin,delta,chirp,savename)
if k==0:
f_alpha_est = f_alpha_est[:tmp.shape[0],:]
f_alpha_est[:,2*k:2*k+2] = tmp
result_dict = {'f_alpha_est': f_alpha_est,'fs':fs,\
'Nw':Nw, 'K':K}
sio.savemat('Dataz/'+savename1+'.mat',result_dict,do_compression=True)
f_alpha_est = np.zeros([x_f_0.shape[0],snrdb.size*2])
chirp = 1
for k in np.r_[:snrdb.size]:
tmp = iaslinearapproxv2(datanoisy_w[:,k],Nw,K,fmaxmin,delta,chirp,savename)
if k==0:
f_alpha_est = f_alpha_est[:tmp.shape[0],:]
f_alpha_est[:,2*k:2*k+2] = tmp
result_dict = {'f_alpha_est': f_alpha_est,'fs':fs,\
'Nw':Nw, 'K':K}
sio.savemat('Dataz/'+savename2+'.mat',result_dict,do_compression=True)
f_alpha_est = np.zeros([x_f_0.shape[0],snrdb.size*2])
chirp = 0
for k in np.r_[:snrdb.size]:
tmp = iaslinearapproxv2(datanoisy_p[:,k],Nw,K,fmaxmin,delta,chirp,savename)
if k==0:
f_alpha_est = f_alpha_est[:tmp.shape[0],:]
f_alpha_est[:,2*k:2*k+2] = tmp
result_dict = {'f_alpha_est': f_alpha_est,'fs':fs,\
'Nw':Nw, 'K':K}
sio.savemat('Dataz/'+savename3+'.mat',result_dict,do_compression=True)
f_alpha_est = np.zeros([x_f_0.shape[0],snrdb.size*2])
chirp = 1
for k in np.r_[:snrdb.size]:
tmp = iaslinearapproxv2(datanoisy_p[:,k],Nw,K,fmaxmin,delta,chirp,savename)
if k==0:
f_alpha_est = f_alpha_est[:tmp.shape[0],:]
f_alpha_est[:,2*k:2*k+2] = tmp
result_dict = {'f_alpha_est': f_alpha_est,'fs':fs,\
'Nw':Nw, 'K':K}
sio.savemat('Dataz/'+savename4+'.mat',result_dict,do_compression=True)
# %% ploting data
plt.rc('font', size=14) # default text size
savename1 = 'D15_ias_numerical_baseline_white'
savename2 = 'D15_ias_linear_approx_numerical_white'
savename3 = 'D15_ias_numerical_baseline_pink'
savename4 = 'D15_ias_linear_approx_numerical_pink'
lfile = [savename1,savename2,savename3,savename4]
for lfilek in np.r_[:4]:
mat_contents = sio.loadmat('Dataz/'+'Numerical_signal', mdict=None, appendmat=True)
for k, v in mat_contents.items():
try:
if v.size==sum(v.shape)-1:
globals()[k]=v.flatten()
else:
globals()[k]=v
except:
globals()[k]=v
del k,v,mat_contents
mat_contents = sio.loadmat('Dataz/'+lfile[lfilek], mdict=None, appendmat=True)
for k, v in mat_contents.items():
try:
if v.size==sum(v.shape)-1:
globals()[k]=v.flatten()
else:
globals()[k]=v
except:
globals()[k]=v
del k,v,mat_contents
fmaxmin = np.r_[170,260]
# -----------------------------------------------------------------------------
Nw = 2*H # for this case Nw = 2*
Nw = int(Nw)
H = int(H)
n = f_alpha_est.shape[1]/2;n = int(n); n = np.r_[:n]
f_est = f_alpha_est[:,2*n]*fs
alpha_est = f_alpha_est[:,2*n+1]*fs
kest = np.where(snrdb==-10)[0][0]# -7.5
f_est_lin = np.zeros(((f_est.shape[0]-2)*H+2*H,f_est.shape[1]))
for k in np.r_[kest:alpha_est.shape[1]]:
f_est_lin[:,k] = f_est_linear(alpha_est[:,k],f_est[:,k],Nw,H)
x_f_0 = x_f_0[:f_est_lin.shape[0],:]
t = np.r_[:len(x_f_0[:,1])]/fs
t2 = np.r_[:len(f_alpha_est)]/fs*H
#--------------------------------------------
plt.close('all')
fig = plt.figure(constrained_layout=True)
gs = GridSpec(5, 5, figure=fig);ax1 = fig.add_subplot(gs[0, 0:])
ax2 = fig.add_subplot(gs[1:, 0:])
ax1.plot(t2,alpha_est);ax2.plot(t2,f_est)
ax1.set_xlim([t2[0],t2[-1]]);ax2.set_xlim([t2[0],t2[-1]])
ax1.set_ylabel("Chirp rate");ax2.set_ylabel('Frequency [Hz]')
ax2.set_xlabel('Time [sec]')
ax1.legend([r'$\hat{\beta}_{1}[n]$']);ax2.legend([r'$\hat{\beta}_{2}[n]$'])
plt.tight_layout()
plt.show()
if lfilek==0:
plt.savefig(Wdir+'/Figures/exp02_awgn_parameters_all_baseline.pdf',bbox_inches='tight')
elif lfilek==1:
plt.savefig(Wdir+'/Figures/exp02_awgn_parameters_all_proposal.pdf',bbox_inches='tight')
elif lfilek==2:
plt.savefig(Wdir+'/Figures/exp02_pink_parameters_all_baseline.pdf',bbox_inches='tight')
elif lfilek==3:
plt.savefig(Wdir+'/Figures/exp02_pink_parameters_all_proposal.pdf',bbox_inches='tight')
#--------------------------------------------
plt.figure()
plt.plot(t,f_est_lin)
plt.plot(t,x_f_0[:,1])
plt.axis((t[0],t[-1],fmaxmin[0],fmaxmin[1]))
plt.xlabel('Time [sec]')
plt.ylabel('Frequency [Hz]')
plt.legend([r'Piecewise approx $\hat{f}_0[n]$','Theoretical IAS'])
plt.tight_layout()
plt.show()
if lfilek==0:
plt.savefig(Wdir+'/Figures/exp02_awgn_f_est_baseline.pdf',bbox_inches='tight')
elif lfilek==1:
plt.savefig(Wdir+'/Figures/exp02_awgn_f_est_proposal.pdf',bbox_inches='tight')
elif lfilek==2:
plt.savefig(Wdir+'/Figures/exp02_pink_f_est_baseline.pdf',bbox_inches='tight')
elif lfilek==3:
plt.savefig(Wdir+'/Figures/exp02_pink_f_est_proposal.pdf',bbox_inches='tight')