Clear Filters
Clear Filters

Can anyone rectify the error from below code?

3 views (last 30 days)
Sadiq
Sadiq on 5 Dec 2023
Answered: Asim on 2 Jan 2024
clear all;clc
% DOA Estimation for Two Incident Plane Waves on ULA using MUSIC Algorithm
% Parameters
num_elements = 8; % Number of elements in the ULA
SNR_dB = 10; % Signal-to-Noise Ratio in dB
theta_1 = 30; % Angle of arrival for the first wave in degrees
theta_2 = 60; % Angle of arrival for the second wave in degrees
% Generate ULA response matrix
ULA_response = @(theta) exp(-1j * 2 * pi * (0:num_elements-1).' * sind(theta));
% Generate signals
signal_1 = exp(1j * 2 * pi * randn * (1:num_elements));
signal_2 = exp(1j * 2 * pi * randn * (1:num_elements));
% Place the signals on the ULA
ULA_output = ULA_response(theta_1) * signal_1 + ULA_response(theta_2) * signal_2;
% Add noise
noise_power = 10^(-SNR_dB / 10);
ULA_output = ULA_output + sqrt(noise_power/2) * (randn(size(ULA_output)) + 1j * randn(size(ULA_output)));
% MUSIC algorithm for DOA estimation
angles = 0:0.1:180;
ULA_response_matrix = ULA_response(angles);
[U, ~, ~] = svd(ULA_response_matrix);
noise_subspace = U(:, num_elements+1:end);
%% Multiplication in the numerator
size(conj(ULA_response_matrix))
ans = 1×2
8 1801
size(U(:, 1:num_elements))
ans = 1×2
8 8
%% Multiplication in the denominator
size(conj(ULA_response_matrix))
ans = 1×2
8 1801
size(noise_subspace)
ans = 1×2
8 0
DOA_spectrum = sum(abs(conj(ULA_response_matrix) * U(:, 1:num_elements)).^2, 1) ./ sum(abs(conj(ULA_response_matrix) * noise_subspace).^2, 1);
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
% Plot results
figure;
subplot(2,1,1);
plot(abs(ULA_output));
title('Received Signal on ULA');
subplot(2,1,2);
plot(angles, 10*log10(DOA_spectrum/max(DOA_spectrum)));
hold on;
plot([theta_1, theta_1], [-30, 0], 'r--', 'LineWidth', 2);
plot([theta_2, theta_2], [-30, 0], 'g--', 'LineWidth', 2);
hold off;
title('DOA Spectrum using MUSIC Algorithm');
xlabel('Angle (degrees)');
ylabel('Magnitude (dB)');
legend('DOA Spectrum', 'True DOA 1', 'True DOA 2');
  1 Comment
Dyuman Joshi
Dyuman Joshi on 5 Dec 2023
There's a size mismatch of the variables you are trying to multiply, see above. Emphasis on the fact the variable noise_subspace is empty.
As it's not clear what exactly you are trying to do, it's difficult to suggest anything specific.
A general suggestion is to make sure that the arrays are compatible for the operation you are tyring to perform.

Sign in to comment.

Answers (1)

Asim
Asim on 2 Jan 2024
Hello Sadiq,
I understand you are facing an issue with matrix dimension mismatches when implementing the MUSIC algorithm for DOA estimation with a custom ULA response. From the error message, it seems that the problem occurs during the matrix multiplication step, where the dimensions of the matrices involved are not aligned for proper matrix multiplication.
The MUSIC algorithm requires the noise subspace, which is obtained from the singular value decomposition (SVD) of the covariance matrix of the received signals. However, in your code, it appears that the SVD is being applied directly to the ULA response matrix, which is not correct. The SVD should be performed on the covariance matrix of the received signal at the ULA.
Here's how you can rectify the error:
  1. Compute the covariance matrix of the received signals at the ULA. This involves averaging over multiple snapshots of the received signal, which in your code is represented by `ULA_output`.
  2. Perform the SVD on the covariance matrix to obtain the signal and noise subspaces.
  3. Use the noise subspace to compute the MUSIC spectrum.
Below is the corrected portion of the code for the MUSIC algorithm:
% Assuming that ULA_output is a matrix with columns as snapshots
% Compute the covariance matrix
R = ULA_output * ULA_output'; % Assuming that ULA_output is a matrix with snapshots as columns
[U, S, V] = svd(R); % Perform SVD on the covariance matrix of the received signals
% Extract the noise subspace
noise_subspace = U(:, num_elements+1:end);
% Calculate MUSIC spectrum
DOA_spectrum = zeros(size(angles));
for k = 1:length(angles)
steering_vector = ULA_response(angles(k));
DOA_spectrum(k) = 1 / (steering_vector' * noise_subspace * noise_subspace' * steering_vector);
end
DOA_spectrum = abs(DOA_spectrum);
% Rest of the plotting code remains the same
Please make sure that `ULA_output` contains multiple snapshots of data for the covariance matrix calculation. If `ULA_output` is just a single snapshot, you will need to collect more snapshots to form a proper covariance matrix.
I hope this resolves the error you were experiencing. If you require further assistance, please feel free to ask.
Best regards,
Asim Asrar

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!