我试图在python中实现这段代码。它在MATLAB
中工作得很好,但是当我使用相同的逻辑时,python解释器给出了不同的答案。
我在finding the correct function or method in python
有问题。需要关于这方面的帮助。代码如下
验证码:
Sensors = 10; %number of receivers
samples = 10000; %number of set of signals
p = 10; %number of signals
theta1 = [10 20 40 60 100 120 140 80 90 110];
angles = theta1*(pi/180);
X = zeros(Sensors,samples);
for k=1:p-1
Array_factor = exp(j*pi*cos(angles(1,k))*(0:Sensors-1 ));
br =ones(1,samples); %creating real values between 1-10000
temp = rand(1,samples); %generate random numbers b/w 1 and 10000
br(find(temp<.5))=-1; %find real numbers
bi =ones(1,samples); %creating imaginary values between 1-10000
temp = rand(1,samples); %generate c omplex random numbers b/w 1 and 10000
bi(find(temp<.5))=-1; %find imaginary numbers
b = br+j*bi;
X = X +Array_factor'*b; %complete Input signal combined with array factor
end
上面是一个MATLAB
代码,现在我已经将其转换为Python
import numpy as np
#number of receivers
num_sensors = 10
#Total samples to scan
samples = 10
#number of signals
p = 10
theta1 = [10,20,40,60,100,120,140,80,90,110]
#angles in degrees
angles = [i*(np.pi/180) for i in theta1]
#Input Signal
X = np.zeros((num_sensors,samples), dtype = complex)
for k in range(1,p-1):
array_factor = np.exp(1j*np.pi*np.cos(angles[k])*np.arange(sensors))
#creating real values
br = np.ones(samples)
temp = np.random.rand(samples)
br[temp < 0.5] = -1
# br[temp<0.5] = -1
bi = np.ones(samples)
temp = np.random.rand(samples)
bi[temp<0.5] = -1
b = br + 1j*bi
X += np.dot(array_factor,b)
print(X)
Python中预期的输出X = 10 x 10000
双b = 1 x 10000
双
1条答案
按热度按时间qltillow1#
Array_factor'*b
表达式的NumPy等价物是:np.atleast_2d(array_factor).conj().T
将array_factor
从行向量转换为列向量,如following answer中所述。.conj().T
是'
运算符的NumPy等价物。np.atleast_2d(b)
将b
从1D数组转换为2D数组。注意,在MATLAB中,行向量是2D数组(所有向量都是矩阵),而在NumPy中,行向量是1D数组。
@
运算符用于矩阵乘法(与*
MATLAB运算符匹配)。我们也可以使用dot product:
np.dot(np.atleast_2d(array_factor).conj().T, np.atleast_2d(b))
注意事项:
由于
Array_factor
是复数,我们必须使用.'
进行转置。'
运算符产生复共轭转置。循环索引中还有另一个bug:
应该是
for k in range(p-1)
而不是for k in range(1,p-1)
。Python代码更新: