Вы находитесь на странице: 1из 90

PRACTICAL NO.

1
Topic: Basic Signals
Problem Statement:
Generate the basic functions like unit impulse, unit step, ramp, exp, sin, cos and display the
output on screen.
Description:
Unit Impulse Function: The impulse function is called the Delta function ((t)) and is defined by,
(n) = 1, n = 0
= 0, n 0
Unit Step Function: The unit-step function is defined by,
u(n) = 1, n 0
= 0, n < 0
Unit-ramp signal: The unit-ramp function is defined by,
r(n) = n, n 0
= 0, n < 0
Exponential signal: The exponential function is defined by,
x(n) = a
n
, n 0
= 0, n < 0
Sine signal: The Sine function is defined by,
y = sin() where is an angle.
Cosine signal: The Cosine function is defined by,
y = cos() where is an angle.
(MATLAB) Functions used:
Function Name Description
zeros(m,n) Returns an m-by-n matrix of zeros.
ones(m,n) Returns an m-by-n matrix of ones.
stem(X,Y) stem function plots discrete sequence data, plots X versus the columns of Y.
exp(X) Returns the exponential for each element of X.
sin(X) Returns the circular sine of the elements of X.
cos(X) Returns the circular cosine for each element of X.
Source Code:
% BASIC STANDARD SIGNALS
t=(0:0.001:1)' ;
y=[1;zeros(99,1)] ; %UNIT IMPULSE
subplot(6,1,1); plot(t(1:100), y(1:100))
y=ones(100,1) ; %UNIT STEP
subplot(6,1,2); plot(t(1:100), y(1:100))
y=t ; %UNIT RAMP
subplot(6,1,3); plot(t(1:100), y(1:100))
x=-2*pi:0.001:2*pi ;
y=exp(x) ;
subplot(6,1,4); plot(x,y) %EXPONENTIAL
y=sin(x) ;
subplot(6,1,5); plot(x,y) %SINE FUNCTION
y=cos(x) ;
subplot(6,1,6); plot(x,y) %COSINE FUNCTION
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
0
0.5
1
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
0
1
2
0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09 0.1
0
0.05
0.1
-8 -6 -4 -2 0 2 4 6 8
0
500
1000
-8 -6 -4 -2 0 2 4 6 8
-1
0
1
-8 -6 -4 -2 0 2 4 6 8
-1
0
1
Output:
PRACTICAL NO. 2
Topic: Frequency, Magnitude and Phase response
Problem Statement:
Generate the signal given bellow, display the plots - magnitude and phase response. Also plot the
frequency content of it.
a. sn = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
b. sn = 3 + 5.657 * cos(2*pi*(0.1)*n)
where array n = 0,1,2..256.
Description:
Frequency Response: It is a complex function that describes the magnitude and phase shift of a
filter over a range of frequencies.
Magnitude Response: It is an absolute value of a filters complex frequency response.
Phase Response: It is an angle component of a filters frequency response.
(MATLAB) Functions used:
Function Name Description
axis([xmin xmax ymin ymax]) sets the limits for the x- and y-axis of the current axes.
freqz(Hq,n,'whole',fs) returns the optional string argument units, specifying the units
for the frequency vector.
sin(X) Returns the circular sine of the elements of X.
cos(X) Returns the circular cosine for each element of X.
Source Code:
% TO FIND THE MAGNITUDE, FEQUENCY & PHASE RESPONSE
clc ;
clear all ;
close all ;
for i = 1:256
sn1(i) = 3.5 * sin(2 * pi * (0.15) * i) + sin(2 * pi * (0.4) * i) ;
sn2(i) = 3 + 5.657 * cos(2 * pi * (0.1) * i) ;
end ;
% PLOTTING THE SIGNALS
figure ;
subplot(2,1,1) ; plot(sn1) ;
title('sn1 = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)') ;
xlabel('n') ;
ylabel('sn1') ;
subplot(2,1,2) ; plot(sn2) ;
title('sn2 = 3 + 5.657 * cos(2*pi*(0.1)*n)') ;
xlabel('n') ;
ylabel('sn2') ;
% PLOTTING THE FREQUENCY & PHASE RESPONSE OF sn1
figure ;
freqz(sn1, 1, 512, 2) ;
title('Frequency response of sn1') ;
% PLOTTING THE FREQUENCY & PHASE RESPONSE OF sn2
figure ;
freqz(sn2, 1, 512, 2) ;
title('Frequency Response of sn2') ;
Output:
PRACTICAL NO. 3
Topic: Z-transform
Problem Statement:
Compute the z-transform of the following using partial fraction method and display the pole-zero
plot.
a. X(z) = z-1/pow((z 0.7071),2)
b. X(z) = pow(z,4) - 1/pow(z,4) + 1
c. X(z) = pow(z,3) - pow(z,2) + z 1/ pow((z + 0.9),3)
Description:
z-tranform: The z-transform of a discrete-time signal x(n) is defined as the power series
X(z) =

n
n
x(n)z
where z is a complex variable.
Residue Method: This method is useful in determining the time-signal x(n) by summing
residiues of [X(z)z
n-1
] at all poles. This is expressed mathematically as
x(n) =

X(z)
poles all
1 - n
z of residues
where the residue for a pole of order m at z = is
Residue = , |

'

