You are on page 1of 6

Assignment 4: Matlab

7):

Code for A:

%summation constant multiplied by array of 1s


h=(1/20).*ones(1,20);

% range of n
n1=0:150;

x=3+0.01.*n1+2.*cos(0.04.*pi.*n1);

%convoluting analog signal and moving average


y=conv(h,x);

length(y)

% 19 added as range of summation


n=0:169;
stem(n,y);
xlabel('n');
ylabel('y[n]');

Plot for A:
Code for B:

% defining moving average and range for N


h=[1 -1];
n1=0:150;

% signal and convolution


x=3-0.01.*n1+2.*cos(0.04.*pi.*n1);
y=conv(h,x);
length(y)

% range for n of signal


n=0:151;

stem(n,y);
xlabel('n');
ylabel('y[n]');

Plot for B:
8:

Code:

clc;

%input frequency
f=1000;

%period
T=1/f;

%Ts in seconds
Ts=2e-4;

t=0:Ts:6*T;

%frequenct conversion
x=cos(2*pi.*f.*t);
N1=20;

%Moving average 20
h1=ones(1,N1);% array of ones size n1
h1=h1./N1;
y1=conv(h1,x);
plot(t,x,t,y1(1:length(t)));

% moving average 40
N2=40;
h2=(1/N2).*ones(1,N2); % array of ones size n2
y2=conv(h2,x);
alpha=0.2; %smoothing
n=0:length(t)-1;

% iir filter
h3=(1-alpha).*(alpha.^n);
y3=conv(h3,x);

%difference filter
h4=[1 -1];

y4=conv(h2,x);
hold on
plot(t,y2(1:length(t)),t,y3(1:length(t)),t,y4(1:length(t)));
legend('x(t)',' N=20',' N=40'...
,'IIR ','Difference ');
title(['frequency = ' num2str(f) 'hz']);

Screenshot of frequency variations:

You might also like