# Why the 2nd code does not behave like the 1st code?

2 ビュー (過去 30 日間)
Sadiq Akbar 2023 年 2 月 27 日
コメント済み: Sadiq Akbar 2023 年 3 月 3 日
In the attached code of "myfunAskMathworks.m", yo and ye both are of the order 36x1 and these are correct. But in the other attached code "codeWithArrays.m", it gives error on line 35. Why is it so? In this also we should get the same order of yo and ye but it gives error.

サインインしてコメントする。

### 採用された回答

Askic V 2023 年 3 月 3 日

This is how I would modified the code to execute for any M r N dimensions:
clear
clc
u = [10 20 30 40];
b = u * (1+0.5*randn); % random deviation between u and b
% Tx antennas
N = 6;
% Array geometry [rx,ry,rz] (example: uniform circular array)
rT = [rx, ry, zeros(N,1)];% Always No. of Antennas x 3
% Rx antennas
M = 10;
% Array geometry [rx,ry,rz] (example: uniform circular array)
rR = [rx, ry, zeros(M,1)];% Always No. of Antennas x 3
% rT is size Nx3, so to make multiplication work,
% [cos(alpha).*cos(beta); sin(alpha).*cos(beta); sin(beta)] must be 3x1
AT = @(alpha, beta) exp(-1j.*rT*pi*[cos(alpha).*cos(beta); sin(alpha).*cos(beta); sin(beta)]);
% rR is size Mx3, so to make multiplication work,
% [cos(alpha).*cos(beta); sin(alpha).*cos(beta); sin(beta)] must be 3x1
AR = @(alpha, beta) exp(-1j.*rR*pi*[cos(alpha).*cos(beta); sin(alpha).*cos(beta); sin(beta)]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculation of yo and ye
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yo = yMatTR(u, AT, AR);
ye = yMatTR(b, AT, AR);
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=norm(yo-ye).^2/(M*N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function yMatTR
%%%%%%%%%%%%%%%%%%%%%%%%5%%%
function y = yMatTR(targetAngle, steerVecT, steerVecR)
steerA = steerVecT(az,el);
steerB = steerVecR(az,el);
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end
##### 1 件のコメント-1 件の古いコメントを表示-1 件の古いコメントを非表示
Sadiq Akbar 2023 年 3 月 3 日
Thanks a lot dear Askic V for your kind response. Yes, now it works. Thank you once again.

サインインしてコメントする。

### その他の回答 (1 件)

Askic V 2023 年 2 月 27 日

It is because in the function "myfunAskMathworks.m" you have function handles defined. So, you are sending the function handle as an input parameter to the function yMatTR.
steerVecT = @(ang) exp(1j*2*pi*d*(0:M-1).'*sin(vecH(ang)));
In the line:
steerA = steerVecT(targetAngle)
you are actually calling the function handle for angles as an input to the function.
In the other file:
AT = exp(-1j*rT*k);
this is not a function, but an array (matrix) of complex numbers.
So in the line:
steerA = steerVecT(targetAngle);
you are trying to access elements of a matrix with indices that are not either integer or logical values.
Essentially, you are confusing function handles with matrices.
##### 12 件のコメント10 件の古いコメントを表示10 件の古いコメントを非表示
Sadiq Akbar 2023 年 3 月 2 日
Thanks a lot dear Askic V for your kind response. Yes, the formula of k is correct i.e.,
k = pi*[cosd(az).*cosd(el), sind(az).*cosd(el), sind(el)].';% Always 3 x No. of sources
Look at the code given by "weetabixharry". He has used this formula in his code which is correct.
In myfunaskMathworks.m code, the array is not made separate but instead it is used in the following two lines:
steerVecT = @(ang) exp(1j*2*pi*d*(0:M-1).'*sin(vecH(ang)));
steerVecR = @(ang) exp(1j*2*pi*d*(0:N-1).'*sin(vecH(ang)));
In this 0:M-1 and 0:N-1 form linear array and then steerVecT and steerVecR for transmiter and receiver are obtained. Both of them are linear i.e., M number of antennas are placed in a line on transmitter side and N number of antennas are placed in a line on receiver side. But in the 2nd code i.e., " codeWithArrays.m", the linear array on both the sides i.e., transmitter side and receiver sides are constructed with the following pieces of code:
% Tx antennas
N = 6;
% Array geometry [rx,ry,rz] (example: uniform circular array)
rT = [rx, ry, zeros(N,1)];% Always No. of Antennas x 3
% Rx antennas
M=6;
% Array geometry [rx,ry,rz] (example: uniform circular array)
rR = [rx, ry, zeros(M,1)];% Always No. of Antennas x 3
And likewise, the wavenumber is calculated with the following formula separately:
k = pi*[cosd(az).*cosd(el), sind(az).*cosd(el), sind(el)].';% Always 3 x No. of sources
and then these two i.e., k and r are used to find the response matrix i.e.,
% Matrix of array response vectors
A = exp(-1j*r*k);
While in the 1st code i.e., "myfunAskMathworks.m ", antenna arrays are not constructed separately but rather, both M and N antennas are written inside the two equations i.e.,
steerVecT = @(ang) exp(1j*2*pi*d*(0:M-1).'*sin(vecH(ang)));
steerVecR = @(ang) exp(1j*2*pi*d*(0:N-1).'*sin(vecH(ang)));
I want to remove these M and N from here and make seaprate antenna arrays; one at Tx side and other at Rx side, and then make use of those arrays to find the corresponding responses.
If you look at the dimensions of K, rR and rT and yo, then you can easily understand that I want to do the same in the other code and calculate yo of the same dimesnions for the same number of M and N.
Sadiq Akbar 2023 年 3 月 3 日
I tried to make the two codes appear the same to you and you understand it better. So I made some changes for you and also added comments for your understanding now. Look at the 1st code below:
clear; clc;
u=[35 39 127 63 14 57];b=u; % Signal source directions
M=10;% Tx antennas
N=10;% Rx antennas
d = 0.5; % constant
vecH = @(MAT) MAT(:).';
steerVecT = @(ang) exp(1j*2*pi*d*(0:M-1).'*sin(vecH(ang)));
steerVecR = @(ang) exp(1j*2*pi*d*(0:N-1).'*sin(vecH(ang)));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Calculation of yo and ye
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
yo = yMatTR(deg2rad(u), steerVecT, steerVecR); % 100x1 in which 100 is due to M*N
ye = yMatTR(deg2rad(b), steerVecT, steerVecR); % 100x1 in which 100 is due to M*N
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=norm(yo-ye).^2/(M*N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function yMatTR
%%%%%%%%%%%%%%%%%%%%%%%%5%%%
function y = yMatTR(targetAngle, steerVecT, steerVecR)
steerA = steerVecT(targetAngle);
steerB = steerVecR(targetAngle);
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end
It is ok and works well. I want to make changes in the following 2nd code so that it also behaves like the above code. The 2nd code is below:
clear; clc;
% Signal source directions
az = [35;39;127]; % Azimuths
el = [63;14;57]; % Elevations
K = length(az); % Number of sources
Am=ones(K,1);% Amplitudes
% ========= (2) RECEIVED SIGNAL ========= %
% Wavenumber vectors (in units of wavelength/2)
k = pi*[cosd(az).*cosd(el), sind(az).*cosd(el), sind(el)].';% 3 x No. of sources
% Constructing Antenna array
N = 10; % Number of antennas
% Array geometry [rx,ry,rz] (example: uniform circular array) in 3D space
r = [rx, ry, zeros(N,1)];% 10 x 3 i.e., No. of Antennas x 3Dimensional Space
% Note: r denotes the final antenna array in the form of circle in 3D space
% Matrix of array response vectors
A = exp(-1j*r*k);% 10 x 2 i.e., No. of Antennas x No. of sources
x = A*Am; % 10 x 1 i.e., No. of antennas x 1
I hope, now it will be easy for you to understand.

サインインしてコメントする。

### カテゴリ

Help Center および File ExchangeAntennas, Microphones, and Sonar Transducers についてさらに検索

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by