You are on page 1of 133

Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

INTRODUCTION TO MATLAB

Desktop Overview

Introduction to the Desktop

Use desktop tools to manage your work and become more productive using MATLAB software.
You can also use MATLAB functions to perform the equivalent of most of the features found in
the desktop tools.
The following illustration shows the default configuration of the MATLAB desktop. You can
modify the setup to meet your needs.

Arranging the Desktop

These are some common ways to customize the desktop:


Show or hide desktop tools via the Desktop menu.
Resize any tool by dragging one of its edges.
Move a tool outside of the desktop by clicking the undock button in the tool's title bar.

Prepared by Thota Sravanthi Page 1


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Reposition a tool within the desktop by dragging its title bar to the new location. As you
drag, a blue box indicates the new tool position until you release the mouse button. You
can drag more than one tools to the same position, in which case they become the same
size and their title bars become tabs. Access a tabbed tool by clicking the tab displaying
its name.
Maximize or minimize (temporarily hide) a tool within the desktop via the Desktop
menu.
Change fonts, customize the toolbar, and access other options by using File >
Preferences.

Start Button

The MATLAB Start button provides easy access to tools, demos, shortcuts, and documentation.
Click the Start button to see the options.

Command Window and Command History

Command Window

Use the Command Window to enter variables and to run MATLAB functions and scripts.
MATLAB displays the results.

Prepared by Thota Sravanthi Page 2


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Press the up arrow key to recall a statement you previously typed. Edit the statement as needed,
and then press Enter to run it. For more information about entering statements in the Command
Window, see Controlling Command Window Input and Output.
There are other tools available to help you remember functions and their syntax, and to enter
statements correctly. For example, to look for functions, use the Function Browser to look for
functionsclick the button at the left of the prompt to open the tool. For more information on
ways to get help while you work in the Command Window, see Avoid Mistakes When Entering
Code.

Command History

Statements you enter in the Command Window are logged with a timestamp in the Command
History. From the Command History, you can view and search for previously run statements, as
well as copy and execute selected statements. You can also create a file from selected statements.

To save the input and output from a MATLAB session to a file, use the diary function.

Prepared by Thota Sravanthi Page 3


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Getting Help

Ways to Get Help

There are different ways to get help, depending on your needs. The following
table summarizes the main ways.

To... Try This More Information

Look for getting In the Help browser To open the Help browser, select Help >
started guides, code Contents pane, expand Product Help.
examples, demos, the listing for a product.
and more.
Find information In the Help browser Searching for Documentation and Demos
about any topic. search field, type words
you want to look for in
the documentation or
demos. Then press
Enter.
View help for a Run doc name to display doc reference page
function or block. the reference page in the help reference page
Help browser.
For quick help in the
Command Window, run
help name. Sometimes,
the help text shows
function names in all
uppercase letters to
distinguish them from
other text. When you use
function names, do not
use all uppercase letters.

Find a function and Select Help > Function Find Functions Using the Function Browser
view help for it. Browser, then search or
browse.

Get syntax and Use colors and other Avoid Mistakes When Entering Code
function hints while cues to determine correct
using the Command syntax.
Window and Editor. While entering a
function, pause after
typing the left

Prepared by Thota Sravanthi Page 4


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

To... Try This More Information

parenthesis. A summary
of syntax options
displays in a temporary
window.

Get specific help Use the context-sensitive See the documentation for a tool to learn about
while using a tool. help, which some tools any special context-sensitive help available.
provide. Access the help
using standard methods,
such as Help buttons and
context menus.
Check code for In the Editor, view M- Avoid Mistakes While Editing Code
problems and get Lint messages.
recommendations
for improvements.

Searching for Documentation and Demos

Use the Help browser to find documentation and demos that contain your search terms:
1. In the Help browser Search field, enter the words you want to look for. Search
finds sections containing all the words, unless you use any of the syntax
options described in the following table.

Option Syntax Example

Exact phrase " " around phrase "word1 word2"


(quotation marks)
Wildcards for partial * in place of characters word*
word searching
Some of the words OR between words word1 OR word2
Exclude words NOT before excluded word1 NOT word2
word

2. For example, enter plot tool* label.


3. Press Enter.
The Search Results pane lists matching sections. An icon indicates the type of information.

Prepared by Thota Sravanthi Page 5


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

4. Arrange results:
The default sort order is by relevance. Change the order by clicking the column
header for Type or Product.
For results sorted by Type or Product, you can collapse and expand results for a
type or product group. To expand or collapse all groups, right-click in the Search
Results pane, and select the option you want from the context menu.
1. Select a result to view the page.
The Help browser highlights the search words in the display pane. To clear
highlights, select Refresh from the Actions button.
To see where the result is within the contents, use the navigation bar at the top of
the page.
Or click the Contents tab.

Prepared by Thota Sravanthi Page 6


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

1. The default presentation includes previews of text for each result found. To show more
results in the Help Navigator, you can hide the previews:
Right-click in the Help Navigator and select Hide Previews from the context
menu.
The Help Navigator only displays section titles and icons for them.
To see the previews again, right-click in the Help Navigator and select Show
Previews
The following illustration shows the effect of hiding text previews and the context menu item
for restoring them.

Prepared by Thota Sravanthi Page 7


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Workspace Browser

The MATLAB workspace consists of the set of variables built up during a MATLAB session and
stored in memory. You add variables to the workspace by using functions, running function and
script files, and loading saved workspaces.
To view the workspace and information about each variable, use the Workspace browser, or use
the functions who and whos.

Prepared by Thota Sravanthi Page 8


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

To delete variables from the workspace, select the variables, and then select Edit > Delete.
Alternatively, use the clearvars or clear functions.
The workspace does not persist after you end the MATLAB session. To save the workspace to a
file that can be read during a later MATLAB session, select File > Save, or use the save function.
Saving preserves the workspace in a binary file called a MAT-file, which has a .mat extension.
You can use options to save to different formats. To read in a MAT-file, select File > Import
Data, or use the load function.

Variable Editor

Double-click a variable in the Workspace browser, or use openvar variablename, to see it in the
Variable Editor. Use the Variable Editor to view and edit a visual representation of variables in
the workspace.

Prepared by Thota Sravanthi Page 9


Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

How MATLAB Helps You Manage Files

MATLAB provides tools and functions to help you:


Find a file you want to view, change, or run
Organize your files
Ensure MATLAB can access a file so you can run or load it

Using the Current Folder Browser to Manage Files

The Current Folder browser is a key tool for managing files.


Open the Current Folder browser by selecting Desktop > Current Folder from the MATLAB
desktop.

Use the Current Folder browser to:


See the contents of the current folder.
View and change the current folder using the address bar.
Find files and folders using the search tool.

Prepared by Thota Sravanthi Page


10
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Arrange information about files and folders using the View menu.
Change files and folders, such as renaming or moving them.
Run, open, get help for, and perform other actions on the selected file or folder by right-
clicking and using the context menu.
To create or edit a file select File > New or File > Open, or use the edit function.
The following image shows the Editor with two documents, collatzall.m and collatz.m open.
Notice the following:
Colors highlight various MATLAB language elements blue for keywords, green for
comments.
The code analysis message bar contains an orange box and bar, indicating there are areas
for improvement in the code.
Other Editor features are described in the image.

You can use any text editor to create files, such as Emacs. Use Editor/Debugger preferences
(accessible from the desktop by selecting File > Preferences > Editor/Debugger) to specify
your default editor. If you use another editor, you still can use the MATLAB Editor for
debugging, or you can use debugging functions, such as dbstop, which sets a breakpoint.

Prepared by Thota Sravanthi Page


11
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

To view the contents of a file, you can display the contents in the Command Window using the
type function.
Use code analysis to help you identify problems and potential improvements in your code. For
details, see Improving and Tuning Your MATLAB Programs.
You can evaluate your code in sections (called code cells). Also, you can publish your code,
including results, to popular output formats like HTML. For more information, see Evaluate
Subsections of Files Using Code Cells in the MATLAB Desktop Tools and Development
Environment documentation.

Identifying Problems and Areas for Improvement

Use code analysis to help you write correct and efficient MATLAB code. Code analysis:
Identifies areas for improvement by underlining code in orange
Identifies errors by underlining code in red
Provides short messages, called Code Analyzer messages, to describe all suspected
trouble spots
Provides extended Code Analyzer messages for many suspected trouble spots
Provides automated fixes for many trouble spots
The following images show code with a message at line 22 and 23. The Details button in the first
message indicates that an extended message is available for that first problem. To have
MATLAB fix a problem for you, click the Fix button, if displayed.

When you click a Details button, the message extends and provides a detailed explanation, a
suggested action, and sometimes links to the documentation.

Prepared by Thota Sravanthi Page


12
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

2-D Plotting

You can visualize vectors of data with 2-D plotting functions that create:Line, area, bar, and pie
charts, Direction and velocity plots ,Histograms ,Polygons and surfaces, Scatter/bubble plots,
Animations

Prepared by Thota Sravanthi Page


13
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

1. Basic Operations on Matrices


Aim: To write a program to perform basic operations on matrices.
Software used: MATLAB 7.0.4
In built functions:
1. inv - Matrix inverse.
inv(x) is the inverse of the square matrix x. A warning message is printed if x is badly
scaled or nearly singular.

Program:
clc
clear
a=[1,2;3,4]
b=[3,4;1,2]
c=[8;2]
d=[7]
e=a+b
f=a-b
g=a*b
h=b*a
i=a/b
j=a'
k=c'
l=size(h)
m=inv(b)
n=a.*b
o=a.*d
p=a./d
q=a./d
r=a.\b
s=d.\a
v=b.*c

Output:

>>a =
1 2
3 4
b=
3 4
1 2

Prepared by Thota Sravanthi Page


14
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

c=
8
2
d=
7
e=
4 6
4 6
f=
-2 -2
2 2
g=
5 8
13 20

h=
15 22
7 10
i=
0 1
1 0
j=
1 3
2 4
k=
8 2
l=
2 2
m=
1.0000 -2.0000
-0.5000 1.5000
n=
3 8
3 8

o=
7 14
21 28
p=
0.1429 0.2857
0.4286 0.5714
q=
0.1429 0.2857
0.4286 0.5714
r=
3.0000 2.0000
Prepared by Thota Sravanthi Page
15
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

0.3333 0.5000
s=
0.1429 0.2857
0.4286 0.5714
t=
2
4
u=
3 4

??? Error using ==> times


Matrix dimensions must agree.

Error in ==> matoprtns at 24


v=b.*c

Result: Various matrix operations are performed and results are verified.

Prepared by Thota Sravanthi Page


16
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

2. Signal generation, operations and sum of sinusoidal signals

Aim: To write a program to generation of sinusoidal signal and operations on it.


Software used: MATLAB 7.0.4
In built functions:
1. sin - Sine of argument in radians.
sin(x) is the sine of the elements of x.

2. plot - Linear plot.


plot(x,y) plots vector y versus vector x. If x or y is a matrix then the vector is plotted
versus the rows or columns of the matrix, whichever line up. If x is a scalar and y is a vector,
length(y) disconnected points are plotted.

3. tittle - Graph title.


tittle('text') adds text at the top of the current axis.

4. xlabel - x-axis label.


xlabel('text') adds text beside the x-axis on the current axis.
xlabel('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of
the specified properties of the xlabel.

5. ylabel y-axis label.


ylabel('text') adds text beside the Y-axis on the current axis.
ylabel('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of
the specified properties of the ylabel.

6. legend - Display legend.


legend (string1,string2,string3, ...) puts a legend on the current plot using the specified
strings as labels. legend works on line graphs, bar graphs, pie graphs, ribbon plots, etc.
You can label any solid-colored patch or surface object. The font size and font name for the
legend strings matches the axes font size and font name.

7. grid - Grid lines.


grid on - adds major grid lines to the current axes.
grid off - removes major and minor grid lines from the current axes.

8. figure - Create figure window.


figure, by itself, creates a new figure window, and returns its handle.

Prepared by Thota Sravanthi Page


17
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Program:
clc;
clear all;
close all;
t=-2*pi:0.01:2*pi;
a=sin(t);
%amplitude scaling
b=2*sin(t);
c=sin(t)/2;
plot(t,a,t,b,t,c)
title ('amplitude scaling')
xlabel('time')
ylabel('amplitude')
legend('a','b','c')
grid on
%time reverse
d=sin(-t);
figure;
plot(t,a,t,d)
title ('time reverse')
xlabel('time')
ylabel('amplitude')
legend('a','d')
grid on
%time scaling
e=sin(2*t);
f=sin(t/2)
figure;
plot(t,e,t,f)
title ('time scaling')
xlabel('time')
ylabel('amplitude')
legend('e','f')
grid on
%time shifting
g=sin(t+2);
h=sin(t-2);
i=sin(2-t);
plot(t,g,t,h,t,i)
figure;
title ('time shifting')
xlabel('time')
ylabel('amliptude')
legend('g','h','i')
grid on

Prepared by Thota Sravanthi Page


18
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

%phase shifting
j=sin(t+pi/2);
k=sin(t+3*pi/2);
plot(t,j,t,k)
figure;
title ('phase shifting')
xlabel('time')
ylabel('amplitude')
legend('j','k')
grid on
%sum of sinisoids
l=sin(3*t)/3;
m=sin(5*t)/5;
n=sin(7*t)/7;
o=sin(9*t)/9;
p=a+l+m+n+o
plot(t,a,t,m,t,n,t,o,t,p)
figure;
title ('sum of sinisoids')
xlabel('time')
ylabel('amplitude')
legend('l','m','n','o','p')
grid on

Output waveforms:

Prepared by Thota Sravanthi Page


19
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Result: Output waveforms for various operations on sinusoidal waves have been observed and
verified.

Prepared by Thota Sravanthi Page


20
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

3. Fast Fourier Transform


Aim: To perform Fast Fourier Transform for a given sequence
a) DFT & IDFT using inbuilt functions
b) DFT & IDFT using equation
c) N-point DFT & IDFT using inbuilt functions
d) N-point DFT & IDFT using equation
Theory:
A fast Fourier transform (FFT) is an algorithm to compute the discrete Fourier transform
(DFT) and its inverse. There are many different FFT algorithms involving a wide range of
mathematics, from simple complex-number arithmetic to group theory and number theory; this
article gives an overview of the available techniques and some of their general properties, while
the specific algorithms are described in subsidiary articles linked below.

The DFT is obtained by decomposing a sequence of values into components of different


frequencies. This operation is useful in many fields (see discrete Fourier transform for properties
and applications of the transform) but computing it directly from the definition is often too slow
to be practical. An FFT is a way to compute the same result more quickly: computing the DFT of
N points in the naive way, using the definition, takes O(N2) arithmetical operations, while an FFT
can compute the same DFT in only O(N log N) operations. The difference in speed can be
enormous, especially for long data sets where N may be in the thousands or millions. In practice,
the computation time can be reduced by several orders of magnitude in such cases, and the
improvement is roughly proportional to N / log(N). This huge improvement made the calculation
of the DFT practical; FFTs are of great importance to a wide variety of applications, from digital
signal processing and solving partial differential equations to algorithms for quick multiplication
of large integers.

The best-known FFT algorithms depend upon the factorization of N, but there are FFTs with O
(N log N) complexity for all N, even for prime N. Many FFT algorithms only depend on the fact
that is an N-th primitive root of unity, and thus can be applied to analogous transforms
over any finite field, such as number-theoretic transforms. Since the inverse DFT is the same as
the DFT, but with the opposite sign in the exponent and a 1/N factor, any FFT algorithm can
easily be adapted for it.

Inbuilt functions:
1. Input: Prompt for user input.
R = input ('How many apples') gives the user the prompt in the text string and then waits for
input from the keyboard. The input can be any MATLAB expression, which is evaluated, using
the variables in the current workspace, and the result returned in R. If the user presses the return
key without entering anything, input returns an empty matrix.

Prepared by Thota Sravanthi Page


21
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

2. length Length of vector.


length (X) returns the length of vector X. It is equivalent to max(size(X)) for non-empty arrays
and 0 for empty ones.

3. fft Discrete Fourier transform.


fft(X) is the discrete Fourier transform (dft) of vector X. For matrices, the fft operation is
applied to each column. For N-D arrays, the FFT operation operates on the first non-singleton
dimension.

4. ifft Inverse discrete Fourier transform.


ifft(X) is the inverse discrete Fourier transform of X.

5. subplot Create axes in tiled positions.


H = subplot(m,n,p), or subplot(mnp), breaks the Figure window into an m-by-n matrix of small
axes, selects the p-th axes for the current plot, and returns the axis handle.

6. stem Discrete sequence or "stem" plot.


stem(Y) plots the data sequence Y as stems from the x axis terminated with circles for the data
value. If Y is a matrix then each column is plotted as a separate series.

7. title Graph title.


TITLE('text') adds text at the top of the current axis.

8. xlabel X-axis label.


xlabel ('text') adds text beside the X-axis on the current axis.
xlabel ('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of
the specified properties of the xlabel.

9. ylabel Y-axis label.


ylabel ('text') adds text beside the Y-axis on the current axis.
ylabel ('text','Property1',PropertyValue1,'Property2',PropertyValue2,...) sets the values of
the specified properties of the ylabel.

Program:
%program for DFT & IDFT using inbuilt functions
clc;
clear all;
close all;
a =input ('input sequence;');
l1 =length (a)
b = fft (a)
l2 = length (b)
c = ifft(b)

Prepared by Thota Sravanthi Page


22
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

l3 = length (c)
subplot(3,1,1);
stem(a);
title ('input sequence');
xlabel ('n');
ylabel ('amplitude');
subplot(3,1,2);
stem(b);
title ('dft');
xlabel ('n');
ylabel ('amplitude');
subplot(3,1,3);
stem(c);
title ('ifft');
xlabel ('n');
ylabel ('amplitude');

Output:

Prepared by Thota Sravanthi Page


23
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

%program for DFT & IDFT using equation


clc;
clear all;
x=input('ip seq:');
N=length(x)
for k=0:N-1
s(k+1)=0;
for n=0:N-1
s(k+1)=s(k+1)+(x(n+1)*exp((-j*2*pi*k*n)/N));
end
end
s

N1=length(s)
for n=0:N1-1
y(n+1)=0;
for k=0:N1-1
y(n+1)=y(n+1)+(s(k+1)*exp((j*2*pi*k*n)/N1));
end
end
y=(y/N1)

subplot(3,1,1);
stem(x);
subplot(3,1,2);

Prepared by Thota Sravanthi Page


24
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

stem(abs(s));
subplot(3,1,3);
stem(abs(y));

OUTPUT:

% N-point DFT & IDFT using inbuilt functions


clc;
clear all;
close all;
a =input ('input sequence;');

Prepared by Thota Sravanthi Page


25
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

l1 =length (a)
N=input(enter input sample value)
b = fft (a,N)
l2 = length (b)
c = ifft(b,N)
l3 = length (c)
subplot(3,1,1);
stem(a);
title ('input sequence');
xlabel ('n');
ylabel ('amplitude');
subplot(3,1,2);
stem(abs(b));
title ('dft');
xlabel ('n');
ylabel ('amplitude');
subplot(3,1,3);
stem(abs(c));
title ('ifft');
xlabel ('n');
ylabel ('amplitude');

Output:

Prepared by Thota Sravanthi Page


26
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

%N DFT and IDFT


clc;
clear all;
x=input('ip seq:');
l=input('ip seq length:');
p=length(x)
x=[x,zeros(1,l-p)]
N=length(x)
for k=0:N-1
s(k+1)=0;
for n=0:N-1
s(k+1)=s(k+1)+(x(n+1)*exp((-j*2*pi*k*n)/N));
end
end
s

N1=length(s)
for n=0:N1-1
y(n+1)=0;
for k=0:N1-1
y(n+1)=y(n+1)+(s(k+1)*exp((j*2*pi*k*n)/N1));
end
end
y=(y/N1)

subplot(3,1,1);

Prepared by Thota Sravanthi Page


27
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

stem(x);
subplot(3,1,2);
stem(abs(s));
subplot(3,1,3);
stem(abs(y));

OUTPUT:
ip seq:[1 1 1]
p=3
x= 1 1 1 0 0 0 0 0
N=8
s = Columns 1 through 7
3.0000 1.7071 - 1.7071i 0 - 1.0000i 0.2929 + 0.2929i 1.0000 + 0.0000i 0.2929 -
0.2929i -0.0000 + 1.0000i
Column 8
1.7071 + 1.7071i
N1 = 8
y = Columns 1 through 7
1.0000 - 0.0000i 1.0000 - 0.0000i 1.0000 + 0.0000i 0.0000 + 0.0000i -0.0000 + 0.0000i
0 + 0.0000i -0.0000 - 0.0000i
Column 8
0.0000 + 0.0000i

Prepared by Thota Sravanthi Page


28
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Results: Fast Fourier Transform for a given sequence with & without inbuilt functions has been
observed & verified.

Prepared by Thota Sravanthi Page


29
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

4. Power Spectral Density


Aim: Write a program to Power spectral density
a) Power Spectral Density of sum of sinusoidal signal without noise
b) Power Spectral Density of sum of sinusoidal signal with noise

Theory:

Power spectral density function (PSD) shows the strength of the variations (energy) as a function
of frequency. In other words, it shows at which frequencies variations are strong and at which
frequencies variations are weak. The unit of PSD is energy per frequency (width) and you can
obtain energy within a specific frequency range by integrating PSD within that frequency range.
Computation of PSD is done directly by the method called FFT or computing autocorrelation
function and then transforming it.
In built functions:
1. sin Sine of argument in radians.
sin(X) is the sine of the elements of X.

2. plot Linear plot.


plot(X,Y) plots vector Y versus vector X. If X or Y is a matrix then the vector is plotted
versus the rows or columns of the matrix, whichever line up. If X is a scalar and Y is a vector,
length(Y) disconnected points are plotted.

3.randn Normally distributed random numbers.


randn(N) is an N-by-N matrix with random entries, chosen from a normal distribution with
mean zero, variance one and standard deviation one.
randn(M,N) and randn ([M,N]) are M-by-N matrices with random entries.
randn (M,N,P,...) or randn ([M,N,P...]) generate random arrays. randn with no arguments is a
scalar whose value changes each time it is referenced. randn (SIZE(A)) is the same size as A.

4. figure Create figure window.


FIGURE, by itself, creates a new figure window, and returns its handle.

Program:
% Power Spectral Density of sum of sinusoidal signal without noise
clc
clear all
fs=100;
t=0:1/fs:10;

Prepared by Thota Sravanthi Page


30
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

x=sin(2*pi*15*t)+sin(2*pi*30*t);
nfft=512;
y=fft(x,nfft);
f=fs*(0:nfft-1)/nfft;
power=y.*conj(y)/nfft;
plot(f,power);

Output:

% Power Spectral Density of sum of sinusoidal signal with noise


clc
clear all
fs=100;
t=0:1/fs:4;
x=sin(2*pi*15*t)+sin(2*pi*30*t);
x1=x+2*randn(size(t));
nfft=512;
y=fft(x1,nfft);
f=fs*(0:nfft-1)/nfft;
power=y.*conj(y)/nfft;
plot(t,x);
figure,plot(t,x1);
figure,plot(f,power);

Prepared by Thota Sravanthi Page


31
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Output:

Prepared by Thota Sravanthi Page


32
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Result: The power spectral density of a signal with & without noise has been observed &
verified.

Prepared by Thota Sravanthi Page


33
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

5. Transfer Function
Aim: To compute frequency response of a transfer function
Theory:
A transfer function (also known as the system function or network function and, when plotted as
a graph, transfer curve) is a mathematical representation, in terms of spatial or temporal
frequency, of the relation between the input and output of a linear time-invariant system with
zero initial conditions and zero-point equilibrium. Transfer functions are commonly used in the
analysis of systems such as single-input single-output filters, typically within the fields of signal
processing, communication theory, and control theory. The term is often used exclusively to
refer to linear, time-invariant systems (LTI), as covered in this article. Most real systems have
non-linear input/output characteristics, but many systems, when operated within nominal
parameters (not "over-driven") have behavior that is close enough to linear that LTI system
theory is an acceptable representation of the input/output behavior.
The descriptions below are given in terms of a complex variable, s = + j*, which bears a brief
explanation. In many applications, it is sufficient to define =0 (and s = j*), which reduces the
3-dimensional Laplace transforms to 2-dimensional Fourier transforms (where and complex
amplitude are the dimensions). The applications where this is common are ones where we are
only interested in the steady-state response of an LTI system, not the fleeting turn-on and turn-off
behaviors or stability issues. That is usually the case for signal processing and communication
theory.

Thus, for continuous-time input signal and output , the transfer function is the

linear mapping of the Laplace transform of the input, , to the Laplace

transform of the output :

or

Prepared by Thota Sravanthi Page


34
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

In discrete-time systems, the function is similarly written as (see Z-transform)


and is often referred to as the pulse-transfer function.

Inbuilt Functions:
1. tf: Creation of transfer functions or conversion to transfer function.
SYS = TF(NUM,DEN) creates a continuous-time transfer function SYS with numerator(s)
NUM and denominator(s) DEN. The output SYS is a TF object.
2. freqz: Digital filter frequency response.
[H,W] = FREQZ(B,A,N) returns the N-point complex frequency response vector H and the N-
point frequency vector W in radians/sample of the filter.
Program:
clc;
clear all;
close all;
a=[1,1,0.1]
b=[1,-0.8,1]
c=[1,2,-0.9,1]
d=[1,0,0.9,0.8]
e=tf(a,b)
f=tf(c,d)
w=0:0.01:2*pi;
[h]=freqz(a,b,w);
[h1]=freqz(c,d,w);
subplot(2,2,1);
plot(w/pi,abs(h));
subplot(2,2,2);
plot(w/pi,angle(h));
subplot(2,2,3);
plot(w/pi,abs(h1));
subplot(2,2,4);
plot(w/pi,angle(h1));

OUTPUT:

a = 1.0000 1.0000 0.1000


b = 1.0000 -0.8000 1.0000
c =1.0000 2.0000 -0.9000 1.0000

Prepared by Thota Sravanthi Page


35
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

d =1.0000 0 0.9000 0.8000


Transfer function:
s^2 + s + 0.1
---------------
s^2 - 0.8 s + 1

Transfer function:
s^3 + 2 s^2 - 0.9 s + 1
-----------------------
s^3 + 0.9 s + 0.8

Result: The frequency response of a transfer function of first & second orders has been
verified.

Prepared by Thota Sravanthi Page


36
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

6. Impulse response
Aim: To obtain the impulse response of First order & second order of a system
Theory:
In signal processing, the impulse response, or impulse response function (IRF), of a dynamic
system is its output when presented with a brief input signal, called an impulse. More generally,
an impulse response refers to the reaction of any dynamic system in response to some external
change. In both cases, the impulse response describes the reaction of the system as a function of
time (or possibly as a function of some other independent variable that parameterizes the
dynamic behavior of the system).
For example, the dynamic system might be a planetary system in orbit around a star; the external
influence in this case might be another massive object arriving from elsewhere in the galaxy; the
impulse response is the change in the motion of the planetary system caused by interaction with
the new object. In all these cases, the 'dynamic system' and its 'impulse response' may refer to
actual physical objects, or to a mathematical system of equations describing these objects.

Inbuilt Functions:
1. impz: Impulse response of digital filter [H,T] = IMPZ(B,A) computes the impulse response
of the filter B/A choosing the number of samples for you, and returns the response in column
vector H and a vector of times (or sample intervals) in T
2. length: Length of vector. LENGTH(X) returns the length of vector X. It is equivalent to
MAX(SIZE(X)) for non-empty arrays and 0 for empty ones.
Program:
clc;
close all;
clear all;
a=[1]
b=[1,1]
c=[1,1,1]
e=tf(a,b)
f=tf(a,c)
N=10;

Prepared by Thota Sravanthi Page


37
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

x=[1,zeros(1,N)]
h=impz(a,b);
h1=impz(a,c);
n=0:length(h)-1;
subplot(2,2,1);
stem(x);
xlabel('samples');
ylabel('amplitude');
title('impulse seqence');
subplot(2,2,2);
stem(n,h);
xlabel('samples');
ylabel('amplitude');
title('impulse response');
n=0:length(h1)-1;
subplot(2,2,3);
stem(x);
xlabel('samples');
ylabel('amplitude');
title('impulse seqence');
subplot(2,2,4);
stem(n,h1);
xlabel('samples');
ylabel('amplitude');
title('impulse response');

OUTPUT:
a =1
b =1 1
c =1 1 1
Transfer function:
1
-----
s+1
Transfer function:
1
-----------
s^2 + s + 1

Prepared by Thota Sravanthi Page


38
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

x =1 0 0 0 0 0 0 0 0 0 0

Result: The impulse response of first order & second order of a system has been verified &
observed.

Prepared by Thota Sravanthi Page


39
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

7. Sinusoidal signal Through Filtering


Aim: To Perform the sinusoidal signal through filtering.
Theory:
Filters are linear time-invariant (LTI) systems used to perform spectral shaping or frequency-
selective filtering. It is a technique widely used in digital signal processing applications, such as
the removal of power line hum from medical instrument signals such as EKG, enhancement of
reception quality using an array of antennas and AM/FM radio broadcasts.

Inbuilt functions:

1. sin Sine of argument in radians.


SIN(X) is the sine of the elements of X.
2. filter One-dimensional digital filter.
Y = FILTER(B,A,X) filters the data in vector X with the filter described by vectors A and B to
create the filtered data Y. The filter is a "Direct Form II Transposed" implementation of the
standard difference equation:
a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

If a(1) is not equal to 1, FILTER normalizes the filter coefficients by a(1).

FILTER always operates along the first non-singleton dimension namely dimension 1 for
column vectors and non-trivial matrices, and dimension 2 for row vectors.
Program:
clc;
close all;
clear all;
f=1000;
fs=8000;
fo=2*pi*f/fs;
t=0:1/fs:10;
x=zeros(1,length(t));
x(1)=1;
b0=1;
b1=sin(fo);
ao=1;
a1=-2*cos(fo);
a2=1;
b=[b0 b1];
a=[ao a1 a2];
y=filter(b,a,x);
N=length(y);
subplot(2,1,1);
plot(t(1:200),y(1:200));

Prepared by Thota Sravanthi Page


40
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

m=2*abs(fft(y)/N);
f1=(0:1:(N-1)/2)*fs/N;
subplot(2,1,2);
plot(f1,m(1:(N+1)/2));

OUTPUT:

Result: Suppressed & interfered signal has been observed from the noise signal

Prepared by Thota Sravanthi Page


41
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

8. DTMF
Aim: To generate the Dual Tone Multi Frequency (DTMF) of a signal
Theory:
Dual-tone multi-frequency signaling (DTMF) is used for telecommunication signaling over
analog telephone lines in the voice-frequency band between telephone handsets and other
communications devices and the switching center. The version of DTMF that is used in push-
button telephones for tone dialing is known as Touch-Tone.
1209 Hz 1336 Hz 1477 Hz
697 Hz 1 2 3
770 Hz 4 5 6
852 Hz 7 8 9
941 Hz * 0 #

