Azzera filtri
Azzera filtri

Unable to perform assignment because the left and right sides have a different number of elements.

1 visualizzazione (ultimi 30 giorni)
I got "Unable to perform assignment because the left and right sides have a different number of elements." error and I don't understand how to fix it. Here is my code:
%% Parameters
rho = 0.95; % Correlation coefficient
N = 2048; % Sequence length
sigma2 = 1; % Noise variance
%% Generate AR-1 sequence
x = zeros(N,1);
x(1) = randn; % Initial value
for n = 2:N
x(n) = rho*x(n-1) + sqrt(sigma2)*randn; % AR-1 model
end
%% Compute theoretical prediction filters
R = autocorr(x,3); % Autocorrelation function up to lag 3
r = R(2:end); % Cross-correlation function up to lag 2
h = [rho; zeros(2,1)]; % Initialize filter coefficients
for k = 1:2
h(k+1) = -r(1:k)'*inv(toeplitz(R(1:k))); % Levinson-Durbin recursion
end
Unable to perform assignment because the left and right sides have a different number of elements.
%% Apply theoretical prediction filter
xp = filter(h,1,x); % Predicted sequence
e = x - xp; % Prediction error
Pe_theory = e'*e/N; % Prediction error energy
%% Estimate autocorrelation function
R_est = xcorr(x,'biased'); % Autocorrelation function estimation
R_est = R_est(N:2*N-1); % Keep lags up to 3
r_est = R_est(2:end); % Cross-correlation function estimation up to lag 2
%% Compute optimal prediction filter from estimated autocorrelation function
h_est = [1; -r_est(1)/R_est(2); -r_est(2)/R_est(3)]; % Optimal filter coefficients
%% Apply estimated prediction filter
xp_est = filter(h_est,1,x); % Predicted sequence using estimated filter
e_est = x - xp_est; % Prediction error using estimated filter
Pe_est = e_est'*e_est/N; % Prediction error energy using estimated filter
%% Display results
fprintf('Theoretical optimal filter coefficients: h = [%f, %f, %f]\n', h)
fprintf('Theoretical prediction error energy: Pe = %f\n', Pe_theory)
fprintf('Estimated optimal filter coefficients: h_est = [%f, %f, %f]\n', h_est)
fprintf('Estimated prediction error energy: Pe_est = %f\n', Pe_est)

Risposte (2)

Torsten
Torsten il 8 Apr 2023
Modificato: Torsten il 8 Apr 2023
Execute the code after changing
for k = 1:2
h(k+1) = -r(1:k)'*inv(toeplitz(R(1:k))); % Levinson-Durbin recursion
end
to
for k = 1:2
disp(size(-r(1:k)'*inv(toeplitz(R(1:k)))))
h(k+1) = -r(1:k)'*inv(toeplitz(R(1:k))); % Levinson-Durbin recursion
end
and you will see that for k=2, -r(1:k)'*inv(toeplitz(R(1:k))) has size 1x2 which cannot be saved in the array element h(k+1) which has size 1x1.

Walter Roberson
Walter Roberson il 8 Apr 2023
h(k+1) = -r(1:k)'*inv(toeplitz(R(1:k))); % Levinson-Durbin recursion
When k becomes 2, toeplitz(R(1:k)) becomes 2 x 2, and inv() of that becomes 2 x 2.
When you do matrix multiplication of an M x 2 matrix and a 2 x 2 matrix, you are going to get out an M x 2 matrix. If M is 1 (which it is in this particular case) then you get out a 1 x 2 matrix. And there is no way to store that in a single location h(k+1)
This cannot be solved by using a different size of matrix on the left side of the * operator: no matter what you multiply by, the result is going to have at least two elements (well, unless it is 0 x 2, in which case the * would give a 0 x 2 result, which you cannot store in a scalar location anyhow.)
Therefore there is no way to solve this problem if inv(toeplitz(R(1:k))) is a correct term -- not unless you store the result into more than one output location.

Categorie

Scopri di più su Polynomials in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by