MATLAB TASK
Q1. (a) Matlab script:
clc
close all
clear all
t=-2:2; % time domain
y=zeros(1,5); % impulse definition
y(1,3)=1;
stem(t,y); % discreet impulse plotting
ylabel('Amplitude');
xlabel('Time Index');
Plot:
(b) Matlab code:
clc
clear all
close all
syms n % universal variable
u(n)=heaviside(n); % step function definition
N = 0.1;
x = u(n) - u(n-N); % expression given in question
ezplot(x); % plotting step response
grid on
Plot:
(c) Matlab code:
clc
close all
clear all
A=10;
f=50; % frequency
fs=600; % sampling frequency
n=-pi:0.1:pi;
x= A*cos((2*pi*f*n)/fs); % expression given in question
plot(n,x); % plotting expression
ylabel('x(n)');
xlabel('time period');
grid on
Plot:
(d) Matlab code:
clc
close all
clear all
figure(1)
m = [-5:0.25:5]; % time domain
x = 5*cos(pi*(m/2)-(pi/2)); % expression
stem(m,x); % discrete plotting
ylabel('x(m)');
xlabel('time period');
grid on
figure(2)
p = [-5:0.25:5]; % time domain
h = 10*cos(pi*(p/2)); % expression
stem(p,h); % discrete plotting
ylabel('h(p)');
xlabel('time period');
grid on
figure(3)
n = [-5:0.25:5]; % time domain
y=conv(x,h,'same') % convolution of both expressions
stem(n,y); % discrete plotting
ylabel('y(n)');
xlabel('time period');
grid on
Plots:
Q2. (a) Matlab code:
close all
clear
a = [1 0.8 0.64]; % constants from the LHS of the equation
b = [0.866]; % constants from the RHS of the equation
impulse = [1 zeros(1,99)]; % impulse function definition
h = filter(b,a,impulse); % impulse response filtration
stem([0:length(h)-1],h) % discrete plotting
Plot:
(b) Matlab code:
close all
clear
n = 0:10 % time domain
a = [1 0.8 0.64]; % constants from the LHS of the equation
b = [0.866]; % constants from the RHS of the equation
[s,x] = dstep(b,a,length(n)); % Step function for the difference eq
stem(n,s) % discrete plotting
title('Step response')
Plot:
(c) Matlab code:
close all
clear
a = [1 0.8 0.64]; % constants from the LHS of the equation
b = [0.866]; % constants from the LHS of the equation
impulse = [1 zeros(1,99)]; % impulse function definition
h = filter(b,a,impulse); % filtration of impulse response
b = fir1(18,30/(100/2),'high',kaiser(19,4)); % finite impulse response
impz(b,h,[],100) % plotting FIR
(d) Comparison of 2b and 2c
Similarities:
Step and Impulse responses are closely related, In discrete time the unit impulse is the
first difference of the unit step, and the unit step is the running sum of the unit impulse.
Differences:
The step response gives information about how the system responds to a signal which
will be there for a longer time the impulse response will say something about how the
system reacts when it gets a smack to start, how long will it go on.
Q3. (a) Matlab code:
clc
close all
clear all
n1=0:25;% Range of x[n]
xn=cos(21*pi*n1/56)+cos(15*pi*n1/56);% x[n]
n2=0:15;% Range y[n]
yn=0.1*n2;% y[n]
N=length(n1)+length(n2)-1;% Total number of non zero samples
Answer:
N = 41
(b) Matlab code:
clc
close all
clear all
n1=0:25;% Range of x[n]
xn=cos(21*pi*n1/56)+cos(15*pi*n1/56);% x[n]
n2=0:15;% Range y[n]
yn=0.1*n2;% y[n]
N=length(n1)+length(n2)-1;% Total number of non zero samples
Xf=fft(xn,N);% N point fft of x[n]
Yf=fft(yn,N);% N point fft of y[n]
Zf=Xf.*Yf;% Convolution in time domain is equivalent to multiplication in
frequency domain
zn=ifft(Zf);% Inverse FFT gives sequence z[n]
stem(zn);% Discrete plot of sequence z[n]
grid;
xlabel('Samples');
title('Z[n] evaluated using FFT');
Plot:
(c) Matlab code:
clc
close all
clear all
n1=0:25;% Range of x[n]
xn=cos(21*pi*n1/56)+cos(15*pi*n1/56);% x[n]
n2=0:15;% Range y[n]
yn=0.1*n2;% y[n]
N=length(n1)+length(n2)-1;% Total number of non zero samples
N2=64;% smallest power of 2 that can be used to evaluate the convolution
Xf2=fft(xn,N2);% 64 point FFT of x[n]
Yf2=fft(yn,N2);% 64 point FFT of y[n]
Zf2=Xf2.*Yf2;% Convolution equivalent to multiplication
zn2=ifft(Zf2);% Inverse FFT gives z[n]
figure;
stem(zn2);% Discrete plot of z[n]
grid;
xlabel('Samples');
title('Z[n]with number of sample equal to smallest power of 2');
Plot:
(d) Matlab code:
clc
close all
clear all
n1=0:25;% Range of x[n]
xn=cos(21*pi*n1/56)+cos(15*pi*n1/56);% x[n]
n2=0:15;% Range y[n]
yn=0.1*n2;% y[n]
N=length(n1)+length(n2)-1;% Total number of non zero samples
zn3=conv(xn,yn);% Inbuilt command to evaluate linear convolution
figure;
stem(zn3);% Plot z[n]
grid;
xlabel('Samples');
title('Z[n]using inbuilt convolution command');
Plot:
Answer: The result obtained using FFT and by using inbuilt command both are equal.
Q4. (a) Matlab code:
clc
close all
clear all
t= 0:0.01:0.1;
x = cos(600*pi.*t)+cos(100*pi.*t);%Given x(t) signal
[pks,locs] = findpeaks(x,t);%finding peak values of x(t) signal
period = max(diff(locs))%finiding period of x(t)
t1=0:1/1e8:period;
y=cos(600*pi.*t1)+cos(100*pi.*t1);% x(t) signal in one period
figure;
plot (t1,y)
title('x(t) plot')
xlabel('t')
ylabel('x(t)')
Plot:
(b) Answer : maximum frequency component of the signal x(t) from above
period is fm
=1/period so minimum sampling frequency = 2*fm
(c) Matlab code:
clc
close all
clear all
t= 0:0.01:0.1;
x = cos(600*pi.*t)+cos(100*pi.*t);%Given x(t) signal
[pks,locs] = findpeaks(x,t);%finding peak values of x(t) signal
period = max(diff(locs))%finiding period of x(t)
t1=0:1/1e8:period;
y=cos(600*pi.*t1)+cos(100*pi.*t1);% x(t) signal in one period
figure;
plot (t1,y)
title('x(t) plot')
xlabel('t')
ylabel('x(t)')
fs=2*(1/period)
fsactual=(4*fs);
n=0:(1/fsactual):period;%c)Required sample points
xn = cos(600*pi.*n)+cos(100*pi.*n);%sampled data
figure;
stem(n,xn)
title('x(n) plot')
xlabel('n')
ylabel('x(n)')
Plot:
(d) Matlab code:
clc
close all
clear all
t= 0:0.01:0.1;
x = cos(600*pi.*t)+cos(100*pi.*t);%Given x(t) signal
[pks,locs] = findpeaks(x,t);%finding peak values of x(t) signal
period = max(diff(locs))%finiding period of x(t)
t1=0:1/1e8:period;
y=cos(600*pi.*t1)+cos(100*pi.*t1);% x(t) signal in one period
figure;
plot (t1,y)
title('x(t) plot')
xlabel('t')
ylabel('x(t)')
fs=2*(1/period)
fsactual=(4*fs);
n=0:(1/fsactual):period;%c)Required sample points
xn = cos(600*pi.*n)+cos(100*pi.*n);%sampled data
figure;
stem(n,xn)
title('x(n) plot')
xlabel('n')
ylabel('x(n)')
d)
z=fft(xn);%finding fft of the sequence
L= length(xn);%length of the sampled signal
P2 = abs(z/L);
P1 = P2(1:(L/2)+1);
P1(2:end-1) = 2*P1(2:end-1);
f = fsactual*(0:(L/2))/L;%frequency vector
figure;
plot(f,P1) %plotting Dft spectrum
title('Single-Sided Amplitude Spectrum of x(n)')
xlabel('f (Hz)')
ylabel('|xn(f)|')
Plot: