Professional Documents
Culture Documents
DSP Lab Manual (12-13)
DSP Lab Manual (12-13)
INTRODUCTION TO MATLAB
Desktop Overview
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.
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
Use the Command Window to enter variables and to run MATLAB functions and scripts.
MATLAB displays the results.
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.
Getting Help
There are different ways to get help, depending on your needs. The following
table summarizes the main ways.
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
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.
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.
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.
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.
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.
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.
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.
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.
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.
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
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
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
Result: Various matrix operations are performed and results are verified.
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
%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:
Result: Output waveforms for various operations on sinusoidal waves have been observed and
verified.
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.
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)
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:
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);
stem(abs(s));
subplot(3,1,3);
stem(abs(y));
OUTPUT:
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:
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);
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
Results: Fast Fourier Transform for a given sequence with & without inbuilt functions has been
observed & verified.
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.
Program:
% Power Spectral Density of sum of sinusoidal signal without noise
clc
clear all
fs=100;
t=0:1/fs:10;
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:
Output:
Result: The power spectral density of a signal with & without noise has been observed &
verified.
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
or
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:
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.
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;
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
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.
Inbuilt functions:
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));
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
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)];
OUTPUT:
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.
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)));
OUTPUT:
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));
OUTPUT:
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.
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;
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));
OUTPUT:
Result: Suppressed & interfered signal has been observed from the noise signal.
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
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.
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);
[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:
OUTPUT
[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:
OUTPUT:
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);
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:
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');
OUTPUT:
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:
OUTPUT:
Result: IIR digital chebyshev type-I filter has been verified successfully.
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:
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
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:
OUTPUT:
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);
OUTPUT:
Result: IIR digital chebyshev type-II filter has been verified successfully.
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.
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);
OUTPUT:
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');
OUTPUT:
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:
[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:
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
[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;
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:
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:
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:
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:
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
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');
ylabel('Y');
OUTPUT:
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)
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:
OUTPUT:
Definition ( odd):
Transform:
Definition:
Transform:
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.
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
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:
Definition ( odd):
Transform:
Definition:
Transform:
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
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);
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));
OUTPUT:
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
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)
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)
Output:
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.
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
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.
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.
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.
The CPU is common to all C67xE devices. The C67x CPU contains:
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:
The ALU can also function as two 16-bit ALUs and perform two 16-bit operations
simultaneously.
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.
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.
21. Convolution
Aim: To perform the linear & circular convolution using DSP Processor.
Theory:
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]);
}
Output:
// 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:
Result: Convolution of two sequences has been performed and verified by 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 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>
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]);
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:
Result: Fast fourier transform has been performed and verified using DSP processor.