0

天線arrayfactor表達我有天線陣列因子表達這裏:如何向量化在Matlab

Antenna Array factor

我已編碼的陣列因子表達下面給出:

lambda = 1; 
M = 100;N = 200; %an M x N array 
dx = 0.3*lambda; %inter-element spacing in x direction 
m = 1:M; 
xm = (m - 0.5*(M+1))*dx; %element positions in x direction 

dy = 0.4*lambda; 
n = 1:N; 
yn = (n - 0.5*(N+1))*dy; 

thetaCount = 360; % no of theta values 

thetaRes = 2*pi/thetaCount; % theta resolution 

thetas = 0:thetaRes:2*pi-thetaRes; % theta values 

phiCount = 180; 

phiRes = pi/phiCount; 

phis = -pi/2:phiRes:pi/2-phiRes; 

cmpWeights = rand(N,M); %complex Weights 

AF = zeros(phiCount,thetaCount); %Array factor 

tic 
for i = 1:phiCount 
    for j = 1:thetaCount 

     for p = 1:M 
      for q = 1:N 

       AF(i,j) = AF(i,j) + cmpWeights(q,p)*exp((2*pi*1j/lambda)*(xm(p)*sin(thetas(j))*cos(phis(i)) + yn(q)*sin(thetas(j))*sin(phis(i)))); 

      end 
     end 
    end 
end 

如何能我矢量化了計算陣列因子(AF)的代碼。

欲行:

AF(i,j) = AF(i,j) + cmpWeights(q,p)*exp((2*pi*1j/lambda)*(xm(p)*sin(thetas(j))*cos(phis(i)) + yn(q)*sin(thetas(j))*sin(phis(i)))); 

要寫入量化形式(通過修改for循環)。

回答

1

方法一:全油門

內部嵌套循環產生這種每次迭代 - cmpWeights(q,p)*exp((2*pi*1j/lambda)*(xm(p)*sin(thetas(j))*cos(phis(i)) + yn(q)*sin(thetas(j))*sin(phis(i)))),這是總結了反覆給我們AF最終輸出。

