How to vectorize this piece of code and why doesn't e come out to be zero though it must come out to be zero because u and b are equal?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sadiq Akbar
le 10 Déc 2022
Commenté : Bruno Luong
le 11 Déc 2022
u=[30 50 75 -30 -50 -75];
b=u;
Noise=5;
M = 6;%Constant1
N = 6;%Constant2
d = 0.5;%Constant3
K = length(u)/2; %Constant4
alpha=ones(1,K);%[1 1 1 1 1];
a=zeros(M,K);%aTx
f=zeros(N,K);%fRx
c=zeros(M*N, length(u)-K);% Extended Matrix
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
%%%%%%%%%%%%%%%%%%%%
% Observed response
%%%%%%%%%%%%%%%%%%%
for i=1:K
for h=1:M
a(h,i)=exp(j*2*pi*(h-1)*d*sind(u(i))); %%%%% a
end
for p=1:N
f(p,i)=exp(j*2*pi*(p-1)*d*sind(u(K+i))); %%%%% f
end
end
for g= 1:K
c(:,g)=kron(a(:,g),f(:,g));% Extended vector
end
yo=c*alpha'; % Observed Response (Noise Free)
yo=awgn(yo,Noise);% Uncomment for Noise consideration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Estimated response
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ae=zeros(M,K);
fe=zeros(N,K);
ce=zeros(M*N, length(u)-K);
for i=1:K
for h=1:M
ae(h,i)=exp(j*2*pi*(h-1)*d*sind(b(i))); %%%%% ae
end
for p=1:N
fe(p,i)=exp(j*2*pi*(p-1)*d*sind(b(K+i))); %%%%% fe
end
end
for g= 1:K
ce(:,g)=kron(ae(:,g),fe(:,g));
end
ye=ce*alpha';% Estimated Response
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
e=0.0;
for s=1:M*N
e=e+(abs(yo(s,1)-ye(s,1))).^2;
end
e=e/M*N;
0 commentaires
Réponse acceptée
Bruno Luong
le 10 Déc 2022
Not tested but this
for i=1:K
for h=1:M
ae(h,i)=exp(j*2*pi*(h-1)*d*sind(b(i))); %%%%% ae
end
for p=1:N
fe(p,i)=exp(j*2*pi*(p-1)*d*sind(b(K+i))); %%%%% fe
end
end
can be replaced by
h=(1:M)';
p=(1:N)';
i=1:K;
br = b(:).';
ae=exp(j*2*pi*(h-1)*d.*sind(br(i)));
fe=exp(j*2*pi*(p-1)*d.*sind(br(K+i)));
And
for g= 1:K
ce(:,g)=kron(ae(:,g),fe(:,g));
end
By
ce = reshape(reshape(ae,K,1,[]).*fe,K,[]);
7 commentaires
Plus de réponses (1)
Walter Roberson
le 10 Déc 2022
Modifié(e) : Walter Roberson
le 10 Déc 2022
format long g
u=[30 50 75 -30 -50 -75];
b=u;
Noise=5;
M = 6;%Constant1
N = 6;%Constant2
d = 0.5;%Constant3
K = length(u)/2; %Constant4
alpha=ones(1,K);%[1 1 1 1 1];
a=zeros(M,K);%aTx
f=zeros(N,K);%fRx
c=zeros(M*N, length(u)-K);% Extended Matrix
%%%%%%%%%%%%%%%%%%%%
% Swapping vector b
%%%%%%%%%%%%%%%%%%%%
[~, ix] = sort(u); % u is my desired vector
[~, ix1(ix)] = sort(b);
b = b(ix1);
%%%%%%%%%%%%%%%%%%%%
% Observed response
%%%%%%%%%%%%%%%%%%%
for i=1:K
for h=1:M
a(h,i)=exp(j*2*pi*(h-1)*d*sind(u(i))); %%%%% a
end
for p=1:N
f(p,i)=exp(j*2*pi*(p-1)*d*sind(u(K+i))); %%%%% f
end
end
for g= 1:K
c(:,g)=kron(a(:,g),f(:,g));% Extended vector
end
yo=c*alpha'; % Observed Response (Noise Free)
yo=awgn(yo,Noise);% Uncomment for Noise consideration
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Estimated response
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ae=zeros(M,K);
fe=zeros(N,K);
ce=zeros(M*N, length(u)-K);
for i=1:K
for h=1:M
ae(h,i)=exp(j*2*pi*(h-1)*d*sind(b(i))); %%%%% ae
end
for p=1:N
fe(p,i)=exp(j*2*pi*(p-1)*d*sind(b(K+i))); %%%%% fe
end
end
for g= 1:K
ce(:,g)=kron(ae(:,g),fe(:,g));
end
ye=ce*alpha';% Estimated Response
%%%%%%%%%%%%%%%%%
% MSE
%%%%%%%%%%%%%%%%%
e=0.0;
for s=1:M*N
e=e+(abs(yo(s,1)-ye(s,1))).^2;
end
e=e/(M*N);
e2 = sum( abs(yo - ye).^2 )
yo - ye
3 commentaires
Walter Roberson
le 10 Déc 2022
I thought I had canceled that post...
I do not see any reason why the values should be the same. awgn() applies randomness, but your ye has not had any randomness applied to it (certainly not exactly the same randomness). There is no reason for them to be the same.
Voir également
Catégories
En savoir plus sur Time Series Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!