Experiments Using Matlab

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

EXPERIMENTS USING MATLAB

1.

Verification of Sampling theorem.

clc;clear all;close all;


%Program to verify the sampling theorem
%Generate 15Hz sinusoidal signal of 0.1s duration sampled at 1000Hz
t=0:0.001:0.1;
fm=15;
x=sin(2*pi*fm*t);
figure(1)
plot(t,x),xlabel('Time'),ylabel('Amplitude');
title('Original Analog signal');
%Analog signal sampled at fs<<2fm
fs=10;
n=0:1/fs:0.1;
xn=sin(2*pi*fm*n);
figure(2)
subplot(2,1,1);stem(n,xn);
xlabel('Time'),ylabel('Amplitude');
title('Undersampled fs<<2fm signal');
subplot(2,1,2);plot(n,xn);
xlabel('Time'),ylabel('Amplitude');
title('Reconstructed undersampled fs<<2fm signal');
%Analog signal sampled at Nyquist rate fs=2fm
fs=30;
n=0:1/fs:0.1;
xn=sin(2*pi*fm*n);
figure(3)
subplot(2,1,1);stem(n,xn);
xlabel('Time'),ylabel('Amplitude');
title('Sampled at Nyquist rate fs=2fm signal'');
subplot(2,1,2);plot(n,xn);
xlabel('Time'),ylabel('Amplitude');
title('Reconstructed Nyquist rate fs=2fm signal');
%Analog signal sampled at oversampling fs>>2fm
fs=500;
n=0:1/fs:0.1;
xn=sin(2*pi*fm*n);
figure(4)
subplot(2,1,1);stem(n,xn);
xlabel('Time'),ylabel('Amplitude');
title('Oversampled fs>>2fm signal');
subplot(2,1,2);plot(n,xn);
xlabel('Time'),ylabel('Amplitude');
title('Reconstructed oversampled fs>>2fm signal');

2.

Impulse response of a given system

clc;clear all;close all;


a=input('Enter the feedback coefficients:');
b=input('Enter the feedforward coefficients:');
h=impz(b,a);
n=0:length(h)-1;
stem(n,h);grid on
xlabel('Time');ylabel('Amplitude');
title('Impulse Response h[n]');

3.

Linear convolution of two given sequences.

clc;clear all;close all;


x=input('Enter x[n]:');
nx=0:length(x)-1;
h=input('Enter h[n]:');
nh=0:length(h)-1;
z=conv(x,h);
nz=0:length(z)-1;
subplot(3,1,1);
stem(nx,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n]');
subplot(3,1,2);
stem(nh,h);
xlabel('Time');ylabel('Amplitude');
title('Impulse response of the system h[n]');
subplot(3,1,3);
stem(nz,z);
xlabel('Time');ylabel('Amplitude');
title('Linear Convolution');

4.

Circular convolution of two given sequences

clc;clear all;close all;


x=input('Enter x[n]:');
h=input('Enter h[n]:');
xl=length(x);
hl=length(h);
m=max(xl,hl);
z=ifft(fft(x,m).*fft(h,m));
zl=length(z);
%Plots
nx=0:xl-1;
nh=0:hl-1;
nz=0:zl-1;
subplot(3,1,1);
stem(nx,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n]');
subplot(3,1,2);
stem(nh,h);
xlabel('Time');ylabel('Amplitude');
title('Impulse response of the system h[n]');

subplot(3,1,3);
stem(nz,z);
xlabel('Time');ylabel('Amplitude');
title('Circular Convolution');

5.

Autocorrelation of a given sequence and verification of its properties.

clc;clear all;close all;


x=input('Enter x[n]:');
n=0:1:length(x)-1;
subplot(2,1,1);
stem(n,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n] ');
Rxx=xcorr(x);
nRxx=-length(x)+1:length(x)-1;
subplot(2,1,2);
stem(nRxx,Rxx);
xlabel('Time');ylabel('Amplitude');
title('Autocorrelation of x[n]');
%Verification of properties
center_index=ceil(length(Rxx)/2);
Rxx_0=Rxx(center_index);
E=sum(x.^2);
if Rxx_0==E
disp('Energy (Mean square value) property is verified.');
else
disp('Energy property is not verified.');
end
Rxx_right=Rxx(center_index:length(Rxx));
Rxx_left=Rxx(center_index:-1:1);
if Rxx_right==Rxx_left
disp('Autocorrelation is an even function. Hence symmetry property is
verified.');
else
disp('Autocorrelation is not even.');
end
m=max(Rxx);
if m==Rxx_0
disp('Maximum value of autocorrelation function is at zero.');
else
disp('Maximum value is not at zero.');
end
z=x.*2;
Rzz=xcorr(z)/2;
Rmn=Rxx+Rxx;
if Rzz==Rmn
disp('Linearity property is verified');
else
disp('Linearity property is not verified');
end

6.

Cross correlation of given sequences and verification of its properties.

clc;clear all;close all;


x=input('Enter the first sequence x[n]:');
y=input('Enter the second sequence y[n]:');
nx=length(x);
ny=length(y);
n=max(nx,ny);
Rxy=xcorr(x,y);
subplot(3,1,1);
nxl=0:1:nx-1;
stem(nxl,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n]');
subplot(3,1,2);
nyl=0:1:ny-1;
stem(nyl,y);
xlabel('Time');ylabel('Amplitude');
title('Input sequence y[n]');
subplot(3,1,3);
l=-n+1:n-1;
stem(l,Rxy);
xlabel('Time');ylabel('Amplitude');
title('Cross correlation of x[n] and y[n]');
%Verification of properties
z=fliplr(Rxy);
Ryx=xcorr(y,x);
if z==Ryx
disp('Rxy(-k)=Ryx(k) - Verified.');
end
a=Rxy.*Rxy;
Rxx=xcorr(x);
Ryy=xcorr(y);
c1=ceil(length(Rxx)/2);
c2=ceil(length(Ryy)/2);
Rxx_0=Rxx(c1);
Ryy_0=Ryy(c2);
if a<=Rxx_0.*Ryy_0
disp('Rxy(k)^2<=Rxx(0).Ryy(0) - Verified.');
end

7.

Solving a given difference equation.

clc;clear all;close all;


a=input('Enter the coefficients of y[n]:');
b=input('Enter the coefficients of x[n]:');
x=input('Enter the input values:');
ic=input('Enter the initial conditions:');
zi=filtic(b,a,ic);
y=filter(b,a,x,zi);
n=0:length(y)-1;
stem(n,y);
xlabel('Time');ylabel('Amplitude');
title('Solution of difference equation: ');

8.
Computation of N point DFT of a given sequence and to plot magnitude and phase
spectrum.
clc;clear all;close all;
x=input('Enter x[n]:');
nx=0:length(x)-1;
N=32;
%Compute DFT
n=0:length(x)-1;
for k=0:N-1
w=exp(-j*2*pi*k*n/N);
dot_prod=x.*w;
X(k+1)=sum(dot_prod);
end
%Plot the input
subplot(3,1,1);
stem(nx,x);
xlabel('Time');ylabel('Input x[n]');
title('Input sequence x[n]
');
title('Computation of DFT ');
%Plot the magnitude spectrum
subplot(3,1,2);
stem(abs(X));
xlabel('Time');ylabel('Amplitude');
title('Magnitude Spectrum');
%Plot the phase spectrum
subplot(3,1,3);
stem(angle(X));
xlabel('Time');ylabel('Angle in radian');
title('Phase Spectrum');

9.

Linear convolution of two sequences using DFT and IDFT.

clc;clear all;close all;


x=input('Enter x[n]:');
h=input('Enter h[n]:');
nx=length(x);
nh=length(h);
n=nx+nh-1;
xnew=[x zeros(1,n-nx)];
hnew=[h zeros(1,n-nh)];
xf=fft(xnew);
hf=fft(hnew);
zf=xf.*hf;
z=ifft(zf);
lx=0:1:nx-1;
lh=0:1:nh-1;
nz=0:1:length(z)-1;
subplot(3,1,1);
stem(lx,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n]');
subplot(3,1,2);

stem(lh,h);
xlabel('Time');ylabel('Amplitude');
title('Impulse Response h[n]');
subplot(3,1,3);
stem(nz,z);
xlabel('Time');ylabel('Amplitude');
title('Linear Convolution ');
%Verification
z1=conv(xnew,hnew)

10.

Circular convolution of two given sequences using DFT and IDFT

clc;clear all;close all;


x=input('Enter x[n]:');
h=input('Enter h[n]:');
nx=length(x);
nh=length(h);
n=max(nx,nh);
xnew=[x zeros(1,n-nx)];
hnew=[h zeros(1,n-nh)];
xf=fft(xnew);
hf=fft(hnew);
zf=xf.*hf;
z=ifft(zf);
lx=0:1:nx-1;
lh=0:1:nh-1;
nz=0:1:length(z)-1;
subplot(3,1,1);
stem(lx,x);
xlabel('Time');ylabel('Amplitude');
title('Input sequence x[n] ');
subplot(3,1,2);
stem(lh,h);
xlabel('Time');ylabel('Amplitude');
title('Impulse Response h[n]');
subplot(3,1,3);
stem(nz,z);
xlabel('Time');ylabel('Amplitude');
title('Circular Convolution');

11.

Design and implementation of FIR filter to meet given specifications.

clc;close all;clear all;


rp=input('Enter the passband ripple:');
rs=input('Enter the stoopband ripple:');
fp=input('Enter the passband frequency:');
fs=input('Enter the stoopband frequency:');
f=input('Enter the sampling frequency:');
wp=2*fp/f;
ws=2*fs/f;
num=-20*log10(sqrt(rp*rs))-13;

den=14.6*(fs-fp)/f;
n=ceil(num/den);
n1=n+1;
if(rem(n,2)~=0)
n1=n;
n=n-1;
end
y=boxcar(n1);
%-----> (1)
b=fir1(n,wp,'low',y);
%-----> (2)
[h,o]=freqz(b,1,256);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(o/pi,m);
title('Magnitude response');
xlabel('Normalised frequency');
ylabel('Gain in dB');
subplot(2,1,2);plot(o/pi,an);
title('Phase response');
xlabel('Normalised frequency');
ylabel('Gain in rad');
%make relevent changes in (1) for diff. window
%
%
%
%
%
%

boxcar------->rectangular window
bartlett----->bartlett window
blackman----->blackman window
chebwin------>chebyshev window
hamming------>hamming window
hanning------>hanning window

%make relevent changes in (2) for diff. types


%low pass-------->(n,wp,'low',y);
%high pass------->(n,wp,'high',y);
%band pass------->(n,[wp ws],'bandpass',y);
%band stop------->(n,[wp ws],'stop',y);
% for kaisar window make these changes
% beta=input('Enter the value of beta----->after line 6
%y=kaiser(n1,beta)------>in line 17
% Eg: (Values work for all types of FIR filters)
% rp=0.02
rs=0.01
fp=1000
fs=1500
f=10000

12.

beta=5.8

Design and implementation of IIR filter to meet given specifications.

%butterworth filter
clc;close all;clear all;
format long;
rp=input('Enter the passband ripple:');
rs=input('Enter the stoopband ripple:');
fp=input('Enter the passband frequency:');
fs=input('Enter the stoopband frequency:');
f=input('Enter the sampling frequency:');

wp=2*fp/f;
ws=2*fs/f;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn,'low');
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);
title('Magnitude response');
xlabel('Normalised frequency');
ylabel('Gain in dB');
subplot(2,1,2);plot(o/pi,an);
title('Phase response');
xlabel('Normalised frequency');
ylabel('Gain in rad');

%--------->(1)
%--------->(2)

%chebyshev type I filter


%(1)------>[n,wn]=cheb1ord(wp,ws,rp,rs);
%(2)------>[b,a]=cheby1(n,rp,wn,'low');
%chebyshev type II filter
%(1)------>[n,wn]=cheb2ord(wp,ws,rp,rs);
%(2)------>[b,a]=cheby2(n,rs,wn,'low');
% rp=.035

rs=35

fp=1500

fs=2000

f=8000

B. LIST OF EXPERIMENTS USING DSP PROCESSOR


1.

Linear convolution of two given sequences.

2.

Circular convolution of two given sequences.

3.

Computation of N- Point DFT of a given sequence

4.
Realization of an FIR filter (any type) to meet given specifications .The input can be a
signal from function generator / speech signal.
5.
Audio applications such as to plot time and frequency (Spectrum) display of Microphone
output plus a cosine using DSP. Read a wav file and match with their respective spectrograms
6.
Noise: Add noise above 3 kHz and then remove; Interference suppression using 400 Hz
tone.
7.

Impulse response of first order and second order system

You might also like