problem with a matlab code for discrete-time convolution
9 views (last 30 days)
Show older comments
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
0 Comments
Answers (3)
Wayne King
on 16 Apr 2012
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]);
0 Comments
Wayne King
on 16 Apr 2012
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
Gui Chen
on 21 Jan 2021
You should take care of the length after the convolution. You could take a look at this video. https://youtu.be/JlUM0XHTSSQ
0 Comments
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!