-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoA_Code.m
282 lines (217 loc) · 6.46 KB
/
DoA_Code.m
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
%%
% HW1 Radar Imaging
% Pouya Aminaie
clc;clear; close all
%%
% Defining the parameters:
parameter.f0 = 77*10^(9); % Carrier Frequency [Hz]
parameter.B = 1*10^(9); % Bandwiths of system [Hz]
parameter.fs=4*10^(9); % Sampling Frequency [Hz]
parameter.rho = 2; % Angular Resolution [deg]
parameter.num_ant = 9; % Number of Antenna
parameter.C=3*10^8; % Speed of light [m/s]
%%
% Initialization:
lambda=parameter.C/(parameter.f0); % Wavelength Calculation
L=lambda/(((2*pi)/180)*parameter.rho); % Length of array Calculation
% Then, the angular FOV should be limited by appling Rayleigth criterion
% on spacing. Therefore, Delta_X < (lambda/2) ===> maximum dx = labmba/2
dx=lambda/2; % space between two consequential antennas
%dx=0.5;
%%
% Antenna Positioning:
Array_x=[]; % Horizontal Position of elements
for i=1:parameter.num_ant
Array_x=[Array_x, (-floor(parameter.num_ant/2)+(i-1))*dx];
end
Array_y=zeros(1,parameter.num_ant); % Vertical Position of element
% Targert Positioning:
Target_x= 14; % Horizoontal Distance[m]
Target_y= 12; % Vertical Distance[m]
rp=sqrt((Target_x)^2 + (Target_y)^2); % Distance of target from the center
%%
% Distance Calculations:
r_n=zeros(1,parameter.num_ant); % Distant of target from element [m]
x0=Target_x; % Horizoontal Distance[m]
y0=Target_y; % Vertical Distance [m]
for i=1:parameter.num_ant
% Try to formulate geometrical distance
r_n(i)=sqrt((Array_x(i)-x0)^2 + (Array_y(i)-y0)^2);
end
%%
% in this part, we scan the space with the resolution of 2 deg
% in order to find the phase of received signal at each elements.
n=[-(numel(Array_x)-1)/2:1:+(numel(Array_x)-1)/2];
angles=0:parameter.rho:180-1;
angles=deg2rad(angles);
%%
%ph_global = [];
ph = [];
syms sin_theta
for i = 1:numel(n)
% Populate ph inside the inner loop
ph = [ph, ((-2*pi)/lambda)*(rp - abs(n(i))*dx*sin_theta)];
% Concatenate ph to ph_global after the inner loop
end
ph_global = ph;
clear i
Srx=exp(i*ph_global);
%%
% % phi calculations:
% phi=[];
% for i=2:numel(Array_x)
% phi= [phi ph_global(i)-ph_global(i-1)];
% end
% %%
% %DoA Calculation
%
% syms S(sin_theta)
% y= asin(delta_phi*lambda/(-2*pi*dx));
% S(sin_theta) = y;
% DoA=rad2deg(S(sin(atan(y0/x0))));
% disp(DoA)
%%
% DoA estimation
syms S(sin_theta)
delta_phi=(2*pi*parameter.f0 /parameter.C)*(r_n(7)-r_n(6));
real_DoA=rad2deg(atan(y0/x0))
estimated_DoA=90+rad2deg(asin(delta_phi*lambda/(2*pi*dx)))
error=abs(abs(real_DoA)-abs(estimated_DoA))
%%
% 2.3 section 4:
% Number of Monte Carlo simulations
%num_simulations = 100;
% Range of noise powers to test
%noise_powers = logspace(-4, 0, 10);
% Initialize arrays to store results
%std_errors = zeros(size(noise_powers))
% received signal at array:
%array_response_true = exp(-2i*pi*parameter.f0*r_n/parameter.C);
% Range of noise powers to test
%noise_powers = logspace(-4, 0, 10); % Adjust as needed
% Initialize arrays to store results
%std_errors = zeros(size(noise_powers));
%for k = 1:length(noise_powers)
% noise_power = noise_powers(k);
% errors = zeros(1, num_simulations);
errors=[];
num_simulations = 500;
for i=1:num_simulations
N=9;
% repeat 100 time with 100 nosie power
randdd=i*((randn(N, 1) + 1j * randn(N, 1)));
% for i = 1:num_simulations
% delta_phi_noisy=abs(ang((5))+abs(ang(4)));
% MUSIC algorithm for DoA estimation
% [~, DoAs] = music(Srx, M, lambda);
% Calculate error in DoA estimation
estimated_DoA=90+rad2deg(asin((delta_phi+randdd*lambda/(2*pi*dx))));
errors=[errors abs(estimated_DoA-real_DoA)];
std_errors=std(errors)
% end
end
%%
% plot error for 500 simulation
close all
figure
plot(std_errors/sqrt(num_simulations))
xlabel("noise power")
ylabel("std")
title("Standard Deviation for 500 simulation");
grid on
%%
% error plot for 500 simulation and a single element
figure
plot(errors(1,:)/500)
xlabel("noise power")
ylabel("error")
title("Error for 500 simulation and a single elemente");
grid on
%%
% plot std
% std_errors=[];
% x_sdt_axis=[-4 -3 -2 -1 0 1 2 3 4];
% for j=1:9
% std_errors=[std_errors std(errors(j,:)/100) ];
%
%
% end
plot(x_sdt_axis, std_errors,'o--r');
title("standard deviation for all elements");
grid on
%%
% Calculate standard deviation of the error
%std_errors(k) = std(errors);
%end
%%
% Plot results
figure;
loglog(noise_powers, std_errors, 'o-', 'LineWidth', 2);
xlabel('Noise Power');
ylabel('Standard Deviation of DoA Estimation Error');
title('Monte Carlo Simulation: DoA Estimation with Noise');
grid on;
%%
% 2D Poositioning
% With respect to nyquist rate, we consider sampling frequency fs=4 GHz
% in order to avoid aliasing.
dt_n=r_n/parameter.C; % Corresponding delay related to each element of array
end_time= 0.5 * 10 ^ (-6);
time=1/parameter.fs:1/parameter.fs:end_time;
%%
% Time of arrival calculation at rx
time_rx=[];
for i=1:numel(dt_n)
time_rx=[time_rx;time+dt_n(i)];
end
%%
% Stx definition
syms t g(t)
g(t)=(sinc(parameter.B*t))*(exp(2i*parameter.f0*t));
%%
% S matrix calculations
Srx=[];
Srx_demod=[];
Stx=g(time);
for i=1:numel(r_n)
Srx=[Srx; g(time_rx(i,:))];
Srx_demod=[Srx_demod;Srx(i,:).*conj(Stx)];
end
%%
% DFT Calculation
% rho = parameter.C/(2*parameter.B); % range of resolution
% r_ax= 5:rho:10;
% Nf=length(r_ax);
%
%
% g=double(Srx_demod(1,:));
% [X,f]=my_dft(g,time,Nf);
%%
%
% pow_vect=((abs(X)).^2)/Nf;
% figure(4444)
% plot(pow_vect(1,:))
%
%
% %%
%
% [s_ddf,f1]=my_dft(abs(g(1,:)),time,Nf); % study the my_dft
%
% figure(3333)
% subplot(2,1,1)
% plot(f1,abs(s_ddf(:,1)))
% xlabel("Frequency");
% ylabel("Amplitude");
% title "AmplitudeSpectrum"
% grid on;
% subplot(2,1,2)
% plot(f1,(angle(s_ddf(:,1))))
% xlabel("Frequency");
% ylabel("Phase");
% title "PhaseSpectrum"
% grid on;
%%
% our method:
%dx = lambda / 2
%phi_time=Srx_demod(6,:)-Srx_demod(5,:);
%[~, DoAs] = music(Srx_demod(6,:), 1, parameter.C / (2 * dx));