Can anyone please help me with the error in the code

5 Ansichten (letzte 30 Tage)
Supratik Das
Supratik Das am 23 Sep. 2021
Kommentiert: Supratik Das am 23 Sep. 2021
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)')

Antworten (2)

Walter Roberson
Walter Roberson am 23 Sep. 2021
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 am 23 Sep. 2021
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 Kommentare
Supratik Das
Supratik Das am 23 Sep. 2021
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
Supratik Das
Supratik Das am 23 Sep. 2021
here it is please check

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by