You are on page 1of 13

EXP 1 Familiarization of MATLAB and Basic commands like Matrix operation,

Condition, Loop, Function etc. Generation and Plotting of basic Signals and operation on them.
Code:
<Predefined Functions and variables of MATLAB>
f= 50;
t= -5 : 0.1/1000 : 10;
y= sin( 2 * pi * f * t);
plot(t,y);
subplot(211),plot(exp(t));
subplot(212),plot(exp(-t));
<Unit-step Function>
t= -5 : 10/1000 : 5;
for i= 1 : length(t)
if t(1,i)<0
u(1,i)= 0;
else
u(1,i)= 1;
end
end
plot(t,u);
% <OR>
u1= (t>0);
plot(t,u1);
<Ramp Function>
for i= 1 : length(t)
if t(1,i)<0
r(1,i)= 0;
else
r(1,i)= t(1,i);
end
end
plot(t,r);
% <OR>
r1= t.*(t>0);
plot(t,r1);
<General Shift Function>
function [y,n]=
genshift(x,m,k)
n=m+k;
y=x;
end
m= -2:10;
x= [1:7 6:-1:1];
[y,n]= genshift(x,
m, 2);
figure
stem(m,x);
hold on
stem(n,y);
hold off
<Properties of Unit-step Function>
t= -1 : 1/1000 : 1;
u= (t>0);
subplot(421),plot
(t,u);
xlabel('time');
ylabel('amplitude');
title('Uni-Step
Signal');
legend('1729058');
subplot(422),plot
(t,2*u);
xlabel('time');
ylabel('amplitude');
title('Amplitude
Scaling');
legend('1729058');
subplot(423),plot
(t,(u-0.5));
xlabel('time');
ylabel('amplitude');
title('Amplitude
Shifting');
legend('1729058');
subplot(424),plot
(t-2,u);
xlabel('time');
ylabel('amplitude');
title('Time
Shifting');
legend('1729058');
subplot(425),plot
(t+2,u);
xlabel('time');
ylabel('amplitude');
title('Time
Shifting');
legend('1729058');
subplot(426),plot
(2*t,u);
xlabel('time');
ylabel('amplitude');
title('Time
Scaling');
legend('1729058');
subplot(427),plot
(t/2,u);
xlabel('time');
ylabel('amplitude');
title('Time
Scaling');
legend('1729058');
subplot(428),plot (-
t,u);
xlabel('time');
ylabel('amplitude');
title('Time
Reversal');
legend('1729058');
EXP 2: Solution of difference equation to analyze an LTI system. (Using Filter
command and also using Z-transform method).
CODES:
<Impulse Response, Filtered Step Response and Determination of Stability>
n= -20:100;
b= 1;
a= [1, -1, 0.9];
% Impulse Response
h= impz(b, a, n);
subplot(211),stem(n,h);
title('Impulse Response');
xlabel('n');
ylabel('h(n)');
legend('1729058')
% Step Response
x= stepseq(0, -20, 100);
s= filter(b,a,x);
subplot(212),stem(n,s);
title('Step Response');
xlabel('n');
ylabel('s(n)');
legend('1729058');
% Stability
p= roots(a); % Roots of quadratic
equation 'a'
magp= abs(p); % Absolute Value of
'a'
if magp < 1
disp('Stable');
else
disp('Unstable');
end
<Noisy Signal and use of Filter Function>
clc; clear all; close all;
rng default;
t= linspace(-pi, pi, 100);
x= sin(t);
y= x+ 0.28391624.*rand(size(t));
windowSize= 9;
b=(1/windowSize)*ones(1,windowSize);
a=1;
z= filter(b,a,y);
subplot(121),plot(t,x);
xlabel('t');
ylabel('Sin(t)');
title('Original Signal');
legend('1729057', 'Location',
'Southeast');
subplot(122),plot(t,y);
hold on;
plot(t,z);
grid on;
legend('Input data', 'Filtered
Data', 'Location', 'Southeast');
title('Input and Filtered Signal');
EXP 3: Find the linear convolution and correlation between two discrete
sequences.
CODES:
Code:
<Convolution without using Pre-defined function>
clc;clear all;close all;
x= input('X :');
h= input('H :');
m = length(x);
n = length(h);
X = [x,zeros(1,n)];
H = [h,zeros(1,m)];
for i = 1 : n+m-1
Y(i)=0;
for j=1:m
if(i-j+1>0)
Y(i)=Y(i)+X(j)*H(ij+1);
else
end
end
end
stem(Y);
ylabel('Y[n]');
xlabel('n');
title('Linear Convolution');
legend('1729057')
< Correlation without using Pre-defined function >
clc;clear all;close all;
x= input('X :');
h= input('H :');
h= fliplr(h);
m = length(x);
n = length(h);
X = [x,zeros(1,n)];
H = [h,zeros(1,m)];
for i = 1 : n+m-1
Y(i)=0;
for j=1:m
if(i-j+1>0)
Y(i)=Y(i)+X(j)*H(i-j+1);
else
end
end
end
stem(Y);
ylabel('Y[n]');
xlabel('n');
title('Linear Correlation');
legend('1729058');
EXP 4: Design of Analog and Digital modulation systems. Generate AM, FM,
ASK modulated signals.
CODES:
<Amplitude Modulation [AM] >
clc; clear all; close all;
fm= 50;
fc= 10.*fm;
t= 0 : 1/1000 : 5/fm;
mt= cos(2 * pi * fm * t);
ct= cos(2 * pi * fc * t);
st= (1 + (0.5).*mt).*cos(2 * pi *
fc * t);
subplot(321),plot(t,mt);
title("Message Signal");
xlabel("m(t)");
ylabel("t");
legend('1729058');
subplot(322),plot(t,ct);
title("Carrier Signal");
xlabel("c(t)");
ylabel("t");
legend('1729058');
subplot(323),plot(t,st);
title("Under Amplitude
Modulation");
xlabel("s(t)");
ylabel("t");
legend('1729058');
st= (1 + mt).*cos(2 * pi * fc *
t);
subplot(324),plot(t,st);
title("Critical Amplitude
Modulation");
xlabel("s(t)");
ylabel("t");
legend('1729058');
st= (1 + 3.*mt).*cos(2 * pi * fc
* t);
subplot(325),plot(t,st);
title("Over Amplitude
Modulation");
xlabel("s(t)");
ylabel("t");
legend('1729058');
< Frequency Modulation [FM] >
clc; clear all; close all;
fm= 50;
fc= 10.*fm;
t= 0 : 1/10000 : 5/fm;
mt= cos(2 * pi * fm * t);
ct= cos(2 * pi * fc * t);
subplot(221),plot(t,mt);
title("Message Signal");
xlabel("m(t)");
ylabel("t");
legend('1729058');
subplot(222),plot(t,ct);
title("Carrier Signal");
xlabel("c(t)");
ylabel("t");
legend('1729058');
st1= cos(2*pi*fc*t +
((0.5).*sin(2*pi*fm*t)));
subplot(223),plot(t,st1);
title("Frequency Modulation (Beta
< 1)");
xlabel("Sfm(t)");
ylabel("t");
legend('1729058');
st2= cos(2*pi*fc*t +
(5.*sin(2*pi*fm*t)));
subplot(224),plot(t,st2);
title("Frequency Modulation (Beta
>> 1)");
xlabel("Sfm(t)");
ylabel("t");
legend('1729058');
< Binary Amplitude Shift Keying [BASK] >
clc; clear all; close all;
bt= [1 0 1 1];
b= ones(1,101);
for i= 1:101
b(i)= b(i).*bt(floor(i/26)+1);
end
t=0:1/25:4;
mt= cos(2 * pi * 40 * t);
s=mt.*b;
plot(s,t);
EXP 5: Determine DTFT of any discrete sequence and verify the different and
properties of the DTFT.
CODES:
<Discrete Time Fourier Series of a Signal and Magnitude and Phase Response>
clc; clear all; close all;
n=0:100;
w=-2.*pi:0.01:2.*pi;
x=(0.5).^n;
%plot(n,x)
W=exp(-1*j*w'*n);
Xw=W*x';
Mx=abs(Xw);
Px=angle(Xw);
subplot(311),stem(n,x);
title('DTFT of a given signal');
xlabel('Time');
ylabel('frequency');
legend('1729058');
subplot(312),plot(w,Mx);
title('Magnitude Spectrum');
xlabel('Time');
ylabel('frequency');
legend('1729058')
subplot(313),plot(w,Px)
title('Phase Response');
xlabel('Time');
ylabel('frequency');
legend('1729058');
< DTFT Magnitude Spectrum >
clc; clear all; close all;
n=0:100;
w=-pi:0.01:pi;
x=sin(pi/2*n);
W=exp(-1*j*w'*n);
Xw=W*x';
Mx=abs(Xw);
Px=angle(Xw);
subplot(211),stem(n,x);
title('DTFT of a given signal');
xlabel('Time');
ylabel('frequency');
legend('1729058');
subplot(212),plot(w,abs(Xw));
title('Magnitude Spectrum');
xlabel('Time');
ylabel('frequency');
legend('1729058');
< Counvolution Property of DTFT >
clc; clear all; close all;
x=input('Enter the input to
s/m');
h=input('s/m impulse response');
y=conv(x,h);
w=-pi:0.001:pi;
n1=0:length(x)-1;
n2=0:length(h)-1;
n3=0:length(y)-1;
Xw=exp(-1*j*w'*n1)*x';
Hw=exp(-1*j*w'*n2)*h';
Yw=exp(-1*j*w'*n3)*y';
Y1=Xw.*Hw;
subplot(511),stem(n1,x);
grid on
title('INPUT');
xlabel('Time');
ylabel('frequency');
legend('1729058')
subplot(512),stem(n2,h);
grid on
title('S/M impulse response');
xlabel('Time');
ylabel('frequency');
legend('1729058');
subplot(513),stem(n3,y);
grid on
title('Convolution');
xlabel('Time');
ylabel('frequency');
legend('1729058');
subplot(514),plot(w,abs(Yw));
grid on
title('S/M impulse response');
xlabel('Time');
ylabel('frequency');
legend('1729058');
subplot(515),plot(w,abs(Y1));
grid on
title('S/M impulse response');
xlabel('Time');
ylabel('frequency');
legend('1729058');
EXP 6: Obtain DFT & IDFT of two discrete sequence and determine the
circular convolution of them using matrix method as well as using the convolution property.
CODES:
<Discrete & Inverse Discrete Fourier Transform of a Signal>
clc; clear all; close all;
x=input('x[n]: ');
[X,k]= dft(x,8);
[y n]= idft(X,8);
subplot(311),stem(0:length(x)-
1,x);
title('Input Signal');
xlabel('n');
ylabel('x[n]');
legend('1729058');
subplot(312),stem(0:length(X)-
1,X);
title('Discrete Fourier
Transform');
xlabel('k');
ylabel('X(k)');
legend('1729058');
subplot(313),stem(0:length(y)-
1,y);
title('Inverse Discrete Fourier
Transform');
xlabel('n');
ylabel('y[n]');
legend('1729058');
< DFT Function Definition >
function [X,K]= DFT(x,N)
n=0:N-1;
k=0:N-1;
w= exp(-1*j*2*pi*k’*n/N);
x= [x zeros(1,N-length(x)];
X= W*x’;
end
< IDFT Function Definition >
function [x,n]= DFT(X,N)
n=0:N-1;
k=0:N-1;
W= exp(-1*j*2*pi*k’*n/N);
X= [x zeros(1,N-length(X)];
x= W*X/N;
end
< Circular Counvolution Property of DFT with verification using “cconv()” >
clc; clear all; close all;
x=[1 2 1 2];
h=[1 2 3 4];
X=dft(x,4);
H=dft(h,4);
Y=X.*H;
y=idft(Y,4);
subplot(221),stem(0:length(x)-
1,x);
title("Input Signal");
ylabel("x[n]");
xlabel("n");
legend("1729058");
subplot(222),stem(0:length(h)-
1,h);
title("System Impulse Signal");
ylabel("h[n]");
xlabel("n");
legend("1729058");
subplot(223),stem(0:length(y)-
1,y)
title("Circular Convolution
using DFT");
ylabel("y[n]");
xlabel("n");
legend("1729058");
subplot(224),stem(0:length(x)-
1,cconv(x,h,4));
title("Circular Counvolution
using cconv()");
ylabel("x[n](*)h[n]");
xlabel("n");
legend("1729058");
EXP 7: Obtain Linear Convolution using Sectional Convolution methods such
as Overlap Add and Overlap Save
CODES:
< Overlap Add >
close all; clear all;
x=input('Enter a sequence: ');
h=input('Enter a impulse
response:');
lx=length(x);
M=length(h);
N=2*M-1;
L=N-M+1;
x=[x,zeros(1,N-1)];
h=[h,zeros(1,N-M)];
k=floor(lx/M)+1;
p=zeros(k,N);
for i=1:k
p(i,1:L)=x((i-1)*L+1:(i1)*L+L);
end
y=zeros(k,N);
for i=1:k
y(i,:)=cconv(p(i,:),h,N);
end
for i=2:k
y(i,1:M-1)=y(i,1:M-1)+y(i1,L+1:N);
end
z=y(:,1:L);
a=z';
subplot(3,1,1),stem(x);
title('Input Signal');
xlabel('n');
ylabel('x[n]');
legend('1729058');
subplot(3,1,2)
stem(h)
title('System Impulse Response');
xlabel('n');
ylabel('h[n]');
legend('1729058');
subplot(3,1,3)
stem(a(:))
title('Overlap Add Method');
xlabel('n');
ylabel('x[n]*h[n]');
legend('1729058');
< Overlap Save >
clc; clear all; close all;
x=input('x[n]: ');
h=input('h[n]: ');
lx=length(x);
M=length(h);
N=2*M-1;
L=N-M+1;
x=[x,zeros(1,N-1)];
h=[h,zeros(1,N-M)];
k=floor(lx/M)+1;
p=zeros(k,N);
for i=1:k
p(i,L:N)=x((i-1)*L+1:(i-1)*L+L);
end
for i=2:k
p(i,1:M-1)=p(i-1,L+1:N);
end
y=zeros(k,N);
for i=1:k
y(i,:)=cconv(p(i,:),h,N);
end
z=y(:,L:N);
u=z';
subplot(311),stem(x);
title('Input Signal');
xlabel('n');
ylabel('x[n]');
legend('1729058');
subplot(312),stem(h);
title('System Impulse
Response');
xlabel('n');
ylabel('h[n]');
legend('1729058');
subplot(313),stem(u(:));
title('Overlap Save Method');
xlabel('n');
ylabel('x[n]*h[n]');
legend('1729058');
EXP 8: Realization of FIR and IIR filters using fdatool and design application
specific filters.
CODES:
< Sample Signal >
Fs=1000; %Sampling frequency
T=1/Fs; %Sampling period
L=1000; %Length of signal
t=(0:L-1)*t %time vector
S=0.7*sin(2*pi*100*t)+
0.6*sin(2*pi*150*t)+
0.5*sin(2*pi*200*t);
EXP:9 Perform up-sampling and down-sampling operation and
also their frequency analysis.
CODES:
< Up-sampled and Down-sampled Signal >
clc; clear all; close all;
l=0:9;
x=rand(size(l));
y1=downsample(x,2);
y2=upsample(y1,2);
subplot(3,1,1);
stem(l,x,”lineWidth”,2);
title(‘Original sequence’);
legend(‘1729058’);
subplot(3,1,2);
stem(1:length(y1),y1,”lineWidth”,2);
title(‘Downsampled sequence’);
legend(‘1729058’);
subplot(3,1,3);
stem(1:length(y2),y2,”lineWidth”,2);
title(‘Upsampled sequence’);
legend(‘1729058’);
< Sampled and Decimated Signal >
clc; clear all; close all;
f1=30; f2=45;
t=0:0.00025:1;
Xt=2.*sin(2*pi*f1*t)+1.5.*sin(2*pi*f2*
t);
y=decimate(Xt,2);
subplot(2,1,1),stem(t,Xt,”lineWidth”,2
);
title(‘Original sequence’);
legend(‘1729058’);
subplot(2,1,2),stem(1:length(y),y,”lin
eWidth”,2);
title(‘Decimated sequence’);
legend(‘1729058’);
< Sampled and Interpoleted Signal >
clc; clear all; close all;
f1=25; f2=30;
t=0:0.001:1;
Xt=2.*sin(2*pi*f1*t)+1.5.*sin(2*pi*f2*t);
y=interp(Xt,2);
subplot(2,1,1),stem(t,Xt);
title(‘Original sequence’);
legend(‘1729058’),subplot(2,1,2);
stem(1:length(y),y);
title(‘Interpolated sequence’);
legend(‘1729058’);

You might also like