1 n m
1 m
1 m
z
X(z)z ) (z
dz
d
lim
1)! - (m
1
(MATLAB) Functions used:
Function Name Description
residuez(b,a) finds the residues, poles, and direct terms of a partial fraction
expansion of the ratio of two polynomials, b(z) and a(z).
Vectors b and a specify the coefficients of the polynomials of
the discrete-time system b(z)/a(z) in descending powers of z.
freqz(Hq,n,'whole',fs) returns the optional string argument units, specifying the units
for the frequency vector.
Source Code:
% Z TRANSFORM USING THE PARTIAL FRACTION & DISPLAY THE
POLE ZERO PLOT THE NUMBER OF ZEROS & POLES IS THE ORDER
OF THE Z IN THE NUMERATOR & DENOMINATOR NUMERATOR =
ZEROS & DENOMINATOR = POLES
clc ;
clear all ;
close all ;
% POLE ZERO PLOT FOR X(Z) = (Z-1) / (Z-0.7071)^2
figure ;
b = [-1, 1] ;
a = [1, -2*0.7071, (0.7071^2)] ;
[r, p, k] = residuez(b, a) ; % r=RESIDUE, p=POLE, k=DIRECT TERMS
r = roots(b) ;
zplane(r, p) ;
%axis([1.5, 1.5, 1.5, 1.5]) ;
title('Pole zero plot for X(z) = (z-1) / pow((z-0.7071), 2)') ;
% POLE ZERO PLOT FOR X(Z) = (Z^4) - 1 / (Z^4) + 1
figure ;
b = [1, 0, 0, 0, -1] ;
a = [1, 0, 0, 0, 1] ;
[r, p, k,] = residuez(b, a) ;
r = roots(b) ;
zplane(r, p) ;
%axis([-1.5, 1.5, -1.5, 1.5]) ;
title('Pole zero plot for X(z) = (pow(z,4) - 1) / (pow(z,4) + 1)') ;
% POLE ZERO PLOT FOR X(z) = (Z^3 - Z^2 + Z - 1) / ((Z + 0.9)^3)
figure ;
b = [1, -1, 1, -1] ;
a = [1, 3*(0.9^2), 3*0.9, 0.9^3] ;
[r, p, k] = residuez(b, a) ;
r = roots(b) ;
zplane(r, p) ;
%axis([1.5, 1.5, 1.5, 1.5]) ;
title('Pole zero ploe for X(z) = (pow(z,3) - pow(z, 2) + z - 1) / (pow((z - 0.9), 3))') ;
Output:
-1.5 -1 -0.5 0 0.5 1 1.5
-1.5
-1
-0.5
0
0.5
1
1.5
2
Real Part
I
m
a
g
i
n
a
r
y

P
a
r
t
Pole-Zero Plot of X(z) = (z - 1)/(z - 0.7071)
2
-1.5 -1 -0.5 0 0.5 1 1.5
-1.5
-1
-0.5
0
0.5
1
1.5
Real Part
I
m
a
g
i
n
a
r
y

P
a
r
t
Pole-Zero Plot of X(z) = (z
4
- 1)/(z
4
+ 1)
-1.5 -1 -0.5 0 0.5 1 1.5
-1.5
-1
-0.5
0
0.5
1
1.5
3
Real Part
I
m
a
g
i
n
a
r
y

P
a
r
t
Pole-Zero Plot of X(z) = (z
3
- z
2
+ z - 1) / (z + 0.9)
3
PRACTICAL NO. 4
Topic: N-DFT
Problem Statement:
Compute the N-point DFT of the following, vary the value of N and visualize the effect with N =
8,16,24,64,128,256.
a. s(n) = 3 * pow(e,-0.1 * n)
b. s(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
c. s(n) = 4 * sin(4 * pi * (0.4) * n) + sin(2 * pi * (0.2) * n)
Description:
The Discrete Fourier Tranform (DFT) computes the values of the z-transform for evenly spaced
points around the unit circle for a given sequence.
If the sequence to be represented is of finite duration, i.e. it has only a finite no. of non-zero
values, the transform used is discrete fourier transform (DFT).
DFT finds its applications in DSP including linear filtering, correlation analysis and spectrum
analysis.
(MATLAB) Functions used:
Function Name Description
length(X) Returns the length of a vector X.
exp(X) Returns the exponential for each element of X.
sin(X) Returns the circular sine of the elements of X.
cos(X) Returns the circular cosine for each element of X.
Source Code:
% COMPUTE THE N-DFT FOR N = 8, 16, 24, 64, 128, 256
clc ;
clear all ;
close all ;
e = 2.72 ;
N = input('Please enter the length of the sequence : ') ;
t = 0:1:N-1 ;
for i = 1:N
sig1(i) = 3 * e^(-0.1*i) ;
sig2(i) = 2 * cos(2 * pi * 0.15 * i) + sin(2 * pi * 0.4 * i) ;
sig3(i) = 4 * sin(4 * pi * 0.4 * i) + sin(2 * pi * 0.2 * i) ;
end
% PLOTTING FIRST SEQUENCE & ITS DFT
figure ;
subplot(2,1,1) ; plot(t, sig1) ;
title(['Plot of sig(n) = 3 * pow(e,-0.1 * n) & N = ', int2str(N)]) ;
subplot(2,1,2) ; stem(t, fft(sig1, N)) ;
title(['FFT of sig(n) = 3 * pow(e,-0.1 * n) & N = ', int2str(N)]) ;
% PLOTTING SECOND SEQUENCE & ITS DFT
figure ;
subplot(2,1,1) ; plot(t, sig2) ;
title(['Plot of sig(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n) & N = ', int2str(N)]) ;
subplot(2,1,2) ; stem(t, fft(sig2, N)) ;
title(['FFT of sig(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n) & N = ', int2str(N)]) ;
% PLOTTING THIRD SEQUENCE & ITS DFT
figure ;
subplot(2,1,1) ; plot(t, sig3) ;
title(['Plot of sig(n) = 4 * sin(4 * pi * (0.4) * n) + sin(2 * pi * (0.2) * n) & N = ', int2str(N)]) ;
subplot(2,1,2) ; stem(t, fft(sig3, N)) ;
title(['FFT of sig(n) = 4 * sin(4 * pi * (0.4) * n) + sin(2 * pi * (0.2) * n) & N = ', int2str(N)]) ;
Output:
PRACTICAL NO. 5
Topic: N-DFT using Twiddle Matrix
Problem Statement:
Compute 4-point DFT using Twiddle matrix. Accept an input sequence x(n) from the user and
display the output sequence y(n). Compute N-point DFT without using Twiddle matrix. Accept
N and an input sequence x(n) from the user and display the output sequence y(n).
Description:
In view of the importance of the DFT in various digital signal processing applications, such as
linear filtering, correlation analysis, and spectrum analysis, its efficient computation is a topic
that has received considerable attention by many mathematicians, engineers, and applied
scientists.
Basically, the computational problem for the DFT is to compute the sequence {X(k)} of N
complex-valued numbers given another sequence of data {x(n)} of length N, according to the
formula
In general, the data sequence x(n) is also assumed to be complex valued. Similarly, The IDFT
becomes
Since DFT and IDFT involve basically the same type of computations, our discussion of efficient
computational algorithms for the DFT applies as well to the efficient computation of the IDFT.
Direct computation of the DFT is basically inefficient primarily because it does not exploit the
symmetry and periodicity properties of the phase factor W
N
. In particular, these two properties
are:
Source Code:
// COMPUTING THE N-DFT USING THE TWIDDLE MATRIX
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<alloc.h>
#define PI 3.142
float *Xn , *Wn ;
void computeTwiddleMat() ;
void computeN_DFT() ;
void main()
{
int N , i ;
clrscr() ;
printf("Please enter the length of the input sequence : ") ;
scanf("%d", &N) ;
Xn = (float*)malloc(N * sizeof(float)) ;
if(Xn == NULL)
{
printf("\nMEMORY NOT AVAILABLE !") ;
exit(0) ;
}
printf("\n\nPlease enter the input sequence : ") ;
for(i=0 ; i < N ; i++)
{
printf("\nX[%d] = ", i) ;
scanf("%f", &Xn[i]) ;
}
computeTwiddleMat(N) ;
computeN_DFT(N) ;
getch() ;
}
void computeTwiddleMat(int N)
{
float real, imag ;
int i, j, tot ;
Wn = (float*)malloc(N * N * sizeof(float)) ;
if(Wn == NULL)
{
printf("\nMEMORY NOT AVAILABLE !") ;
exit(0) ;
}
tot = N * 2 ;
for(i=0 ; i < N ; i++)
{
for(j=0 ; j < N ; j++ )
{
real = cos((2.0 * PI * i * j) / N) ;
imag = -sin((2.0 * PI * i * j) / N) ;
/* A 2D ARRAY IS TAKEN, ONE FOR REAL & THE
OTHER FOR IMAGINARY SO EACH ELEMENT WILL
TAKE TWO BLOCKS */
*(Wn+(i*tot)+(j*2)+0) = real ;
*(Wn+(i*tot)+(j*2)+1) = imag ;
}
}
// PRINTING THE TWIDDLE MATRIX
printf("\n") ;
for(i=0 ; i < N ; i++)
{
for(j=0 ; j < N ; j++)
{
printf("%.2f + j(%.2f) | \t", *(Wn+(i*tot)+(j*2)+0),
*(Wn+(i*tot)+(j*2)+1)) ;
}
printf("\n") ;
}
return ;
}
void computeN_DFT(int N)
{
float realSum, imagSum ;
float *Yn ;
int i, j, tot ;
Yn = (float*)malloc(N * N * sizeof(float)) ;
if(Yn == NULL)
{
printf("\nMEMORY NOT AVAILABLE !") ;
exit(0) ;
}
tot = N * 2 ;
for(i=0 ; i < N ; i++)
{
realSum = imagSum = 0 ;
for(j=0 ; j < N ; j++)
{
// j BCOZ ROWS ARE MULTIPLIED WITH THE
COLUMNS
realSum = realSum + (*(Xn+j) * (*(Wn+(j*tot)+(i*2)+0)) ) ;
imagSum = imagSum + (*(Xn+j) * (*(Wn+(j*tot)+(i*2)+1)) ) ;
}
printf("\nReal = %.2f \t Imag = %.2f", realSum, imagSum) ;
*(Yn+(i*2)+0) = realSum ;
*(Yn+(i*2)+1) = imagSum ;
}
// PRINTING THE DFT
printf("\n\nThe %d-DFT of the Input Sequence is as follows : ", N) ;
for(i=0 ; i < N ; i++)
{
printf("\nY[%d] = %.2f + j(%.2f)", i, *(Yn+(i*2)+0), *(Yn+(i*2)+1)) ;
}
return ;
}
Output:
Please enter the length of the input sequence : 4
Please enter the input sequence :
X[0] = 1
X[1] = 2
X[2] = 3
X[3] = 4
1.00 + j(-0.00) | 1.00 + j(-0.00) | 1.00 + j(-0.00) | 1.00 + j(-0.00) |
1.00 + j(-0.00) | -0.00 + j(-1.00) | -1.00 + j(0.00) | 0.00 + j(1.00) |
1.00 + j(-0.00) | -1.00 + j(0.00) | 1.00 + j(-0.00) | -1.00 + j(0.00) |
1.00 + j(-0.00) | 0.00 + j(1.00) | -1.00 + j(0.00) | -0.00 + j(-1.00) |
Real = 7.00 Imag = 0.00
Real = 1.00 Imag = 2.00
Real = 1.00 Imag = 0.00
Real = 1.00 Imag = -2.00
The 4-DFT of the Input Sequence is as follows :
Y[0] = 7.00 + j(0.00)
Y[1] = 1.00 + j(2.00)
Y[2] = 1.00 + j(0.00)
Y[3] = 1.00 + j(-2.00)
PRACTICAL NO. 6
Topic: Linear Convolution
Problem Statement:
Write a program for determining the Linear Convolution of a finite duration sequence x(n) and
h(n). Accept the sequences x(n) and h(n) from the user. display the output sequence y(n). Plot all
the three sequences. Test on input : x(n) = [1 1 1 1 1], h(n) = [1 2 3 4 5 6 7 8].
Description:
Convolution is a mathematical operation equivalent to finite impulse response (FIR) filtering.
Convolution is important in digital signal processing because convolving two sequences in the
time domain is equivalent to multiplying the sequences in the frequency domain. Convolution
finds its application in processing signals especially analyzing the output of a system. Consider
the signals x
1
(n) and x
2
(n). The convolution of these two signals is given by,
y(n) = x
1
(n) * x
2
(n)
= k) (n (k)x x
2
k
1


(MATLAB) Functions used:
Function Name Description
conv(u,v) Convolves vectors u and v. Algebraically, convolution is the same
operation as multiplying the polynomials whose coefficients are the
elements of u and v.
Source Code:
% LINEAR CONVOLUTION
clc ;
clear all ;
close all ;
% ACCEPT THE SEQUENCES FROM THE USER
n1 = input('Please enter the length of sequence x(n) : ') ;
for i = 1:n1
x(i) = input(['Enter x(', int2str(i), ') : ']) ;
end
n2 = input('Please enter the length of sequence h(n) : ') ;
for i = 1:n2
h(i) = input(['Enter h(', int2str(i), ') : ']) ;
end
% DISPLAY THE SEQUENCES X(n), H(n) & THEIR CONVOLUTION
subplot(3,1,1) ;
stem(x) ;
title('Input Sequence x(n)') ;
subplot(3,1,2) ;
stem(h) ;
title('Impulse Response h(n)') ;
y=conv(x, h) ;
subplot(3,1,3) ;
stem(y) ;
title('Convolution of x(n) & h(n)') ;
Output:
1 1.5 2 2.5 3 3.5 4
0
2
4
Input Sequence x(n)
1 2 3 4 5 6 7 8 9 10
0
5
10
Impulse Response h(n)
0 2 4 6 8 10 12 14
0
50
100
Convolution of x(n) & h(n)
PRACTICAL NO. 7
Topic: Circular Convolution
Problem Statement:
Write a program for determining Circular Convolution of the sequence x(n) and h(n). Accept the
sequences x(n) and h(n) from the user. Calculate and display the output sequence y(n). Plot all
the three sequences. Test on input: x(n)= [1 2 4], h(n)=[1 2].
Description:
Circular Convolution is also known as Periodic Convolution. For computing the Circular
Convolution, 2 sequences must be of same length L where L=N1+N2-1, where N
1
is the length
of 1
st
sequence and N
2
is the length of 2
nd
sequence). Consider the signals x
1
(n) and x
2
(n). The
circular convolution of these two signals is given by,
y(n) = x
1
(n) * x
2
(n)
= k) (n x (k)
1 N
0 k
x
2
1
1

n = 0,1,., N
1
+ N
2
- 1
(MATLAB) Functions used:
Function Name Description
length(X) Returns the length of a vector X.
conv(u,v) Convolves vectors u and v. Algebraically, convolution is the same
operation as multiplying the polynomials whose coefficients are the
elements of u and v.
Source Code:
% CIRCULAR CONVOLUTION
% HERE, LENGTH OF THE SEQUENCES IS THE SAME
clc ;
clear all ;
close all ;
% ACCEPTING SEQUENCES FROM THE USER
n = input('Please enter the length of the sequences : ') ;
for i = 1:n
x(i) = input(['Enter x(', int2str(i), ') : ']) ;
end
for i = 1:n
h(i) = input(['Enter h(', int2str(i), ') : ']) ;
end
% DISPLAYING THE SEQUENCES
subplot(3,1,1) ;
stem(x) ;
title('Input Sequence X(n)') ;
subplot(3,1,2) ;
stem(h) ;
title('Impulse Response H(n)') ;
y = conv(x,h) ;
subplot(3,1,3) ;
stem(y) ;
title('Circular Convolution of X(n) & H(n)') ;
Output:
1 1.5 2 2.5 3 3.5 4
0
2
4
Input Sequence X(n)
1 1.5 2 2.5 3 3.5 4
0
5
10
Impulse Response H(n)
1 2 3 4 5 6 7
0
50
100
Circular Convolution of X(n) & H(n)
PRACTICAL NO. 8
Topic: Low-pass Filter
Problem Statement:
Design an N-point FIR low-pass filter with the cutoff frequency 0.2 * pi using Rectangular,
Hamming, Kaiser windows. Plot for N =16, 32, 64, 128, 256. Compare with N = 1024 and
record your observations. Input the Fourier Series: x(n) = 4*sin(2*pi*f*t) +
(4/3)*sin(2*pi*3*f*t) + (4/5)*sin(2*pi*5*f*t) + (4/7)*sin(2*pi*7*f*t) and obtain the output
when f=0.8, 0.6, 0.4, 0.2 and display.
Plot the magnitude and phase response of the filter.
Description:
Low-pass filter: Low-pass filters are used to block unwanted high-frequency signals, whilst
passing the lower frequencies. The low frequencies to be filtered out are relative to the unwanted
higher frequencies and therefore do not have a definitive range. The frequencies that are cut vary
from filter to filter. A low-pass filter is the opposite of a high-pass filter.
Cut-off Frequency: Frequency at which the power gain of an amplifier falls below 50% of
maximum.
Window Function: Window functions are applied to avoid discontinuities at the beginning and
the end of a set of data. The smaller these discontinuities are, the faster the side slopes drop.
(MATLAB) Functions used:
Function Name Description
BOXCAR(N) Returns the N-point rectangular window.
HAMMING(N) Returns the N-point symmetric Hamming window in a column
vector
KAISER(N,BETA) Returns the BETA-valued N-point Kaiser window
fir1(n,Wn,'ftype') Specifies a filter type, where 'ftype (e.g. bandpass, stop,
high, low etc.)
FREQZ(B,A,N,Fs) Return frequency vector F (in Hz), where Fs is the sampling
frequency (in Hz) where A and B are numerator and denominator
coefficients in vectors
UNWRAP(P) Unwraps radian phases P by changing absolute jumps greater than
pi to their 2*pi complement.
ANGLE(H) Returns the phase angles, in radians, of a matrix with complex
elements
ABS(X) Returns absolute value of the elements of X.
Source Code:
% LOW-PASS FIR FILTER
clc ;
clear all ;
close all ;
N = input('Please enter the number of samples : ') ;
Wn = 0.2 * pi ;
N1 = 1024 ;
beta = 5.8 ;
% RECTANGULAR WINDOW
y = rectwin(N) ;
figure ;
% PLOTTING THE RECTANGULAR WINDOW
subplot(2,2,1) ;
plot(y) ;
title(['Rectangular Window N = ', int2str(N)]) ;
grid ;
% DESIGNING THE FILTER
b = fir1(N-1, Wn, 'low', y) ; % DESIGNING WINDOW-BASED FIR FILTER
[H,F] = freqz(b,1,N) ; % RETURNS FREQ. RESPONSE VECTOR 'H' & FREQ.
VECTOR 'F'
m = 20 * log10(abs(H)) ; % CALCULATING THE MAGNITUDE RESPONSE
a = rad2deg(unwrap(angle(H))) ; % CALCULATING THE PHASE RESPONSE
% PLOTTING THE MAGNITUDE RESPONSE
subplot(2,2,2) ;
plot(F/pi, m) ;
title('Magnitude Response') ;
grid ;
% PLOTTING THE PHASE RESPONSE
subplot(2,2,3) ;
plot(F/pi, a) ;
title('Phase Response') ;
grid ;
% PLOTTING THE RECTANGULAR WINDOW FOR N = 1024
y1 = rectwin(N1) ;
subplot(2,2,4) ;
plot(y1) ;
title('Rectangular Window for N = 1024') ;
grid ;
% HAMMING WINDOW
y = hamming(N) ;
figure ;
% PLOTTING THE HAMMING WINDOW
subplot(2,2,1) ;
plot(y) ;
title(['Hamming window for N = ', int2str(N)]) ;
grid ;
% DESIGNING THE FILTER
b = fir1(N-1, Wn, 'low', y) ;
[H,F] = freqz(b,1,N) ;
m = 20*log10(abs(H)) ;
a = rad2deg(unwrap(angle(H))) ;
% PLOTTING THE MAGNITUDE RESPONSE
subplot(2,2,2) ;
plot(F/pi, m) ;
title('Magnitude Response') ;
grid ;
% PLOTTING THE PHASE RESPONSE
subplot(2,2,3) ;
plot(F/pi, a) ;
title('Phase Response') ;
grid ;
% PLOTTING THE HAMMING WINDOW FOR N = 1024
y1 = hamming(N1) ;
subplot(2,2,4) ;
plot(y1) ;
title('Hamming Window for N = 1024') ;
grid ;
% KAISER WINDOW
y = kaiser(N, beta) ;
figure ;
% PLOTTING THE KAISER WINDOW
subplot(2,2,1) ;
plot(y) ;
title(['Kaiser Window for N = ', int2str(N)]) ;
grid ;
% DESIGNING THE FILTER
b = fir1(N-1, Wn, 'low', y) ;
[H,F] = freqz(b,1,N) ;
m = 10*log10(abs(H)) ;
a = rad2deg(unwrap(angle(H))) ;
% PLOTTING THE MAGNITUDE RESPONSE
subplot(2,2,2) ;
plot(F/pi, m) ;
title('Magnitude Response') ;
grid ;
% PLOTTING THE PHASE RESPONSE
subplot(2,2,3) ;
plot(F/pi, a) ;
title('Phase Response') ;
grid ;
% PLOTTING THE KAISER WINDOW FOR N = 1024
y1 = kaiser(N1, beta) ;
subplot(2,2,4) ;
plot(y1) ;
title('Kaiser Window for N = 1024') ;
grid ;
% FOURIER SERIES
f = 0.2:0.2:0.8 ;
t = 0:0.1:10 ;
for i = 1:length(f)
for j = 1:length(t)
x(j) = 4 * sin(2*pi*f(i)*t(j)) + (4/3) * sin(2*pi*f(i)*t(j)) + (4/5) * sin(2*pi*f(i)*t(j)) + (4/7)
* sin(2*pi*f(i)*t(j)) ;
end
end
% PLOTTING THE FOURIER SERIES
figure ;
plot(t,x) ;
title('Fourier Series') ;
grid ;
% DESIGNING THE FIR FILTERS
b = fir1(length(x)-1, Wn, 'low', x) ;
[H,F] = freqz(b,1,N) ;
m = 20*log10(abs(H)) ;
a = rad2deg(unwrap(angle(H))) ;
figure ;
% PLOTTING THE MAGNITUDE RESPONSE
subplot(2,1,1) ;
plot(F/pi, m) ;
title('Magnitude Response') ;
grid ;
% PLOTTING THE PHASE RESPONSE
subplot(2,1,2) ;
plot(F/pi, a) ;
title('Phase Response') ;
grid ;
Output:
PRACTICAL NO. 9
Topic: High-pass FIR filter
Problem Statement:
Design an N-point FIR high-pass filter with the cutoff frequency 0.2 * pi using
i. Blackman, ii. Hanning, iii. Kaiser windows. Plot for N =16,32,64,128, 256. Compare with N =
1024 and record your observations. Input the Fourier Series: x(n) = 4*sin(2*pi*f*t) +
(4/3)*sin(2*pi*3*f*t) + (4/5)*sin(2*pi*5*f*t) + (4/7)*sin(2*pi*7*f*t) and obtain the output
when f=0.8, 0.6, 0.4, 0.2 and display. Plot the magnitude and phase response of the filter.
Description:
High-pass Filter: A high-pass filter passes 'high' frequencies fairly well, but attenuates 'low'
frequencies. Therefore it is better called a low-cut filter or bass-cut filter. The term rumble filter
is sometimes used. A high-pass filter is the opposite of a low-pass filter. See also bandpass filter.
Hence it is useful as a filter to block any unwanted low frequency components of a complex
signal whilst passing the higher frequencies. Of course, the meanings of 'low' and 'high'
frequencies are relative, actually desired values would influence choice of component values in
an implementation.
Cut-off Frequency: Frequency at which the power gain of an amplifier falls below 50% of
maximum.
Window Function: Window functions are applied to avoid discontinuities at the beginning and
the end of a set of data. The smaller these discontinuities are, the faster the side slopes drop.
(MATLAB) Functions used:
Function Name Description
BLACKMAN(N) Returns the N-point symmetric Blackman window in a column
vector.
HANNING(N) Returns the N-point symmetric Hanning window in a column
vector. Note: first and last zero-weighted window samples are
not included.
KAISER(N,BETA) Returns the BETA-valued N-point Kaiser window
fir1(n,Wn,'ftype') Specifies a filter type, where 'ftype (e.g. bandpass, stop,
high, low etc.)
FREQZ(B,A,N,Fs) Return frequency vector F (in Hz), where Fs is the sampling
frequency (in Hz) where A and B are numerator and denominator
coefficients in vectors
UNWRAP(P) Unwraps radian phases P by changing absolute jumps greater than
pi to their 2*pi complement.
ANGLE(H) Returns the phase angles, in radians, of a matrix with complex
elements
ABS(X) Returns absolute value of the elements of X.
Source Code:
% HIGH PASS FIR FILTER
clc ;
clear all ;
close all ;
N = input('Please enter the number of samples : ') ;
Wn = 0.2 * pi ;
N1 = 1024 ;
beta = 5.4 ;
% BLACKMAN WINDOW
y = blackman(N) ;
figure ;
% PLOTTING THE BLACKMAN WINDOW
subplot(2,2,1) ;
plot(y) ;
title(['Blackman Window for N = ', int2str(N)]) ;
grid ;
% DESIGNING THE FILTER
b = fir1(N-1, Wn, y) ; % SINCE THE FILTER WILL BE CREATED OF SIZE N SO (N-1)
[H,F] = freqz(b,1,N) ;
m = 20 * log10(abs(H)) ;
a = unwrap(angle(H)) * 180/pi ;
% PLOTTING THE MAGNITUDE RESPONSE
subplot(2,2,2) ;
plot(F/pi, m) ;
title('Magnitude Response for Blackman Window') ;
grid ;
% PLOTTING THE PHASE RESPONSE
subplot(2,2,3) ;
plot(F/pi, a) ;
title('Phase Response for Blackman Window') ;
grid ;
% PLOTTING THE BLACKMAN WINDOW FOR N = 1024
y1 = blackman(N1) ;
subplot(2,2,4) ;
plot(y1) ;
title('Blackman Window for N=1024') ;
grid ;
% HANNING WINDOW
y = hanning(N) ;
figure ;
% PLOTTING THE HANNING WINDOW
subplot(2,2,1) ;
plot(y) ;
title(['Hanning Window for N = ', int2str(N)]) ;
grid ;
% DESIGNING THE FILTER
b = fir1(N-1, Wn, y) ;
[H,F] = freqz(b,1,N) ;
m = 20 * log10(abs(H)) ;
a = unwrap(angle(H)) * 180/pi ;
% PLOTTING THE MAGNITUDE RESPONSE
subplot(2,2,2) ;
plot(F/pi, m) ;
title('Magnitude Response for Hanning Window') ;
grid;
% PLOTTING THE PHASE RESPONSE
subplot(2,2,3) ;
plot(F/pi,a) ;
title('Phase Response for Hanning Window') ;
grid ;
% PLOTTING THE HANNING WINDOW FOR N=1024
y1 = hanning(N1) ;
subplot(2,2,4) ;
plot(y1) ;
title('Hanning Window for N = 1024') ;
grid ;
% KAISER WINDOW
y = kaiser(N,beta) ;
figure ;
% PLOTTING THE KAISER WINDOW
subplot(2,2,1) ;
plot(y) ;
title(['Kaiser Window for N = ' , int2str(N)]) ;
grid ;
% DESIGNING FIR FILTER
b = fir1(N-1,Wn,y) ;
[H,F] = freqz(b,1,N) ;
m = 20 * log10(abs(H)) ;
a = unwrap(angle(H)) * 180/pi ;
% PLOTTING MAGNITUDE RESPONSE
subplot(2,2,2) ;
plot(F/pi,m) ;
title('Magnitude Response for Kaiser Window') ;
grid ;
% PLOTTING PHASE RESPONSE
subplot(2,2,3) ;
plot(F/pi,a) ;
title('Phase Response for Kaiser Window') ;
grid ;
% PLOTTING KAISER WINDOW FOR N = 1024
y1 = kaiser(N1,beta) ;
subplot(2,2,4) ;
plot(y1) ;
title('Kaiser Window for N = 1024') ;
grid ;
% FOURIER SERIES
f = 0.2:0.2:0.8 ;
t = 0:0.1:10 ;
for i = 1:length(f)
for j = 1:length(t)
x(j) = 4*sin(2*pi*f(i)*t(j)) + (4/3)*sin(2*pi*f(i)*t(j)) + (4/5)*sin(2*pi*f(i)*t(i)) +
(4/7)*sin(2*pi*f(i)*t(i)) ;
end
end
% PLOTTING THE FOURIER SERIES
figure ;
plot(t,x) ;
title('Fourier Series') ;
grid ;
% DESIGNING THE FIR FILTER
b = fir1(length(x)-1, Wn, 'high', x) ;
[H,F] = freqz(b,1,N) ;
m = 20 * log10(abs(H)) ;
a = unwrap(angle(H)) ;
% PLOTTING THE MAGNITUDE RESPONSE
figure ;
subplot(2,1,1) ;
plot(F/pi,m) ;
title('Magnitude Response for Fourier Series') ;
grid ;
% PLOTTING THE PHASE RESPONSE
subplot(2,1,2) ;
plot(F/pi,a) ;
title('Phase Response for Fourier Series') ;
grid ;
Output:
PRACTICAL NO. 10
Topic: High-pass and Low-pass FIR filters on various inputs
Problem Statement:
Design low-pass and high-pass filters for the following input and find the output of the filter.
Plot the input and output.
a. sn = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
b. s(n) = 3 * pow(e,-0.1 * n)
c. s(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
Description:
Low-pass filter: Low-pass filters are used to block unwanted high-frequency signals, whilst
passing the lower frequencies. The low frequencies to be filtered out are relative to the unwanted
higher frequencies and therefore do not have a definitive range. The frequencies that are cut vary
from filter to filter. A low-pass filter is the opposite of a high-pass filter.
High-pass Filter: A high-pass filter passes 'high' frequencies fairly well, but attenuates 'low'
frequencies. Therefore it is better called a low-cut filter or bass-cut filter. The term rumble filter
is sometimes used. A high-pass filter is the opposite of a low-pass filter. See also bandpass filter.
Hence it is useful as a filter to block any unwanted low frequency components of a complex
signal whilst passing the higher frequencies. Of course, the meanings of 'low' and 'high'
frequencies are relative, actually desired values would influence choice of component values in
an implementation.
Cut-off Frequency: Frequency at which the power gain of an amplifier falls below 50% of
maximum.
(MATLAB) Functions used:
Function Name Description
fir1(n,Wn,'ftype') Specifies a filter type, where 'ftype (e.g. bandpass, stop,
high, low etc.)
FREQZ(B,A,N,Fs) Return frequency vector F (in Hz), where Fs is the sampling
frequency (in Hz) where A and B are numerator and denominator
coefficients in vectors
Source Code:
% HIGH PASS FILTER & LOW PASS FILTERS ON VARIOUS INPUTS
clc ;
clear all ;
close all ;
f = 0.2:0.2:0.8 ;
t = 1:0.1:10 ;
Wn = 3/5 ;
N = 256 ;
for n=1:length(t)
sn1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n) ;
sn2(n) = 3 * exp(-0.1 * n) ;
sn3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n) ;
end ;
% DESIGNING A LOW PASS FILTER FOR sn1
figure ;
b = fir1(length(sn1)-1, Wn, 'low', sn1) ;
grid ;
freqz(b,1,N) ;
title('Low Pass Filter for sn = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)') ;
% DESIGNING A HIGH PASS FILTER FOR sn1
figure ;
b = fir1(length(sn1)-1, Wn, 'high', sn1) ;
grid ;
freqz(b,1,N) ;
title('High Pass Filter for sn = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)') ;
% DESIGNING A LOW PASS FILTER FOR sn2
figure ;
b = fir1(length(sn2)-1, Wn, 'low', sn2) ;
grid ;
freqz(b,1,N) ;
title('Low Pass Filter for sn = 3 * pow(e,-0.1 * n)') ;
% DESIGNING A HIGH PASS FILTER FOR sn2
figure ;
b = fir1(length(sn2)-1, Wn, 'high', sn2) ;
grid ;
freqz(b,1,N) ;
title('High Pass Filter for sn = 3 * pow(e,-0.1 * n)') ;
% DESIGNING A LOW PASS FILTER FOR sn3
figure ;
b = fir1(length(sn3)-1, Wn, 'low', sn3) ;
grid ;
freqz(b,1,N) ;
title('Low Pass Filter for sn = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)') ;
% DESIGNING A HIGH PASS FILTER FOR sn3
figure ;
b = fir1(length(sn3)-1, Wn, 'high', sn3) ;
grid ;
freqz(b,1,N) ;
title('High Pass Filter for sn = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)') ;
Output:
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-6000
-4000
-2000
0
Normalized Frequency ( rad/sample)
P
h
a
s
e

(
d
e
g
r
e
e
s
)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-100
-50
0
50
Normalized Frequency ( rad/sample)
M
a
g
n
i
t
u
d
e

(
d
B
)
Low Pass Filter for x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-4000
-3000
-2000
-1000
0
1000
Normalized Frequency ( rad/sample)
P
h
a
s
e

(
d
e
g
r
e
e
s
)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-150
-100
-50
0
50
Normalized Frequency ( rad/sample)
M
a
g
n
i
t
u
d
e

(
d
B
)
High Pass Filter for x1(n) = 3.5 * sin(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-6000
-4000
-2000
0
Normalized Frequency ( rad/sample)
P
h
a
s
e

(
d
e
g
r
e
e
s
)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-100
-50
0
50
Normalized Frequency ( rad/sample)
M
a
g
n
i
t
u
d
e

(
d
B
)
Low Pass Filter for x2(n) = 3 * exp(-0.1 * n)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-4000
-3000
-2000
-1000
0
1000
Normalized Frequency ( rad/sample)
P
h
a
s
e

(
d
e
g
r
e
e
s
)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-150
-100
-50
0
50
Normalized Frequency ( rad/sample)
M
a
g
n
i
t
u
d
e

(
d
B
)
High Pass Filter for x2(n) = 3 * exp(-0.1 * n)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-6000
-4000
-2000
0
Normalized Frequency ( rad/sample)
P
h
a
s
e

(
d
e
g
r
e
e
s
)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-100
-50
0
50
Normalized Frequency ( rad/sample)
M
a
g
n
i
t
u
d
e

(
d
B
)
Low Pass Filter for x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-4000
-3000
-2000
-1000
0
1000
Normalized Frequency ( rad/sample)
P
h
a
s
e

(
d
e
g
r
e
e
s
)
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
-150
-100
-50
0
50
Normalized Frequency ( rad/sample)
M
a
g
n
i
t
u
d
e

(
d
B
)
High Pass Filter for x3(n) = 2 * cos(2 * pi * (0.15) * n) + sin(2 * pi * (0.4) * n)
PRACTICAL NO. 11
Topic: Band-pass and Band-stop FIR filters
Problem Statement:
Design a Band-Pass and Band-Stop FIR filter using Chebyshev window to meet the following
specification.
Passband frequency:- 900 1100 Hz
Passband ripple: - < 0.87 dB
Stopband attenuation: - > 30 dB
Sampling frequency: - 15 kHz
Stopband frequency: - 4500 Hz
Plot the filter spectrum. Magnitude and Phase response.
Description:
Band-pass filter: A tuned circuit designed to pass a band of frequencies between a lower cut-off
frequency (f1) and a higher cut-off frequency (f2). Frequencies above and below the pass band
are heavily attenuated.
Band-stop filter: A tuned circuit designed to stop frequencies between a lower cut-off frequency
(f
1
) and a higher cut-off frequency (f
2
) of the amplifier while passing all other frequencies.
(MATLAB) Functions used:
Function Name Description
fir1(n,Wn,'ftype') Specifies a filter type, where 'ftype (e.g. bandpass, stop,
high, low etc.)
FREQZ(B,A,N,Fs) Return frequency vector F (in Hz), where Fs is the samplin
frequency (in Hz) where A and B are numerator and denominator
coefficients in vectors
Source Code:
% BAND-PASS & BAND-STOP FILTERS
clc ;
clear all ;
close all ;
% BAND-PASS FILTERS
Rp = 0.3 ; % PASS-BAND RIPPLE
Rs = 40 ; % STOP-BAND RIPPLE
Fp = 1100 ; % PASS-BAND FREQUENCY
Fs = 4500 ; % STOP-BAND FREQUENCY
F = 15000 ; % SAMPLING FREQUENCY
Wp = 2 * Fp / F ;
Ws = 2 * Fs / F ;
% DESIGNING THE CHEBYSHEV FILTER
[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs,'s') ;
Wn = [Wp,Ws] ; % THIS IS USED SINCE Wn MUST HAVE 2 ELEMENTS IN THE
CHEBY1 FUNCTION
[b,a] = cheby1(n,Rp,Wn,'bandpass','s') ;
w = 0:0.01:pi ;
[H,F] = freqs(b,a,w) ;
m = 20*log10(abs(H)) ;
a = angle(H) ;
figure ;
% MAGNITUDE RESPONSE
subplot(2,1,1) ;
plot(F/pi, m) ;
xlabel('Normalized Frequency') ;
ylabel('Gain in db') ;
title('Magnitude Response for the Bandpass Filter') ;
% PHASE RESPONSE
subplot(2,1,2) ;
plot(F/pi,a) ;
xlabel('Normalized Frequency') ;
ylabel('Phase in Radians') ;
title('Phase Response for the Bandpass Filter') ;
% BAND-STOP FILTERS
Rp = 0.15 ; % PASS-BAND RIPPLE
Rs = 30 ; % STOP-BAND RIPPLE
Fp = 2000 ; % PASS-BAND FREQUENCY
Fs = 2400 ; % STOP-BAND FREQUENCY
F = 7000 ; % SAMPLING FREQUENCY
% DESIGNING THE CHEBYSHEV FILTER
[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs,'s') ;
Wn = [Wp,Ws] ;
[b,a] = cheby1(n,Rp,Wn,'stop','s') ;
w = 0:0.01:pi ;
[H,F] = freqs(b,a,w) ;
m = 20*log10(abs(H)) ;
a = angle(H) ;
figure ;
% MAGNITUDE RESPONSE
subplot(2,1,1) ;
plot(F/pi,m) ;
xlabel('Normalized Frequency') ;
ylabel('Gain in dB') ;
title('Magnitude Response of a Band-stop Filter') ;
% PHASE RESPONSE
subplot(2,1,2) ;
plot(F/pi,a) ;
xlabel('Normalized Frequency') ;
ylabel('Phase in Radians') ;
title('Phase Response of a Band-Stop Filter') ;
Output:
PRACTICAL NO. 12
Topic: Digital IIR Filters
Problem Statement:
Design Buttorworth low-pass, high-pass, Band-pass and Band-stop digital IIR filters. Create
pass-band ripple, stop-band ripple, pass-band frequency, stop-band frequency and sampling
frequency as part of design using the specifications in 11 as reference. Plot the magnitude and
phase response of the filters. Test the filter by creating suitable inputs.
Description:
Butterworth filter: A type of active filter characterized by a constant gain (flat response) across
the mid-band of the circuit and a 20 dB per decade roll-off rate for each pole contained in the
circuit.
(MATLAB) Functions used:
Function Name Description
BUTTER(N,Wn,'high') Designs a high pass filter.
BUTTER(N,Wn,'low') Designs a low pass filter.
BUTTER(N,Wn,'bandpass') Designs a band-pass filter.
BUTTER(N,Wn,'stop') Designs a band-stop filter if Wn = [W1 W2].
FREQZ(B,A,N,Fs) Return frequency vector F (in Hz), where Fs is the sampling
frequency (in Hz) where A and B are numerator and denominator
coefficients in vectors
Source Code:
% BUTTERWORTH FILTERS
clc ;
clear all ;
close all ;
% LOW-PASS FILTER
figure ;
Rp = 0.5 ;
Rs = 50 ;
Fp = 1200 ;
Fs = 2400 ;
F = 10000 ;
Wp = 2 * Fp / F ;
Ws = 2 * Fs / F ;
% DESIGNING THE BUTTERWORTH FILTER
[n,Wn] = buttord(Wp,Ws,Rp,Rs) ;
[b,a] = butter(n,Wn,'low') ;
w = 0:0.01:pi ;
[H,F] = freqz(b,a,w) ;
m = 20 * log10(abs(H)) ;
a = angle(H) ;
% MAGNITUDE RESPONSE
subplot(2,1,1) ;
plot(F/pi,m) ;
xlabel('Normalized Frequency') ;
ylabel('Gain in dB') ;
title('Low-Pass Filter') ;
% PHASE RESPONSE
subplot(2,1,2) ;
plot(F/pi,a) ;
xlabel('Normalized Frequency') ;
ylabel('Phase in Radians') ;
% HIGH-PASS FILTER
figure ;
Rp = 0.5 ;
Rs = 50 ;
Fp = 1200 ;
Fs = 2400 ;
F = 10000 ;
Wp = 2 * Fp / F ;
Ws = 2 * Fs / F ;
% DESIGNING THE BUTTERWORTH FILTER
[n,Wn] = buttord(Wp,Ws,Rp,Rs) ;
[b,a] = butter(n,Wn,'high') ;
w = 0:0.01:pi ;
[H,F] = freqz(b,a,w) ;
m = 20 * log10(abs(H)) ;
a = angle(H) ;
% MAGNITUDE RESPONSE
subplot(2,1,1) ;
plot(F/pi,m) ;
xlabel('Normalized Frequency') ;
ylabel('Gain in dB') ;
title('High-Pass Filter') ;
% PHASE RESPONSE
subplot(2,1,2) ;
plot(F/pi,a) ;
xlabel('Normalized Frequency') ;
ylabel('Phase in Radians') ;
% BAND-PASS FILTER
figure ;
Rp = 0.3 ;
Rs = 40 ;
Fp = 1500 ;
Fs = 2000 ;
F = 9000 ;
Wp = 2 * Fp / F ;
Ws = 2 * Fs / F ;
% DESIGNING THE BUTTERWORTH FILTER
[n,Wn] = buttord(Wp,Ws,Rp,Rs) ;
Wn = [Wp,Ws] ;
[b,a] = butter(n,Wn,'bandpass') ;
w = 0:0.01:pi ;
[H,F] = freqz(b,a,w) ;
m = 20 * log10(abs(H)) ;
a = angle(H) ;
% MAGNITUDE RESPONSE
subplot(2,1,1) ;
plot(F/pi,m) ;
xlabel('Normalized Frequency') ;
ylabel('Gain in dB') ;
title('Band-Pass Filter') ;
% PHASE RESPONSE
subplot(2,1,2) ;
plot(F/pi,a) ;
xlabel('Normalized Frequency') ;
ylabel('Phase in Radians') ;
% BAND-STOP FILTER
figure ;
Rp = 0.4 ;
Rs = 46 ;
Fp = 1100 ;
Fs = 2200 ;
F = 6000 ;
Wp = 2 * Fp / F ;
Ws = 2 * Fs / F ;
% DESIGNING THE BUTTERWORTH FILTER
[n,Wn] = buttord(Wp,Ws,Rp,Rs) ;
Wn = [Wp,Ws] ;
[b,a] = butter(n,Wn,'stop') ;
w = 0:0.01:pi ;
[H,F] = freqz(b,a,w) ;
m = 20 * log10(abs(H)) ;
a = angle(H) ;
% MAGNITUDE RESPONSE
subplot(2,1,1) ;
plot(F/pi,m) ;
xlabel('Normalized Frequency') ;
ylabel('Gain in dB') ;
title('Band-Stop Filter') ;
% PHASE RESPONSE
subplot(2,1,2) ;
plot(F/pi,a) ;
xlabel('Normalized Frequency') ;
ylabel('Phase in Radians') ;
Output:
PRACTICAL NO. 13
Topic: Radix 2 DIF FFT Algorithm
Problem Statement:
Write a program in MATLAB to implement Radix2 Decimation In Frequency (DIF) FFT
Algorithm.
Description:
By adopting a divide and conquer approach computationally efficient algorithm for the
DFT can be developed. This approach depends on the decomposition of an N-point DFT into
successively smaller size DFTs. FFT decomposes DFT by recursively splitting the sequence
element X(k) in the Frequency domain into set of smaller and smaller sub-sequences.
Decimation In Frequency (DIF) algorithm: To derive Decimation in Frequency FFT algorithm
for N a power of 2, the input sequence x(n) Is divided into the first half and the last half of the
points as discussed below,
(N/2)-1
X(k)= [x(n)+(-1)
k
x(n+N/2)]W
N
nk
, 0<=k<=N-1
n=0
(MATLAB) Functions used:
Function Name Description
Y = fft(X) Returns the Discrete Fourier Transform (DFT) of vector X
computed with a Fast Fourier Transform (FFT) algorithm.
Source Code:
% RADIX-2 DIF FFT ALGORITHM
clc ;
clear all ;
close all ;
format short ;
N = input('Please enter the length of the DFT : ') ;
for i = 1:N+1
if N == 2^i
x = input('Please enter the input sequence : ') ;
Z = fft(x,N) ;
disp('FFT Sequence') ;
disp(Z) ;
subplot(2,1,1) ;
stem(x) ;
xlabel('N') ;
ylabel('Amplitude') ;
title('Input Sequence') ;
subplot(2,1,2) ;
stem(Z) ;
xlabel('N') ;
ylabel('Amplitude') ;
title('FFT of the Input Sequence') ;
break ;
elseif N > 2^i
else
disp('Not a proper number') ;
break ;
end ;
end ;
Output:
Enter Length Of FFT : - 8
Input The Sequence [1 2 3 4 5 6 7 8]
FFT Sequence
Columns 1 through 6
36.0000 -4.0000 + 9.6569i -4.0000 + 4.0000i -4.0000 + 1.6569i -4.0000
-4.0000 - 1.6569i
Columns 7 through 8
-4.0000 - 4.0000i -4.0000 - 9.6569i
1 2 3 4 5 6 7 8
0
2
4
6
8
n---->
A
m
p
l
i
t
u
d
e
-
-
-
-
>
Original Signal
-5 0 5 10 15 20 25 30 35 40
-10
-5
0
5
10
n---->
A
m
p
l
i
t
u
d
e
-
-
-
-
>
FFT Signal
PRACTICAL NO. 14
Topic: Radix 2 DIT FFT Algorithm
Problem Statement:
Write a program in MATLAB to implement Radix2 Decimation In Time (DIT) FFT
Algorithm.
Description:
By adopting a divide and conquer approach computationally efficient algorithm for the
DFT can be developed. This approach depends on the decomposition of an N-point DFT into
successively smaller size DFTs. FFT decomposes DFT by recursively splitting the sequence
element X(k) in the Time domain into set of smaller and smaller sub-sequences..
Decimation In Time (DIT) algorithm: In this case, let us assume that x(n) represents a
sequence of N values where N is an integer power of 2, i.e. N=2
L
. The given sequence is
broken down into N/2-point sequences consisting of the even and odd numbered values of x(n)
is given by
N-1
nk
X(k)= x(n) W
N
, 0<=k<=N-1
n=0
k+N/2 K
Using the symmetry of W
N
=
-W
N,
k
G(k)+W
N
H(k)
,
0<= k <= N/2-1
X(k)=
(k+N/2)
G( k + N/2) + W
N
H(k+ N/2), N/2<=k <= N-1
(MATLAB) Functions used:
Function Name Description
Y = fft(X) Returns the Discrete Fourier Transform (DFT) of vector X
computed with a Fast Fourier Transform (FFT) algorithm.
Source Code:
% RADIX-2 DIT FFT ALGORITHM
clc ;
clear all ;
close all ;
format short ;
N = input('Please enter the length of the input sequence : ') ;
for i = 1:N+1
if N == 2^i
x = input('Please enter the input sequence : ') ;
y = bitrevorder(x) ;
Z = fft(y,N) ;
disp('Input Sequence After Reversal') ;
disp(y) ;
disp('FFT Sequence') ;
disp(Z) ;
subplot(2,1,1) ;
stem(x) ;
xlabel('N') ;
ylabel('Amplitude') ;
title('Input Sequence') ;
subplot(2,1,2) ;
stem(Z) ;
xlabel('N') ;
ylabel('Amplitude') ;
title('FFT of the Input Sequence') ;
break ;
elseif N > 2^i
else
disp('Not a proper number') ;
break ;
end ;
end ;
Output:
Enter Length Of FFT : - 8
Input The Sequence [1 2 3 4 5 6 7 8]
1 5 3 7 2 6 4 8
FFT Sequence
Columns 1 through 6
36.0000 -4.0000 + 9.6569i -4.0000 + 4.0000i -4.0000 + 1.6569i -4.0000 -4.0000
- 1.6569i
Columns 7 through 8
-4.0000 - 4.0000i -4.0000 - 9.6569i
1 2 3 4 5 6 7 8
0
2
4
6
8
n---->
A
m
p
l
i
t
u
d
e
-
-
-
-
>
Original Signal
-5 0 5 10 15 20 25 30 35 40
-10
-5
0
5
10
n---->
A
m
p
l
i
t
u
d
e
-
-
-
-
>
FFT Signal
PRACTICAL NO. 15
Topic: Power Spectral Density
Problem Statement:
Plot the spectrogram of the signal x = cos(2*pi*f *t) + randn(size(t)) and analyze the spectrum
for f = 200, 2000, 20000
Description:
Power spectral density (PSD) of a given bandwidth of electromagnetic radiation is the
total power in this bandwidth divided by the specified bandwidth.
Spectral density is usually expressed in watts per hertz (W/Hz).
Note that the total energy in the frequency domain equals the total energy in the time
domain:
This is a result of Parseval's theorem.
(MATLAB) Functions used:
Function Name Description
randn(size(A)) Returns an array of random entries that is the same size as A
[Pxx,w] = periodogram(x) returns the power spectral density (PSD) estimate Pxx of the
sequence x using a periodogram. The power spectral density is
calculated in units of power per radians per sample. The
corresponding vector of frequencies w is computed in radians per
sample, and has the same length as Pxx.
Source Code:
% POWER SPECTAL DENSITY
clc ;
clear all ;
close all ;
t = 0:0.001:0.3 ;
% TRY FOR THE SAMPLING FREQUENCIES F = 200, 2000, 20000
F = input('Please enter the sampling frequency : ') ;
x = cos(2*pi*F*t) + randn(size(t)) ;
[Pxx,w] = periodogram(x,[],'two-sided',512,F) ;
psdplot(Pxx,w,'','',['Power Spectral Density for F = ', int2str(F)]) ;
%title(['Power Spectral Density for F = ', int2str(F)]) ;
Output:
PRACTICAL NO. 16
Topic: Remez Exchange Algorithm
Problem Statement:
Design a linear-phase FIR filter using the remez algorithm implemented as remez(n,f,a) where n
is the total no. of coefficients, f is the normalized frequency and a is the desired amplitude
response. Test on the input: f = [0 0.3 0.4 0.6 0.7 1] , a = [0 0 1 1 0 0], n = 17.
Description:
The Remez exchange algorithm is one suitable method for designing quite good filters
semi-automatically.
Remez exchange algorithm, is an application of the Chebyshev alternation theorem that
constructs the polynomial of best approximation to certain functions under a number of
conditions. The Remez algorithm in effect goes a step beyond the minimax
approximation algorithm to give a slightly finer solution to an approximation problem.
(MATLAB) Functions used:
Function Name Description
remez(n,f,a) Returns row vector b containing the n+1 coefficients of
order n FIR filter whose frequency-amplitude characteristics
match those given by vectors f and a.
freqz(Hq,n,'whole',fs) Uses n points around the entire unit circle to calculate the
frequency response. Frequency vector f has length n and has
values ranging from 0 to fs Hz.
Source Code:
% REMEZ EXCHANGE ALGORITHM
clc ;
clear all ;
close all ;
% GIVEN
f = [0, 0.3, 0.4, 0.6, 0.7, 1] ;
a = [0, 0, 1, 1, 0, 0] ;
n = 17 ;
% REMEZ FILTER
b = remez(n,f,a) ;
[H,F] = freqz(b,1,512) ;
figure ;
plot(f,a,F/pi,abs(H)) ;
title('Remez FIR Filter') ;
xlabel('Amplitude (a)') ;
ylabel('Frequency (f)') ;
legend('Ideal', 'Remez Design') ;
% REMEZ DESIGN USING HILBERT TRANSFORM
b = remez(n,f,a,'h') ;
[H,F] = freqz(b,1,512) ;
figure ;
plot(f,a,F/pi,abs(H)) ;
title('Remez FIR Filter') ;
xlabel('Amplitude (a)') ;
ylabel('Frequency (f)') ;
legend('Ideal', 'Remez Design') ;
% REMEZ DESIGN USING DIFFERENTIATOR
b = remez(n,f,a,'d') ;
[H,F] = freqz(b,1,512) ;
figure ;
plot(f,a,F/pi,abs(H)) ;
title('Remez FIR Filter') ;
xlabel('Amplitude (a)') ;
ylabel('Frequency (f)') ;
legend('Ideal', 'Remez Design') ;
Output:
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
0.2
0.4
0.6
0.8
1
1.2
1.4
REMEZ FIR FILTER
f
a
Ideal
Remez Design
PRACTICAL NO. 17
Topic: Two-Dimensional Linear Convolution
Problem Statement:
Write a program for determining the Two-dimensional Linear Convolution of a finite duration
sequence x(n1,n2) and h(n1,n2). Accept the sequences x(n1,n2) and h(n1,n2) from the user.
display the output sequence y(n1,n2). Plot all the three sequences. Test on suitable input
sequences. For MATLAB use the random input sequences.
Description:
2-D Convolution
An arbitrary 2-D sequence can be decomposed into a linear combination of shifted
impulses.



