problem with a matlab code for discrete-time convolution

7 ビュー (過去 30 日間)
Benjamin
Benjamin 2012 年 4 月 16 日
回答済み: Gui Chen 2021 年 1 月 21 日
Here is my code.
n= -25:25
x= cos(2*pi*n/.3).*(n>=0)
h= (cos(pi*n/.3)+(sin(pi*n/3)/sqrt(3))).*(n>=0)
y= conv(h,x)
plot(n,x)
hold on
plot(n,h)
hold on
plot(n,y)
title ('Plot of input X[n], impulse response h[n] and the convolution of the two Y[n]')
xlabel ('Variable n')
ylabel ('amplitude of signal')
hold on
I have got to plot the input x, impulse response h and the convolution of the two using the conv command but i am getting the error
"??? Error using ==> plot
Vectors must be the same lengths.
Error in ==> lab6p3 at 9
plot(n,y)"
I am using the R2011a student version of matlab
thank you in advance

回答 (3 件)

Wayne King
Wayne King 2012 年 4 月 16 日
n= -25:25;
x= cos(2*pi*n/.3).*(n>=0);
h= (cos(pi*n/.3)+(sin(pi*n/3)/sqrt(3))).*(n>=0);
y= conv(h,x);
n1 = -25:length(y)-1-25;
stem(n1,y,'markerfacecolor',[0 0 1]);

Wayne King
Wayne King 2012 年 4 月 16 日
You have to realize the result of linearly convolving two input signals, h and x, is length(h)+length(x)-1
so you need a new n vector that matches your output y in length.
Your y vector has the expected length of length(h)+length(x)-1 = 101
  1 件のコメント
Benjamin
Benjamin 2012 年 4 月 16 日
How would I do that?
sorry still getting used to matlab

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


Gui Chen
Gui Chen 2021 年 1 月 21 日
You should take care of the length after the convolution. You could take a look at this video. https://youtu.be/JlUM0XHTSSQ

カテゴリ

Help Center および File ExchangeSignal Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by