讓我們打電話exp(....部分爲B。現在,B主要有兩個部分,一個是標量(2*pi*1j/lambda)和從依賴於 原糊塗版本中使用的四個迭代變量構成的另一部分 (xm(p)*sin(thetas(j))*cos(phis(i)) + yn(q)*sin(thetas(j))*sin(phis(i))) - ​​。爲便於以後參考,我們將其他部分稱爲C

讓我們把所有的進入角度:

  • 糊塗的版本有AF(i,j) = AF(i,j) + cmpWeights(q,p)*exp((2*pi*1j/lambda)*(xm(p)*sin(thetas(j))*cos(phis(i)) + yn(q)*sin(thetas(j))*sin(phis(i)))),這是相當於現在AF(i,j) = AF(i,j) + cmpWeights(q,p)*B,其中B = exp((2*pi*1j/lambda)*(xm(p)*sin(thetas(j))*cos(phis(i)) + yn(q)*sin(thetas(j))*sin(phis(i))))

  • B可以簡化爲B = exp((2*pi*1j/lambda)* C),其中C = (xm(p)*sin(thetas(j))*cos(phis(i)) + yn(q)*sin(thetas(j))*sin(phis(i)))

  • C將取決於迭代器 -​​。

因此,移植到一個量化的方式之後,它最終會因爲這 -

%// 1) Define vectors corresponding to iterators used in the loopy version 
I = 1:phiCount; 
J = 1:thetaCount; 
P = 1:M; 
Q = 1:N; 

%// 2) Create vectorized version of C using all four vector iterators 
mult1 = bsxfun(@times,sin(thetas(J)),cos(phis(I)).'); %//' 
mult2 = bsxfun(@times,sin(thetas(J)),sin(phis(I)).'); %//' 

mult1_xm = bsxfun(@times,mult1(:),permute(xm,[1 3 2])); 
mult2_yn = bsxfun(@times,mult2(:),yn); 
C_vect = bsxfun(@plus,mult1_xm,mult2_yn); 

%// 3) Create vectorized version of B using vectorized C 
B_vect = reshape(exp((2*pi*1j/lambda)*C_vect),phiCount*thetaCount,[]); 

%// 4) Final output as matrix multiplication between vectorized versions of B and C 
AF_vect = reshape(B_vect*cmpWeights(:),phiCount,thetaCount); 

方法2:更少的內存密集型

這第二種方法會減少內存流量,並使用指數分佈式屬性 - exp(A+B) = exp(A)*exp(B)

現在,原來糊塗的版本是這樣的 -

AF(i,j) = AF(i,j) + cmpWeights(q,p)*exp((2*pi*1j/lambda)*... 
    (xm(p)*sin(thetas(j))*cos(phis(i)) + yn(q)*sin(thetas(j))*sin(phis(i)))) 

因此,使用分配律後,我們會像這樣的東西endup -

K = (2*pi*1j/lambda) 
part1 = K*xm(p)*sin(thetas(j))*cos(phis(i)); 
part2 = K*yn(q)*sin(thetas(j))*sin(phis(i)); 
AF(i,j) = AF(i,j) + cmpWeights(q,p)*exp(part1)*exp(part2); 

因此,相關的量化方法將成爲這樣的東西 -

%// 1) Define vectors corresponding to iterators used in the loopy version 
I = 1:phiCount; 
J = 1:thetaCount; 
P = 1:M; 
Q = 1:N; 

%// 2) Define the constant used at the start of EXP() call 
K = (2*pi*1j/lambda); 

%// 3) Perform the sine-cosine operations part1 & part2 in vectorized manners 
mult1 = K*bsxfun(@times,sin(thetas(J)),cos(phis(I)).'); %//' 
mult2 = K*bsxfun(@times,sin(thetas(J)),sin(phis(I)).'); %//' 

%// Perform exp(part1) & exp(part2) in vectorized manners 
part1_vect = exp(bsxfun(@times,mult1(:),xm)); 
part2_vect = exp(bsxfun(@times,mult2(:),yn)); 

%// Perform multiplications with cmpWeights for final output 
AF = reshape(sum((part1_vect*cmpWeights.').*part2_vect,2),phiCount,[]) 

快速標杆

下面是與原始糊塗的做法的問題列出的輸入數據運行時和提出的方法#2 -

---------------------------- With Original Approach 
Elapsed time is 358.081507 seconds. 

---------------------------- With Proposed Approach #2 
Elapsed time is 0.405038 seconds. 

的運行時建議用一個瘋狂的性能改進Approach #2

1

基本的技巧是找出什麼東西是恆定的,什麼東西取決於下標詞 - 因此是矩陣項。

的總和:

  • C(n,m)是矩陣
  • 2π/λ是常數
  • sin(θ)cos(φ)是常數
  • x(m)y(n)是矢量

這樣的兩件事我會做的是:

  1. 使用meshgrid()
  2. 採取一切外循環常數項的東西展開​​和ym成矩陣。

像這樣:

... 

piFactor = 2 * pi * 1j/lambda; 

[xgrid, ygrid] = meshgrid(xm, ym);          % xgrid and ygrid will be size (N, M) 

for i = 1:phiCount 
    for j = 1:thetaCount 

     xFactor = sin(thetas(j)) * cos(phis(i)); 
     yFactor = sin(thetas(j)) * sin(phis(i)); 

     expFactor = exp(piFactor * (xgrid * xFactor + ygrid * yFactor)); % expFactor is size (N, M) 

     elements = cmpWeights .* expFactor;        % elements of sum, size (N, M) 

     AF(i, j) = AF(i, j) + sum(elements(:));       % sum and then integrate. 

    end 
end 

你也許可以弄清楚如何vectorise外環太多,但希望這給你一個起點。