implement the root MUSIC algorithm as a function in matlab that takes as input covariance matrix R and number of sources K, and returns the frequencies in the interval [-1/2, 1/2] instead of [0,1].
Assistant
Certainly! Below is a MATLAB function implementing the Root MUSIC algorithm. This function takes a covariance matrix R and the number of sources K as inputs and returns the estimated frequencies in the interval ([-0.5, 0.5]).
matlab
function freq = rootMUSIC(R, K)
% rootMUSIC Estimates frequencies using the Root-MUSIC algorithm
%
% freq = rootMUSIC(R, K) estimates the frequencies of K sources
% given the covariance matrix R. The frequencies are returned in
% the interval [-0.5, 0.5], corresponding to normalized frequency
% (cycles per sample).
%
% Inputs:
% R - Covariance matrix (N x N)
% K - Number of sources
%
% Output:
% freq - Estimated frequencies (1 x K) in [-0.5, 0.5]
% Validate inputs
if nargin < 2
error('rootMUSIC requires two input arguments: R and K.');
end
[N1, N2] = size(R);
if N1 ~= N2
error('Covariance matrix R must be square.');
end
N = N1; % Number of array elements
if K >= N
error('Number of sources K must be less than the number of array elements N.');
end
% Eigen decomposition
[E, D] = eig(R);
eigenvalues = diag(D);
% Sort eigenvalues in descending order
[eigenvalues_sorted, idx] = sort(eigenvalues, 'descend');
E = E(:, idx);
% Extract noise subspace (last N-K eigenvectors)
En = E(:, K+1:end);
% Form the polynomial from the noise subspace
% Using the last row of the noise subspace to form the polynomial
% p(z) = En(N,:) + En(N-1,:) * z^{-1} + ... + En(1,:) * z^{-(N-1)}
% This creates a polynomial in z^{-1}, multiply by z^{N-1} to make it standard
p = flipud(En(end, :)'); % Take the last row of En and flip it
% Form a polynomial with coefficients p
% Normalize to make it a monic polynomial
p = p / p(end);
% Find the roots of the polynomial
roots_p = roots(p.');
% Select roots that are close to the unit circle
% Compute the magnitude of each root
mag = abs(roots_p);
% Define a threshold for selecting roots near the unit circle
% Typically, roots corresponding to signals lie close to the unit circle
threshold = 0.1; % Adjust as necessary
% Find roots within the threshold
unit_roots = roots_p(mag > (1 - threshold) & mag < (1 + threshold));
% If not enough roots are close to the unit circle, widen the threshold
if length(unit_roots) < K
unit_roots = roots_p;
end
% Compute angles of the selected roots
angles = angle(unit_roots);
% Compute frequencies from angles
% Frequency f is related to angle theta by f = theta / (2*pi)
% Since frequencies are in [-0.5, 0.5], ensure mapping accordingly
freq_candidates = angles / (2*pi);
% Wrap frequencies to [-0.5, 0.5]
freq_candidates = wrapToInterval(freq_candidates, -0.5, 0.5);
% Remove duplicate frequencies within a small tolerance
tol = 1e-3;
freq_candidates = unique(round(freq_candidates / tol) * tol);
% Sort frequencies
freq_candidates = sort(freq_candidates);
% Select the K frequencies closest to the unit circle
% Here, we assume that the most likely candidates are already selected
if length(freq_candidates) < K
warning('Not enough roots near the unit circle. Returning available estimates.');
end
freq = freq_candidates(1:min(K, end));
% If more than K frequencies are found, select the most probable ones
if length(freq) > K
% Optionally, you can implement additional criteria to select the best K
freq = freq(1:K);
end
end
function wrapped = wrapToInterval(x, lower, upper)
% wrapToInterval Wrap values to the interval [lower, upper)
% This function wraps the input x to be within the specified interval.
%
% wrapped = wrapToInterval(x, lower, upper)
range = upper - lower;
wrapped = mod(x - lower, range) + lower;
end
Explanation of the Function
Input Validation:
Ensures that the covariance matrix R is square.
Checks that the number of sources K is less than the number of array elements N.
Eigen Decomposition:
Performs eigen decomposition of the covariance matrix R.
Sorts the eigenvalues in descending order and rearranges the eigenvectors accordingly.
Extracts the noise subspace (En) from the eigenvectors corresponding to the smallest N-K eigenvalues.
Polynomial Formation:
Constructs a polynomial using the last row of the noise subspace. This is based on the property that the noise subspace is orthogonal to the signal subspace.
The polynomial is normalized to make it monic (leading coefficient is 1).
Root Finding:
Finds the roots of the polynomial.
Selects roots that are close to the unit circle, as these are associated with the signal frequencies.
Frequency Estimation:
Computes the angles of the selected roots.
Transforms these angles into normalized frequencies in the interval ([-0.5, 0.5]).
Post-processing:
Removes duplicate frequency estimates within a specified tolerance.
Sorts the frequency estimates and selects the top K frequencies.
Helper Function (wrapToInterval):
Wraps the frequency estimates to ensure they lie within the specified interval ([-0.5, 0.5]).
Usage Example
Here's how you might use the rootMUSIC function:
matlab
% Example usage of rootMUSIC
% Number of array elements
N = 8;
% True frequencies (normalized to [-0.5, 0.5])
f_true = [-0.2, 0.3];
% Number of sources
K = length(f_true);
% Generate steering matrix
A = exp(-1j*2*pi*(0:N-1)'*f_true);
% Generate signals
S = randn(K, 100) + 1j*randn(K, 100); % Complex signals
% Generate covariance matrix with noise
sigma2 = 0.1; % Noise variance
R = A * (S * S') / size(S,2) + sigma2 * eye(N);
% Estimate frequencies using rootMUSIC
f_est = rootMUSIC(R, K);
% Display results
disp('True Frequencies:');
disp(f_true);
disp('Estimated Frequencies:');
disp(f_est);
Notes
Threshold Adjustment: The threshold parameter determines how close a root must be to the unit circle to be considered as a valid frequency candidate. You may need to adjust this value based on the noise level and other system parameters.
Handling Multiple Roots: If more roots are found near the unit circle than the number of sources K, the function currently selects the first K frequencies. Depending on the application, you might want to implement a more sophisticated selection criterion.
Uniqueness and Sorting: The function removes duplicate frequencies within a small tolerance and sorts the estimated frequencies. This helps in providing clean and non-redundant frequency estimates.
Feel free to integrate this function into your MATLAB projects and adjust parameters as needed for your specific application.