Can anyone please help me with the error in the code (2024)

2 views (last 30 days)

Show older comments

Supratik Das on 23 Sep 2021

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code

  • Link

    Direct link to this question

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code

Commented: Supratik Das on 23 Sep 2021

Open in MATLAB Online

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 Comments

Show NoneHide None

Supratik Das on 23 Sep 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1749779

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1749779

Error using /

Matrix dimensions must agree.

Supratik Das on 23 Sep 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1749789

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1749789

Sign in to comment.

Sign in to answer this question.

Answers (2)

Walter Roberson on 23 Sep 2021

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#answer_793574

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#answer_793574

Open in MATLAB Online

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)')

Can anyone please help me with the error in the code (5)

figure(2)

plot(1:100,v2(1:100),'b','linewidth',1.2)

grid on

xlabel('n')

title('plot of v_2(n)')

Can anyone please help me with the error in the code (6)

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)')

Can anyone please help me with the error in the code (7)

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)')

Can anyone please help me with the error in the code (8)

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 Comment

Show -1 older commentsHide -1 older comments

Supratik Das on 23 Sep 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750349

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750349

so how to solve this problem?

Sign in to comment.

Shayan Sepahvand on 23 Sep 2021

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#answer_793454

  • Link

    Direct link to this answer

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#answer_793454

Open in MATLAB Online

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 Comments

Show 3 older commentsHide 3 older comments

Supratik Das on 23 Sep 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750039

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750039

okay

Supratik Das on 23 Sep 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750074

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750074

But i used covar and covm as functions which are called in the program, they have separate code.

covam.m and covar.m

Walter Roberson on 23 Sep 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750104

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750104

We need to see the code for your covar.m then.

Supratik Das on 23 Sep 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750134

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750134

Open in MATLAB Online

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 on 23 Sep 2021

Direct link to this comment

https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750139

  • Link

    Direct link to this comment

    https://support.mathworks.com/matlabcentral/answers/1459084-can-anyone-please-help-me-with-the-error-in-the-code#comment_1750139

here it is please check

Sign in to comment.

Sign in to answer this question.

See Also

Categories

Signal ProcessingSignal Processing ToolboxDigital and Analog FiltersDigital Filter Analysis

Find more on Digital Filter Analysis in Help Center and File Exchange

Tags

  • error using _/_ matrix dimensions must agree.

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Can anyone please help me with the error in the code (16)

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom(English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本Japanese (日本語)
  • 한국Korean (한국어)

Contact your local office

Can anyone please help me with the error in the code (2024)
Top Articles
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 6648

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.