Filter löschen
Filter löschen

problem with a matlab code for discrete-time convolution

8 Ansichten (letzte 30 Tage)
Benjamin
Benjamin am 16 Apr. 2012
Beantwortet: Gui Chen am 21 Jan. 2021
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

Antworten (3)

Wayne King
Wayne King am 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]);

Wayne King
Wayne King am 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
Gui Chen am 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

Kategorien

Mehr zu Signal Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by