0% found this document useful (0 votes)
12 views16 pages

Matlab Task: All All

This document contains MATLAB code and plots for various signal processing tasks. It includes code to generate and plot signals, take the Fourier transform, use filtering and convolution. It also discusses sampling theory and shows code to sample a continuous signal and plot the discrete-time version.

Uploaded by

hr.rayhman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Matlab Task: All All

This document contains MATLAB code and plots for various signal processing tasks. It includes code to generate and plot signals, take the Fourier transform, use filtering and convolution. It also discusses sampling theory and shows code to sample a continuous signal and plot the discrete-time version.

Uploaded by

hr.rayhman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

MATLAB TASK

Q1. (a) Matlab code:


clc
close all
clear all

t=-2:2;
y=zeros(1,5);
y(1,3)=1;
stem(t,y);
ylabel('Amplitude');
xlabel('Time Index');

Plot:

(b) Matlab code:


clc
clear all
close all

syms n

u(n)=heaviside(n);
N = 0.1;

x = u(n) - u(n-N);

ezplot(x);
grid on

Plot:

(c) Matlab code:


clc
close all
clear all

A=10;
f=50;
fs=600;
n=-pi:0.1:pi;

x= A*cos((2*pi*f*n)/fs);

plot(n,x);
ylabel('x(n)');
xlabel('time period');
grid on
Plot:

(d) Matlab code:


clc
close all
clear all

figure(1)
m = [-5:0.25:5];
x = 5*cos(pi*(m/2)-(pi/2));
stem(m,x);
ylabel('x(m)');
xlabel('time period');
grid on

figure(2)
p = [-5:0.25:5];
h = 10*cos(pi*(p/2));
stem(p,h);
ylabel('h(p)');
xlabel('time period');
grid on

figure(3)
n = [-5:0.25:5];
y=conv(x,h,'same')
stem(n,y);
ylabel('y(n)');
xlabel('time period');
grid on

Plots:
Q2. (a) Matlab code:
close all
clear

a = [1 0.8 0.64];
b = [0.866];

impulse = [1 zeros(1,99)];
h = filter(b,a,impulse);
stem([0:length(h)-1],h)

Plot:
(b) Matlab code:
close all
clear

n = 0:10
a = [1 0.8 0.64];
b = [0.866];

[s,x] = dstep(b,a,length(n));
stem(n,s)
title('Step response')

Plot:
(c) Matlab code:
close all
clear

a = [1 0.8 0.64];
b = [0.866];

impulse = [1 zeros(1,99)];
h = filter(b,a,impulse);

b = fir1(18,30/(100/2),'high',kaiser(19,4));
impz(b,h,[],100)
(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:

(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:

You might also like