0

I am trying to implement this code in python. It works fine in MATLAB But when I use same logic the python interpreter gives a different answer.

I am having problem in finding the correct function or method in python. Need help regarding this. Code is given below

Code:

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

The above is a MATLAB code Now I have converted it in 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)

Output expected in Python

X = 10 x 10000 double b = 1 x 10000 double

  • 1
    Could you please edit your post to make it clear which bit you’re having trouble with? Does your Python code not work? If so, please show the error messages. Does it compute something different? If so, please show what it computes and how that differs from the MATLAB results. The more obvious you make the problem, the more likely you are of someone knowing what the problem is. As it stands, we’d have to run your Python code and your MATLAB code, and compare. That’s a lot of work, few people will enjoy doing that for free. – Cris Luengo Apr 20 '23 at 19:48
  • Thanks for the feedback @CrisLuengo I will edit edit it to make it understandable – owais ishtiaq Apr 21 '23 at 00:12

1 Answers1

1

The NumPy equivalent for Array_factor'*b expression is:

np.atleast_2d(array_factor).conj().T @ np.atleast_2d(b)

  • np.atleast_2d(array_factor).conj().T converts array_factor from row vector to column vector as described in the following answer.
    .conj().T is the NumPy equivalent of ' operator.
  • np.atleast_2d(b) converts b from 1D array to 2D array.
    Note that in MATLAB rows vectors are 2D arrays (all vectors are matrices) and in NumPy the row vector is 1D array.
  • The @ operator is used for matrix multiplication (matches * MATLAB operator).
    We may also use dot product: np.dot(np.atleast_2d(array_factor).conj().T, np.atleast_2d(b))

Note:
Since Array_factor is complex we have to use .' for transposing.
' operator produces complex conjugate transpose.


There is another bug in the loop indexing:
It should be for k in range(p-1) instead of for k in range(1,p-1).


Updated Python code:

import numpy as np

#number of receivers
num_sensors = 10

#Total samples to scan
samples = 10000

#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(p-1): #for k in range(1,p-1):
    array_factor = np.exp(1j*np.pi*np.cos(angles[k])*np.arange(num_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)
    X += np.atleast_2d(array_factor).conj().T @ np.atleast_2d(b)

print(X)
Rotem
  • 30,366
  • 4
  • 32
  • 65