1 2
) , ( ) , ( ) , (
2 2 1 1 2 1 2 1
k k
k n k n k k x n n x
Applying a linear shift-invariant system T that operates on spatial indices (n
1
, n
2
) to
the input signal x(n
1
, n
2
)
1
]
1


1 2
) , ( ) , ( ) , (
2 2 1 1 2 1 2 1
k k
k n k n k k x T n n y
Applying addivity,
, |



1 2
) , ( ) , ( ) , (
2 2 1 1 2 1 2 1
k k
k n k n k k x T n n y
Applying homogeneity with respect to (n
1
, n
2
),
, |



1 2
) , ( ) , ( ) , (
2 2 1 1 2 1 2 1
k k
k n k n T k k x n n y
The term h(n
1
, n
2
) = T[(n
1
, n
2
) ] is the impulse response of the system.
A linear shift-invariant system is uniquely characterized by its impulse
response.
Substituting h(n
1
, n
2
), we obtain the two-dimensional linear convolution
formula:



1 2
) , ( ) , ( ) , (
2 2 1 1 2 1 2 1
k k
k n k n h k k x n n y
Two-dimensional linear convolution denoted with two asterisks:
h x y
Conceptually: same as 1-D
Mechanically: a lot more work
(MATLAB) Functions used:
Function Name Description
rand(n) Returns an n-by-n matrix of random entries.
conv2(A,B) Computes the two-dimensional convolution of matrices A and
B.
Source Code:
% TWO-DIMENSIONAL CONVOLUTION
clc ;
clear all ;
close all ;
% GENERATING THE SEQUENCES
x = rand(4) ;
h = rand(3) ;
y = conv2(x,h) ;
% PLOTTING THE SEQUENCES
figure ;
subplot(3,1,1) ;
stem(x) ;
title('Input Sequence X(n)') ;
subplot(3,1,2) ;
stem(h) ;
title('Impulse Response H(n)') ;
subplot(3,1,3) ;
stem(y) ;
title('Output Sequence Y(n)') ;
Output:
1 1.5 2 2.5 3 3.5 4
0
0.5
1
Input Sequence x(n)
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
0
0.5
1
Input Sequence h(n)
1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6
0
1
2
3
Output Sequence y(n)
PRACTICAL NO. 18
Topic: Two-Dimensional Cross-correlation and auto-correlation
Problem Statement:
Write a program to compute cross-correlation and auto-correlation of
i. 1-dimensional sequence ii. 2-dimensional sequence. Test your program on suitable inputs.
Description:
Autocorrelation is a mathematical tool used frequently in signal processing for
analysing functions or series of values, such as time domain signals. It is the cross-
correlation of a signal with itself. Autocorrelation is useful for finding repeating patterns
in a signal, such as determining the presence of a periodic signal which has been buried
under noise, or identifying the fundamental frequency of a signal which doesn't actually
contain that frequency component, but implies it with many harmonic frequencies.
Cross correlation is generally used when measuring information between two different
time series. The range of the data is -1 to 1 such that the closer the cross-correlation value
is to 1, the more closely the information sets are.
(MATLAB) Functions used:
Function Name Description
xcorr(x) Returns autocorrelation sequence for the vector x.
xcorr2(x,y) Returns the cross-correlation of matrices x and y with no
scaling.
Source Code:
% TWO-DIMENSIONAL CROSS-CORRELATION & AUTO-CORELATION
clc ;
clear all ;
close all ;
% AUTOCORELATION OF A ONE-DIMENSIONAL SEQUENCE
figure ;
x = [1 2 3 4] ;
y = xcorr(x) ;
subplot(2,1,1) ;
stem(x) ;
xlabel('x(n)') ;
title('Autocorelation of 1-D sequence X(n)') ;
subplot(2,1,2) ;
stem(y) ;
xlabel('y(n)') ;
% CROSS-CORELATION OF A ONE-DIMENSIONAL SEQUENCE
figure ;
x1 = [1 2 3 4] ;
x2 = [1 0 0 1 2 3 0] ;
y = xcorr(x1,x2) ;
subplot(3,1,1) ;
stem(x1) ;
title('Cross-Corelation of a 1D Sequence') ;
xlabel('x1(n)') ;
subplot(3,1,2) ;
stem(x2) ;
xlabel('x2(n)') ;
subplot(3,1,3) ;
stem(y) ;
xlabel('y(n)') ;
% AUTO-CORELATION OF A TWO-DIMENSIONAL SEQUENCE
figure ;
x = [1 2 3 4 ; 0 1 3 2 ; 4 3 2 1 ; 2 0 2 1] ;
y = xcorr2(x) ;
subplot(2,1,1) ;
stem(x) ;
xlabel('x(n)') ;
title('Auto-Corelation of a 2D Sequence') ;
subplot(2,1,2) ;
stem(y) ;
xlabel('y(n)') ;
% CROSS-CORELATION OF A TWO-DIMENSIONAL SEQUENCE
figure ;
x1 = [1 2 3 4 ; 0 1 3 2 ; 4 3 2 1 ; 2 0 2 1] ;
x2 = [1 2 3 4 5 ; 0 1 3 4 6 ; 2 4 5 4 4 ; 0 2 3 4 5] ;
y = xcorr2(x1,x2) ;
subplot(3,1,1) ;
stem(x1) ;
xlabel('x1(n)') ;
title('Cross-Corelation of a 2D Sequence') ;
subplot(3,1,2) ;
stem(x2) ;
xlabel('x2') ;
subplot(3,1,3) ;
stem(y) ;
xlabel('y(n)') ;
% AUTO-CORELATION USING AUTOCORR FUNCTION
figure ;
x = [1 2 3 4 5] ;
y = filter([1 -1 1],1,x) ;
[ACF,Lags,Bounds] = autocorr(y,[],2) ; % COMPUTE THE ACF
subplot(2,1,1) ;
stem(x) ;
xlabel('x(n)') ;
title('Auto-Corelation of x(n) using AutoCorelation function') ;
subplot(2,1,2) ;
stem([ACF,Lags,Bounds]) ;
xlabel('y(n)') ;
Output:
1 1.5 2 2.5 3 3.5 4
0
1
2
3
4
Sequence x-------
1 2 3 4 5 6 7 8 9 10
-2
-1
0
1
2
3
--------Auto-Correlation----------
1 2 3 4
0
1
2
3
4
x(n)
0 2 4 6 8
0
10
20
30
y(n)
1 2 3 4
0
2
4
6
8
x(n1,n2)
0 2 4 6 8
0
50
100
150
200
250
y(n1,n2)
PRACTICAL NO. 19
Topic: Stability Check
Problem Statement:
Write a program to determine whether a given system is stable as per Bounded Input Bounded
Output (BIBO) criteria. Input system coefficients and print the result.
Description:
BIBO stands for Bounded Input/Bounded Output. If a system is BIBO stable (e.g., a low-pass
filter) then the output will be bounded provided that the input to the system is bounded.
(MATLAB) Functions used:
Function Name Description
ALL(X) Operates on the columns of X, returning a row vector of 1's and
0's.
ABS(X) Returns the absolute value of the elements of X
POLY2RC(A) Returns the reflection coefficients, K, based on the prediction
polynomial, A.
Source Code:
% TO DETERMINE WHETHER A GIVEN SYSTEM IS STABLE AS PER BIBO CRITERIA
clc ;
clear all ;
close all ;
a = [1.000, -0.6149, 0.9899, 0, 0.0031, -0.0082] ;
y = poly2rc(a) ;
x = abs(poly2rc(a)) < 1 ;
stable = all(abs(poly2rc(a)) < 1) ;
if stable ~= 1
disp('The system is stable') ;
else
disp('The system is not stable') ;
end ;
al = [1 1 1 0 0 0.2] ;
y = poly2rc(al) ;
x = abs(poly2rc(al)) < 1 ;
stable = all(abs(poly2rc(al)) < 1) ;
if stable ~= 1
disp('The system is stable') ;
else
disp('The system is not stable') ;
end ;
Output:
a =
1.0000 -0.6149 0.9899 0 0.0031 -0.0082
stable =
1
The system is not stable
a1 =
1 1 0 0 0 0
stable =
0
The system is stable
PRACTICAL NO. 20
Topic: Bit-reversal algorithm
Problem Statement:
Write a C/C++ program to implement the bit-reversal algorithm. Using MATLAB use
bitrevorder function. Output a table with headings
[Linear Index] [Bits] [Bit-Reversed] [Bit-Reversed Index]
Source Code:
// BIT REVERSAL ALGORITHM
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define N 3
#define total pow(2,N)
int binary[8][3], revBinary[8][3], bitRev[8] ;
//int total = pow(2, N) ;
void toBinary() ;
void toReversedBinary() ;
void toDecimal() ;
void main()
{
int i , j ;
clrscr() ;
/*printf("Please enter the number of bits to represent : ") ;
scanf("%d", &N) ;*/
toBinary() ;
toReversedBinary() ;
toDecimal() ;
printf("**********BIT REVERSAL ALGORITHM***********\n\n") ;
printf("\nLINEAR INDEX \t BITS \t BIT REVERSED \t BIT REVERSED INDEX\n") ;
printf("--------------------------------------------------------------------------------\n") ;
for(i=0 ; i < total ; i++)
{
printf("\n\t %d\t\t " , i) ;
for(j=0 ; j < N ; j++)
{
printf("%d", binary[i][j]) ;
}
printf("\t\t") ;
for(j=0 ; j < N ; j++)
printf("%d", revBinary[i][j]) ;
printf("\t\t") ;
printf("%d", bitRev[i]) ;
}
getch() ;
}
// FUNCTION TO CONVERT TO BINARY
void toBinary()
{
int i , j , num , rem ;
for(i=0 ; i < total ; i++)
{
num = i ;
printf("\nnum = %d", num) ;
j=0 ;
while(num != 0)
{
rem = num % 2 ;
binary[i][3-(j+1)] = rem ;
num = num / 2;
j++ ;
}
}
return ;
}
// FUNCTION TO REVERSE THE BINARY NUMBER
void toReversedBinary()
{
int i , j , k ;
for(i=0 ; i < total ; i++)
{
for(j=0 ; j < N ; j++)
{
revBinary[i][j] = binary[i][3-j-1] ;
}
}
return ;
}
// FUNCTION TO CONVERT TO DECIMAL
void toDecimal()
{
int i , j , k , sum ;
for(i=0 ; i < total ; i++)
{
sum = 0 ;
k = 0 ;
for(j=N-1 ; j >= 0 ; j--)
{
sum += (revBinary[i][j] * pow(2,k)) ;
k++ ;
}
bitRev[i] = sum ;
}
return ;
}
Output:
****************BIT REVERSAL ALGORITHM*****************
LINEAR INDEX BITS BIT REVERSED BIT REVERSED INDEX
------------------------------------------------------------------------------------------
0 000 000 0
1 001 100 4
2 010 010 2
3 011 110 6
4 100 001 1
5 101 101 5
6 110 011 3
7 111 111 7

Вам также может понравиться