-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmusic_algoritm_aoa_simulation.py
880 lines (684 loc) · 30 KB
/
music_algoritm_aoa_simulation.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
# Scipts performing MUSIC algorithm, plotting pseudospectrum,
# running parametric and statistical (Monte-Carlo) analyses
# %%
# TODO: introduce optional Numba JIT
# TODO: Reorder file structure
# import modules
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rc
from matplotlib.ticker import ScalarFormatter
CB_color_cycle = ['#377eb8', '#ff7f00', '#4daf4a',
'#f781bf', '#a65628', '#984ea3',
'#999999', '#e41a1c', '#dede00']
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=CB_color_cycle)
rc('text', usetex=True)
rc('text.latex', preamble=r'\usepackage{gensymb}')
# !python numbers=disable
fig_width_pt = 426.0 # Get this from LaTeX using \showthe\columnwidth result:
inches_per_pt = 1.0 / 72.27 # Convert pt to inches
golden_mean = (np.sqrt(5) - 1.0) / 2.0 # Aesthetic ratio
fig_width = fig_width_pt * inches_per_pt # width in inches
fig_height = fig_width * golden_mean # height in inches
fig_size = [fig_width, fig_height]
params = {'backend': 'ps',
'axes.labelsize': 10,
'font.size': 10,
'legend.fontsize': 10,
'xtick.labelsize': 8,
'ytick.labelsize': 8,
'text.usetex': True,
'figure.figsize': fig_size}
mpl.rcParams.update(params)
mpl.rcParams["font.family"] = ["Latin Modern Roman"]
# !python numbers=disable
# pylab.axes([0.125,0.2,0.95-0.125,0.95-0.2])
plt.rcParams['path.simplify'] = True
print("\nFinished importing modules!\n")
# %%
# Functions:
# generate signal autocorrelation matrix - forward averaging
def autocorrelate(signals_mat, snap_size):
return 1.0 / snap_size * np.transpose(signals_mat) @ np.conjugate(signals_mat)
# generate signal autocorrelation matrix - forward-backward averaging
def autocorrelate_fb(signals_mat, snap_size):
d_J = np.eye(signals_mat.shape[1], signals_mat.shape[1])
d_J = np.fliplr(d_J)
out_matrix = 1.0 / snap_size * np.transpose(signals_mat) @ np.conjugate(signals_mat)
return 0.5 * out_matrix + (0.5 / snap_size) * d_J @ np.conjugate(out_matrix) @ d_J
# antenna array creation, equally placed around zero
def gen_antenna_vec(lambda_separation, n_rx):
return np.linspace(-(n_rx - 1) * lambda_separation / 2, (n_rx - 1) * lambda_separation / 2, n_rx)
# CPP: ant_loc = np.zeros(n_rx)
# CPP: for j in range (n_rx):
# CPP: ant_loc[j] = ant_separation*0.5*(n_rx-1-2*j)
# generate theta angles for evaluation in range of 0-180deg
def gen_theta_vector(pspec_length):
return np.linspace(0, np.pi, pspec_length)
# generate theta vector
# CPP: d_theta = np.zeros(pspec_length,dtype=float)
# CPP: d_theta[0] = 0.0
# CPP: theta_prev = 0.0
# CPP: for k in range(1,pspec_length):
# CPP: theta = theta_prev+180.0/pspec_length
# CPP: theta_prev = theta
# CPP: d_theta[k] = np.pi*theta/180.0
# array response matrix formation
def gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec):
vii_temp = np.zeros(n_rx)
arr_resp_mat = np.ndarray([pspec_length, n_rx], dtype=complex)
arr_resp_mat_trans = np.ndarray([n_rx, pspec_length], dtype=complex)
for ii in range(pspec_length):
vii_temp = np.exp(-1j * 2 * np.pi * np.cos(theta_vec[ii]) * antenna_vec)
arr_resp_mat[ii, :] = vii_temp
arr_resp_mat_trans = arr_resp_mat.conj().T
return arr_resp_mat, arr_resp_mat_trans
# core MUSIC algorithm
def calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_resp_mat, arr_resp_mat_trans):
# determine EVD of the auto-correlation matrix
eig_val, eig_vect = np.linalg.eig(autocorr_mat)
# noise subspace
U_N = eig_vect[:, n_sources:n_rx]
# CPP: eig_vec.cols(0, d_num_ant_ele-d_num_targets-1);
U_N_sq = U_N @ (U_N.conj().T)
# determine pseudo-spectrum for each value of theta in [0.0, 180.0)
pspec_out_vec = np.zeros(pspec_length, dtype=float)
for ii in range(pspec_length):
Q_temp = arr_resp_mat_trans[:, ii] @ U_N_sq @ arr_resp_mat[ii, :]
pspec_out_vec[ii] = 1.0 / (Q_temp.real)
# null-spectrum
# pspec_out_vec[ii] = Q_temp.real
pspec_out_vec = 10.0 * np.log10(pspec_out_vec / np.max(pspec_out_vec))
return pspec_out_vec
# calculate array response vector coefficients for specified theta
def array_response_vector(antena_vec, theta):
return np.exp(-1j * 2 * np.pi * antena_vec * np.cos(theta))
# create real case sinusoidal signal with ADC params (redundant)
def gen_real_case_signal():
# Generate signals based on provided params:
# TO DO:
# +add automatic phase offset calculation for specified AOA
# +add mulitple signals (more harmonics at different angles)
# +(optional)add phase noise
# +IQ imbalance?
# signal (single harmonic wave)
sig_freq = 1e3
# print("Signal frequency after downconversion: %d [Hz]"%(sig_freq))
# sample rate
sampl_rate = 1e5
# print("RX sample rate: %d"%(sampl_rate))
snap_size = 1024
# print("Snapshot size: %d"%(snap_size))
obs_time_len = snap_size / sampl_rate
# print("Resultant observation time: %.3f ms"%(1000*obs_time_len))
# angle vector values
angle_vec = 2 * np.pi * sig_freq * np.linspace(0, obs_time_len, snap_size)
# timestep vector values
time_vec = np.linspace(0, snap_size / sampl_rate, snap_size)
n_rx = 2
snap_size = 1024
ampl_diff_vec = np.array([1, 1])
# print("Amplitude coefficients vector:", ampl_diff_vec)
phase_diff_vec = np.array([0, 0.7 * np.pi])
# print("Phase coefficients vector:", phase_diff_vec)
add_noise_flag = True
sig_mat = np.ndarray([snap_size, n_rx], dtype=complex)
for idx in range(n_rx):
cx_signal_vec = np.array((ampl_diff_vec[idx] * np.cos(angle_vec + phase_diff_vec[idx]) + (
1j * ampl_diff_vec[idx] * np.sin(angle_vec + phase_diff_vec[idx]))))
if add_noise_flag:
awgn = np.random.normal(0, 0.05, snap_size) + (1j * np.random.normal(0, 0.05, snap_size))
cx_signal_vec = cx_signal_vec + awgn
sig_mat[:, idx] = cx_signal_vec
"""
plt.figure(1)
plt.xlabel("Time [s]")
plt.subplot(211)
plt.title("Signals in time")
plt.plot(time_vec,np.real(sig_mat[:,0]),label = "Real")
plt.plot(time_vec,np.imag(sig_mat[:,0]),label = "Imag")
plt.legend()
plt.subplot(212)
plt.plot(time_vec,np.real(sig_mat[:,1]),label = "Real")
plt.plot(time_vec,np.imag(sig_mat[:,1]),label = "Imag")
plt.xlabel("Time [s]")
plt.legend()
plt.show()
"""
return sig_mat
# N = array.shape()
# v = np.exp(-1j*2*np.pi*array*np.cos(theta))
# return v/np.sqrt(N)
# create artificial testing signal
def gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec=None, source_power_vec=None,
power_diff_vec=None, snr_db=float('inf'), print_params=False):
if (source_theta_vec is None):
# random source directions
source_theta_vec = np.pi * (np.random.rand(n_sources))
if (source_power_vec is None):
# random source powers
source_power_vec = np.sqrt(1 / 2) * (np.random.randn(n_sources) + np.random.randn(n_sources) * 1j)
if (power_diff_vec is None):
power_diff_vec = np.ones(n_rx)
if (print_params == True):
print("Sources theta: ", source_theta_vec)
print("Sources power: ", abs(source_power_vec))
print("RX node power diff coefficient: ", power_diff_vec)
# generate signal samples
signals_mat = np.zeros((n_samples, n_rx), dtype=complex)
for sample_idx in range(n_samples):
array_sync_sample_vec = np.zeros(n_rx)
for source_idx in range(n_sources):
phase = np.exp(1j * 2 * np.pi * np.random.randn(1))
array_sync_sample_vec = array_sync_sample_vec + phase * source_power_vec[
source_idx] * array_response_vector(antenna_vec, source_theta_vec[source_idx])
signals_mat[sample_idx, :] = array_sync_sample_vec
# add white gaussian noise
for sample_vec_idx in range(signals_mat.shape[1]):
signals_mat[:, sample_vec_idx] = add_awgn_vec(signals_mat[:, sample_vec_idx], snr_db)
power_diff_vec = np.array(power_diff_vec)
signals_mat = signals_mat * power_diff_vec.T
return signals_mat, source_theta_vec
def rmse(predictions, target):
return np.sqrt(((predictions - target) ** 2).mean())
def add_awgn_vec(in_vec, snr_db):
sig_pow = np.sum(np.square(np.abs(in_vec))) / len(in_vec)
noise_pow = sig_pow / 10 ** (snr_db / 10)
imp = 1 # assuming impedance is 1 Ohm
noise_vec = (np.sqrt(imp * noise_pow / 2)) * (np.random.randn(len(in_vec)) + 1j * np.random.randn(len(in_vec)))
# print("In vec var:", np.var(in_vec))
# print("Noise vec var:", np.var(noise_vec))
# print("SNR [dB]:", 10*np.log10(np.var(in_vec)/np.var(noise_vec)))
return in_vec + noise_vec
print("\nFinished declaring functions!\n")
# %%
# Music algorithm simulation - reference run
# rng seed
np.random.seed(5)
n_sources = 2 # number of sources
n_rx = 4 # number of ULA elements
snr = 10 # signal to noise ratio
antenna_separation = 0.5 #
pspec_length = 1800 #
n_samples = 1024 #
# antenna vector creation
antenna_vec = gen_antenna_vec(antenna_separation, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
# custom source powers, thetas and power reception diffs
source_theta_vec = np.radians(np.array([50, 120]))
source_power_vec = np.array([1, 1])
power_diff_vec = np.array([1, 1, 1, 1])
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec, source_power_vec,
power_diff_vec, snr)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
plt.plot(np.degrees(theta_vec), pspec_vec, label='_nolegend_')
# plot reference - real theta values vertical lines
for source_theta_val in source_theta_vec:
plt.axvline(np.degrees(source_theta_val), linewidth=0.5, color='k')
plt.xlim(0.0, 180.0)
plt.title("MUSIC algorithm pseudospectrum")
plt.xlabel("Angle of arrival [°]")
plt.ylabel("Power [dB]")
plt.legend(["Real AoA"])
plt.tight_layout()
plt.savefig("figs/init_aoa_sim.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# SNR SWEEP
# rng seed
np.random.seed(5)
n_sources = 2 # number of sources
n_rx = 4 # number of ULA elements
snr = 10 # signal to noise ratio
antenna_separation = 0.5 #
pspec_length = 1800 #
n_samples = 1024 #
# antenna vector creation
antenna_vec = gen_antenna_vec(antenna_separation, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
# custom source powers, thetas and power reception diffs
source_theta_vec = np.radians(np.array([50, 120]))
source_power_vec = np.array([1, 1])
snr_vec = [-10, 0, 10]
for snr_val in snr_vec:
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec,
source_power_vec, None, snr_val)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
plt.plot(np.degrees(theta_vec), pspec_vec, label='%d' % snr_val)
# plot reference - real theta values vertical lines
for source_theta_val in source_theta_vec:
plt.axvline(np.degrees(source_theta_val), linewidth=0.5, color='k')
plt.xlim(0.0, 180.0)
plt.title("MUSIC algorithm pseudospectrum")
plt.xlabel("Angle of arrival [°]")
plt.ylabel("Power [dB]")
plt.legend(loc="lower right", title="SNR [dB]")
plt.tight_layout()
plt.savefig("figs/aoa_sim_snr_sweep.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# N antennas SWEEP
# rng seed
np.random.seed(5)
n_sources = 2 # number of sources
n_rx = 4 # number of ULA elements
snr = 10 # signal to noise ratio
antenna_separation = 0.5 #
pspec_length = 1800 #
n_samples = 1024 #
ant_num_vec = [4, 8, 16] # number of ULA elements
for n_rx in ant_num_vec:
# antenna vector creation
antenna_vec = gen_antenna_vec(antenna_separation, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
# custom source powers, thetas and power reception diffs
source_theta_vec = np.radians(np.array([50, 120]))
source_power_vec = np.array([1, 1])
power_diff_vec = np.array([1, 1, 1, 1])
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec,
source_power_vec, None, snr)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate_fb(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
plt.plot(np.degrees(theta_vec), pspec_vec, label='%d' % n_rx)
# plot reference - real theta values vertical lines
for source_theta_val in source_theta_vec:
plt.axvline(np.degrees(source_theta_val), linewidth=0.5, color='k')
plt.xlim(0.0, 180.0)
plt.title("MUSIC algorithm pseudospectrum")
plt.xlabel("Angle of arrival [°]")
plt.ylabel("Power [dB]")
plt.legend(loc="lower right", title="N antennas")
plt.tight_layout()
plt.savefig("figs/aoa_sim_n_rx_sweep.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# SNAPSHOT LENGTH SWEEP
# rng seed
np.random.seed(5)
n_sources = 2 # number of sources
snr = 10 # signal to noise ratio
n_rx = 4 # number of ULA elements
antenna_separation = 0.5 #
pspec_length = 1800 #
n_samples_vec = [10, 100, 1000] #
for snap_size in n_samples_vec:
# antenna vector creation
antenna_vec = gen_antenna_vec(antenna_separation, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
# custom source powers, thetas and power reception diffs
source_theta_vec = np.radians(np.array([50, 120]))
source_power_vec = np.array([1, 1])
power_diff_vec = np.array([1, 1, 1, 1])
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, snap_size, source_theta_vec,
source_power_vec, None, snr)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
plt.plot(np.degrees(theta_vec), pspec_vec, label='%d' % snap_size)
# plot reference - real theta values vertical lines
for source_theta_val in source_theta_vec:
plt.axvline(np.degrees(source_theta_val), linewidth=0.5, color='k')
plt.xlim(0.0, 180.0)
plt.title("MUSIC algorithm pseudospectrum")
plt.xlabel("Angle of arrival [°]")
plt.ylabel("Power [dB]")
plt.legend(loc="lower right", title="Snapshot length")
plt.tight_layout()
plt.savefig("figs/aoa_snap_size_sweep.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# ANTENNA SPACING SWEEP
# rng seed
np.random.seed(5)
n_sources = 2 # number of sourceszz
snr = 10 # signal to noise ratio
n_rx = 8 # number of ULA elements
pspec_length = 1800 #
n_samples = 1024 #
ant_sep_list = [0.4, 0.5, 0.6] #
for ant_sep in ant_sep_list:
# antenna vector creation
antenna_vec = gen_antenna_vec(ant_sep, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
# custom source powers, thetas and power reception diffs
source_theta_vec = np.radians(np.array([20, 30]))
source_power_vec = np.array([1, 1])
power_diff_vec = np.array([1, 1, 1, 1])
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec,
source_power_vec, None, snr)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
plt.plot(np.degrees(theta_vec), pspec_vec, label='%1.1f' % ant_sep)
# plot reference - real theta values vertical lines
for source_theta_val in source_theta_vec:
plt.axvline(np.degrees(source_theta_val), linewidth=0.5, color='k')
plt.xlim(0.0, 180.0)
plt.title("MUSIC algorithm pseudospectrum")
plt.xlabel("Angle of arrival [°]")
plt.ylabel("Power [dB]")
plt.legend(loc="lower right", title="RX spacing [$\lambda$]")
plt.tight_layout()
plt.savefig("figs/aoa_ant_sep_sweep.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# EVAL OF RX POWER DIFFERENCES
np.random.seed(5)
n_sources = 2 # number of sources
n_rx = 4 # number of ULA elements
snr = 10 # signal to noise ratio
antenna_separation = 0.5 #
pspec_length = 1800 #
n_samples = 1024 #
# antenna vector creation
antenna_vec = gen_antenna_vec(antenna_separation, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
rx_power_coeff_vec = np.linspace(0.5, 1.5, 5)
source_theta_vec = np.radians(np.array([110, 120]))
source_power_vec = np.array([0.8, 1.2])
for rx_power_coeff in rx_power_coeff_vec:
power_diff_vec = np.array([rx_power_coeff, 1, 1, 1])
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec,
source_power_vec, None, snr)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
plt.plot(np.degrees(theta_vec), pspec_vec, linewidth=1, label='%1.2f' % rx_power_coeff)
for source_theta_val in source_theta_vec:
tmp = plt.axvline(np.degrees(source_theta_val), linewidth=0.5, color='k')
tmp.set_label("Real AoA")
plt.xlim(0.0, 180.0)
plt.title("MUSIC algorithm pseudospectrum")
plt.xlabel("Angle of arrival [angle]")
plt.ylabel("Power [dB]")
plt.legend(title="RX1 power coefficient:", loc='upper right')
plt.tight_layout()
plt.savefig("figs/aoa_rx_pow_diff_sweep.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# SNR VS RMSE ERROR MONTE CARLO SIMULATION
# rng seed
np.random.seed(5)
n_sources = 1 # number of sources
n_rx = 4 # number of ULA elements
snr = 10 # signal to noise ratio
antenna_separation = 0.5 #
pspec_length = 18000 #
n_samples = 1024 #
# antenna vector creation
antenna_vec = gen_antenna_vec(antenna_separation, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
# custom source powers, thetas and power reception diffs
angles = [50]
source_theta_vec = np.radians(np.array(angles))
source_power_vec = np.array([1])
power_diff_vec = np.array([1, 1, 1, 1])
n_runs = 500
snr_vec = np.arange(-15, 31, 1, dtype='int32')
print(snr_vec)
rmse_arr = np.ndarray(len(snr_vec))
for snr_idx in range(len(snr_vec)):
aoa_estimate = np.ndarray(n_runs)
for run_idx in range(n_runs):
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec,
source_power_vec, power_diff_vec, snr_vec[snr_idx])
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
peak = np.argmax(pspec_vec) / 100
aoa_estimate[run_idx] = peak
rmse_arr[snr_idx] = rmse(aoa_estimate, angles[0])
fig, ax = plt.subplots()
plt.plot(snr_vec, rmse_arr, '.-')
plt.yscale('log', basey=2)
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.xaxis.set_major_formatter(ScalarFormatter())
x_tick_vals = np.linspace(-15, 30, 10)
ax.set_xticks(x_tick_vals)
y_tick_vals = np.power(2, np.linspace(-5, 1, 7))
print(y_tick_vals)
y_strings = ["%1.5f" % number for number in y_tick_vals]
y_strings = [s.rstrip("0") for s in y_strings]
y_strings = [s.rstrip(".") for s in y_strings]
plt.yticks(y_tick_vals, y_strings)
# # tick_vals = np.power(2,np.linspace(1,10,10))
# # ax.set_xticks(tick_vals)
# # ax.set_xticklabels(tick_vals)
plt.xlim([-20, 35])
plt.title("Angle estimation error in regard to signal to noise ratio")
plt.xlabel("Signal to noise ratio [dB]")
plt.ylabel("RMSE of esitmation [°]")
plt.grid(which="both", ls="-")
plt.tight_layout()
plt.savefig("figs/aoa_sim_snr_mc_rmse.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# SNAPSHOT LENGTH VS RMSE ERROR MONTE CARLO SIMULATION
# rng seed
np.random.seed(5)
n_sources = 1 # number of sources
n_rx = 4 # number of ULA elements
snr = 10 # signal to noise ratio
antenna_separation = 0.5 #
pspec_length = 1800 #
n_samples = 1024 #
# antenna vector creation
antenna_vec = gen_antenna_vec(antenna_separation, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
# custom source powers, thetas and power reception diffs
angles = [50]
source_theta_vec = np.radians(np.array(angles))
source_power_vec = np.array([1])
power_diff_vec = np.array([1, 1, 1, 1])
n_runs = 500
snap_len_vec = np.unique(np.logspace(2, 10, 50, base=2, dtype='int32'))
rmse_arr = np.ndarray(len(snap_len_vec))
def rmse(predictions, target):
return np.sqrt(((predictions - target) ** 2).mean())
for snap_idx in range(len(snap_len_vec)):
aoa_estimate = np.ndarray(n_runs)
for run_idx in range(n_runs):
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, snap_len_vec[snap_idx],
source_theta_vec, source_power_vec, power_diff_vec, snr)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, snap_len_vec[snap_idx])
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
peak = np.argmax(pspec_vec) / 10
aoa_estimate[run_idx] = peak
rmse_arr[snap_idx] = rmse(aoa_estimate, angles[0])
fig, ax = plt.subplots()
plt.loglog(snap_len_vec, rmse_arr, '.-', base=2)
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.xaxis.set_major_formatter(ScalarFormatter())
x_tick_vals = np.power(2, np.linspace(1, 11, 11))
ax.set_xticks(x_tick_vals)
y_tick_vals = np.power(2, np.linspace(-3, 3, 7))
y_strings = ["%1.3f" % number for number in y_tick_vals]
y_strings = [s.rstrip("0") for s in y_strings]
y_strings = [s.rstrip(".") for s in y_strings]
plt.yticks(y_tick_vals, y_strings)
tick_vals = np.power(2, np.linspace(1, 10, 10))
ax.set_xticks(tick_vals)
plt.xlim([2, 2048])
plt.ylim([0.125, 8])
plt.title("Angle estimation error in regard to snapshot length")
plt.xlabel("Length of snapshot")
plt.ylabel("RMSE of esitmation [°]")
plt.grid(which="both", ls="-")
plt.tight_layout()
plt.savefig("figs/aoa_sim_snap_mc_rmse.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# ANTENNA SPACING VS RMSE ERROR MONTE CARLO SIMULATION
# rng seed
np.random.seed(5)
n_sources = 1 # number of sources
n_rx = 4 # number of ULA elements
snr = 10 # signal to noise ratio
antenna_separation = 0.5 #
pspec_length = 1800 #
n_samples = 1024 #
angles = [15]
source_theta_vec = np.radians(np.array(angles))
source_power_vec = np.array([1])
power_diff_vec = None
# monte carlo analysis
n_runs = 500
fig, ax = plt.subplots()
ant_arr_spacing_vec = 0.5 * np.logspace(-1, 0, 50)
rmse_arr = np.ndarray(len(ant_arr_spacing_vec))
def rmse(predictions, target):
return np.sqrt(((predictions - target) ** 2).mean())
for spacing_idx in range(len(ant_arr_spacing_vec)):
aoa_estimate = np.ndarray(n_runs)
ant_spacing_tmp = ant_arr_spacing_vec[spacing_idx]
# antenna vector creation
antenna_vec = gen_antenna_vec(ant_spacing_tmp, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
for run_idx in range(n_runs):
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec,
source_power_vec, power_diff_vec, snr)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
peak = np.argmax(pspec_vec) / 10
aoa_estimate[run_idx] = peak
rmse_arr[spacing_idx] = rmse(aoa_estimate, angles[0])
plt.loglog(ant_arr_spacing_vec, rmse_arr, '.-', base=2)
ax.yaxis.set_major_formatter(ScalarFormatter())
ax.xaxis.set_major_formatter(ScalarFormatter())
x_tick_vals = np.power(2, np.linspace(-4, -1, 4))
plt.xticks(x_tick_vals, x_tick_vals)
y_tick_vals = np.power(2, np.linspace(-1, 3, 5))
y_strings = ["%1.3f" % number for number in y_tick_vals]
y_strings = [s.rstrip("0") for s in y_strings]
y_strings = [s.rstrip(".") for s in y_strings]
plt.yticks(y_tick_vals, y_strings)
plt.ylim([0.25, 16])
plt.xlim([0.04, 0.75])
plt.title("Angle estimation error in regard to antenna spacing")
plt.xlabel("Antenna spacing $[\lambda]$")
plt.ylabel("RMSE of esitmation [°]")
plt.grid(which="both", ls="-")
plt.tight_layout()
plt.savefig("figs/aoa_sim_ant_spacing_mc_rmse.png", dpi=600, bbox_inches='tight')
plt.show()
# %%
# Music POWER DIFF MONTE CARLO SIMULATION
# rng seed
np.random.seed(5)
n_sources = 1 # number of sources
n_rx = 4 # number of ULA elements
snr = 10 # signal to noise ratio
antenna_separation = 0.5 #
pspec_length = 1800 #
n_samples = 1024 #
angles = [15]
source_theta_vec = np.radians(np.array(angles))
source_power_vec = np.array([1])
# monte carlo analysis
n_runs = 500
fig, ax = plt.subplots()
ant_spacing = 0.5
pow_vec = np.linspace(0.1, 2, 30)
rmse_arr = np.ndarray(len(pow_vec))
def rmse(predictions, target):
return np.sqrt(((predictions - target) ** 2).mean())
for pow_idx in range(len(pow_vec)):
ampl_val = pow_vec[pow_idx]
power_diff_vec = [ampl_val, 1, 1, 1]
aoa_estimate = np.ndarray(n_runs)
# antenna vector creation
antenna_vec = gen_antenna_vec(ant_spacing, n_rx)
# print("Antenna vector: ", antenna_vec)
theta_vec = gen_theta_vector(pspec_length)
# print("Theta vector: ", theta_vec)
arr_response_mat, arr_response_mat_trans = gen_array_response_mat(pspec_length, n_rx, theta_vec, antenna_vec)
for run_idx in range(n_runs):
# generate signal
sig_mat, source_theta_vec = gen_signals_mat(n_rx, antenna_vec, n_sources, n_samples, source_theta_vec,
source_power_vec, power_diff_vec, snr)
# generate autocorrelation matrix (n_inputs x n_inputs)
# print("Autocorrelation matrix shape: ",acorr_sig_mat.shape)
autocorr_mat = np.ndarray([n_rx, n_rx], dtype=complex)
autocorr_mat = autocorrelate(sig_mat, n_samples)
pspec_vec = calc_aoa_music(n_sources, n_rx, autocorr_mat, arr_response_mat, arr_response_mat_trans)
peak = np.argmax(pspec_vec) / 100
aoa_estimate[run_idx] = peak
rmse_arr[pow_idx] = rmse(aoa_estimate, angles[0])
print("FINISHED!")
plt.loglog(pow_vec ** 2, rmse_arr, '.-')
x_tick_vals = np.power(2, np.linspace(-4, -1, 4))
plt.xticks(x_tick_vals, x_tick_vals)
y_tick_vals = np.power(2, np.linspace(-1, 3, 5))
y_strings = ["%1.3f" % number for number in y_tick_vals]
y_strings = [s.rstrip("0") for s in y_strings]
y_strings = [s.rstrip(".") for s in y_strings]
plt.yticks(y_tick_vals, y_strings)
plt.title("Angle estimation error in regard to antenna spacing")
plt.xlabel("Antenna spacing $[\lambda]$")
plt.ylabel("RMSE of esitmation [°]")
plt.grid(which="both", ls="-")
plt.tight_layout()
plt.savefig("figs/aoa_sim_rx_pow_diff_mc_rmse.png", dpi=600, bbox_inches='tight')
plt.show()