Question: Could you share MATLAB code for this python example: import numpy as np import matplotlib.pyplot as plt from scipy.linalg import eigh from numpy.linalg import svd

Could you share MATLAB code for this python example:
import numpy as np
import matplotlib.pyplot as plt
from scipy.linalg import eigh
from numpy.linalg import svd
# Parameters
M =8 # number of sensors
d =0.5 # inter-element spacing in wavelengths
thetas = np.array([-5,15,25]) # DOAs in degrees
P = np.array([2,2,1]) # signal powers
N =100 # number of snapshots
k0, k1, k2=0,2,5
sigma_n2=0.1*(1+ k2) # noise variance
# Generate the steering matrix
def steering_vector(M, d, theta):
angles = np.radians(theta)
return np.exp(-1j *2* np.pi * d * np.arange(M)[:, np.newaxis]* np.sin(angles))
A = steering_vector(M, d, thetas)
# Generate the signals and noise
S = np.random.randn(len(thetas), N)+1j * np.random.randn(len(thetas), N)
S *= np.sqrt(P[:, np.newaxis])
noise = np.sqrt(sigma_n2/2)*(np.random.randn(M, N)+1j * np.random.randn(M, N))
# Received signal
X = A @ S + noise
# Compute the covariance matrix
R = X @ X.conj().T / N
# Perform eigendecomposition
eigvals, eigvecs = eigh(R)
signal_eigvecs = eigvecs[:,-len(thetas):] # last len(thetas) eigenvectors
noise_eigvecs = eigvecs[:, :-len(thetas)] # remaining eigenvectors
# MUSIC pseudo-spectrum
def music_spectrum(theta_scan, A, noise_eigvecs):
a_scan = steering_vector(M, d, theta_scan)
return 1/ np.sum(np.abs(a_scan.conj().T @ noise_eigvecs)**2, axis=1)
theta_scan = np.linspace(-90,90,180)
P_music = music_spectrum(theta_scan, A, noise_eigvecs)
# Plot the pseudo-spectrum
plt.figure(figsize=(10,6))
plt.plot(theta_scan, 10* np.log10(P_music))
plt.title("MUSIC Pseudo-Spectrum")
plt.xlabel("Angle (degrees)")
plt.ylabel("Pseudo-Spectrum (dB)")
plt.grid(True)
plt.show()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!