MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Passband Modulation
on 21 Sep 2012

Generate x[n]=2cos(0.2*pie*n), with a length of N=64 , that is,0<n<63 , using “x=2*cos(0.2*pi*n)” with “n=[0:63]”. Use the following MATLAB code to produce y[n] from x[n]:

x1=[0 0 0 0 x]; x2=[0 0 0 x 0]; x3=[0 0 x 0 0]; x4=[0 x 0 0 0]; x5=[x 0 0 0 0]; y=(x1+x2+x3+x4+x5)/5; y=y(1:64); plot(n,x,'b',n,y,'r') legend('x[n]','y[n]');

What is the purpose of the command line “y=y(1:64)”?

*No products are associated with this question.*

Answer by Wayne King
on 21 Sep 2012

Edited by Wayne King
on 21 Sep 2012

Accepted answer

It gives you an output signal the same length as your original signal, x. The following operations create a signal with 68 samples.

x1=[0 0 0 0 x]; x2=[0 0 0 x 0]; x3=[0 0 x 0 0]; x4=[0 x 0 0 0]; x5=[x 0 0 0 0]; y=(x1+x2+x3+x4+x5)/5;

So y = y(1:64);

gives you a signal with length 64.

Passband Modulation
on 21 Sep 2012

many thx, i would also like to ask can i obtain an equation to relate x[n] and y[n] for n>4?

Wayne King
on 21 Sep 2012

It's just a moving average filter:

y(n) = \sum_{k=0}^{N-1} h(k)x(n-k)

with h(n) = 1/N

Answer by Image Analyst
on 21 Sep 2012

The purpose seems to be to crop a convolved signal strangely so as to cause a phase shift. It's essentially doing a convolution with a box filter (a sliding mean window). See this code:

yConvFull = conv(x, [1 1 1 1 1]/5); yConvSame = conv(x, [1 1 1 1 1]/5, 'same'); yConvCroppedWeirdly = yConvFull(1:64); % Warning - the x axes are not aligned. % Need to align xSame = 0:63; xFull = -2:65; figure; plot(xFull, yConvFull, 'r-', 'LineWidth', 7); hold on; plot(xSame, yConvSame, 'b-'); plot(xSame, yConvCroppedWeirdly, 'g-'); grid on; legend('full', 'same', 'Weird'); % Enlarge figure to full screen. set(gcf, 'units','normalized','outerposition',[0 0.5 1 .5]);

You'll notice that my "yConvCroppedWeirdly" is the same as your y, and that is a filtered signal cropped asymmetrically so as to include some "invalid" values (values where x was not in all 5 parts of the signal, but was 0 instead). I'd guess this homework exercise is to illustrate to you **how you need to be careful when getting near the edges of your signal when you do digital filters**. You need to take into account "edge effects" and the values of the "x" for the elements as the x can change depending on what kind of convolution you do (i.e., what kind of edge effect handling you want to use.)

Image Analyst
on 21 Sep 2012

No problem. Hopefully though you read what I said and understood it as there was valuable information there.

## 0 Comments