Can anyone please help me with the error in the code

1 次查看(过去 30 天)
clc, clear all, close all
d=sin(0.05*pi*(1:200)+2*pi*rand);
g=randn(1,200);
v1=filter(1,[1 -0.8],g);
v2=filter(1,[1 0.6],g);
x=d+v1;
figure(1)
plot(1:100,x(1:100),'b','linewidth',1.2)
hold on
plot(1:100,d(1:100),'r','linewidth',1.2)
grid on
xlabel('n')
legend('x(n)','d(n)')
title('plot of x(n) and d(n)')
figure(2)
plot(1:100,v2(1:100),'b','linewidth',1.2)
grid on
xlabel('n')
title('plot of v_2(n)')
Rv2=covar(v2,4);
figure(3)
stem(Rv2,'b','linewidth',1.2)
grid on
xlabel('k')
title('autocorrelation of v_2(n)')
rxv2=convm(x,4)'*convm(v2,4)/(length(x)-1);
figure(4)
stem(rxv2,'b','linewidth',1.2)
grid on
xlabel('k')
title('cross-correlation between x(n) and v_2(n)')
w=rxv2(1,:)/Rv2;
v1hat=filter(w,1,v2);
dhat=x-v1hat;
figure(5)
plot(dhat(1:100))
hold on
plot(d(1:100),'r')
xlabel('n')
title('Estimated d(n) vs actual d(n)')
legend('Estimate d(n)', 'Actual d(n)')

回答(2 个)

Walter Roberson
Walter Roberson 2021-9-23
clc, clear all, close all
d=sin(0.05*pi*(1:200)+2*pi*rand);
g=randn(1,200);
v1=filter(1,[1 -0.8],g);
v2=filter(1,[1 0.6],g);
x=d+v1;
figure(1)
plot(1:100,x(1:100),'b','linewidth',1.2)
hold on
plot(1:100,d(1:100),'r','linewidth',1.2)
grid on
xlabel('n')
legend('x(n)','d(n)')
title('plot of x(n) and d(n)')
figure(2)
plot(1:100,v2(1:100),'b','linewidth',1.2)
grid on
xlabel('n')
title('plot of v_2(n)')
Rv2=covar(v2,4);
ans = 1×2
204 5
size(Rv2)
ans = 1×2
5 5
figure(3)
stem(Rv2,'b','linewidth',1.2)
grid on
xlabel('k')
title('autocorrelation of v_2(n)')
cmx = convm(x,4);
cmv2 = convm(v2,4);
rxv2 = cmx'*cmv2/(length(x)-1);
size(cmx), size(cmv2), size(rxv2)
ans = 1×2
203 4
ans = 1×2
203 4
ans = 1×2
4 4
figure(4)
stem(rxv2,'b','linewidth',1.2)
grid on
xlabel('k')
title('cross-correlation between x(n) and v_2(n)')
size(rxv2), size(Rv2)
ans = 1×2
4 4
ans = 1×2
5 5
w=rxv2(1,:)/Rv2;
Error using /
Matrix dimensions must agree.
v1hat=filter(w,1,v2);
dhat=x-v1hat;
figure(5)
plot(dhat(1:100))
hold on
plot(d(1:100),'r')
xlabel('n')
title('Estimated d(n) vs actual d(n)')
legend('Estimate d(n)', 'Actual d(n)')
function R = covar(x,p)
%
% This function sets up a covariance matrix
%
x = x(:);
m = length(x);
x = x - ones(m,1)*(sum(x)/m);
cm = convm(x,p+1);
size(cm)
R = cm'*cm/(m-1);
end
function X = convm(x,p)
%
% This function sets up a convolution matrix
%
N = length(x)+2*p-2;
x = x(:);
xpad = [zeros(p-1,1);x;zeros(p-1,1)];
for i=1:p
X(:,i)=xpad(p-i+1:N-i+1);
end
end
What is happening is that you are creating one of your variables by calling covar(), which adds 1 to the second parameter (4) to get the size -- so it will be something by 5. But the other variable you get by calling convm(), which does not add 1 to the second parameter (4), so it will be something by 4. The 5 and 4 then become incompatible sizes.

Shayan Sepahvand
Shayan Sepahvand 2021-9-23
Hi,
the first argument of
covar(sys, w)
should be some LTI system (discrete in your case), I suggest you to first derive the LTI form of v2 using z transform, then use covar
good luck
  5 个评论
Supratik Das
Supratik Das 2021-9-23
for covar.m
function R = covar(x,p)
%
% This function sets up a covariance matrix
%
x = x(:);
m = length(x);
x = x - ones(m,1)*(sum(x)/m);
R = convm(x,p+1)'*convm(x,p+1)/(m-1);
end
for covm.m
function X = convm(x,p)
%
% This function sets up a convolution matrix
%
N = length(x)+2*p-2;
x = x(:);
xpad = [zeros(p-1,1);x;zeros(p-1,1)];
for i=1:p
X(:,i)=xpad(p-i+1:N-i+1);
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Digital Filter Analysis 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by