Inbuilt functions:
1. zeros: Zeros array.
ZEROS(N) is an N-by-N matrix of zeros.ZEROS(M,N) or ZEROS([M,N]) is an M-by-N
matrix of zeros.
2. filter: One-dimensional digital filter.
Y = FILTER(B,A,X) filters the data in vector X with the filter described by vectors A and B
to create the filtered data Y.
Program:
clc;
clear all;
close all;
fs=8000;
t=0:1/fs:1;
x=zeros(1,length(t));
x(1)=1;
fl=852;
bl=[0 sin(2*pi*fl/fs)];
al=[1 -2*cos(2*pi*fl/fs) 1];
yl=filter(bl,al,x);
fh=1336;
bh=[0 sin(2*pi*fh/fs)];

Prepared by Thota Sravanthi Page


42
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

ah=[1 -2*cos(2*pi*fh/fs) 1];


yh=filter(bh,ah,x);
Y=yl+yh;
N=length(Y);
if(fl==697&&fh==1209)
key=1;
disp('generated DTMF tone key1');
end
if(fl==697&&fh==1336)
key=2;
disp('generated DTMF tone key2');
end
if(fl==697&&fh==1477)
key=3;
disp('generated DTMF tone key3');
end
if(fl==770&&fh==1209)
key=4;
disp('generated DTMF tone key4');
end
if(fl==770&&fh==1336)
key=5;
disp('generated DTMF tone key5');
end
if(fl==770&&fh==1477)
key=6;
disp('generated DTMF tone key6');
end
if(fl==852&&fh==1209)
key=7;
disp('generated DTMF tone key7');
end
if(fl==852&&fh==1336)
key=8;
disp('generated DTMF tone key8');
end
if(fl==852&&fh==1477)
key=9;
disp('generated DTMF tone key9');
end
if(fl==941&&fh==1209)
key='*';
disp('generated DTMF tone key*');
end
if(fl==941&&fh==1336)
key='0';
Prepared by Thota Sravanthi Page
43
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

disp('generated DTMF tone key0');


end
if(fl==941&&fh==1477)
key='#';
disp('generated DTMF tone key#');
end
subplot(2,1,1);
plot(t(1:200),Y(1:200));
M=2*abs(fft(Y))/N;
f=(0:1:(N-1)/2)*fs/N;
subplot(2,1,2);
plot(f,M(1:(N+1)/2));

OUTPUT:

Generated DTMF tone key8

Prepared by Thota Sravanthi Page


44
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

9. Decimation
Aim: To perform the decimation process
Theory:
In signal processing, downsampling (or "subsampling") is the process of reducing the sampling
rate of a signal. This is usually done to reduce the data rate or the size of the data.

The downsampling factor (commonly denoted by M) is usually an integer or a rational fraction


greater than unity. This factor multiplies the sampling time or, equivalently, divides the sampling
rate. For example, if compact disc audio at 44,100 Hz is downsampled to 22,050 Hz before
broadcasting over FM radio, the bit rate is reduced in half, from 1,411,200 bit/s to 705,600 bit/s,
assuming that each sample retains its bit depth of 16 bits. The audio was therefore downsampled
by a factor of 2.

Inbuilt functions:
1. Decimate: Resample data at a lower rate after low pass filtering.
Y = DECIMATE(X,R) resample the sequence in vector X at 1/R times the original sample rate.
The resulting resampled vector Y is R times shorter, LENGTH(Y) = LENGTH(X)/R.
Program:
clc;
clear all;
close all;
N=7;
M=3;
t=0:0.01:2*pi;
x=sin(2*pi*15*t)+sin(2*pi*30*t);
y=decimate(x,M);
subplot(2,1,1);
stem(x(1:N));
subplot(2,1,2);
stem(y(1:N/M)));

Prepared by Thota Sravanthi Page


45
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

Result: Decimation of a signal has been performed & verified.

Prepared by Thota Sravanthi Page


46
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

10. Interpolation
Aim: To perform the interpolation process
Theory:
In many practical applications of digital signal processing, one is faced with the problem of
changing the sampling rate of a signal, either increasing it or decreasing it by some amount.
An increase in the sampling rate by an integer factor L can be accomplished by interpolating
L-1 new samples between successive values of the signal. The interpolation process can be
accomplished in a variety of ways in both fields of digital signal processing or numerical
analysis.
There is one very simple and straightforward approach to changing the sampling rate of a
digital signal. This approach, called the analog approach, merely reconstructs the continuous-
time signal from the original set of samples and then resamples the signal at the new rate. In
practice this approach suffers from one major problem, namely, that the ideal operations
required reconstructing the continuous-time signal from the original samples and to resample
the signal at the new rate cannot be implemented exactly [1]. An alternative is the so-called
direct digital approach that does the job without going through D/A or A/D conversion.
Inbuilt functions:
1. interp: Resample data at a higher rate using lowpass interpolation.
Y = INTERP(X,R) resamples the sequence in vector X at R times the original sample rate. The
resulting resampled vector Y is R times longer, LENGTH(Y) = R*LENGTH(X).
Program:
clc;
close all;
clear all;
N=7;
L=3;
t=0:0.01:2*pi;
x=sin(2*pi*15*t)+sin(2*pi*30*t);
y=interp(x,L);
subplot(2,1,1);
stem(x(1:N));
subplot(2,1,2);
stem(y(1:N*L));

Prepared by Thota Sravanthi Page


47
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

Result: Decimation of a signal has been performed & verified.

Prepared by Thota Sravanthi Page


48
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

11. Noise Removal


Aim: To add noise above 3 KHz removing the interference & suppression by a 400Hz tone
Theory:
Noise reduction is the process of removing noise from a signal. All recording devices, both
analogue or digital, have traits which make them susceptible to noise. Noise can be random or
white noise with no coherence, or coherent noise introduced by the device's mechanism or
processing algorithms.

In electronic recording devices, a major form of noise is hiss caused by random electrons that,
heavily influenced by heat, stray from their designated path. These stray electrons influence the
voltage of the output signal and thus create detectable noise.

In the case of photographic film and magnetic tape, noise (both visible and audible) is introduced
due to the grain structure of the medium. In photographic film, the size of the grains in the film
determines the film's sensitivity, more sensitive film having larger sized grains. In magnetic tape,
the larger the grains of the magnetic particles (usually ferric oxide or magnetite), the more prone
the medium is to noise. To compensate for this, larger areas of film or magnetic tape may be used
to lower the noise to an acceptable level.

Inbuilt functions:
1. fir1 FIR filter design using the window method.
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 linear phase. The
normalized gain of the filter at Wn is -6 dB.

2. 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.

3. sin Sine of argument in radians.


SIN(X) is the sine of the elements of X.
4. filter One-dimensional digital filter.
Y = FILTER(B,A,X) filters the data in vector X with the filter described by vectors A and B to
create the filtered data Y. The filter is a "Direct Form II Transposed" implementation of the
standard difference equation:
a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb) - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

If a(1) is not equal to 1, FILTER normalizes the filter coefficients by a(1).

FILTER always operates along the first non-singleton dimension namely dimension 1 for
column vectors and non-trivial matrices, and dimension 2 for row vectors.

Prepared by Thota Sravanthi Page


49
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Program:
clc;
close all;
clear all;
N=200;
fs=800;
f1=100;
f2=400;
n=0:N-1;
x=3*sin(2*pi*f1*n/fs)+2*sin(2*pi*f2*n/fs);
fc=400;
b=fir1(55,fc/fs);
y=filter(b,1,x);
subplot(3,1,1);
plot(n,x,n,y,'g');
a=length(x);
m=2*abs(fft(x)/a);
f=(1:a/2)*fs/a;
subplot(3,1,2);
plot(f,m(1:a/2));
N1=length(y);
m1=2*abs(fft(y)/N1);
f1=(1:N1/2)*fs/N1;
subplot(3,1,3);
plot(f,m1(1:a/2));

Prepared by Thota Sravanthi Page


50
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

Result: Suppressed & interfered signal has been observed from the noise signal.

Prepared by Thota Sravanthi Page


51
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

12. IIR Digital butterworth Filter Design

Aim: To write a program of IIR digital butterworth filters


a) Butterworth low pass filter
b) Butterworth high pass filter
c) Butterworth band pass filter
d) Butterworth band stop filter
Theory:
The Butterworth filter is a type of signal processing filter designed to have as flat a
frequency response as possible in the passband. It is also referred to as a maximally flat
magnitude filter Butterworth had a reputation for solving "impossible" mathematical problems.
At the time, filter design required a considerable amount of designer experience due to
limitations of the theory then in use. The filter was not in common use for over 30 years after its
publication. Butterworth stated that: "An ideal electrical filter should not only completely reject
the unwanted frequencies but should also have uniform sensitivity for the wanted frequencies".
Such an ideal filter cannot be achieved but Butterworth showed that successively closer
approximations were obtained with increasing numbers of filter elements of the right values. At
the time, filters generated substantial ripple in the passband, and the choice of component values
was highly interactive. Butterworth showed that a low pass filter could be designed whose cutoff
frequency was normalized to 1 radian per second and whose frequency response (gain) was

where is the angular frequency in radians per second and n is the number of poles in the filter
equal to the number of reactive elements in a passive filter.

Inbuilt functoions:
1. Buttord
syntax:
[n,Wn] = buttord(Wp,Ws,Rp,Rs)
[n,Wn] = buttord(Wp,Ws,Rp,Rs,'s')

Description

Prepared by Thota Sravanthi Page


52
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

buttord calculates the minimum order of a digital or analog Butterworth filter required to meet a
set of filter design specifications

2. Butter
hd = butter(d) designs a Butterworth IIR digital filter using the specifications supplied in the
object d.

hd = butter(d,'matchexactly',match) returns a Butterworth IIR filter where the filter response


matches the specified response exactly for one filter band. match, which specifies which filter
band to match, is either passband--match the passband specification exactly in the final filter.
stopband--match the specified stopband performance exactly in the final filter. This is the default
setting.
3. freqz
Syntax
[h,w] = freqz(ha)
Description
The next sections describe common freqz operation with adaptive, discrete-time, and multirate
filters. For more input options, refer to freqz in the Signal Processing Toolbox.

Program:
% Butterworth low pass filter
clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1500;
ws=2000;
rp=0.3;
rs=16;
F=fs/2;
w1=wp/F;

Prepared by Thota Sravanthi Page


53
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

w2=ws/F;
[n,wc]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wc);
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using butterworth');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

Prepared by Thota Sravanthi Page


54
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Butterworth high pass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1500;
ws=2000;
rp=0.3;
rs=16;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wc,'high');
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using butterworth');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

Prepared by Thota Sravanthi Page


55
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT

% Butterworth bandpass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1500;
ws=2000;
rp=0.3;
rs=16;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=buttord(w1,w2,rp,rs);

Prepared by Thota Sravanthi Page


56
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

[b,a]=butter(n,wc,'bandpass');
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using butterworth');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

Prepared by Thota Sravanthi Page


57
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Butterworth bandstop filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1500;
ws=2000;
rp=0.3;
rs=16;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=buttord(w1,w2,rp,rs);
[b,a]=butter(n,wc,'bandstop');
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using butterworth');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

Prepared by Thota Sravanthi Page


58
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

Result: IIR digital butterworth filter has been verified successfully.

Prepared by Thota Sravanthi Page


59
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

13. IIR Digital Chebyshev type-I Filter Design

Aim: To write a program of IIR digital Chebyshev type-I filters


a) Chebyshev type-I low pass filter
b) Chebyshev type-I high pass filter
c) Chebyshev type-I band pass filter
d) Chebyshev type-I band stop filter
Thoery:
Chebyshev filters are analog or digital filters having a steeper roll-off and more passband
ripple (type I) or stopband ripple (type II) than Butterworth filters. Chebyshev filters have the
property that they minimize the error between the idealized and the actual filter characteristic
over the range of the filter, but with ripples in the passband. This type of filter is named after
Pafnuty Chebyshev because its mathematical characteristics are derived from Chebyshev
polynomials. Because of the passband ripple inherent in Chebyshev filters, the ones that have a
smoother response in the passband but a more irregular response in the stopband are preferred
for some applications.

These are the most common Chebyshev filters. The gain (or amplitude) response as a function of
angular frequency of the nth-order low-pass filter is

where is the ripple factor, is the cutoff frequency and is a Chebyshev polynomial of the
th order.

Inbuit Functions:
1. Cheb1ord:
Syntax
[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs)
[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs,'s')

Description
cheb1ord calculates the minimum order of a digital or analog Chebyshev Type I filter required to
meet a set of filter design specifications.

2.Cheby1:
Prepared by Thota Sravanthi Page
60
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Syntax
hd = cheby1(d)
hd = cheby1(d,'matchexactly',match)

Description
hd = cheby1(d) designs a Chebyshev I IIR digital filter using the specifications supplied in the
object d.
3. freqz
Syntax
[h,w] = freqz(ha)
Description
The next sections describe common freqz operation with adaptive, discrete-time, and multirate
filters. For more input options, refer to freqz in the Signal Processing Toolbox.

Program:
% Chebyshev type-I low pass filter
clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wc);
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);

Prepared by Thota Sravanthi Page


61
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

plot(f_Hz,m);
title('frequency response of highpass using chebyshev type1');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

% Chebyshev type-I high pass filter


clc;

Prepared by Thota Sravanthi Page


62
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wc,high);
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using chebyshev type1');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

Prepared by Thota Sravanthi Page


63
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

% Chebyshev type-I bandpass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
wp=1400;

Prepared by Thota Sravanthi Page


64
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
wc=[w1,w2];
[n]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wc,'bandpass');
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);

OUTPUT:

Prepared by Thota Sravanthi Page


65
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Chebyshev type-I bandstop filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
wc=[w1,w2];
[n]=cheb1ord(w1,w2,rp,rs);
[b,a]=cheby1(n,rp,wc,'stop');
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);

Prepared by Thota Sravanthi Page


66
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

Result: IIR digital chebyshev type-I filter has been verified successfully.

Prepared by Thota Sravanthi Page


67
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

14. IIR Digital Chebyshev type-II Filter Design

Aim: To write a program of IIR digital filters


a) Chebyshev type-II low pass filter
b) Chebyshev type-II high pass filter
c) Chebyshev type-II band pass filter
d) Chebyshev type-II band stop filter
Thoery:
Chebyshev filters are analog or digital filters having a steeper roll-off and more passband ripple
(type I) or stopband ripple (type II) than Butterworth filters. Chebyshev filters have the property
that they minimize the error between the idealized and the actual filter characteristic over the
range of the filter, but with ripples in the passband. This type of filter is named after Pafnuty
Chebyshev because its mathematical characteristics are derived from Chebyshev polynomials.

Because of the passband ripple inherent in Chebyshev filters, the ones that have a smoother
response in the passband but a more irregular response in the stopband are preferred for some
applic Also known as inverse Chebyshev, this type is less common because it does not roll off as
fast as type I, and requires more components. It has no ripple in the passband, but does have
equiripple in the stopband. The gain is:

In the stopband, the Chebyshev polynomial will oscillate between 0 and 1

Inbuilt Functions:

1. cheb2ord:
Syntax
[n,Wn] = cheb2ord(Wp,Ws,Rp,Rs)
[n,Wn] = cheb2ord(Wp,Ws,Rp,Rs,'s')

Description
cheb2ord calculates the minimum order of a digital or analog Chebyshev Type II filter required
to meet a set of filter design specifications

2. cheby2:

Prepared by Thota Sravanthi Page


68
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Syntax
hd = cheby2(d)
hd = cheby2(d,'matchexactly',match)

Description
hd = cheby2(d) designs a Chebyshev II IIR digital filter using the specifications supplied in the
object d.
3. freqz
Syntax
[h,w] = freqz(ha)
Description
The next sections describe common freqz operation with adaptive, discrete-time, and multirate
filters. For more input options, refer to freqz in the Signal Processing Toolbox.

Program:
% Chebyshev type-II low pass filter
clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=cheb2ord(w1,w2,rp,rs);
[b,a]=cheby2(n,rs,wc);
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
Prepared by Thota Sravanthi Page
69
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

plot(f_Hz,m);
title('frequency response of highpass using chebyshev type1');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

Prepared by Thota Sravanthi Page


70
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Chebyshev type-II highpass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=cheb2ord(w1,w2,rp,rs);
[b,a]=cheby2(n,rs,wc,high);
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using chebyshev type1');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

Prepared by Thota Sravanthi Page


71
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

% Chebyshev type-II bandpass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
Prepared by Thota Sravanthi Page
72
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
wc=[w1,w2];
[n]=cheb2ord(w1,w2,rp,rs);
[b,a]=cheby2(n,rs,wc,'bandpass');
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);

Prepared by Thota Sravanthi Page


73
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Chebyshev type-II band stop filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
wc=[w1,w2];
[n]=cheb2ord(w1,w2,rp,rs);
[b,a]=cheby2(n,rs,wc,'stop');
[H,f_Hz]=freqz(b,a,512,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);

Prepared by Thota Sravanthi Page


74
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

Result: IIR digital chebyshev type-II filter has been verified successfully.

Prepared by Thota Sravanthi Page


75
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

15. IIR Analog Butterworth Filter Design

Aim: To write a program of IIR digital butterworth filters


e) Butterworth low pass filter
f) Butterworth high pass filter
g) Butterworth band pass filter
h) Butterworth band stop filter
Theory:
The Butterworth filter is a type of signal processing filter designed to have as flat a
frequency response as possible in the passband. It is also referred to as a maximally flat
magnitude filter Butterworth had a reputation for solving "impossible" mathematical problems.
At the time, filter design required a considerable amount of designer experience due to
limitations of the theory then in use. The filter was not in common use for over 30 years after its
publication. Butterworth stated that: "An ideal electrical filter should not only completely reject
the unwanted frequencies but should also have uniform sensitivity for the wanted frequencies".
Such an ideal filter cannot be achieved but Butterworth showed that successively closer
approximations were obtained with increasing numbers of filter elements of the right values.

Inbuilt functions:
1. Buttord
syntax:
[n,Wn] = buttord(Wp,Ws,Rp,Rs)
[n,Wn] = buttord(Wp,Ws,Rp,Rs,'s')

Description
buttord calculates the minimum order of a digital or analog Butterworth filter required to meet a
set of filter design specifications
2. Butter
hd = butter(d) designs a Butterworth IIR digital filter using the specifications supplied in the
object d.

Prepared by Thota Sravanthi Page


76
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

hd = butter(d,'matchexactly',match) returns a Butterworth IIR filter where the filter response


matches the specified response exactly for one filter band. match, which specifies which filter
band to match, is either passband--match the passband specification exactly in the final filter.
stopband--match the specified stopband performance exactly in the final filter. This is the default
setting.
3. freqz
Syntax
[h,w] = freqz(ha)
Description
The next sections describe common freqz operation with adaptive, discrete-time, and multirate
filters. For more input options, refer to freqz in the Signal Processing Toolbox.

Program:
% Butterworth low pass filter
clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1500;
ws=2000;
rp=0.3;
rs=16;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=buttord(w1,w2,rp,rs,s);
[b,a]=butter(n,wc,s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);

Prepared by Thota Sravanthi Page


77
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

title('frequency response of highpass using butterworth');


xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

b) % Butterworth high pass filter


clc;
close all;
clear all;
fs=8000;

Prepared by Thota Sravanthi Page


78
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1500;
ws=2000;
rp=0.3;
rs=16;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=buttord(w1,w2,rp,rs,s);
[b,a]=butter(n,wc,'high',s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using butterworth');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

Prepared by Thota Sravanthi Page


79
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

% Butterworth bandpass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
Prepared by Thota Sravanthi Page
80
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

title('discrete signal');
xlabel('f');
ylabel('X');
wp=1500;
ws=2000;
rp=0.3;
rs=16;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=buttord(w1,w2,rp,rs,s);
[b,a]=butter(n,wc,'bandpass',s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using butterworth');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

Prepared by Thota Sravanthi Page


81
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Butterworth bandstop filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1500;
ws=2000;
rp=0.3;
rs=16;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=buttord(w1,w2,rp,rs,s);

Prepared by Thota Sravanthi Page


82
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

[b,a]=butter(n,wc,'bandstop',s);
[H,f_Hz]=freqs(b,a,fs,s);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using butterworth');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

Prepared by Thota Sravanthi Page


83
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Result: Analog Butterworth filter has been verified.

16. IIR Analog Chebyshev type-I Filter Design

Aim: To write a program of IIR digital Chebyshev type-I filters


e) Chebyshev type-I low pass filter
f) Chebyshev type-I high pass filter
g) Chebyshev type-I band pass filter
h) Chebyshev type-I band stop filter
Thoery:
Chebyshev filters are analog or digital filters having a steeper roll-off and more passband ripple
(type I) or stopband ripple (type II) than Butterworth filters. Chebyshev filters have the property
that they minimize the error between the idealized and the actual filter characteristic over the
range of the filter, but with ripples in the passband. This type of filter is named after Pafnuty
Chebyshev because its mathematical characteristics are derived from Chebyshev polynomials.

Because of the passband ripple inherent in Chebyshev filters, the ones that have a smoother
response in the passband but a more irregular response in the stopband are preferred for some

Inbuit Functions:
1. Cheb1ord:
Syntax

Prepared by Thota Sravanthi Page


84
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs)
[n,Wn] = cheb1ord(Wp,Ws,Rp,Rs,'s')

Description
cheb1ord calculates the minimum order of a digital or analog Chebyshev Type I filter required to
meet a set of filter design specifications.

2.Cheby1:
Syntax
hd = cheby1(d)
hd = cheby1(d,'matchexactly',match)

Description
hd = cheby1(d) designs a Chebyshev I IIR digital filter using the specifications supplied in the
object d.
3. freqz
Syntax
[h,w] = freqz(ha)
Description
The next sections describe common freqz operation with adaptive, discrete-time, and multirate
filters. For more input options, refer to freqz in the Signal Processing Toolbox.

Program:
% Chebyshev type-I low pass filter
clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1400;
ws=1800;

Prepared by Thota Sravanthi Page


85
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=cheb1ord(w1,w2,rp,rs,s);
[b,a]=cheby1(n,rp,wc,s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using chebyshev type1');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

Prepared by Thota Sravanthi Page


86
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Chebyshev type-I high pass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1400;
Prepared by Thota Sravanthi Page
87
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=cheb1ord(w1,w2,rp,rs,s);
[b,a]=cheby1(n,rp,wc,high,s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using chebyshev type1');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:

Prepared by Thota Sravanthi Page


88
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Chebyshev type-I bandpass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
wp=1400;
ws=1800;
rp=0.25;
rs=40;

Prepared by Thota Sravanthi Page


89
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

F=fs/2;
w1=wp/F;
w2=ws/F;
wc=[w1,w2];
[n]=cheb1ord(w1,w2,rp,rs,s);
[b,a]=cheby1(n,rp,wc,'bandpass',s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);

OUTPUT:

% Chebyshev type-I bandstop filter

Prepared by Thota Sravanthi Page


90
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
wc=[w1,w2];
[n]=cheb1ord(w1,w2,rp,rs,s);
[b,a]=cheby1(n,rp,wc,'stop',s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);

OUTPUT:

Prepared by Thota Sravanthi Page


91
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

RESULT: Analog chebyshev type-I filter has been verified.

17. IIR Analog Chebyshev type-II Filter Design


Prepared by Thota Sravanthi Page
92
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Aim: To write a program of IIR analog filters


e) Chebyshev type-II low pass filter
f) Chebyshev type-II high pass filter
g) Chebyshev type-II band pass filter
h) Chebyshev type-II band stop filter
Theory:
Chebyshev filters are analog or digital filters having a steeper roll-off and more passband ripple
(type I) or stopband ripple (type II) than Butterworth filters. Chebyshev filters have the property
that they minimize the error between the idealized and the actual filter characteristic over the
range of the filter, but with ripples in the passband. This type of filter is named after Pafnuty
Chebyshev because its mathematical characteristics are derived from Chebyshev polynomials.

Because of the passband ripple inherent in Chebyshev filters, the ones that have a smoother
response in the passband but a more irregular response in the stopband are preferred for some
applic Also known as inverse Chebyshev, this type is less common because it does not roll off as
fast as type I, and requires more components.

Inbuilt Functions:

1. cheb2ord:
Syntax
[n,Wn] = cheb2ord(Wp,Ws,Rp,Rs)
[n,Wn] = cheb2ord(Wp,Ws,Rp,Rs,'s')

Description
cheb2ord calculates the minimum order of a digital or analog Chebyshev Type II filter required
to meet a set of filter design specifications

2. cheby2:
Syntax
hd = cheby2(d)
hd = cheby2(d,'matchexactly',match)

Description
hd = cheby2(d) designs a Chebyshev II IIR digital filter using the specifications supplied in the
object d.
3. freqz
Syntax
[h,w] = freqz(ha)
Description

Prepared by Thota Sravanthi Page


93
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

The next sections describe common freqz operation with adaptive, discrete-time, and multirate
filters. For more input options, refer to freqz in the Signal Processing Toolbox.

Program:
% Chebyshev type-II low pass filter
clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
title('discrete signal');
xlabel('f');
ylabel('X');
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=cheb2ord(w1,w2,rp,rs,s);
[b,a]=cheby2(n,rs,wc,s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using chebyshev type1');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');

Prepared by Thota Sravanthi Page


94
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

ylabel('Y');

OUTPUT:

% Chebyshev type-II highpass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
Prepared by Thota Sravanthi Page
95
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

title('discrete signal');
xlabel('f');
ylabel('X');
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
[n,wc]=cheb2ord(w1,w2,rp,rs,s);
[b,a]=cheby2(n,rs,wc,high,s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
title('frequency response of highpass using chebyshev type1');
xlabel('f');
ylabel('m');
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);
title('inverse signal');
xlabel('f');
ylabel('Y');

OUTPUT:
Prepared by Thota Sravanthi Page
96
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Chebyshev type-II bandpass filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
wp=1400;
ws=1800;
rp=0.25;
rs=40;
Prepared by Thota Sravanthi Page
97
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

F=fs/2;
w1=wp/F;
w2=ws/F;
wc=[w1,w2];
[n]=cheb2ord(w1,w2,rp,rs,s);
[b,a]=cheby2(n,rs,wc,'bandpass',s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);

OUTPUT:

Prepared by Thota Sravanthi Page


98
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

% Chebyshev type-II band stop filter


clc;
close all;
clear all;
fs=8000;
N=1024;
n=0:N-1;
f1=500;
f2=2000;
x=3*sin(2*pi*f1*n/fs)+cos(2*pi*f2*n/fs);
X=2*abs(fft(x,N))/N;
f=[0:1:(N-1)]*fs/N;
subplot(3,1,1);
plot(f,X);
wp=1400;
ws=1800;
rp=0.25;
rs=40;
F=fs/2;
w1=wp/F;
w2=ws/F;
wc=[w1,w2];
[n]=cheb2ord(w1,w2,rp,rs,s);
[b,a]=cheby2(n,rs,wc,'stop',s);
[H,f_Hz]=freqs(b,a,fs);
m=20*log10(abs(H));
subplot(3,1,2);
plot(f_Hz,m);
y=filter(b,a,x);
Y=2*abs(fft(y,N))/N;
f=(0:1:N-1)*fs/N;
subplot(3,1,3);
plot(f,Y);

OUTPUT:

Prepared by Thota Sravanthi Page


99
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Result: Analog Chebyshev type-II filter has been verified.

Prepared by Thota Sravanthi Page


100
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

18. Frequency Response of windows

Aim: To obtain the frequency Response of windows.


Theory:
Rectangular Window
The rectangular window is defined as

Definition ( odd):

Transform:

The DTFT of a rectangular window is shown in Fig

Bartlett (``Triangular'') Window

Definition:

Transform:

Prepared by Thota Sravanthi Page


101
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

The DTFT of a bartlett window is shown in Fig

Hamming window
Definition:
1 2 n
w[n] = 1 - cos , 0 n M

2 M

The DTFT of a hamming window is shown in Fig

Hanning window

Prepared by Thota Sravanthi Page


102
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

If y represents the output sequence Hanning {X}, the Hanning Window VI obtains the elements
of y from
yi = xi[0.54 0.46cos(w)]

for i = 0, 1, 2, , n 1,
where n is the number of elements in the input sequence X.
The DTFT of a hanning window is shown in Fig

Blackman window
2 n 4 n
w[n] =
0.42 - 0.5cos + 0.08cos
M M

The DTFT of a blackman window is shown in Fig

Prepared by Thota Sravanthi Page


103
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Inbuilt functions:
1.boxcar: rectangular window
Syntax: m=boxcar (N) returns N th order rectangular window
2.bartlett: triangular window
Syntax: m=boxcar (N) returns N th order bartlettwindow
3.hamming :hamming window
Syntax: m=boxcar (N) returns N th order hamming window
4.hanning: hanning window
Syntax: m=boxcar (N) returns N th order hanning window
5.blackman: blackman window
Syntax: m=boxcar (N) returns N th order blackman window

Program:
%Response of windows
clc;
close all;
Prepared by Thota Sravanthi Page
104
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

clear all;
N=25;
n=0:1:N-1;
%Rectangular window
w1=boxcar(N);
subplot(5,2,1);
plot(n,w1);
w=0:0.01:pi;
h1=freqz(w1,1,w);
subplot(5,2,2);
plot(abs(h1));
%triangular window
w2=bartlett(N);
subplot(5,2,3);
plot(n,w2);
w=0:0.01:pi;
h2=freqz(w2,1,w);
subplot(5,2,4);
plot(abs(h2));
%hamming window
w3= hamming (N);
subplot(5,2,5);
plot(n,w3);
w=0:0.01:pi;
h3=freqz(w3,1,w);
subplot(5,2,6);
plot(abs(h3));
%hanning window
w4= hanning (N);
subplot(5,2,7);
plot(n,w4);
Prepared by Thota Sravanthi Page
105
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

w=0:0.01:pi;
h4=freqz(w4,1,w);
subplot(5,2,8);
plot(abs(h4));
%blackman window
w5= blackman (N);
subplot(5,2,9);
plot(n,w5);
w=0:0.01:pi;
h5=freqz(w5,1,w);
subplot(5,2,10);
plot(abs(h5));
OUTPUT:

Result: frequency response of windowing sequence has been verified.

Prepared by Thota Sravanthi Page


106
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

19. FIR Filter Design using windows

Aim: To write a program of FIR Filter design using windows


a) Low pass filter using windows
b) High pass filter using windows
c) Band pass filter using windows
d) Band reject filter using windows
Theory:
Rectangular Window
The rectangular window is defined as

Definition ( odd):

Transform:

Bartlett (``Triangular'') Window

Definition:

Transform:

Prepared by Thota Sravanthi Page


107
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Hamming window
Definition:
1 2 n
w[n] = 1 - cos , 0 n M

2 M

Hanning window
If y represents the output sequence Hanning {X}, the Hanning Window VI obtains the elements
of y from
yi = xi[0.54 0.46cos(w)]

for i = 0, 1, 2, , n 1,
where n is the number of elements in the input sequence X.
Blackman window
2 n 4 n
w[n] =
0.42 - 0.5cos + 0.08cos
M M

Inbuilt functions:
1.boxcar: rectangular window
Syntax: m=boxcar (N) returns N th order rectangular window
2.bartlett: triangular window
Syntax: m=boxcar (N) returns N th order bartlettwindow
3.hamming :hamming window
Syntax: m=boxcar (N) returns N th order hamming window
4.hanning: hanning window
Syntax: m=boxcar (N) returns N th order hanning window
5.blackman: blackman window
Syntax: m=boxcar (N) returns N th order blackman window

