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

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
Askic V 2023년 3월 3일
편집: 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)
radius = 0.5/sind(180/N);
rx = radius*cosd(360*(0:N-1).'/N);
ry = radius*sind(360*(0:N-1).'/N);
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)
radius = 0.5/sind(180/M);
rx = radius*cosd(360*(0:M-1).'/M);
ry = radius*sind(360*(0:M-1).'/M);
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)
az = deg2rad(targetAngle(1:2));
el = deg2rad(targetAngle(3:4));
steerA = steerVecT(az,el);
steerB = steerVecR(az,el);
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end

댓글 수: 1

Thanks a lot dear Askic V for your kind response. Yes, now it works. Thank you once again.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Askic V
Askic V 2023년 2월 27일
편집: 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

Thanks a lot dear Askic V for your kind response. So, how can we make changes in the 2nd code i.e., in "codeWithArrays.m" so that it also behaves like the 1st function?
The main difference is that in your case you have a function of two variables (az and el). These are vectors of angles (azimuth and elevation).
In the example you provided there is only one variable (ang). So basically the task is how this to make it work for functions of two variables.
As a start, you need to define functions of two variables. For example, define two function handles:
AT = @(alpha, beta) exp(-1j.*rT*pi.*[cos(alpha).*cos(beta),sin(alpha).*cos(beta), sin(beta)].');
AR = @(alpha, beta) exp(-1j.*rR*pi.*[cos(alpha).*cos(beta), sin(alpha).*cos(beta), sin(beta)].')
I used here cos and sin instead of cosd and sind becaue later you use deg2rad function in your code.
If that is what you want, the next setp would be to pack two angles (or two arrays of angles) as an input parameter to the function yMatTR. The function would look something like this:
function y = yMatTR(targetAngle, steerVecT, steerVecR)
% az = targetAngle(1,:) - first row is az
% el = targetAngle(2,:) - second row is el
steerA = steerVecT(targetAngle(1,:), targetAngle(2,:));
steerB = steerVecR(targetAngle(1,:), targetAngle(2,:));
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end
So the function call would look like something like this:
yo = yMatTR([deg2rad(az);deg2rad(el)], AT, AR); % pack two angles in a single input param.
I think you now have all necessary inputs to make this work.
Thanks a lot dear Askic V for your kind response. I changed my code as you said like below:
close all; clear all; clc;
u=[10 20 30 40];b=u;
az = u(1:2)'; %Azimuths
el = u(3:4)'; % Elevations
M = length(az); % No. of sources
L = 1;
m = randn(M,L);
% Tx antennas
N = 6;
% Array geometry [rx,ry,rz] (example: uniform circular array)
radius = 0.5/sind(180/N);
rx = radius*cosd(360*(0:N-1).'/N);
ry = radius*sind(360*(0:N-1).'/N);
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)
radius = 0.5/sind(180/M);
rx = radius*cosd(360*(0:M-1).'/M);
ry = radius*sind(360*(0:M-1).'/M);
rR = [rx, ry, zeros(M,1)];% Always No. of Antennas x 3
AT = @(alpha, beta) exp(-1j.*rT*pi.*[cos(alpha).*cos(beta),sin(alpha).*cos(beta), sin(beta)].');
AR = @(alpha, beta) exp(-1j.*rR*pi.*[cos(alpha).*cos(beta), sin(alpha).*cos(beta), sin(beta)].');
yo = yMatTR([deg2rad(az);deg2rad(el)], AT, AR); % pack two angles in a single input param.
function y = yMatTR(targetAngle, steerVecT, steerVecR)
% az = targetAngle(1,:) - first row is az
% el = targetAngle(2,:) - second row is el
steerA = steerVecT(targetAngle(1,:), targetAngle(2,:));
steerB = steerVecR(targetAngle(1,:), targetAngle(2,:));
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end:
But when I run it, it gives me the following error:
Arrays have incompatible sizes for this operation.
Error in MathworksMIMObyAskic>@(alpha,beta)exp(-1j.*rT*pi.*[cos(alpha).*cos(beta),sin(alpha).*cos(beta),sin(beta)].') (line 25)
AT = @(alpha, beta) exp(-1j.*rT*pi.*[cos(alpha).*cos(beta),sin(alpha).*cos(beta), sin(beta)].');
Error in MathworksMIMObyAskic>yMatTR (line 32)
steerA = steerVecT(targetAngle(1,:), targetAngle(2,:));
Error in MathworksMIMObyAskic (line 27)
yo = yMatTR([deg2rad(az);deg2rad(el)], AT, AR); % pack two angles in a single input param.
Related documentation
Please examine this code, whether it is what you want:
clear
clc
close all; clear all; clc;
u=[10 20 30 40];b=u;
az = u(1:2)'; %Azimuths
el = u(3:4)'; % Elevations
M = length(az); % No. of sources
L = 1;
m = randn(M,L);
% Wavenumber vectors
%k = pi*[cosd(az).*cosd(el), sind(az).*cosd(el), sind(el)].';% Always 3 x No. of sources
% Tx antennas
N = 6;
% Array geometry [rx,ry,rz] (example: uniform circular array)
radius = 0.5/sind(180/N);
rx = radius*cosd(360*(0:N-1).'/N);
ry = radius*sind(360*(0:N-1).'/N);
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)
radius = 0.5/sind(180/M);
rx = radius*cosd(360*(0:M-1).'/M);
ry = radius*sind(360*(0:M-1).'/M);
rR = [rx, ry, zeros(M,1)];% Always No. of Antennas x 3
% Matrices AT and AR
% Always (TX Antennas x No. of sources)
AT = @(alpha, beta) exp(-1j.*rT*pi.*[cos(alpha).*cos(beta),sin(alpha).*cos(beta), sin(beta)].');
AR = @(alpha, beta) exp(-1j.*rR*pi.*[cos(alpha).*cos(beta), sin(alpha).*cos(beta), sin(beta)].');% Always (RX Antennas x No. of sources)
az=az';
el=el';
yo = yMatTR([deg2rad(az);deg2rad(el)], AT, AR); % 36x1 in which 36 is due to M*N
ye = yMatTR([deg2rad(az);deg2rad(el)], AT, AR);
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=norm(yo-ye).^2/(M*N)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function yMatTR
%%%%%%%%%%%%%%%%%%%%%%%%5%%%
function y = yMatTR(targetAngle, steerVecT, steerVecR)
% az = targetAngle(1,:); el = targetAngle(2,:)
steerA = steerVecT(targetAngle(1,:), targetAngle(2,:));
steerB = steerVecR(targetAngle(1,:), targetAngle(2,:));
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end
Thanks a lot dear Askic V for your kind response. Yes, now it woks and error is also zero. But if you look at the code of "myfunAskMathworks.m", it has used 'b' also but in your code there is no use of b. But I want to use 'b' because I will provide 'b' from outside having different values but will be of same size as is u. So how will we use 'b' also in this code?
Yes, in the function "myfunAskMathworks.m" the vector b is used in a function call in the line:
ye = yMatTR([deg2rad(az);deg2rad(el)], AT, AR);
so I suggest to define vector b to be a little different than u, for example
b = u * (1+0.5*randn); % random number with mean 1 and deviation 0.5
an then extract az and el variables from b in the same way you extracted from vector u, and then call ye = yMatTR([deg2rad(b(1:2...
I'm sure you can figure it out on you own. A little bit effort is needed.
Thanks a lot dear Askic V for your kind response. I am going to accept your answer as you did a great job. But plese guide me a little bit further becase when I changed it like this:
u=[10 20 30 40];b=u;
az_u = u(1:2)'; %Azimuths
el_u = u(3:4)'; % Elevations
az_b = b(1:2)'; %Azimuths
el_b = b(3:4)'; % Elevations
M = length(az_u); % No. of sources
L = 1;
m = randn(M,L);
% Wavenumber vectors
%k = pi*[cosd(az).*cosd(el), sind(az).*cosd(el), sind(el)].';% Always 3 x No. of sources
% Tx antennas
N = 6;
% Array geometry [rx,ry,rz] (example: uniform circular array)
radius = 0.5/sind(180/N);
rx = radius*cosd(360*(0:N-1).'/N);
ry = radius*sind(360*(0:N-1).'/N);
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)
radius = 0.5/sind(180/M);
rx = radius*cosd(360*(0:M-1).'/M);
ry = radius*sind(360*(0:M-1).'/M);
rR = [rx, ry, zeros(M,1)];% Always No. of Antennas x 3
% Matrices AT and AR
% Always (TX Antennas x No. of sources)
AT = @(alpha, beta) exp(-1j.*rT*pi.*[cos(alpha).*cos(beta),sin(alpha).*cos(beta), sin(beta)].');
AR = @(alpha, beta) exp(-1j.*rR*pi.*[cos(alpha).*cos(beta), sin(alpha).*cos(beta), sin(beta)].');% Always (RX Antennas x No. of sources)
az_u=az_u';
el_u=el_u';
yo = yMatTR([deg2rad(az_u);deg2rad(el_u)], AT, AR); % 36x1 in which 36 is due to M*N
az_b=az_b';
el_b=el_b';
ye = yMatTR([deg2rad(az_b);deg2rad(el_b)], AT, AR);
%%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%%
e=norm(yo-ye).^2/(M*N);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function yMatTR
%%%%%%%%%%%%%%%%%%%%%%%%5%%%
function y = yMatTR(targetAngle, steerVecT, steerVecR)
% az = targetAngle(1,:); el = targetAngle(2,:)
steerA = steerVecT(targetAngle(1,:), targetAngle(2,:));
steerB = steerVecR(targetAngle(1,:), targetAngle(2,:));
y=sum( steerA.*permute(steerB,[3,2,1]) ,2);
y=y(:);
end
it runs successfully. But when I set N=8 and M=8 on lines 19 and 27 respectively, then itgives me the following error:
Arrays have incompatible sizes for this operation.
Error in ByAskic>@(alpha,beta)exp(-1j.*rT*pi.*[cos(alpha).*cos(beta),sin(alpha).*cos(beta),sin(beta)].') (line 37)
AT = @(alpha, beta) exp(-1j.*rT*pi.*[cos(alpha).*cos(beta),sin(alpha).*cos(beta), sin(beta)].');
Error in ByAskic>yMatTR (line 55)
steerA = steerVecT(targetAngle(1,:), targetAngle(2,:));
Error in ByAskic (line 41)
yo = yMatTR([deg2rad(az_u);deg2rad(el_u)], AT, AR); % 36x1 in which 36 is due to M*N
Related documentation
It should work for any number of M and N. So how to change it so that it can work for any number of M and N.
Note: The M on line 10 is different.Please don't confuse it with M on line 27. Because both are different variables.
The reason of the mismatch in the dimension originates from this line in you start file:
k = pi*[cosd(az).*cosd(el), sind(az).*cosd(el), sind(el)].';% Always 3 x No. of sources
sine you have two angles for az and two for el, this line will always produce a vector of 6 elements.
Remember, in your original template file there is a line:
steerVecT = @(ang) exp(1j*2*pi*d*(0:M-1).'*sin(vecH(ang)));
this is a fiunction handle, that was later called with complete vector u, which means for all angles. What you did was to extract az and el and use then separately. You need to rethink what you really trying to achive.
Also, in your code there are lines with questionable purpose.
For example, what is this line needed?
m = randn(M,L);
Thanks a lot dear Askic V for your kind response. Yes, you are right. The following lines are not used:
% M = length(az_u); % No. of sources
% L = 1;
% m = randn(M,L);
But now what to do with the above code so that it works for any M and N values?
@Sadiq Akbar here you need to know what function exactly you want to apply to the angles.
Here is the problem:
[cos(alpha).*cos(beta),sin(alpha).*cos(beta), sin(beta)]
In your example function you had only sin() applied to all angles. In in this particular case you apply three functions and make a new vector which dimension doesn't correspond to -1j.*rR*pi part.
You need to review these functions. How you get them? Are you sure they are correct one?
Why az and el contain two values each?
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)
radius = 0.5/sind(180/N);
rx = radius*cosd(360*(0:N-1).'/N);
ry = radius*sind(360*(0:N-1).'/N);
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)
radius = 0.5/sind(180/M);
rx = radius*cosd(360*(0:M-1).'/M);
ry = radius*sind(360*(0:M-1).'/M);
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.
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
radius = 0.5/sind(180/N);
rx = radius*cosd(360*(0:N-1).'/N);
ry = radius*sind(360*(0:N-1).'/N);
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
% Received signal
x = A*Am; % 10 x 1 i.e., No. of antennas x 1
I hope, now it will be easy for you to understand.

댓글을 달려면 로그인하십시오.

카테고리

질문:

2023년 2월 27일

댓글:

2023년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by