フィルターのクリア

Can anyone please help me with the error in the code

1 回表示 (過去 30 日間)
Supratik Das
Supratik Das 2021 年 9 月 23 日
コメント済み: Supratik Das 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);
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 件のコメント
Supratik Das
Supratik Das 2021 年 9 月 23 日
Error using /
Matrix dimensions must agree.
Supratik Das
Supratik Das 2021 年 9 月 23 日
please help

サインインしてコメントする。

回答 (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.
  1 件のコメント
Supratik Das
Supratik Das 2021 年 9 月 23 日
so how to solve this problem?

サインインしてコメントする。


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
Supratik Das
Supratik Das 2021 年 9 月 23 日
here it is please check

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDigital Filter Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by