Prepared by Thota Sravanthi Page


108
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Program:
%Low pass filter using windows
clc;
close all;
clear all;
N=25;
n=0:1:N-1;
a=(N-1)/2;
e=0.001;
wc=0.5*pi;
hd=(sin(wc*(n-a+e)))./(pi*(n-a+e));
%Rectangular window
w1=boxcar(N);
hn=hd.*w1;
w=0:0.01:pi;
h=freqz(hn,1,w);
subplot(3,2,1);
plot(abs(h));
%triangular window
w2=bartlett(N);
hn1=hd.*w2;
w=0:0.01:pi;
h1=freqz(hn,1,w);
subplot(3,2,2);
plot(abs(h1));
%hamming window
w3= hamming (N);
hn2=hd.*w3;
w=0:0.01:pi;
h2=freqz(hn2,1,w);

Prepared by Thota Sravanthi Page


109
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

subplot(3,2,3);
plot(abs(h2));
%hanning window
w4= hanning (N);
hn3=hd.*w4;
w=0:0.01:pi;
h3=freqz(hn3,1,w);
subplot(3,2,4);
plot(abs(h3));
%blackman window
w5= blackman (N);
hn4=hd.*w5;
w=0:0.01:pi;
h4=freqz(hn4,1,w);
subplot(3,2,5);
plot(abs(h4));

Prepared by Thota Sravanthi Page


110
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

OUTPUT:

%High pass filter using windows


clc;
close all;
clear all;
N=25;
n=0:1:N-1;
a=(N-1)/2;
e=0.001;
wc=0.5*pi;
hd=(sin(wc*(n-a+e))-(sin(pi*(n-a+e))))./(pi*(n-a+e));
%Rectangular window
w1=boxcar(N);

Prepared by Thota Sravanthi Page


111
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

hn=hd.*w1;
w=0:0.01:pi;
h=freqz(hn,1,w);
subplot(3,2,1);
plot(abs(h));
%triangular window
w2=bartlett(N);
hn1=hd.*w2;
w=0:0.01:pi;
h1=freqz(hn,1,w);
subplot(3,2,2);
plot(abs(h1));
%hamming window
w3= hamming (N);
hn2=hd.*w3;
w=0:0.01:pi;
h2=freqz(hn2,1,w);
subplot(3,2,3);
plot(abs(h2));
%hanning window
w4= hanning (N);
hn3=hd.*w4;
w=0:0.01:pi;
h3=freqz(hn3,1,w);
subplot(3,2,4);
plot(abs(h3));
%blackman window
w5= blackman (N);
hn4=hd.*w5;
w=0:0.01:pi;
h4=freqz(hn4,1,w);
Prepared by Thota Sravanthi Page
112
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

subplot(3,2,5);
plot(abs(h4));
output

%bandpasspass filter using windows


clc;
close all;
clear all;
N=25;
n=0:1:N-1;
a=(N-1)/2;
e=0.001;
wc1=0.5*pi;

Prepared by Thota Sravanthi Page


113
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

wc2=0.7*pi;
hd=(sin(wc2*(n-a+e))-(sin(wc1*(n-a+e))))./(pi*(n-a+e));
%Rectangular window
w1=boxcar(N);
hn=hd.*w1;
w=0:0.01:pi;
h=freqz(hn,1,w);
subplot(3,2,1);
plot(abs(h));
%triangular window
w2=bartlett(N);
hn1=hd.*w2;
w=0:0.01:pi;
h1=freqz(hn,1,w);
subplot(3,2,2);
plot(abs(h1));
%hamming window
w3= hamming (N);
hn2=hd.*w3;
w=0:0.01:pi;
h2=freqz(hn2,1,w);
subplot(3,2,3);
plot(abs(h2));
%hanning window
w4= hanning (N);
hn3=hd.*w4;
w=0:0.01:pi;
h3=freqz(hn3,1,w);
subplot(3,2,4);
plot(abs(h3));
%blackman window
Prepared by Thota Sravanthi Page
114
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

w5= blackman (N);


hn4=hd.*w5;
w=0:0.01:pi;
h4=freqz(hn4,1,w);
subplot(3,2,5);
plot(abs(h4));
OUTPUT

%bandstop filter using windows


clc;
close all;
clear all;
N=25;
n=0:1:N-1;
a=(N-1)/2;
e=0.001;
wc1=0.5*pi;

Prepared by Thota Sravanthi Page


115
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

wc2=0.7*pi;
hd=(sin(wc1*(n-a+e))-(sin(wc2*(n-a+e)))+(sin(pi*(n-a+e))))./(pi*(n-a+e));
%Rectangular window
w1=boxcar(N);
hn=hd.*w1;
w=0:0.01:pi;
h=freqz(hn,1,w);
subplot(3,2,1);
plot(abs(h));
%triangular window
w2=bartlett(N);
hn1=hd.*w2;
w=0:0.01:pi;
h1=freqz(hn,1,w);
subplot(3,2,2);
plot(abs(h1));
%hamming window
w3= hamming (N);
hn2=hd.*w3;
w=0:0.01:pi;
h2=freqz(hn2,1,w);
subplot(3,2,3);
plot(abs(h2));
%hanning window
w4= hanning (N);
hn3=hd.*w4;
w=0:0.01:pi;
h3=freqz(hn3,1,w);
subplot(3,2,4);
plot(abs(h3));
%blackman window
Prepared by Thota Sravanthi Page
116
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

w5= blackman (N);


hn4=hd.*w5;
w=0:0.01:pi;
h4=freqz(hn4,1,w);
subplot(3,2,5);
plot(abs(h4));

Output:

Result: FIR filter design using windows has been verified.

Prepared by Thota Sravanthi Page


117
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

20. Architecture of DSP chips-TMS 320C 6713 DSP


Processor

A signal can be defined as a function that conveys information, generally about the state or
behavior of a physical system. There are two basic types of signals viz Analog (continuous time
signals which are defined along a continuum of times) and Digital (discrete-time).
Remarkably, under reasonable constraints, a continuous time signal can be adequately
represented by samples, obtaining discrete time signals. Thus digital signal processing is an ideal
choice for anyone who needs the performance advantage of digital manipulation along with
todays analog reality.

Hence a processor which is designed to perform the special operations(digital


manipulations) on the digital signal within very less time can be called as a Digital signal
processor. The difference between a DSP processor, conventional microprocessor and a
microcontroller are listed below.

Microprocessor or General Purpose Processor such as Intel xx86 or Motorola 680xx family
Contains - only CPU
-No RAM
-No ROM
-No I/O ports
-No Timer
Microcontroller such as 8051 family
Contains - CPU
- RAM
- ROM
-I/O ports
- Timer &
- Interrupt circuitry
Some Micro Controllers also contain A/D, D/A and Flash Memory

DSP Processors such as Texas instruments and Analog Devices


Contains - CPU
- RAM
-ROM
- I/O ports
- Timer
Optimized for fast arithmetic
- Extended precision
- Dual operand fetch
- Zero overhead loop
- Circular buffering

Prepared by Thota Sravanthi Page


118
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

The basic features of a DSP Processor are

Feature Use
Fast-Multiply accumulate Most DSP algorithms, including filtering, transforms, etc. are
multiplication- intensive

Multiple access memory Many data-intensive DSP operations require reading a program
architecture instruction and multiple data items during each instruction cycle
for best performance

Specialized addressing modes Efficient handling of data arrays and first-in, first-out buffers in
memory
Specialized program control Efficient control of loops for many iterative DSP algorithms.
Fast interrupt handling for frequent I/O operations.

On-chip peripherals and I/O On-chip peripherals like A/D converters allow for small low cost
interfaces system designs. Similarly I/O interfaces tailored for common
peripherals allow clean interfaces to off-chip I/O devices.

ARCHITECTURE OF 6713 DSP PROCESSOR

This chapter provides an overview of the architectural structure of the TMS320C67xx DSP,
which comprises the central processing unit (CPU), memory, and on-chip peripherals. The
C67xE DSPs use an advanced modified Harvard architecture that maximizes processing power
with eight buses. Separate program and data spaces allow simultaneous access to program
instructions and data, providing a high degree of parallelism. For example, three reads and one
write can be performed in a single cycle. Instructions with parallel store and application-specific
instructions fully utilize this architecture. In addition, data can be transferred between data and
program spaces. Such
Parallelism supports a powerful set of arithmetic, logic, and bit-manipulation operations that can
all be performed in a single machine cycle. Also, the C67xx DSP includes the control
mechanisms to manage interrupts, repeated operations, and function calling.

Prepared by Thota Sravanthi Page


119
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Fig BLOCK DIAGRAM OF TMS 320VC 6713

Bus Structure

The C67xx DSP architecture is built around eight major 16-bit buses (four program/data buses
and four address buses):
_ The program bus (PB) carries the instruction code and immediate operands from program
memory.
_ Three data buses (CB, DB, and EB) interconnect to various elements, such as the CPU, data
address generation logic, program address generation logic, on-chip peripherals, and data
memory.
_ The CB and DB carry the operands that are read from data memory.
_ The EB carries the data to be written to memory.
_ Four address buses (PAB, CAB, DAB, and EAB) carry the addresses needed for instruction
execution.
Prepared by Thota Sravanthi Page
120
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

The C67xx DSP can generate up to two data-memory addresses per cycle using the two auxiliary
register arithmetic units (ARAU0 and ARAU1). The PB can carry data operands stored in
program space (for instance, a coefficient table) to the multiplier and adder for
multiply/accumulate operations or to a destination in data space for data move instructions
(MVPD and READA). This capability, in conjunction with the feature of dual-operand read,
supports the execution of single-cycle, 3-operand instructions such as the FIRS instruction. The
C67xx DSP also has an on-chip bidirectional bus for accessing on-chip peripherals. This bus is
connected to DB and EB through the bus exchanger in the CPU interface. Accesses that use this
bus can require two or more cycles for reads and writes, depending on the peripherals structure.

Central Processing Unit (CPU)

The CPU is common to all C67xE devices. The C67x CPU contains:

_ 40-bit arithmetic logic unit (ALU)


_ Two 40-bit accumulators
_ Barrel shifter
_ 17 17-bit multiplier
_ 40-bit adder
_ Compare, select, and store unit (CSSU)
_ Data address generation unit
_ Program address generation unit

Arithmetic Logic Unit (ALU)

The C67x DSP performs 2s-complement arithmetic with a 40-bit arithmetic logic unit (ALU) and
two 40-bit accumulators (accumulators A and B). The ALU can also perform Boolean operations.
The ALU uses these inputs:

_ 16-bit immediate value


_ 16-bit word from data memory
_ 16-bit value in the temporary register, T
_ Two 16-bit words from data memory
_ 32-bit word from data memory
_ 40-bit word from either accumulator

The ALU can also function as two 16-bit ALUs and perform two 16-bit operations
simultaneously.

Prepared by Thota Sravanthi Page


121
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Fig ALU UNIT


Accumulators

Accumulators A and B store the output from the ALU or the multiplier/adder block. They can
also provide a second input to the ALU; accumulator A can be an input to the multiplier/adder.
Each accumulator is divided into three parts:
_ Guard bits (bits 3932)
_ High-order word (bits 3116)
_ Low-order word (bits 150)
Instructions are provided for storing the guard bits, for storing the high- and the low-order
accumulator words in data memory, and for transferring 32-bit accumulator words in or out of
data memory. Also, either of the accumulators can be used as temporary storage for the other.

Barrel Shifter

The C67x DSP barrel shifter has a 40-bit input connected to the accumulators or to data memory
(using CB or DB), and a 40-bit output connected to the ALU or to data memory (using EB). The
barrel shifter can produce a left shift of 0 to 31 bits and a right shift of 0 to 16 bits on the input
data. The shift requirements are defined in the shift count field of the instruction, the shift count
field (ASM) of status register ST1, or in temporary register T (when it is designated as a shift
count register).The barrel shifter and the exponent encoder normalize the values in an
Prepared by Thota Sravanthi Page
122
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

accumulator in a single cycle. The LSBs of the output are filled with 0s, and the MSBs can be
either zero filled or sign extended, depending on the state of the sign-extension mode bit (SXM)
in ST1. Additional shift capabilities enable the processor to perform numerical scaling, bit
extraction, extended arithmetic,
and overflow prevention operations.
Multiplier/Adder Unit
The multiplier/adder unit performs 17 _ 17-bit 2s-complement multiplication with a 40-bit
addition in a single instruction cycle. The multiplier/adder block consists of several elements: a
multiplier, an adder, signed/unsigned input control logic, fractional control logic, a zero detector,
a rounder (2s complement), overflow/saturation logic, and a 16-bit temporary storage register
(T). The multiplier has two inputs: one input is selected from T, a data-memory operand, or
accumulator A; the other is selected from program memory, data memory, accumulator A, or an
immediate value. The fast, on-chip multiplier allows the C54x DSP to perform operations
efficiently such as convolution, correlation, and filtering. In addition, the multiplier and ALU
together execute multiply/accumulate (MAC) computations and ALU operations in parallel in a
single instruction cycle. This function is used in determining the Euclidian distance and in
implementing symmetrical and LMS filters, which are required for complex DSP algorithms. See
section 4.5, Multiplier/Adder Unit, on page 4-19, for more details about the multiplier/adder unit.

Prepared by Thota Sravanthi Page


123
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Fig MULTIPLIER/ADDER UNIT

Fig MULTIPLIER/ADDER UNIT

These are the some of the important parts of the processor and you are instructed to go through
the detailed architecture once which helps you in developing the optimized code for the required
application.

Prepared by Thota Sravanthi Page


124
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

21. Convolution

Aim: To perform the linear & circular convolution using DSP Processor.
Theory:

Convolution is a formal mathematical operation, just as multiplication, addition, and integration.


Addition takes two numbers and produces a third number, while convolution takes two signals
and produces a third signal. Convolution is used in the mathematics of many fields, such as
probability and statistics. In linear systems, convolution is used to describe the relationship
between three signals of interest: the input signal, the impulse response, and the output signal.

Circular convolution is another way of finding the convolution sum of two input signals. It
resembles the linear convolution, except that the sample values of one of the input signals is
folded and right shifted before the convolution sum is found.

Program:
#include<stdio.h>
main()
{
int m=4;
int n=4;
int i=0,j;
int x[10]={1,2,3,4,0,0,0};
int h[10]={1,2,3,4,0,0,0};
int y[10];
for(i=0;i<m+n-1;i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];
}
for(i=0;i<m+n-1;i++)
printf("%d\n",y[i]);
}

Prepared by Thota Sravanthi Page


125
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Output:

Prepared by Thota Sravanthi Page


126
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

// circular convolution//
#include<stdio.h>

void main()
{
int m,n,x[10],h[10],y[10],i,j,k,x2[10],a[10];
printf(" enter the length of the first sequence\n");
scanf("%d",&m);
printf(" enter the length of the second sequence\n");
scanf("%d",&n);
printf(" enter the first sequence\n");
for(i=0;i<m;i++)
scanf("%d",&x[i]);
printf(" enter the second sequence\n");
for(j=0;j<n;j++)
scanf("%d",&h[j]);
if((m-n)!=0)
{
if(m>n)
{
for(i=n;i<m;i++)
h[i]=0;
n=m;
}

for(i=m;i<n;i++)
x[i]=0;
m=n;
}

y[0]=0;
a[0]=h[0];
for(j=1;j<n;j++)
a[j]=h[n-j];
for(i=0;i<n;i++)
y[0]+=x[i]*a[i];
for(k=1;k<n;k++)
{
y[k]=0;
for(j=1;j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];
for(i=0;i<n;i++)
Prepared by Thota Sravanthi Page
127
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

{
a[i]=x2[i];
y[k]+=x[i]*x2[i];
}
}
printf(" the circular convolution is\n");
for(i=0;i<n;i++)
printf("%d \t",y[i]);

Output:

Prepared by Thota Sravanthi Page


128
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Result: Convolution of two sequences has been performed and verified by using DSP
processor.

Prepared by Thota Sravanthi Page


129
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

22. Fast Fourier Transform

Aim: To perform fast fourier transform of a sequence using DSP processor.

Theory:
A fast Fourier transform (FFT) is an algorithm to compute the discrete Fourier transform
(DFT) and its inverse. There are many different FFT algorithms involving a wide range of
mathematics, from simple complex-number arithmetic to group theory and number theory; this
article gives an overview of the available techniques and some of their general properties, while
the specific algorithms are described in subsidiary articles linked below.

The DFT is obtained by decomposing a sequence of values into components of different


frequencies. This operation is useful in many fields (see discrete Fourier transform for properties
and applications of the transform) but computing it directly from the definition is often too slow
to be practical. An FFT is a way to compute the same result more quickly: computing the DFT of
N points in the naive way, using the definition, takes O(N2) arithmetical operations, while an FFT
can compute the same DFT in only O(N log N) operations. The difference in speed can be
enormous, especially for long data sets where N may be in the thousands or millions. In practice,
the computation time can be reduced by several orders of magnitude in such cases, and the
improvement is roughly proportional to N / log(N). This huge improvement made the calculation
of the DFT practical; FFTs are of great importance to a wide variety of applications, from digital
signal processing and solving partial differential equations to algorithms for quick multiplication
of large integers.

The best-known FFT algorithms depend upon the factorization of N, but there are FFTs with
O(N log N) complexity for all N, even for prime N. Many FFT algorithms only depend on the
fact that is an N-th primitive root of unity, and thus can be applied to analogous transforms
over any finite field, such as number-theoretic transforms. Since the inverse DFT is the same as
the DFT, but with the opposite sign in the exponent and a 1/N factor, any FFT algorithm can
easily be adapted for it.

Program:
#include <stdio.h>
#include <math.h>

void butterfly(double *,double *,double *,double *,double *,double *);


int main()
{
int i;
double x[8]={1,2,3,4,4,3,2,1};
//double x[8]={4,3,2,1,0,0,0,0};
double XR[8];
double XI[8]={0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0};

Prepared by Thota Sravanthi Page


130
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

double WNR[4]={0.999969, 0.707092, 0.0, -0.707092};


double WNI[4]={0.0 , -0.707092,-0.999969,-0.707092};
double spectrum[8];

// store data in bit reversal order


XR[0]=x[0];
XR[1]=x[4];
XR[2]=x[2];
XR[3]=x[6];
XR[4]=x[1];
XR[5]=x[5];
XR[6]=x[3];
XR[7]=x[7];
// FIRST STAGE OF BUTTERFLY
butterfly(&XR[0],&XI[0],&XR[1],&XI[1],&WNR[0],&WNI[0]);
butterfly(&XR[2],&XI[2],&XR[3],&XI[3],&WNR[0],&WNI[0]);
butterfly(&XR[4],&XI[4],&XR[5],&XI[5],&WNR[0],&WNI[0]);
butterfly(&XR[6],&XI[6],&XR[7],&XI[7],&WNR[0],&WNI[0]);

// SECOND STAGE OF BUTTERFLY

butterfly(&XR[0],&XI[0],&XR[2],&XI[2],&WNR[0],&WNI[0]);
butterfly(&XR[1],&XI[1],&XR[3],&XI[3],&WNR[2],&WNI[2]);
butterfly(&XR[4],&XI[4],&XR[6],&XI[6],&WNR[0],&WNI[0]);
butterfly(&XR[5],&XI[5],&XR[7],&XI[7],&WNR[2],&WNI[2]);

// THIRD STAGE OF BUTTERFLY


butterfly(&XR[0],&XI[0],&XR[4],&XI[4],&WNR[0],&WNI[0]);
butterfly(&XR[1],&XI[1],&XR[5],&XI[5],&WNR[1],&WNI[1]);
butterfly(&XR[2],&XI[2],&XR[6],&XI[6],&WNR[2],&WNI[2]);
butterfly(&XR[3],&XI[3],&XR[7],&XI[7],&WNR[3],&WNI[3]);

for(i=0;i<8;i++)
{
XR[i]*=64.0;
XI[i]*=64.0;
}
for(i=0;i<8;i++)
{
printf("\n FFT XR[%d] = %lf XI[%d]= %lf",i,XR[i],i,XI[i]);
}
return(0);
}
void butterfly(double *ar,double *ai,double *br,double *bi,double *wr,double *wi)
{
double tr,ti;
Prepared by Thota Sravanthi Page
131
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

*ar/=4.0;
*ai/=4.0;
tr=*ar*2;
ti=*ai*2;
*br/=4.0;
*bi/=4.0;
*ar+=*br * *wr - *bi * *wi;
*ai+=*br * *wi + *bi * *wr;
*br= tr - *ar;
*bi= ti - *ai;
}
Output:

Prepared by Thota Sravanthi Page


132
Vignana Bharathi Insitute of Technology DSP Lab Manual (2012-2013)

Result: Fast fourier transform has been performed and verified using DSP processor.

Prepared by Thota Sravanthi Page


133

You might also like