How to reduce its execution time and why the value of e is a column vector?
Show older comments
I have the following piece of code.
clc;clear all;
c = 340;
f = 3400;
d = c/f/2;
T = 1;
Sig = 2;
M = 6;
N = 7;
theta = linspace(-60, 60, Sig);
p = 6;
SNR = 10;
Fc=[2*10^3:2*10^3/(Sig-1):5*10^3];
T_Vector=1/f;
p_N = [0:M/p:M*(N-1)/p];
p_M = [0:N:(M-1)*N];
P = union(p_N,p_M);
A = zeros(length(P),Sig);
SigVec = zeros(Sig,T);
for Q = 1:Sig
A(:,Q) = exp(-j*P'*2*pi*d*sin(theta(Q)*pi/180)*f/c);
SigVec(Q,:) = exp(1j*2*pi*Fc(Q).*T_Vector);
end
%%%%%%%%%%%%%%%%%%
% xo calculation
%%%%%%%%%%%%%%%%%%
xo = A*SigVec;
%%%%%%%%%%%%%%%%%%%%%%%%%%
% xe calculation
%%%%%%%%%%%%%%%%%%%%%%%%%
b=theta;
for Q = 1:Sig
Ae(:,Q) = exp(-j*P'*2*pi*d*sin(b(Q)*pi/180)*f/c);
SigVec_est(Q,:) = exp(1j*2*pi*Fc(Q).*T_Vector);
end
xe = Ae*SigVec_est;
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%
e = mean(abs(xo-xe).^2,2)
I want to reduce its execution time. Further, I want a single value of e as zero but it gives me a column vector of e.
1 Comment
Sadiq Akbar
on 3 Jan 2023
Accepted Answer
More Answers (1)
@Sadiq Akbar: OK. See below.
Note that SigVec has T columns because of its pre-allocation, but that SigVec_est has one column because it was not pre-allocated in the original code. I've kept them like that (change T to something > 1 to see the difference).
clc;clear all;
c = 340;
f = 3400;
d = c/f/2;
T = 1;
Sig = 2;
M = 6;
N = 7;
theta = linspace(-60, 60, Sig);
p = 6;
SNR = 10;
Fc=[2*10^3:2*10^3/(Sig-1):5*10^3];
T_Vector=1/f;
p_N = [0:M/p:M*(N-1)/p];
p_M = [0:N:(M-1)*N];
P = union(p_N,p_M);
% A = zeros(length(P),Sig);
% SigVec = zeros(Sig,T);
%
% for Q = 1:Sig
% A(:,Q) = exp(-j*P'*2*pi*d*sin(theta(Q)*pi/180)*f/c);
% SigVec(Q,:) = exp(1j*2*pi*Fc(Q).*T_Vector);
% end
A = exp(-1j*P(:)*2*pi*d.*sin(theta*pi/180)*f/c);
SigVec = exp(1j*2*pi*Fc(:).*T_Vector*ones(1,T));
%%%%%%%%%%%%%%%%%%
% xo calculation
%%%%%%%%%%%%%%%%%%
xo = A*SigVec;
%%%%%%%%%%%%%%%%%%%%%%%%%%
% xe calculation
%%%%%%%%%%%%%%%%%%%%%%%%%
b=theta;
% for Q = 1:Sig
% Ae(:,Q) = exp(-j*P'*2*pi*d*sin(b(Q)*pi/180)*f/c);
% SigVec_est(Q,:) = exp(1j*2*pi*Fc(Q).*T_Vector);
% end
Ae = exp(-1j*P(:)*2*pi*d.*sin(b*pi/180)*f/c);
SigVec_est = exp(1j*2*pi*Fc(:).*T_Vector);
xe = Ae*SigVec_est;
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%
e = mean(abs(xo-xe).^2,2)
14 Comments
Sadiq Akbar
on 4 Jan 2023
Walter Roberson
on 4 Jan 2023
P = union(p_N,p_M);
p_N and p_M are built up from constants assigned inside the function, with values independent of the inputs (the inputs are b and u in this context.) So P will have a size independent of the inputs, but the details of the size could vary if you changed the constants. More duplicates could occur (or you could end up with non-integral values that might not exactly match due to round-off errors in calculations.) In this case it ends up having 12 elements.
theta=u;
You are passing in a row vector 1 x 6 so theta becomes 1 x 6
A = exp(-1j*P(:)*2*pi*d.*sin(theta*pi/180)*f/c);
With P being 1 x 12, P(:) is 12 x 1, giving A 12 rows. Meanwhile theta is the 1 x 6 passed in, so A will have 6 columns. A will therefore becomes 12 x 6.
Fc=[2*10^3:2*10^3/(Sig-1):5*10^3];
with Sig being assigned inside the function, Fc works out as being 1 x 8.
SigVec = exp(1j*2*pi*Fc(:).*T_Vector*ones(1,T));
T is 1, T_vector is a scalar. With Fc being 1 x 8, Fc(:) will be 8 x 1, and there is only a single column in the other variables in the calculation. So SigVec will be 8 x 1.
xo = A*SigVec;
So you are doing matrix multiplication between an 12 x 6 and an 8 x 1. There is no way to make that fit.
What size were you expecting as the output stored in xo ?
You run into exactly the same problems with xe = Ae*SigVec_est with Ae being the same size as A and SigVec_est being the same size as SigVec .
Sadiq Akbar
on 4 Jan 2023
Walter Roberson
on 4 Jan 2023
In order for xo to become a column vector with the same number of entries as P, then Fc must have the same number of rows as the number of columns in SigVec . So you need to make your SigVec have 6 columns. Consider using linspace() instead of the colon operator.
Sadiq Akbar
on 4 Jan 2023
Walter Roberson
on 4 Jan 2023
I did not expect it to work, as it is not at all clear to me how the columns of the u input just happen to correspond to the frequencies you are building in SigVec . Like if that is a given fact, that the different u entries correspond to different frequency, then I would have expected that the function would take an additional parameter that is the frequency vector.
Sadiq Akbar
on 5 Jan 2023
Walter Roberson
on 7 Jan 2023
If you have defined something in a non-anonymous function, using a function statement, then you can create a non-anonymous function handle to it by prefixing the function name with @ . Or you can create an anonymous function from it by using @(varargin)FunctionName(varargin{:})
For example,
handle_to_anonyous_defined_inline = @(x) sin(x.^2)
handle_to_non_anonymous = @non_anonymous
handle_to_non_anonymous_made_anonymous = @(varargin) non_anonymous(varargin{:});
function y = non_anonymous(x)
y = sin(x.^2);
end
Walter Roberson
on 7 Jan 2023
What I am suggesting is something like
Fc_start = 2*10^3;
Fc_end = 5*10^3;
Fc = linspace(Fc_start, Fc_end, length(u));
for n=1:Runs %------------(2)
nn=nn+1;
[best,fmin,time]=fpa(10,0.8,200,dim,lb,ub,@(b)VectorizedByVoss(b,u,Fc));
and
function e = VectorizedByVoss(b,u,Fc)
and delete
Fc=[2*10^3:2*10^3/(Sig-1):5*10^3];
In this way, you could guarantee that your frequency array has the same number of elements as u has.
But possibly what you need is in terms of the size of P. You could pre-calculate P in mainVoss and calculate Fc in terms of the size of P, and pass the two of them together into VectorizedByVoss, using the same technique I show above.
Sadiq Akbar
on 8 Jan 2023
Sadiq Akbar
on 8 Jan 2023
Walter Roberson
on 8 Jan 2023
If you could guarantee that the estimated values matched the exact values, then the calculation would not be an estimate and would instead be a different (possibly shorter) way of calculating the exact values.
Unless you are generating an error message, then the problem is now one of verifying that you are using the correct mathematics for your calculations. Which is not something that I get involved with.
Sadiq Akbar
on 9 Jan 2023
Sadiq Akbar
on 9 Jan 2023
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!