Professional Documents
Culture Documents
Compiled by
Name: ...........................................................................
Class: .............................................................................
Semester: ........................................................................
Batch:.............................................................................
Mission and Vision of the Institution
Institute Vision
Institute Mission
Department Vision
Department Mission
At the end of the course, the students will be able to attain the following
skills.
Write code to carry out various operations on signals and verify them
CO1
using OCTAVE software
CO2 Evaluate DFT’S of different signals and verify properties of DFT
CO3 Understand the implementation of Digital filter (IIR & FIR)
Simulate the programs and execute them on the DSP Starter Kit using
CO4
Code Composer Studio Software tool.
CO5 Write a Report on the Conducted experiment and answer for viva-voce
References
QTOCTAVE
QTOCTAVE is a front-end user interface for the Octave.
It simplifies the use of Octave for user who are not familiar with the
original, command line based version of Octave.
EXPERIMENTS USING
OCTAVE
OCTAVE Program:
Program 4
fs=8000;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=sin(2*pi*4000*t)
xk=fft(x);
m=abs(xk);
f=[0:length(xk)-1]*fs/length(xk);
subplot(3,1,1)
plot(f,m)
grid;
zoom on;
xlabel('FREQUENCY')
ylabel('MAGNITUDE')
title('Sampling at Nyquist Rate')
fs=16000;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=sin(2*pi*4000*t)
xk=fft(x);
fs=2000;
t=0.0:1/fs:0.2 % t from 0 to 0.2 in steps of 1/fs
x=sin(2*pi*4000*t)
xk=fft(x);
m=abs(xk);
f=[0:length(xk)-1]*fs/length(xk);
subplot(3,1,3)
plot(f,m)
grid;
zoom on;
xlabel('FREQUENCY')
ylabel('MAGNITUDE')
title('Sampling at half the Nyquist Rate')
Viva Questions
1. Define plot, stem, and subplot in OCTAVE.
2. List all the functions used in the above program.
3. Briefly explain sampling theorem.
4. What is nyquist rate?
5. What is aliasing?
Algorithm:
1. Input the two sequences as x1, x2
2. Convolve both to get output y.
3. Plot the sequences.
OCTAVE Implementation:
OCTAVE recognizes index 1 to positive maximum. Index 0 is also not recognized. The
timing information for a sequence is provided by another vector, say n=-3:5; creates a
vector with values from -3 to 5 with an increment of 1.
Result
x1 =1 2 3 2 1 3 4
n1 =-3 -2 -1 0 1 2 3
x2 =2 -3 4 -1 0 1
n2 =-1 0 1 2 3 4
linear con of x1 & x2 is
y = 2 1 4 2 6 9 3 2 15 -3 3 4
Fig.3.2 shows the plots
the index n k N
implies circular shifting operation and k N
implies folding
OCTAVE Implementation:
OCTAVE recognizes index 1 to be positive maximum. Index 0 is not recognized.
Hence in the below program wherever y, x and h sequences are accessed, the index is
added with +1. the modulo index calculation for circular convolution is carried out using
the function - MOD Modulus (signed remainder after division). MOD(x,y) is x -
y.*floor(x./y) if y ~= 0. By convention, MOD(x,0) is x.The input x and y must be real
OCTAVE PROGRAM:
x=[1 2 3 4];
h=[1 2 3 4];
N=length(x);
%Compute the output
for n=0:N-1
y(n+1)=0;
for k=0:N-1
i=mod((n-k),N);
if i<0
i=i+N;
end
y(n+1)=y(n+1)+h(k+1)*x(i+1);
end
end
disp('circular convolution of x & h is y=');
disp(y);
%plot
n1=0:N-1;
stem(n1,y);
title('Circular convolution output y(n)');
RESULT
25
20
15
10
0
0 0.5 1 1.5 2 2.5 3
RESULT:
Note: The second sequence is entered in matrix(circulated) form
25
20
y(n)
15
10
0
1 1.5 2 2.5 3 3.5 4
n
for i=1:n
c(i,1)=x(i)
end
p=1; k=1;
for j=2:n
t=1;
for i=1:n
if i<j
c(i,j)=c((n-p)+t,1)
t=t+1
else
c(i,j)=c(i-k,1)
end
i
end
p=p+1
k=k+1
j
end
cnv=c*h'
Result:
x=
2 3 4 5
h=
1 1 1
10
0
0 0.5 1 1.5 2 2.5 3
The linear convolution is the convolution process for the discrete time
systems. The convolution of discrete time (DT) signals possesses the following
properties:
Result :
% Commutative Property
y1=circonv(x,h1);
y2=circonv(h1,x);
if y1==y2
disp('Commutative Property is proved');
else
disp('Commutative Property is not proved');
end
%Associative Property
y3= circonv(x,circonv(h1,h2));
y4=circonv(circonv(x,h1),h2);
if y3==y4
Note : Write the function file separately and save in the same
folder with file name as: function name with dot m (.m)
extension.
Viva Questions :
1. Explain how linear convolution can be obtained.
2. Explain different properties of convolution
3. What is the length of the output sequence of linear convolution
4. Give the different methods for to find convolution
5. Write A Program to find the circular convolution of two sequences using
Matrix Method
6. Explain how circular convolution can be obtained.
7. Explain different properties of circular convolution
8. List out the difference between linear and circular convolution.
9. Why is zero padding used in circular convolution .
10. Give the different methods for performing circular convolution
Aim: To obtain autocorrelation of the given sequence and verify its properties.
Theory:
Correlation is mathematical technique which indicates whether 2 signals are
related and in a precise quantitative way how much they are related. A measure of
similarity between a pair of energy signals x[n] and y[n] is given by the cross
correlation sequence rxy[l] defined by rxy [l ] x[n] y[n l ]; l 0,1,2,... .
n
The parameter ‘l’ called ‘lag’ indicates the time shift between the pair.
Autocorrelation sequence of x[n] is given by rxx [l ] x[n]x[n l ]; l 0,1,2,...
n
o At zero lag, i.e., at l=0, the sample value of the autocorrelation sequence has its
maximum value (equal to the total energy of the signal x) i.e.,
rxx [l ] rxx [0] x x
n
2
[ n] .
This is verified in Fig. 5.1, where the autocorrelation of the rectangular pulse
(square) has a maximum value at l=0. All other samples are of lower value. Also
the maximum value = 11 = energy of the pulse [12+12+12..].
o A time shift of a signal does not change its autocorrelation sequence. For example,
let y[n]=x[n-k]; then ryy[l] = rxx[l] i.e., the autocorrelation of x[n] and y[n] are the
same regardless of the value of the time shift k. This can be verified with a sine
and cosine sequences of same amplitude and frequency will have identical
autocorrelation functions.
o For power signals the autocorrelation sequence is given by
k
1
rxx [l ] lim
k 2k 1
x[n]x[n l ]; l 0,1,2,... and for periodic signals with
n k
periodic with N. This is verified in Fig. 5.3 where we use the periodicity property
of the autocorrelation sequence to determine the period of the periodic signal y[n]
which is x[n] (=cos(0.25*pi*n)) corrupted by an additive uniformly distributed
random noise of amplitude in the range [-0.5 0.5]
Algorithm:
1. Input the sequence as x.
2. Use the ‘xcorr’ function to get auto correlated output r.
3. Plot the sequences.
OCTAVE Implementation:
OCTAVE has the inbuilt function XCORR(A), when A is a vector, is the auto-
correlation sequence. If A is of length M vector (M>1), then the xcorr function returns
the length 2*M-1 auto-correlation sequence. The zeroth lag of the output correlation
is in the middle of the sequence at element M.
XCORR(...,MAXLAG) computes the (auto/cross) correlation over the range of lags:
-MAXLAG to MAXLAG, i.e., 2*MAXLAG+1 lags. If missing, default is MAXLAG
= M-1.
[C,LAGS] = XCORR(...) returns a vector of lag indices (LAGS).
OCTAVE Programs
Computation of Autocorrelation of rectangular Sequence [with out in-built function]
n = -5:5;
N=10;
%Generate the square sequence
x = ones(1,11);
%Compute the correlation sequence
r = conv(x,fliplr(x));
disp('autocorrelation sequence r=');
disp(r);
%plot the sequences
subplot(2,1,1)
stem(n,x);
Result:
The output plot is in Fig.5.1
Autocorrelation sequence r = 1.0000 2.0000 3.0000 4.0000
5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000
10.0000 9.0000 8.0000 7.0000 6.0000 5.0000 4.0000
3.0000 2.0000 1.0000
Maximum value of energy = 11
2) The cross correlation of two sequences x[n] and y[n]=x[n-k] shows a peak
at the value of k. Hence cross correlation is employed to compute the exact
value of the delay k between the 2 signals. Used in radar and sonar
applications, where the received signal reflected from the target is the delayed
version of the transmitted signal (measure delay to determine the distance of
the target).
3) The ordering of the subscripts xy specifies that x[n] is the reference
sequence that remains fixed in time, whereas the sequence y[n] is shifted w.r.t
x[n]. If y[n] is the reference sequence then ryx [l ] rxy [ l ] . Hence ryx[l] is
Inference:
Strong peak of 15 at lag = -2 implies the delay between xi and x2 is 2.
Inference:
Strong peak of 10 at lag = 2 implies the delay between xr and x2 is 2, but since
10<15, it implies that xr and x2 are uncorrelated slightly (may be due to noise,etc).
ryx [l ] rxy [ l ] is verified.
Algorithm:
1. Input the sequence as x and y.
2. Use the ‘xcorr’ function to get cross correlated output r.
3. Plot the sequences.
OCTAVE Implementation:
OCTAVE has the inbuilt function XCORR: Say C = XCORR(A,B), where A and B
are length M vectors (M>1), returns the length 2*M-1 cross-correlation sequence C. If
A and B are of different length, the shortest one is zero-padded. Using convolution to
implement correlation, the instruction is FLIPLR Flip matrix in left/right direction.
FLIPLR(X) returns X with row preserved and columns flipped in the left/right
direction. X = 1 2 3 becomes 3 2 1.
Result:
Type the reference sequence = [1 -2 6 1]
20
15
10
Amplitude
-5
-10
-3 -2 -1 0 1 2 3
Lag index
Columns 10 through 17
Note: For sequences with different lengths, the length of the output using xcorr is
2*max(n1,n2)-1 = 2* 9-1=17 whereas using conv program, the length of output is
n1+n2-1.
Viva Questions
1. Explain the differences between convolution and correlation
2. List the methods for performing correlation
3. Which are the OCTAVE functions used to perform correlation
4. List out the properties of Auto-correlation.
5. Explain the following functions
rand(1,N) - 0.5
fliplr(y)
xcorr(y)
6. Explain the differences between cross correlation and autocorrelation
7. List the methods for performing cross correlation
8. List out the applications of cross correlation.
9. Which are the OCTAVE functions used to perform cross correlation
Algorithm:
1. Input the two sequences as a and b representing the coefficients of y and x.
2. If IIR response, then input the length of the response required (say 100, which
can be made constant).
3. Compute the output response using the ‘filter’ command.
4. Plot the input sequence & impulse response (and also step response, etc if
required).
OCTAVE Implementation:
OCTAVE has an inbuilt function ‘filter’ to solve difference equations numerically,
given the input and difference equation coefficients (b,a).
y=filter(b,a,x)
where x is the input sequence, y is the output sequence which is of same length as x.
Given a difference equation a0 y[n]+a1 y[n-1]+a2 y[n-2]=b0x[n]+b2 x[n-2], the
coefficients are written in a vector as b=[b0 0 b2] and a=[a0 a1 a2]. Note the zero in b
(x[n-1] term is missing).
Also remember a0, the coefficient of y[n] should always be 1.
For impulse response x[n] {1,0,0,0,....} the number of zeros = the length of the IIR
OCTAVE Program
To find Impulse Response
N=input('Length of response required=');
b=[2]; %x[n] coefficient
a=[1,0.5]; %y coefficients
%impulse input
x=[1,zeros(1,N-1)];
%time vector for plotting
n=0:1:N-1;
%impulse response
h=filter(b,a,x);
%plot the waveforms
subplot(2,1,1);
stem(n,x);
title('impulse input');
xlabel('n');
ylabel('δ(n)');
subplot(2,1,2);
stem(n,h);
title('impulse response');
xlabel('n');
ylabel('h(n)');
Result:
Length of response required=8
?(n)
0.5
0
0 1 2 3 4 5 6 7
n
impulse response
2
1
h(n)
-1
0 1 2 3 4 5 6 7
n
step input
1
u(n)
0.5
0
0 1 2 3 4 5 6 7 8 9
n
step response
2
1.5
y(n)
0.5
0
0 1 2 3 4 5 6 7 8 9
n
Result:
Length of response required=5
y=
input sequence
20
15
x(n)
10
0
0 0.5 1 1.5 2 2.5 3 3.5 4
n
system response
30
20
y(n)
10
0
0 0.5 1 1.5 2 2.5 3 3.5 4
n
Result:
Length of response required=25
y=
Columns 1 through 10
2.0000 0.9754 1.4144 1.0748 1.0806 0.8739 0.7386 0.5387 0.3487
0.1385
Columns 11 through 20
-0.0693 -0.2782 -0.4789 -0.6685 -0.8413 -0.9936 -1.1213 -1.2214 -1.2914
-1.3297
Columns 21 through 25
-1.3352 -1.3078 -1.2482 -1.1579 -1.0391
0.5
x(n)
-0.5
-1
0 5 10 15 20 25
n
steady state response
2
1
y(n)
-1
-2
0 5 10 15 20 25
n
y[n]
1
x[n] x[n 1] x[n 2] 0.95 y[n 1] 0.9025y[n 2]; n≥0;
3
where
n
x[ n] cos u[n]
3
and the initial conditions are
y[-1]=-2; y[-2]=-3; x[-1]=1; x[-2]=1.
Solution: Use OCTAVE function filter(b,a,x,xic), where xic are the initial conditions
computed as xic=filtic(b,a,Y,X), where Y=[y(-1) y(-2)…y(N-1)] & X=[x(-1) x(-
2)…x(-M)] are the initial condition arrays. If x[n]=0 for n<0, then X need not be
specified.
Questions
1. Explain the OCTAVE function filter
2. How to find various responses using filter function.
3. Given y(n) – 0.25y(n-1) – 0.375y(n-2) = -x(n) + 2x(n-1) find
a. Impulse response
b. Step response
c. Response of the system to the input given by x(n) = 2n u(n).
d. Steady state response.
OCTAVE Implementation:
Create a OCTAVE function dft. The procedure is as follows.
The commands and functions that comprise the new function must be put in a file
whose name defines the name of the new function, with a filename extension of '.m'.
At the top of the file must be a line that contains the syntax definition for the new
function. For example, the existence of a file on disk called dft.m with body as
follows
function[Xk] =dft(xn,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
Xk=xn*WNnk;
defines a new function called dft that calculates the DFT of a sequence xn. The
variables within the body of the function are all local variables
In the Main body of the program call this function to calculate the DFT.
The magnitude spectrum is computed using the function abs (Absolute value).
abs(x) is the absolute value of the elements of x. When x is complex, abs(x) is the
complex modulus (magnitude) of the elements of x.
The phase spectrum is computed using the function angle (Phase angle). angle (h)
returns the phase angles, in radians, of a matrix with complex elements.
Columns 1 through 6
Columns 7 through 8
0 1.0000 + 2.4142i
3
MAGNITUDE
0.5 2
0 0
0 2 4 6 8 0 42 6 8
K
phase plot signal sequence constituted from spectrum
2 1
1
phase
0 0.5
-1
-2 0
0 2 4 6 8 0 2 4 6 8
K
clc;
clear all;
xn=input('enter the sequence x(n) = ');
N=input('enter the number of points of computation = ');
y=dft(xn,N);
disp(y);
mag=abs(y);
phase=angle(y);
subplot(2,2,1);
stem(xn);
title('Input Sequence x(n)');
subplot(2,2,2);
stem(mag);
xlabel('K');
ylabel('MAGNITUDE');
title('magnitude plot');
subplot(2,2,3)
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
Note : Write the function file separately and save in the same folder with file
name as: function name with dot m (.m) extension.
function[xk]=dft(xn,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
xk=xn*WNnk;
Columns 1 through 3
4.0000 1.0000 - 2.4142i -0.0000 - 0.0000i
Columns 4 through 6
1.0000 - 0.4142i 0 - 0.0000i 1.0000 + 0.4142i
Columns 7 through 8
0.0000 - 0.0000i 1.0000 + 2.4142i
3
MAGNITUDE
0.5 2
0 0
0 2 4 6 8 0 2 4 6 8
K
phase plot
2
0
phase
-2
-4
0 2 4 6 8
K
subplot(2,1,2)
stem(phase);
xlabel('K');
ylabel('phase');
title('phase plot');
Write the function file separately and save in the same folder with file name as:
function name with dot m (.m) extension
function[xn]=idft(XK,N)
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^(-nk);
xn=(XK*WNnk)/N;
RESULT:
enter the sequence X(K) = [4 0-2i 0 0+2i]
enter the number of points of computation = 4
Columns 1 through 3
Column 4
0 + 0.0000i
1.5
MAGNITUDE
0.5
0
1 1.5 2 2.5 3 3.5 4
K
phase plot
2
1.5
phase
0.5
0
1 1.5 2 2.5 3 3.5 4
K
Fig.8.3 IDFT
Viva Questions
1. Explain DFT
2. What are the methods of finding DFT
3. Explain differences between DFT and FFT
4. Explain how functions can be declared in MATLB.
5. Explain OCTAVE function fft(x)
6. How can user create a OCTAVE function.
x1=[1,2,3,4];
x2=[1,1,1,1];
a1=1;
a2=1;
Result :
Linearity property proved
LHS=
14.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i
RHS=
14.0000 + 0.0000i -2.0000 + 2.0000i -2.0000 + 0.0000i -2.0000 - 2.0000i
Parsevals Property
clc;
clear all;
close all;
% Parseval's theoerm
x=[1,2,3,4];
XK =fft (x);
en1 = sum(x.^2);
en2= (1/4)* sum(abs(XK).^2);
if (en1==en2)
disp('Parevals theorem proved');
disp('Energy=');
disp(en2);
end
Theory:
By multiplying two N-point DFTs in the frequency domain, we get the
circular convolution in the time domain.
N DFT
y[n] x[n] h[n] Y (k ) X (k ) H (k )
Circular Convolution is made identical to linear convolution by choosing the
number of points in DFT as N ≥ xlength + hlength -1.
For Circular Convolution, N = max (xlength, length).
Algorithm:
1. Input the two sequences as x1, x2
2. Compute N=max(xlength + hlength))
3. Obtain N-point DFTs (X1, X2) of both the sequences.
4. Multiply both the DFTs (Y=X1×X2).
5. Perform IDFT on Y to get y[n] (the linearly convolved sequence) in time
domain.
6. Verify using the ‘conv’ command.
7. Plot the sequences.
%obtain DFTs
X1= fft(x1,N);
X2= fft(x2,N);
Result:
circular convolution of x1 &x2 is yn= 26 28 26 20
Circular convolution output y(n)
30
25
20
15
10
0
1 1.5 2 2.5 3 3.5 4
x = [3 1 5 2 4]';
Result :
clc;clear all;
close all;
N=5;
n = 0:N-1;
x = [3 6 2 4 7]';
X = fft(x);
G = X(mod(-n,N)+1);
g = ifft(G);
disp ('the Time reverse of x = ');
disp(g);
Result :
the Time reverse of x =
3
7
4
2
6
Result :
X(n) =
3.0000 + 2.0000i 5.0000 - 3.0000i 6.0000 + 1.0000i 4.0000 - 7.0000i 8.0000 +
9.0000i
X*(n)=
3.0000 - 2.0000i 5.0000 + 3.0000i 6.0000 - 1.0000i 4.0000 + 7.0000i 8.0000 -
9.0000i
Result :
X(n) =
3.0000 + 2.0000i 5.0000 - 3.0000i 6.0000 + 1.0000i 4.0000 - 7.0000i 8.0000 +
9.0000i
X*(n)=
3.0000 - 2.0000i 5.0000 + 3.0000i 6.0000 - 1.0000i 4.0000 + 7.0000i 8.0000 -
9.0000i
OCTAVE Program:
To find DFT of Square pulse
clc;
clear all;
close all;
% generating square pulse
x1=[zeros(1,32),ones(1,64),zeros(1,32)];
X1K= fft(x1);
x2= fftshift(X1K);
subplot(2,1,1);
stem(x1);
title('time domain');
subplot(2,1,2);
plot(abs(x2));
title('frequency domain');
Result :
OCTAVE IMPLEMENTATION
FIR1 Function
B = FIR1(N,Wn) designs an N'th order lowpass FIR digital filter and returns the filter
coefficients in length N+1 vector B. The cut-off frequency Wn must be between 0 <
Wn < 1.0, with 1.0 corresponding to half the sample rate. The filter B is real and has
4 1 . 8
Rectangular 21db B
M M
=FIR1(N,Wn,boxcar)
8 6.1
Bartlett 25db
M M
B=FIR1(N,Wn,bartlett)
8 6 .2
Hanning 44db
M M
B=FIR1(N,Wn,hanning)
8 6 .6
Hamming 53db B=
M M
FIR1(N,Wn)
RECTANGULAR WINDOW
RESULT:
enter the passband ripple:0.05
enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000
0 0
gain in db
gain in db
-50 -50
-100 -100
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq
20 5
0 0
gain in db
gain in db
-20 -5
-40 -10
-60 -15
-80 -20
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq
HAMMING WINDOW
RESULT:
enter the passband ripple:0.05
enter the stopband ripple:0.001
enter the passband frequency:1200
enter the stopband frequency:1700
enter the sampling frequency:9000
50 50
0
0
gain in db
gain in db
-50
-50
-100
-150 -100
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq
0 5
0
-50
gain in db
gain in db
-5
-10
-100
-15
-150 -20
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq
RESULT:
enter the passband ripple:0.05
enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000
-10
-10
gain in db
gain in db
-20
-20
-30
-40 -30
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq
0 2
-10 0
gain in db
gain in db
-20 -2
-30 -4
-40 -6
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq
HANNING WINDOW
b=fir1(n,wp,y); b=fir1(n,wn,'stop',y);
[h,o]= freqz(b,1,256); [h,o]=freqz(b,1,256);
m=20*log10(abs(h)); m=20*log10(abs(h));
subplot(2,2,1); subplot(2,2,4);
plot(o/pi,m) plot(o/pi,m)
ylabel('gain in db'); ylabel('gain in db');
xlabel('(a)normalised freq'); xlabel('(d)normalised freq');
grid on; grid on;
RESULT:
enter the passband ripple:0.03
enter the stopband ripple:0.01
enter the passband frequency:1400
enter the stopband frequency:2000
enter the sampling frequency:8000
50 50
0
0
gain in db
gain in db
-50
-50
-100
-150 -100
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq
0 5
0
-50
gain in db
gain in db
-5
-100
-10
-150 -15
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq
RESULT:
enter the passband ripple:0.05
enter the stopband ripple:0.04
enter the passband frequency:1500
enter the stopband frequency:2000
enter the sampling frequency:9000
enter the beta value5.8
0
0
gain in db
gain in db
-50
-50
-100
-150 -100
0 0.5 1 0 0.5 1
(a)normalised freq (b)normalised freq
0 2
-20
0
gain in db
gain in db
-40
-2
-60
-4
-80
-100 -6
0 0.5 1 0 0.5 1
(c)normalised freq (d)normalised freq
Questions
1. What is a Filter? What are its specifications?
2. Mention the types of filters.
3. Compare analog and digital filters.
4. Define a FIR filter.
5. What are the important properties of FIR filter?
6. What are the window techniques used?
7. Why the Kaiser window is important?
8. What is Gibb’s Phenomenon?
9. How do you calculate the length of Kaiser Window?
10. What are the functions used in designing a FIR filter in OCTAVE?
Theory: There are two methods of stating the specifications as illustrated in previous
program. In the first program, the given specifications are directly converted to digital
form and the designed filter is also implemented. In the last two programs the
butterworth and chebyshev filters are designed using bilinear transformation (for
theory verification).
Method I: Given the order N, cutoff frequency fc, sampling frequency fs and the IIR
filter type (butterworth, cheby1, cheby2).
Step 1: Compute the digital cut-off frequency Wc (in the range -π < Wc < π, with
π corresponding to fs/2) for fc and fs in Hz. For example let fc=400Hz,
fs=8000Hz Wc = 2*π* fc / fs = 2* π * 400/8000 = 0.1* π radians. For OCTAVE
the Normalized cut-off frequency is in the range 0 and 1, where 1 corresponds to
fs/2 (i.e.,fmax)). Hence to use the OCTAVE commands wc = fc / (fs/2) =
400/(8000/2) = 0.1
Note: if the cut off frequency is in radians then normalized frequency is computed
as wc = Wc / π
Step 2: Compute the Impulse Response [b,a] coefficients of the required IIR filter
and the response type (lowpass, bandpass, etc) using the appropriate butter,
cheby1, cheby2 command. For example given a butterworth filter, order N=2, and
a high pass response, the coefficients [b,a] of the filter are computed using the
OCTAVE inbuilt command ‘butter’ as [b,a] =butter(N, wc , 'high');
Method 2: Given the pass band (Wp in radians) and Stop band edge (Ws in radians)
frequencies, Pass band ripple Rp and stopband attenuation As.
Step 1: Since the frequencies are in radians divide by π to obtain normalized
frequencies to get wp=Wp/pi and ws=Ws/pi. If the frequencies are in Hz
(note: in this case the sampling frequency should be given), then obtain
OCTAVE IMPLEMENTATION
BUTTORD Butterworth filter order selection. [N, Wn] = BUTTORD(Wp, Ws, Rp,
Rs) returns the order N of the lowest order digital Butterworth filter that loses no
more than Rp dB in the passband and has at least Rs dB of attenuation in the
stopband. Wp and Ws are the passband and stopband edge frequencies, normalized
from 0 to 1 (where 1 corresponds to pi radians/sample). For example,
Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1
Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7]
BUTTORD also returns Wn, the Butterworth natural frequency (or, the "3 dB
frequency") to use with BUTTER to achieve the specifications. [N, Wn] =
BUTTORD(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in which
case Wp and Ws are in radians/second. When Rp is chosen as 3 dB, the Wn in
BUTTER is equal to Wp in BUTTORD.
OCTAVE PROGRAMS
BUTTERWORTH FILTERS
1.Butterworth LPF
clc;
clear all;
close all;
format long
lp=input('Enter the passband attenuation:');
ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=buttord(w1,w2,lp,ls);
[b,a]=butter(n,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
figure(1);
subplot(2,1,1);
title('Butterworth Low Pass Filter');
plot(om/pi,m);
RESULT:
Enter the passband attenuation: 0.5
Enter the stopband attenuation: 50
Enter the passband frequency: 1200
Enter the stopband frequency: 2400
Enter the sampling frequency:10000
200
Gain in db-->
-200
-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a)Normalised frequency-->
4
Phase in radians-->
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b)Normalised frequency-->
RESULT:
Enter the passband attenuation: 0.5
Enter the stopband attenuation: 50
Enter the passband frequency: 1200
Enter the stopband frequency: 2400
Enter the sampling frequency: 10000
Gain in db-->
0
-200
-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a)Normalised frequency-->
4
Phase in radians-->
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b)Normalised frequency-->
RESULT:
enter the passband ripple 0.3
enter the stopband ripple 40
enter the passband frequency 1500
enter the stopband frequency 2000
enter the sampling frequency 9000
-200
gain in db-->
-400
-600
-800
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a) normalised frequency-->
4
phase in radians-->
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b) normalised frequency-->
RESULT:
enter the passband ripple 0.4
enter the stopband ripple 46
enter the passband frequency 1100
enter the stopband frequency 2200
100
0
gain in db-->
-100
-200
-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a) normalised frequency-->
4
phase in radians-->
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b) normalised frequency-->
CHEBYSHEV FILTERS
1. Chebyshev LPF
clc;
close all;
clear all;
format long
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
fs=input('enter the sampling frequency');
w1=2*wp/fs;
w2=2*ws/fs;
[n,wn]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wn);
w=0:0.01:pi;
[h,om]=freqz(b,a,w);
m=20*log10(abs(h));
an=angle(h);
subplot(2,1,1);plot(om/pi,m);
ylabel('gain in db-->');
RESULT:
enter the passband ripple 0.2
enter the stopband ripple 45
enter the passband frequency 1300
enter the stopband frequency 1500
enter the sampling frequency 10000
0
gain in db-->
-200
-400
-600
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a) normalised frequency-->
4
phase in radians-->
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b) normalised frequency-->
2. Chebyshev HPF
clc;
close all;
clear all;
format long
rp=input('enter the passband ripple');
rs=input('enter the stopband ripple');
wp=input('enter the passband frequency');
ws=input('enter the stopband frequency');
RESULT:
enter the passband ripple0.3
enter the stopband ripple60
enter the passband frequency1500
enter the stopband frequency2000
enter the sampling frequency9000
-100
gain in db-->
-200
-300
-400
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a) normalised frequency-->
4
phase in radians-->
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b) normalised frequency-->
0
Gain in db-->
-200
-400
-600
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a)Normalised frequency-->
4
Phase in radians-->
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b)Normalised frequency-->
4. Chebyshev BSF
clc;
clear all;
close all;
format long
lp=input('Enter the passband attenuation:');
ls=input('Enter the stopband attenuation:');
wp=input('Enter the passband frequency:');
ws=input('Enter the stopband frequency:');
fs=input('Enter the sampling frequency:');
w1=2*wp/fs;
w2=2*ws/fs;
[n]=cheb1ord(w1,w2,lp,ls);
RESULT:
Enter the passband attenuation:0.4
Enter the stopband attenuation:46
Enter the passband frequency:1100
Enter the stopband frequency:2200
Enter the sampling frequency:6000
wn = 0.36666666666667 0.73333333333333
Gain in db-->
-100
-200
-300
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(a)Normalised frequency-->
4
Phase in radians-->
-2
-4
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
(b)Normalised frequency-->
Viva Questions:
1. Define an IIR filter.
2. Compare Impulse invariance and bilinear transformations.
3. How do you convert analog filter prototype to a digital filter?
4. What are the functions used in OCTAVE for designing a digital
Butterworth and Chebyshev low pass filter using BLT?
5. Draw typical responses of Chebyshev filter for order odd & even.
6. Compare FIR & IIR filters.
C Program
# include<stdio.h>
# include<math.h>
main()
{
float h[4] = { 2,2,2,2}; float x[4] ={1,2,3,4};
float y[10];
int xlen=4; int hlen=4;
int N=xlen+hlen-1;
int k,n;
for(n=0;n<N;n++) //outer loop for y[n] array
{
y[n]=0;
for(k=0;k<hlen;k++)
//inner loop for computing each y[n] point
{
if (((n-k)>=0) & ((n-k)<xlen))
y[n]=y[n]+h[k]*x[n-k]; //compute output
} //end of inner for loop
printf("%f\t", y[n]);
} //end of outer for loop
} //end of main
Result
6.000000 12.000000 20.000000 18.000000 14.000000 8.000000
h[k ]x n k
The circular convolution sum is y[n] x[n] N h[n] N
k
C Program
# include<stdio.h>
# include<math.h>
main()
{
float x[5]={1,2,3,4,5};
float h[5]={2,1,3,4,5};
float y[10]; //output sequence
int N=5; // N=max of xlen and hlen//
int k,n,i;
for(n=0;n<N;n++) //outer loop for y[n] array
{
y[n]=0;
for(k=0;k<N;k++) //inner loop for computing each y[n] point
{
i=(n-k)%N; //compute the index modulo N
if(i<0) //if index is <0, say x[-1], then convert to x[N-1]
i=i+N;
y[n]=y[n]+h[k]*x[i]; //compute output
} //end of inner for loop
printf("%f\t",y[n]);
} //end of outer for loop
} //end of main
Result
41.000000 51.000000 51.000000 46.000000 36.000000
Theory:
The N point DFT of discrete time signal x[n] is given by the equation
N -1 j 2kn
X (k ) x[n]e N
; k 0,1,2,....N - 1
n 0
C Program
#include <stdio.h>
#include <math.h>
main()
{
float y[16]; //for 8 point DFT to store real & imaginary
float x[4]={1,3,2,5}; //input only real sequence
float w;
int n,k,k1,N=8,xlen=4;
for(k=0;k<2*N;k=k+2)
{
y[k]=0; y[k+1]=0; //initialize real & imag parts
k1=k/2; //actual k index
for(n=0;n<xlen;n++)
{
w= -2*3.14*k1*n/N; //careful about minus sign
y[k]=y[k]+x[n]*cos(w);
y[k+1]=y[k+1]+x[n]*sin(w);
Note: For a first order system just enter a2=0 and b2=0.
Result (on std out)
0.067500 0.212053 0.282012 0.234804 0.151967 0.076771
0.025017 -0.003096 -0.013866 -0.014571 -0.010931 -
0.006479 -0.002893 -0.000632 0.000471 0.000800 0.000720
0.000492 0.000266 0.000100
#include "FIRcfg.h"
#include "dsk6713.h"
#include "dsk6713_aic23.h"
float filter_Coeff[] ={0.000000,-0.001591,-0.002423,0.000000,0.005728, 0.011139,
0.010502,-0.000000, -0.018003,-0.033416,-0.031505,0.000000,0.063010, 0.144802,
0.220534,0.262448,0.220534,0.144802,0.063010,0.000000,-0.031505,-0.033416,
-0.018003, 0 ,0.010502, 0.011139,0.005728,0.000000, -0.002423,-0.001591, 0.0};
static short in_buffer[100];
DSK6713_AIC23_Config config = { \
0x0017, /* 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume */ \
0x0017, /* 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume
*/\
0x00d8, /* 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume */
\
0x00d8, /* 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume
*/ \
0x0011, /* 4 DSK6713_AIC23_ANAPATH Analog audio path control */ \
0x0000, /* 5 DSK6713_AIC23_DIGPATH Digital audio path control */ \
0x0000, /* 6 DSK6713_AIC23_POWERDOWN Power down control */ \
0x0043, /* 7 DSK6713_AIC23_DIGIF Digital audio interface format */ \
while(1)
{ /* Read a sample to the left channel */
while (!DSK6713_AIC23_read(hCodec, &l_input));
/* Read a sample to the right channel */
while (!DSK6713_AIC23_read(hCodec, &r_input));
l_output=(Int16)FIR_FILTER(&filter_Coeff
,l_input);
r_output=l_output;
while (!DSK6713_AIC23_write(hCodec, l_output)); /* Send o/p to the left channel */
while (!DSK6713_AIC23_write(hCodec, r_output)); /* Send o/p to the right channel
*/
}
DSK6713_AIC23_closeCodec(hCodec); /* Close the codec */
}
Result: The output plot is observed on the CRO. This behaves as a highpass filter.
See the effect of filtering by giving a sinusoidal input to DSP kit and slowly varying
its frequency in the range from 0 to 3.5 kHz. What is the cutoff frequency of the
filter?
Change the filter co-efficients for different types of window & Cut-off frequencies
(refer FIR filter design part from experiment 11 in part A-OCTAVE)