Calculating convolution using matlab

0

I want to calculate the convolution of the following equation. $$x[n] = \sin{\frac{n*\pi}{6}}(u[n] - u[n-12])$$ I got up to the following line of code:

>> stem(5, y);
>> plot(n, y);
>> syms n
>> u = int(exp(-x)*heaviside(x), x, -Inf, Inf);
>> a = sin((n*pi)/6);
>> x = a*(u -u[n-12]);

However, the last line gives me an error. Moreover, I'm having trouble figuring out how I'd use this equation to figure out the convolutions to plot $y[n]$ in MATLAB. I know to use the command conv but I'm not able to figure out how. I'm defining \$h[n] = \delta[n-3]\$ and using \$y[n] = x[n]*h[n]\$ as my equation.

Christian

Posted 2017-01-26T08:13:08.377

Reputation:

Where is the convolution? you have shown $x[n] $ but not the equation for $ y[n] $ are you doing this? $( x\ast y)[n] $? or this? $ (x\ast u)[n] $ ? Did you know $ (f \ast g)[n] = \sum_{n=-\infty}^{\infty} {f[n-m]g[m] }$ – Voltage Spike – 2017-01-26T17:09:46.200

Ooops, sorry about that. I have edited the equation. – None – 2017-01-26T20:53:46.023

Can you add the rest of the code that's relevant to your issue? Hard to debug it when the code you provided won't even run. – None – 2017-01-27T19:12:39.827

Why aren't you using conv? – Dmitry Grigoryev – 2017-01-30T14:06:06.710

@DmitryGrigoryev, I would like to use conv, but I had trouble getting it work.. – None – 2017-01-30T20:06:37.430

@dgreenheck I defined n=-5:5; and y=heavyside(n); . The rest of the code follows as written above. – None – 2017-01-30T20:08:45.273

Answers

1

Suppose we have vectors:

u = [1 2 1];
v = [1 1 0 0 0  2 ];

so for \$ x[n] = u[n]\ast v[n] \$ if the vector operations are broken down

x[0] =                    1*1
x[1] =           1*2 + 1*1
x[2] = 1*1 + 1*2 + 0*1
x[3] = 1*1 + 0*2 + 0*1
x[4] = 0*1 + 0*2 + 0*1
x[5] = 0*1 + 0*2 + 2*1
x[6] = 0*1 + 2*2
x[7] = 2*1

or with zero padding:
x[0] = 0*1 + 0*2 + 1*1
x[1] = 0*1 + 1*2 + 1*1
x[2] = 1*1 + 1*2 + 0*1
x[3] = 1*1 + 0*2 + 0*1
x[4] = 0*1 + 0*2 + 0*1
x[5] = 0*1 + 0*2 + 2*1
x[6] = 0*1 + 2*2 + 0*1
x[7] = 2*1 + 0*2 + 0*1

Notice that 1) The x vector is bigger than both u and v and for the starting operations 2) In the starting and end cases you need to pad with zeros
if

u = [1 2 1];
v = [0 0 1 1 0 0 0 2 0 0 ]; 
x = conv(u,v)
x = [0 0 1 3 3 1 0 2 4 2 0 0]; % result for x

or

v =[0 0 0 1 1 0 0 0 2 0 0 0];
x = conv(u,v) 
x = [0 0 0 1 3 3 1 0 2 4 2 0 0 0]; % result for x

it makes a difference on the vector size if there are zeros, but not the values that are convoluted. This is called zero padding, with convolution you need to handle the edge cases.

(Also remember there is matlab element zero starts at one, I used math notation with the first element of vectors starting at zero)

So why do I tell you this?

because when you do convolution on a computer the edge cases can be confusing. when you do this operation x = a*(u -u[n-12]); and your asking for u[n-12] which to matlab is u[-11] for the first value of the array, and in memory that doesn't exist for that array. So you need to zero pad and\or truncate and realize that edge cases are different for convolution.

I'll give an example for x[n] = u[n] + u[n-3]:

 u = [ 1 2 3 4 5 6 ];
 x = u + [u(3:6) 0 0 ];

Voltage Spike

Posted 2017-01-26T08:13:08.377

Reputation: 347