Submitted By:: Ravi Kumar ROLL NO. 2K13/EL/069
Submitted By:: Ravi Kumar ROLL NO. 2K13/EL/069
Processing Lab
Submitted by:
RAVI KUMAR
ROLL NO. 2K13/EL/069
EXPERIMENT-1
Program for verification of sampling theorem.
CODE :
clc; % clears the command window
t=0:.001:0.1;
f1=input ('Enter the input frequency1 = ');
f2=input ('Enter the input frequency2 = ');
y=cos(2*pi*f1*t)+cos(2*pi*f2*t);
f3=max(f1,f2);
% under sampling
fs=f3; %fs = sampling freequency
ts=1/fs;
tx=0:ts:0.1;
m=max(size(tx));
ys=cos(2*f1*pi*tx)+cos(2*pi*f2*tx);
figure(1);
subplot(3,1,1);
plot(t,y);
title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
subplot(3,1,2);
stem(tx,ys);
title('The sinusoidal signal sampled at fs Hz');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
subplot(3,1,3);
plot(tx,ys);
title('The recovered sinusoidal sampled at fs Hz');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
% Right sampling
fs=2*f3; %fs = sampling freequency
ts=1/fs;
tx=0:ts:0.1;
m=max(size(tx));
ys=cos(2*pi*f1*tx)+cos(2*pi*f2*tx);
figure(2);
subplot(3,1,1);
plot(t,y);
title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
subplot(3,1,2);
stem(tx,ys);
title('The sinusoidal signal sampled at fs Hz');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
subplot(3,1,3);
plot(tx,ys);
title('The recovered sinusidal sampled at fs Hz');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
% over sampling
fs=3*f3; %fs = sampling freequency
ts=1/fs;
tx=0:ts:0.1;
m=max(size(tx));
ys=cos(2*pi*f1*tx)+cos(2*pi*f2*tx);
figure(3);
subplot(3,1,1);
plot(t,y);
title('The sinusoidal signal cos(2*pi*f1*t)+cos(2*pi*f2*t)');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
subplot(3,1,2);
stem(tx,ys);
title('The sinusoidal signal sampled at fs Hz');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
subplot(3,1,3);
plot(tx,ys);
title('The recovered sinusidal sampled at fs Hz');
xlabel('Time in seconds');
ylabel('Amplitude in volts');
OUTPUT:
Undersampling :
Right Sampling :
Oversampling :
EXPERIMENT-2
Program to find impulse response of a given system.
CODE :
clc;
b=input('Enter the coefficiants of x(n) in the order x(n),x(n-1)...in the Matrix form = ');
a=input('Enter the coefficiants of y(n) in the order y(n),y(n-1)...in the Matrix form = ');
n=[-5:20];
x=[(n==0)];
h=filter(b,a,x);
figure(1);
subplot(2,1,1);
stem(n,x);
xlabel('n');
ylabel('amplitude');
title('impulse sequence');
subplot(2,1,2);
stem(n,h);
xlabel('n');
ylabel('amplitude');
title('impulse response');
OUTPUT:
EXPERIMENT-3
Program to perform linear convolution of two given sequences.
CODE:
clc;
clear all;
close all;
x=input('enter the 1st sequence: ');
OUTPUT:
EXPERIMENT-4
Program to perform circular convolution of two given sequences.
CODE :
clc;
clear;
a = input('enter the sequence x(n) = ');
b = input('enter the sequence h(n) = ');
n1=length(a);
n2=length(b);
N=max(n1,n2);
x = [a zeros(1,(N-n1))];
for i = 1:N
k = i;
for j = 1:n2
H(i,j)=x(k)* b(j);
k = k-1;
if (k == 0)
k = N;
end
end
end
y=zeros(1,N);
M=H;
for j = 1:N
for i = 1:n2
y(j)=M(i,j)+y(j);
end
end
disp('The output sequence is y(n)= ');
disp(y);
stem(y);
title('Circular Convolution');
xlabel('n');
ylabel('y(n)');
OUTPUT:
EXPERIMENT-5
Program to perform cross-correlation of two given sequences.
CODE :
clc;
clear all;
close all;
x=input('enter the 1st sequence: ');
h=input('enter the 2nd sequence: ');
y=xcorr(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel('Amplitude');
xlabel('(a) n');
subplot(3,1,2);
stem(h);
ylabel('Amplitude');
xlabel('(b) n');
subplot(3,1,3);
stem(fliplr(y));
ylabel('Amplitude');
xlabel('(c) n');
disp('The resultant signal is');
fliplr(y);
OUTPUT:
EXPERIMENT-6
Program to perform auto-correlation of two given sequences.
CODE:
x=input('Enter the sequence: ');
y=xcorr(x,x);
figure;subplot(2,1,1);
stem(x);ylabel('Amplitude- ');
xlabel('(a) n');
subplot(2,1,2);
stem(fliplr(y));ylabel('Amplitude-');
xlabel('(b) n');
disp('The resultant signal is: ');
fliplr(y)
OUTPUT:
EXPERIMENT-7
Program for solution of difference equation.
CODE :
OUTPUT :
EXPERIMENT-8
To compute DFT of the sequence and plot magnitude and phase spectra.
CODE :
clc;
clear all;
close all;
% input sequence
xn=input('Enter the input sequence: ');
N=input('Enter the number of points: ');
xk=dft(xn,N);
%magnitude of dft
magxk=abs(xk);
%phase of dft
phasexk=angle(xk);
k=0:N-1;
subplot(2,1,1);
stem(k,magxk);
title('DFT sequence');
xlabel('Frequency');
ylabel('Magnitude');
subplot(2,1,2);
stem(k,phasexk);
title('Phase of DFT sequence');
xlabel('Frequency');
ylabel('Phase');
OUTPUT :
EXPERIMENT-9
To compute linear convolution of two sequences using DFT and IDFT
CODE :
clc;
clear all;
x=input('Input First Sequence');
h=input('Input Second Sequence');
n1=length(x);
n2=length(h);
N=n1+n2-1;
x1=[x zeros(1,N-n1)];
x2=[h zeros(1,N-n2)];
a=fft(x, N);
b=fft(h, N);
c=a.*b;
d=ifft(c, N);
disp('First Sequence');
x
disp('Second Sequence');
h
disp('Convolved Sequence');
d
n=0:N-1;
subplot(3,1,1);
stem(x);
title('First Sequence');
ylabel('Signal');
xlabel('Time');
subplot(3,1,2);
stem(h);
title('Second Sequence');
ylabel('Signal');
xlabel('Time');
subplot(3,1,3);
stem(d);
title('Convolved Sequence');
ylabel('Signal');
xlabel('Time')
OUTPUT :
EXPERIMENT-9
To compute circular convolution of two sequences using DFT and IDFT
CODE :
clc;
clear all;
close all;
x1=input('Enter the 1st input:');
x2=input('Enter the 2nd input:');
lx1=length(x1);
lx2=length(x2);
l=max(lx1,lx2);
if(l==lx1)
x2=[x2,zeros(1,l-lx2)];end;
if(l==lx2)
x1=[x1,zeros(1,l-lx1)];end;
for k=1:l
x1n(k)=0;
x2n(k)=0;
for n=1:l
x1n(k)=x1n(k)+(x1(n)*exp((-1j)*2*pi*(k-1)*(n-1)/l));
x2n(k)=x2n(k)+(x2(n)*exp((-1j)*2*pi*(k-1)*(n-1)/l));
end;
end
y=x1n.*x2n;
ly=length(y);
for n=1:ly
y1(n)=0;
for k=1:ly
y1(n)=y1(n)+y(k)*exp(1j*2*pi*(k-1)*(n-1)/ly);
end;
y1(n)=y1(n)/ly;
end;
disp(y1);
subplot(2,2,1);
stem(x1);
title('1st input');
subplot(2,2,2);
stem(x2);
title('2nd input');
subplot(2,2,3:4);
stem(abs(y1));
title('circular convolution using DFT & IDFT');
OUTPUT :
EXPERIMENT-12
PROGRAM TO IMPLEMENT SOME ELEMENTARY SIGNALS
CODE :
clc;
t=0:0.1:pi;
y=sin(2*pi*t);
subplot(3,2,1);
stem(t,y);
ylabel('Amplitude');
xlabel('Time');
title('SINE SEQUENCE');
t=0:0.1:pi;
y=cos(2*pi*t);
subplot(3,2,2);
stem(t,y);
ylabel('Amplitude');
xlabel('Time');
title('COSINE SEQUENCE');
n1=input('Enter the length of the ramp signal:');
t=0:n1;
subplot(3,2,3);
stem(t,t);
ylabel('Amplitude');
xlabel('Time');
title('RAMP SEQUENCE');
n2=input('Enter the length of the exponential sequence:');
t=0:n2;
a=input('Enter the "a" value:');
y2=exp(a*t);
subplot(3,2,4);
stem(t,y2);
ylabel('Amplitude');
xlabel('Time');
title('EXPONENTIAL SEQUENCE');
n3=input('Enter the length of the unit sequence:');
t=0:1:n3-1;
y1=ones(1,n3);
subplot(3,2,5);
stem(t,y1);
ylabel('Amplitude');
xlabel('Time');
title('UNIT STEP SEQUENCE');
t=-5:5;
y=[zeros(1,5),ones(1,1),zeros(1,5)];
subplot(3,2,6);
stem(t,y);
ylabel('Amplitude');
xlabel('Time');
title('UNIT IMPULSE SEQUENCE');
OUTPUT :
EXPERIMENT-12
CODE :
clc;
clear all;
close all;
rp=input('enter passband ripple');
rs=input('enter the stopband ripple');
fp=input('enter passband freq');
fs=input('enter stopband freq');
f=input('enter sampling freq ');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
c=input('enter your choice of window function 1. rectangular 2. triangular 3.kaiser: \n ');
if(c==1)
y=rectwin(n1);
disp('Rectangular window filter response');
end
if (c==2)
y=triang(n1);
disp('Triangular window filter response');
end
if(c==3)
y=kaiser(n1);
disp('kaiser window filter response');
end
%LPF
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,1,1);plot(o/pi,m);
title('LPF');
ylabel('Gain in dB-->');
xlabel('(a) Normalized frequency-->');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
c=input('enter your choice of window function 1. rectangular 2. triangular 3.kaiser: \n ');
if(c==1)
y=rectwin(n1);
disp('Rectangular window filter response');
end
if (c==2)
y=triang(n1);
disp('Triangular window filter response');
end
if(c==3)
y=kaiser(n1);
disp('kaiser window filter response');
end
%LPF
b=fir1(n,wp,y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,1,1);plot(o/pi,m);
title('LPF');
ylabel('Gain in dB-->');
xlabel('(a) Normalized frequency-->');
grid on
OUTPUT:
EXPERIMENT-13
PROGRAM TO IMPLEMENT A HP FIR FILTER FOR GIVEN SEQUENCE
CODE :
clc;
clear all;
close all;
rp=input('enter passband ripple');
rs=input('enter the stopband ripple');
fp=input('enter passband freq');
fs=input('enter stopband freq');
f=input('enter sampling freq ');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;
dem=14.6*(fs-fp)/f;
n=ceil(num/dem);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
c=input('enter your choice of window function 1. rectangular 2. triangular 3.kaiser: \n ');
if(c==1)
y=rectwin(n1);
disp('Rectangular window filter response');
end
if (c==2)
y=triang(n1);
disp('Triangular window filter response');
end
if(c==3)
y=kaiser(n1);
disp('kaiser window filter response');
%HPF
b=fir1(n,wp,'high',y);
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
subplot(2,1,2);plot(o/pi,m);
title('HPF');
ylabel('Gain in dB-->');
xlabel('(b) Normalized frequency-->');
OUTPUT:
EXPERIMENT-14
PROGRAM TO IMPLEMENT A LP IIR FILTER FOR GIVEN SEQUENCE
CODE :
clc;
close all;
clear all;
format long;
rp=input('Enter the passband ripple(rp):');
rs=input('Enter the stopband ripple(rs):');
fp=input('Enter the passband frequency(fp):');
fs=input('Enter the stopband frequency(fs):');
f=input('Enter the sampling frequency(f):');
wp=2*fp/f;
ws=2*fs/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,o]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);
plot(o/pi,m);
ylabel('Gain(db)->');
xlabel('(a)Normalised frequency->');
subplot(2,1,2);
plot(o/pi,an);
ylabel('Phase(radians)->');
xlabel('(b)Normalised frequency->');
OUTPUT:
Enter the passband ripple(rp):0.4
Enter the stopband ripple(rs):30
Enter the passband frequency(fp):400
Enter the stopband frequency(fs):800
Enter the sampling frequency(f):2000
EXPERIMENT-15
PROGRAM TO IMPLEMENT DECIMINATION PROCESS
CODE :
% DECIMATION
clc;
clear all;
close all;
disp('Let us take a sinusoidal sequence which has to be decimated: ');
fm=input('Enter the signal frequency fm: ');
fs=input('Enetr the sampling frequnecy fs: ');
T=input('Enter the duration of the signal in seconds T: ');
dt=1/fs;
t=dt:dt:T
M=length(t);
m=cos(2*pi*fm*t);
r=input('Enter the factor by which the sampling frequency has to be reduced r: ');
md=decimate(m,r);
figure(1);
subplot(3,1,1);
plot(t,m);
grid;
xlabel('t-->');
ylabel('Amplitude-->');
title('Sinusoidal signal before sampling');
subplot(3,1,2);
stem(m);
grid;
xlabel('n-->');
ylabel('Amplitudes of m -->');
title('Sinusoidal signal after sampling before decimation');
subplot(3,1,3);
stem(md);
grid;
title('Sinusoidal after decimation');
xlabel('n/r-->');
ylabel('Amplitude of md-->');
OUTPUT:
Amplitude of md-->
Amplitudes of m -->
Amplitude-->
1
0
-1
0.1
0.2
0.3
0.4
10
20
30
40
10
15
0.5
0.6
0.7
0.8
t-->
Sinusoidal signal after sampling before decimation
0.9
80
90
100
40
45
50
0
-1
50
60
70
n-->
Sinusoidal after decimation
0
-1
20
25
n/r-->
30
35
EXPERIMENT-15
PROGRAM TO IMPLEMENT INTERPOLATION PROCESS
CODE :
%INTERPOLATION
clc;
clear all;
close all;
disp('Let us take a sinusoidal sequence which has to be interpolated: ');
fm=input('Enter the signal frequency fm: ');
fs=input('Enetr the sampling frequnecy fs: ');
T=input('Enter the duration of the signal in seconds T: ');
dt=1/fs;
t=dt:dt:T
M=length(t);
m=cos(2*pi*fm*t);
r=input('Enter the factor by which the sampling frequency has to be increased r: ');
md=interp(m,r);
figure(1);
subplot(3,1,1);
plot(t,m);
grid;
xlabel('t-->');
ylabel('Amplitude-->');
title('Sinusoidal signal before sampling');
subplot(3,1,2);
stem(m);
grid;
xlabel('n-->');
ylabel('Amplitudes of m -->');
title('Sinusoidal signal after sampling before interpolation');
subplot(3,1,3);
stem(md);
grid;
title('Sinusoidal after interpolation');
xlabel('n x r-->');
ylabel('Amplitude of md-->');
OUTPUT :
Let us take a sinusoidal sequence which has to be interpolated:
Enter the signal frequency fm: 2
Enetr the sampling frequnecy fs: 100
Enter the duration of the signal in seconds T: 1
Enter the factor by which the sampling frequency has to be increased r: 2
Amplitude-->
Amplitudes of m -->
Amplitude of md-->
1
0
-1
0.1
0.2
0.3
0.4
10
20
30
40
20
40
60
80
0.5
0.6
0.7
0.8
t-->
Sinusoidal signal after sampling before interpolation
0.9
80
90
100
160
180
200
0
-1
50
60
70
n-->
Sinusoidal after interpolation
0
-2
100
120
n x r-->
140
EXPERIMENT-17
PROGRAM TO DETERMINE POWER SPECTRUM OF A GIVEN SEQUENCE
CODE :
clc;
clear all;
close all;
f1=input('Enter the frequency of first sequence in Hz: ');
f2=input('Enter the frequency of the second sequence in Hz: ');
fs=input('Enter the sampling frequency in Hz: ');
t=0:1/fs:1;
x=2*sin(2*pi*f1*t)+3*sin(2*pi*f2*t)+rand(size(t));
px1=abs(fft(x).^2);
px2=abs(fft(xcorr(x),length(t)));
subplot(211)
plot(t*fs,10*log10(px1));%square magnitude
grid;
xlabel('Freq.in Hz-->');
ylabel('Magnitude in dB-->');
title('PSD using square magnitude method');
subplot(212)
plot(t*fs,10*log10(px2));%autocorrelation
grid;
xlabel('Freq.in Hz-->');
ylabel('Magnitude in dB-->');
title('PSD using auto correlation method');
OUTPUTS:
Enter the frequency of first sequence in Hz: 200
Enter the frequency of the second sequence in Hz: 400
Enter the sampling frequency in Hz: 1000
Magnitude in dB-->
100
50
0
-50
100
200
100
200
Magnitude in dB-->
60
300
400
500
600
700
Freq.in Hz-->
PSD using auto correlation method
800
900
1000
800
900
1000
50
40
30
300
400
500
600
Freq.in Hz-->
700