0% found this document useful (0 votes)
203 views

Digital Signal Processing Lab 5th

The document provides an introduction to MATLAB and some basic MATLAB commands. It discusses: 1) What MATLAB is and how it can be used for numerical computation, matrix manipulation, signal processing, and more. 2) How to start MATLAB and an overview of the MATLAB desktop interface. 3) Examples of basic MATLAB commands for arithmetic operations, assigning values to variables, plotting functions, and getting help. 4) Programs for generating common signal types like impulse, step, ramp and exponential signals. 5) Programs for generating continuous and discrete time sinusoidal signals. 6) A program demonstrating sampling and aliasing of signals.

Uploaded by

Mohsin Bhat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
203 views

Digital Signal Processing Lab 5th

The document provides an introduction to MATLAB and some basic MATLAB commands. It discusses: 1) What MATLAB is and how it can be used for numerical computation, matrix manipulation, signal processing, and more. 2) How to start MATLAB and an overview of the MATLAB desktop interface. 3) Examples of basic MATLAB commands for arithmetic operations, assigning values to variables, plotting functions, and getting help. 4) Programs for generating common signal types like impulse, step, ramp and exponential signals. 5) Programs for generating continuous and discrete time sinusoidal signals. 6) A program demonstrating sampling and aliasing of signals.

Uploaded by

Mohsin Bhat
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 31

DIGITAL SIGNAL PROCESSING LAB

Introduction

MATLAB stands for MATrix LABoratory. It is a technical computing environment for high
performance numeric computation and visualisation. It integrates numerical analysis, matrix
computation, signal processing and graphics in an easy-to-use environment, where problems and
solutions are expressed just as they are written mathematically, without traditional programming.
MATLAB allows us to express the entire algorithm in a few dozen lines, to compute the solution
with great accuracy in a few minutes on a computer, and to readily manipulate a three-
dimensional display of the result in colour.
MATLAB is an interactive system whose basic data element is a matrix that does not require
dimensioning. It enables us to solve many numerical problems in a fraction of the time that it
would take to write a program and execute in a language such as FORTRAN, BASIC, or C. It
also features a family of application specific solutions, called toolboxes. Areas in which
toolboxes are available include signal processing, image processing, control systems design,
dynamic systems simulation, systems identification, neural networks, wavelength communication
and others. It can handle linear, non-linear, continuous-time, discrete-time, multivariable and
multirate systems. This chapter gives simple programs to solve specific problems that are
included in the previous chapters. All these MATLAB programs have been tested under version
7.1 of MATLAB and version 6.12 of the signal processing toolbox.

Starting MATLAB:

After logging into your account, you can enter MATLAB by double-clicking on the MATLAB

shortcut icon (MATLAB 7.0.4) on your Windows desktop. When you start MATLAB, a special

window called the MATLAB desktop appears. The desktop is a window that contains other

windows. The major tools within or accessible from the desktop are:

The Command Window

The Command History

The Workspace

The Current Directory

The Help Browser

The Start button


Figure 1.1: The graphical interface to the MATLAB workspace When MATLAB is started for the
first time, the screen looks like the one that shown in the Figure 1.1.

 This illustration also shows the default configuration of the MATLAB desktop. You can
customize the arrangement of tools and documents to suit your needs. Now, we are interested
in doing some simple calculations. We will assume that you have sufficient understanding of
your computer under which MATLAB is being run.
 MATLAB desktop on your computer, which contains the prompt (>>) in the Command
Window. Usually, there are 2 types of prompt:
>> For full version
EDU> for educational version
Note: To simplify the notation, we will use this prompt, >>, as a standard prompt sign,
MATLAB COMMANDS;
Using MATLAB as a calculator
As an example of a simple interactive calculation, just type the expression you want to
evaluate. Let’s start at the very beginning. For example, let’s suppose you want to calculate the
expression, 1 + 2 × 3.
Type it at the prompt command (>>) as follows,
>> 1+2*3
ans = 7
if you do not specify an output variable, MATLAB uses a default variable ans, short for answer, to
store the results of the current calculation. Note that the variable ans is created (or overwritten, if it is
already existed). To avoid this, you may assign a value to a variable or output argument name. For
example
>> x = 1+2*3
x =7
Will result in x being given the value 1 + 2*3=7. This variable name can always
be used to refer to the results of the previous computations. Therefore, computing 4 result in
>> 4*x
ans =28.0000
 Before we conclude this minimum session, Table 1.1 gives the partial list of arithmetic
Operators.
Basic arithmetic operators
Symbol Operation Example
+ Addition 2 + 3
- Subtraction 2 - 3
* Multiplication 2*3
/ Division 2/3

3 Quitting MATLAB
To end your MATLAB session, type quit in the Command Window, or select File
MATLAB in the desktop main menu.

Getting started
After learning the minimum MATLAB session, we will now learn to use some additional operations.
1 Creating MATLAB variables
MATLAB variables are created with an assignment statement. The syntax of variable assignment is
Variable name = a value (or an expression)

For example,
>> x = expression
Where expression is a combination of numerical values, mathematical operators, variables, and
function calls. On other words, expression can involve:
manual entry
built-in functions
user-defined functions

Overwriting variable
Once a variable has been created, it can be reassigned. In addition, if you do not wish to see the
intermediate results, you can suppress the numerical output by putting a semicolon (;) at the end of the
line. Then the sequence of commands looks like this:
>> t = 5;
>> t = t+1
t =6
Error messages
If we enter an expression incorrectly, MATLAB will return an error message. For example, in the
following, we left out the multiplication sign, *, in the following expression
>> x = 10;
>> 5x

Making corrections
To make corrections, we can, of course retype the expressions. But if the expression is
Lengthy, we make more mistakes by typing a second time. A previously typed command can be
recalled with the up-arrow key When the command is displayed at the command prompt, it can be
modified if needed and executed.

Controlling the hierarchy of operations or precedence


Let’s consider the previous arithmetic operation, but now we will include
example, 1 + 2×3 will become (1 + 2) ×3
>> (1+2)*3
ans =9
and, from previous example
>> 1+2*3

In MATLAB, it becomes

>> 1/(2+3^2)+4/5*6/7

ans =0.7766.

or, if parentheses are missing,

>> 1/2+3^2+4/5*6/7

ans =10.1857.

Miscellaneous commands

Here are few additional useful commands:

•To clear the Command Window, type clc

•To abort a MATLAB computation, type ctrl-c

• To continue a line, type . . .


Getting help

To view the online documentation, select MATLAB Help from Help menu or MATLAB Help
directly in the Command Window. The preferred method is to use the Help Browser. The Help
Browser can be started by selecting the? Icon from the desktop toolbar. On the other hand,
information about any command is available by typing

>> help Command

Another way to get help is to use the look for command. The look for commandeers

from the help command. The help command searches for an exact function name match, while the
look for command searches the quick summary information in each function for a match. For
example, suppose that we were looking for a function to take

The inverse of a matrix. Since MATLAB does not have a function named inverse, the command help
inverse will produce nothing. On the other hand, the command look for inverse will produce detailed
information, which includes the function of interest, inv

>> look for inverse

Note - At this particular time of our study, it is important to emphasize one main point.

Because MATLAB is a huge program; it is impossible to cover all the details

of each function one by one. However, we will give you information how to get help. Here are some
examples

• Use on-line help to request info on a specific function

>> help sqrt

• In the current version (MATLAB version 7), the doc function opens the on-line version of the help
manual. This is very helpful for more complex commands

>> Doc plot

• Use look for to find functions by keywords. The general form is

>>look for Function Name

Common Procedure to all Programs in MATLAB

1. Click on the MATLAB Icon on the desktop.

2. MATLAB window open.


3. Click on the ‘FILE’ Menu on menu bar.

4. Click on NEW M-File from the file Menu.

5. An editor window open, start typing commands.

6. Now SAVE the file in directory.

7. Then Click on DEBUG from Menu bar and Click Run.

1.GENERATION OF BASIC SIGNALS USING MATLAB

AIM : To generate basic signals like unit impulse, unit step, unit ramp signal and Exponential
signals.

Objective: To generate basic signals like unit impulse, unit step, unit ramp signal and Exponential
signals using MATlab.

Requirements : Computer with MATLAB software

(a). Program for the generation of UNIT impulse signal

clc; close all; clear all;

t=-2:1:2;

y=[zeros(1,2),ones(1,1),zeros(1,2)] figure(1)

subplot(2,2,1); stem(t,y);

title('unit impulse');

(b). Program for the generation of UNIT step signal

clc; close all; clear all;

n=input('enter the n value');

t=0:1:n-1;

y=ones(1,n);

figure(2)

subplot(2,2,2);

stem(t,y);

title('unit step');
(c).Program for the generation of unit RAMP signal

clc; close all; clear all;

n=input('enter the n value');

t=0:n;

y=ones(1,n);

figure(3)

subplot(2,2,3);

stem(t,t);

title('unit ramp');

(d).Program for the generation of Exponential signal

clc; close all; clear all;

n=input('the length of i/p sequency');

t=0:n

a=input('enter the a value');

y=exp(a*t); figure(4)

subplot(2,2,4);

stem(t,y);

xlabel('x-axis'); ylabel('y-axis'); title('unit exponential');


2. AIM : To Generate continuous time sinusoidal signal, Discrete time cosine signal.

Requirements : Computer with MATLAB software

% Program for Continuous time signal

clc;

close all;

clear all;

t=0:.01:pi;

y= sin(2*pi*t);

subplot(4,1,1);

plot(t,y);

ylabel('amp...');xlabel('(a)n...');title('sin signal')

%Program for Discrete time cosine signal :

t=0:.03:pi/3;

y= cos(2*pi*t);

subplot(4,1,2);

stem(t,y);

xlabel('a(n)');ylabel('amplitude');title('cosinusoidal');

Aim
To perform a Sampling and effect of aliasing Using MATLAB.

%Program
clc;
close all;
clear all;
f1=1/128;
f2=5/128;
n=0:255;
fc=50/128;
x=cos(2*pi*f1*n);
x1=cos(2*pi*f2*n);
xa=cos(2*pi*fc*n);
xamp=x.*xa;
subplot(2,2,1);plot(n,x);title('x(n)');
xlabel('n --.');ylabel('amplitude');
subplot(2,2,2);plot(n,xa);title('xa(n)');
xlabel('n --.');ylabel('amplitude');
subplot(2,2,3);plot(n,xamp);
xlabel('n --.');ylabel('amplitude');

BASIC OPERATIONS ON SIGNALS


Aim :To develop program for some basic operations like addition, subtraction, shifting and folding on
signal.

Basic Operations
Signal Adding: {x1 (n)}+ {x2(n)}
This is a sample-by-sample addition given by and the length of x1(n) and x2(n) must be the same.

Signal Multiplication:
This is a sample-by-sample multiplication (or “dot” multiplication) given by

ADDITION:
x=input(„ENTER THE FIRST SEQUENCE:‟);
subplot(3,1,1);
stem(x);
title('X');
y=input(„ENTER THE SECOND SEQUENCE:‟);
subplot(3,1,2);
stem(y);
title('Y');
z=x+y;
disp(z)
subplot(3,1,3);
stem(z);
title('Z=X+Y');
OUTPUT:
ENTER THE FIRST SEQUENCE:[2 3 1 4 5]
ENTER THE SECOND SEQUENCE:[1 -1 0 1 -1]
32154

SUBTRACTION
clc;
clear all;
close all;
n1=-2:1;
x=input('ENTER THE FIRST SEQUENCE:');
n2=0:3;
y=input('ENTER THE SECOND SEQUENCE:');
subplot(3,1,1);
stem(n1,x);
xlabel ('time')
ylabel ('amplitude')
title('FIRST SEQUENCE') ;
axis([-4 4 -5 5]);
subplot(3,1,2);
stem(n2,y);
xlabel ('time')
ylabel ('amplitude')
title('SECOND SEQUENCE');
axis([-4 4 -5 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1 ) , max ( n2 ) ); % finding the duration of output signal
s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x;
% signal x with the duration of output signal 'sub'
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y;
% signal y with the duration of output signal 'sub'
sub=s1 - s2; % subtraction
disp('subtracted sequence')
disp(sub)
subplot(3,1,3)
stem(n3,sub)
xlabel ('time')
ylabel ('amplitude')
OUTPUT:
ENTER THE FIRST SEQUENCE:[2 4 6 8]
ENTER THE SECOND SEQUENCE:[1 3 5 7]
subtracted sequence
2 4 5 5 -5 -7

MULTIPLICATION
%PROGRAM:
clc;
clear all;
close all;
n1=-2:1;
x=input('ENTER THE FIRST SEQUENCE:');
n2=0:3;
y=input('ENTER THE SECOND SEQUENCE:');
subplot(3,1,1);
stem(n1,x);
xlabel ('time')
ylabel ('amplitude')
title('FIRST SEQUENCE') ;
axis([-4 4 -5 5]);
subplot(3,1,2);
stem(n2,y);
xlabel ('time')
ylabel ('amplitude')
title('SECOND SEQUENCE');
axis([-4 4 -5 5]);
n3 =min (min(n1) ,min( n2 ) ) : max ( max ( n1 ) , max ( n2 ) ); % finding the duration of output signal (out )
s1 =zeros(1,length (n3) );
s2 =s1;
s1 (find ( ( n3>=min( n1 ) ) & ( n3 <=max ( n1 ) )==1 ) )=x;
% signal x with the duration of output signal 'mul'
s2 (find ( ( n3>=min ( n2 ) ) & ( n3 <=max ( n2 ))==1) )=y;
% signal y with the duration of output signal 'mul'
mul=s1 .* s2; % multiplication
disp('MULTIPLIED SEQUENCE')
disp(mul)
subplot(3,1,3)
stem(n3,mul)
xlabel ('time')
ylabel ('amplitude')
OUTPUT:
ENTER THE FIRST SEQUENCE:[2 4 6 8]
ENTER THE SECOND SEQUENCE:[2 3 4 5]
MULTIPLIED SEQUENCE
0 0 12 24 0 0
SHIFTING
%PROGRAM:
clc;
clear all;
close all;
n1=input('Enter the amount to be delayed');
n2=input('Enter the amount to be advanced');
n=-2:2;
x=input('ENTER THE SEQUENCE');
subplot(3,1,1);
stem(n,x);
title('Signal x(n)');
m=n+n1;
y=x;
subplot(3,1,2);
stem(m,y);
title('Delayed signal x(n-n1)');
t=n-n2;
z=x;
subplot(3,1,3);
stem(t,z);
title('Advanced signal x(n+n2)');
OUTPUT:
Enter the amount to be delayed 3
Enter the amount to be advanced4
ENTER THE SEQUENCE[1 2 3 4 5]

FOLDING or REVERSING:
%PROGRAM:
clc;
clear all;
close all;
n=-1:2;
x=input('ENTER THE SEQUENCE');
subplot(2,1,1)
stem(n,x);
axis([-3 3 -5 5]);
title('Signal x(n)');
c=fliplr(x);
y=fliplr(-n);
disp('FOLDED SEQUENCE')
disp(c)
subplot(2,1,2);
stem(y,c);
axis([-3 3 -5 5]);
title('Reversed Signal x(-n)') ;
OUTPUT:
ENTER THE SEQUENCE[1 -1 2 -3]
FOLDED SEQUENCE
-3 2 -1 1

FREQUENCY RESPONSE:

3. To find frequency response of a given system given in (Transfer Function/ Differential equation
form).

AIM:- To write a MATLAB program to evaluate the Frequency response of the system .

Objective: To write a MATLAB program to evaluate the Frequency response of the system .

EQUIPMENTS:

Operating System - Windows XP

Constructor - Simulator

Software - CCStudio 3 & MATLAB 7.5

THEORY:-

The Difference equation is given as

y[n]-0.25y[n-1]+0.45y[n-2]=1.55x[n]+1.95x[n-1]+ 2.15x[n]

The frequency response is a representation of the system's response to sinusoidal inputs at varying
frequencies. The output of a linear system to a sinusoidal input is a sinusoid of the same frequency but
with a different magnitude and phase. Any linear system can be completely described by how it
changes the amplitude and phase of cosine waves passing through it. This information is called the
system's frequency response. Since both the impulse response and the frequency response contain
complete information about the system, there must be a one-to-one correspondence between the two.
Given one, you can calculate the other. The relationship between the impulse response and the
frequency response is one of the foundations of signal processing: A system's frequency response is
the Fourier Transform of its impulse response Since h [ ] is the common symbol for the impulse
response, H [ ] is used for the frequency response.

PROGRAM:

clc;

clear all;

close all;

% Difference equation of a second order system

% y[n]-0.25y[n-1]+0.45y[n-2]=1.55x[n]+1.95x[n-1]+ 2.15x[n-2]

b=input('enter the coefficients of x(n),x(n-1)-----');

a=input('enter the coefficients of y(n),y(n-1)----');

N=input('enter the number of samples of frequency response ');

[h,t]=freqz(b,a,N);

subplot(2,1,1);

% figure(1);

plot(t,h);

subplot(2,1,2);

% figure(2);

stem(t,h);

title('plot of frequency response');

ylabel('amplitude');

xlabel('time index----->N');

disp(h);

grid on;

OUTPUT:

enter the coefficients of x(n),x(n-1)-----[1.55 1.95 2.15]

enter the coefficients of y(n),y(n-1)----[1 -.25 .45]

enter the number of samples of frequency response 1500


2. IMPULSE RESPONSE OF A GIVEN SYSTEM

AIM:- To write a MATLAB program to evaluate the impulse response of the system .

OBJECTIVE:- To write a MATLAB program to evaluate the impulse response of the system using
MATlab.

EQUIPMENTS:

Operating System - Windows XP

Constructor - Simulator

Software - CCStudio 3 & MATLAB 7.5

The Difference equation is given as

y(n) = x(n)+0.5x(n-1)+0.85x(n-2)+y(n-1)+y(n-2)

THEORY:-

LTI Discrete time system is completely specified by its impulse response i.e. knowing the impulse
response we can compute the output of the system to any arbitrary input. Let h[n] denotes the impulse
response of the LTI discrete time systems. Since discrete time system is time invariant, its response to
[n-1] will be h[n-1] .Likewise the response to [n+2] , [n-4] and [n-6] will be h[n+2], h[n-4] and h[n-6]
.

From the above result arbitrary input sequence x[n] can be expressed as a weighted linear
combination of delayed and advanced unit sample in the form k=+

X[n] = x[k][n-k]

k=-

where weight x[k] on the right hand side denotes specifically the k th sample value of the sequence.
The response of the LTI discrete time system to the sequence

x[k] [n-k] will be x[k] h [n-k].

As a result, the response y[n] of the discrete time system to x[n] will be given by

k=+

y[n] = x[k] h [n-k] …………..(1)

k=-

Which can be alternately written as

k=+

y[n] = x[n-k] h [k]…………(2)

k=-

The above equation (1) and (2) is called the convolution sum of the sequences x[n]

and h[n] and represented compactly as y[n]=x[n] * h[n] Where the notation * denotes the convolution
sum.

Structure for Realization of Linear Time Invariant systems:

Let us consider the first order system Y(n)=-a 1y(n-1)+b0 x(n) +b1 x(n-1)

This realization uses separate delays(memory) for both the input and output samples and it is called as
Direct form one structure.

A close approximation reveals that the two delay elements contain the same input

w(n) and hence the same output w(n-1).consequently these two elements can be

merged into one delay. In contrast to the direct form I structure , this new realization

requires only one delay for auxiliary quantity w(n) ,and it is more efficient in terms
of memory requirements. It is called the direct form II structure and it is used

extensively.

PROGRAM:-

clc;

clear all;

close all;

% Difference equation of a second order system

% y(n) = x(n)+0.5x(n-1)+0.85x(n-2)+y(n-1)+y(n-2)

b=input('enter the coefficients of x(n),x(n-1)-----');

a=input('enter the coefficients of y(n),y(n-1)----');

N=input('enter the number of samples of imp response ');

[h,t]=impz(b,a,N);

subplot(2,1,1);

% figure(1);

plot(t,h);

title('plot of impulse response');

ylabel('amplitude');

xlabel('time index----->N');

subplot(2,1,2);

% figure(2);

stem(t,h);

title('plot of impulse response');

ylabel('amplitude');

xlabel('time index----->N');

disp(h);

grid on;
Output

enter the coefficients of x(n),x(n-1)-----[1 0.5 0.85] enter the coefficients of y(n),y(n-1)-----[1 -1 -1]
enter the number of samples of imp respons 4

1.0000

1.5000

3.3500

4.8500

GRAPH:-

CALCULATIONS:-

y(n) = x(n)+0.5x(n-1)+0.85x(n-2)+y(n-1)+y(n-2)

y(n) - y(n-1) - y(n-2) = x(n) + 0.5x(n-1) + 0.85x(n-2) Taking Z transform on both sides,

Y(Z) - Z-1 Y(Z)- Z-2 Y(Z) = X(Z) + 0.5 Z-1 X(Z) + 0.85 Z-2 X(Z) Y(Z)[1 - Z-1 - Z-2] = X(Z)[1 +
0.5 Z-1 + 0.85 Z-2 ]

But, H(Z) = Y(Z)/X(Z)

= [1 + 0.5 Z-1 + 0.85 Z-2 ]/ [1 - Z-1 - Z-2] By dividing we get

H(Z) = 1 + 1.5 Z-1 + 3.35 Z-2 + 4.85 Z-3

h(n) = [1 1.5 3.35 4.85]


CORRELATION
Aim :
To develop program for correlation.

CROSS CORRELATION
% Program for computing cross-correlation of the sequences x5[1, 2, 3, 4] and h5[4, 3, 2, 1]
clc;
clear all;
close all;
x=input(„enter the 1st sequence‟);
h=input(„enter the 2nd sequence‟);
y=crosscorr(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel(„Amplitude --.‟);
xlabel(„(a) n --.‟);
title(„input sequence‟);
subplot(3,1,2);
stem(h);
ylabel(„Amplitude --.‟);
xlabel(„(b) n --.‟);
title(„impulse sequence‟);
subplot(3,1,3);
stem(fliplr(y));
ylabel(„Amplitude --.‟);
xlabel(„(c) n --.‟);
title(„Cross correlated sequence‟);
disp(„The resultant signal is‟);
fliplr(y)
OUTPUT:
enter the 1st sequence [1 2 3 4]
enter the 2nd sequence [4 3 2 1]
The resultant signal is
Y=1.0000 4.0000 10.0000 20.↑0000 25.0000 24.0000 16.0000
AUTO CORRELATION
% Program for computing autocorrelation function
x=input(„enter the sequence‟);
y=crosscorr(x,x);
figure;subplot(2,1,1);
stem(x);ylabel(„Amplitude --.‟);
xlabel(„(a) n --.‟);
title(„original signal‟);
subplot(2,1,2);
stem(fliplr(y));ylabel(„Amplitude --.‟);
xlabel(„(a) n --.‟);
title („Auto correlated sequence‟);
disp(„The resultant signal is‟);
fliplr(y)
OUTPUT:
enter the sequence [1 2 3 4]
The resultant signal is
Y=4 11 20 ↑30 20 11 4
LINEAR CONVOLUTION
Aim
To perform a Linear Convolution Using MATLAB.
% Program for linear convolution of the sequence x5[1, 2] and h5[1, 2, 4]
clc;
clear all;
close all;
x=input('enter the 1st sequence');
h=input('enter the 2nd sequence');
y=conv(x,h);
figure;
subplot(3,1,1);
stem(x);
ylabel('Amplitude --.');
xlabel('(a) n --.');
title('first sequence');
subplot(3,1,2);
stem(h);ylabel('Amplitude --.');
xlabel('(b) n --.');
title('Second sequence');
subplot(3,1,3);
stem(y);
ylabel('Amplitude --.');
xlabel('(c) n --.');
title('Convoluted sequence');
disp('The resultant signal is');
Output:
enter the 1st sequence [1 2]
enter the 2nd sequence [1 2 4]
The resultant signal is
Y= 1 4 8 8
CIRCULAR CONVOLUTION
Aim
To perform a Circular Convolution Using MATLAB.
Program
clc;
clear all;
a = input(„enter the sequence x(n) = ‟);
b = input(„enter the sequence h(n) = ‟);
n1=length(a);
n2=length(b);
N=max(n1,n2);
x = [a zeros(1,(N-n1))];
for i = 1:N
k = i;
for j = 1:n2
H(i,j)=x(k)* b(j);
k = k-1;
if (k == 0)
k = N;
end
end
end
y=zeros(1,N);
M=H‟;
for j = 1:N
for i = 1:n2
y(j)=M(i,j)+y(j);
end
end
disp(„The output sequence is y(n)= „);
disp(y);

stem(y);
title(„Circular Convolution‟);
xlabel(„n‟);
ylabel(‚y(n)„);
OUTPUT:
enter the sequence x(n) = [1 2 4]
enter the sequence h(n) = [1 2]
The output sequence is y(n)= 9 4 8

% Program for Computing Circular Convolution with zero padding


clc;
close all;
clear all;
x=input('enter the first sequence');
h=input('enter the 2nd sequence');
y=x'*h;
n1=length(x);
n2=length(h);
figure
subplot(3,1,1)
stem(x);
title('Input sequence');
xlabel('n1');
ylabel('x(n1)');
subplot(3,1,2)
stem(h);
title('Impulse sequence');
xlabel('n2');
ylabel('h(n2)');
n=n1+n2-1;
c=zeros(n);
for i=1:n1
for j=1:n2
c(i+j-1)=c(i+j-1)+y(i,j);
end
end
for i=1:n
d(i)=c(i,1);
end
disp('convoluted sequence');
disp(d);
n3=1:n;
subplot(3,1,3)
stem(n3-1,c);
title('Convoluted sequence');
xlabel('time');
ylabel('Amplitude');
OUTPUT:
enter the first sequence [1 2 4]
enter the 2nd sequence [1 2]
The resultant signal is y=1 4 8 8

STUDY FREQUENCY RESPONSE OF SECOND ORDER SYSTEM


%frequency response of differential equation %
clc ;
clear all;
b=[1,4];
a=[1,-5];
w=-2*pi:pi/8:2*pi;
[h]=freqz(b,a,w);
subplot(2,1,1);
stem(w,abs(h));
xlabel('freq/w');
ylabel('magnitude');
grid;
title('magnitude response of diffenrtial equataion');
subplot(2,1,2);
stem(w,angle(h));
xlabel('freq/w');
ylabel('phase in rad');
grid;
title('phase response of diffenrtial equataion');

2. DFT/IDFT OF A SEQUENCE WITHOUT USING THE INBUILT FUNCTIONS


Given a sequence of N samples f(n), indexed by n = 0..N-1, the Discrete Fourier
Transform (DFT) is defined as F(k), where k=0..N-1:

F(k) are often called the 'Fourier Coefficients' or 'Harmonics'.


The sequence f(n) can be calculated from F(k) using the Inverse Discrete Fourier Transform
(IDFT):

PROGRAM:-
%DFT%
clc;
clear all;
close all;
a=input ('enter the input sequence');
N=length(a);
disp('length of input sequence is ');
N
for k=1:N;
x(k)=0;
for i=1:N;
x(k)=x(k)+a(i)*exp((-j*pi*2/N)*((i-1)*(k-1)));
end;
end;
k=1:N;
disp('the output is');
x(k)
subplot(2,1,1);
stem(k,abs(x(k)));
grid;
xlabel ('discrete frequency');
ylabel('magnitude');
title('magnitude response of dft');
subplot(2,1,2);
stem(angle(x(k))*180/(pi));
grid;
xlabel('discrete frequency');
ylabel('phase angle');
title('phase response of dft');
%IDFT%
clc;
clear all;
close all;
a=input('enter the input sequence');
disp('the length of input sequence is');
N=length(a);
N
for n=1:N;
x(n)=0;
for k=1:N;
x(n)=x(n)+a(k)*exp((j*pi*2*(n-1)*(k-1)/N));
end;
end;
n=1:N;
x=1/N*x(n);
disp('the output is');
x(n)
stem(n,abs(x));
grid;
xlabel('discrete time');
ylabel('magnitude');
title('magnitude response of the idft');
grid;

3. TO FIND DFT / IDFT OF GIVEN DT SIGNAL using Inbuilt functions.


AIM: To find the DFT / IDFT of given signal.
Objective: To wite the MATlab code to find the DFT / IDFT of given signal.
EQUIPMENTS:
Operating System - Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
THEORY:
Discrete Time Fourier Transform:
The discrete-time Fourier transform (DTFT) X(ejω) of a sequence x[n] is defined

In general X(ejω) is a complex function of the real variable ω and can be written as

where Xre(ejω) and Xim(ejω) are, respectively, the real and imaginary parts of X(ejω), and are real
functions of ω. X(ejω) can alternately be expressed in the form

The quantity |X(ejω)| is called the magnitude function and the quantity θ(ω) is called the phase
function

In many applications, the Fourier transform is called the Fourier spectrum and, likewise, |X(ejω)| and
θ(ω) are referred to as the magnitude spectrum and phase spectrum, respectively.

The DTFT X(ejω) is a periodic continuous function in ω with a period 2π. The DTFT satisfies a
number of useful properties that are often uitilized in a number of applications.

MATLAB COMMANDS:
For complex Z, the magnitude R and phase angle theta are given by:
R = abs (Z)
Theta = angle (Z)
Y = fft(X) returns the discrete Fourier transform of vector X, computed with a fast Fourier transform
(FFT) algorithm.
Y = fft(X)
Y = fft(X,n) returns the n-point FFT.
Y = fft(X, n)
Compute the discrete Fourier transform of the following function analytically and Then plot the
magnitude and phase:
Its DTFT is given as:

MATLAB Code:
w = [0:500]*pi/500;
z = exp(-j*w);
x = 3*(1-0.9*z).^(-1);
a = abs(x);
b = angle(x)*180/pi;
subplot(2,1,1);
plot(w/pi,a);
subplot(2,1,2);
plot(w/pi,b);

MATLAB CODE:
% Evaluation of the DTFT
clc;
% %Compute the frequency samples of the DTFT
w = -4*pi:8*pi/511:4*pi;
num = [2 1];
den = [1 -0.6];
h = freqz(num, den, w);
% Plot the DTFT
subplot(2,2,1)
plot(w/pi,real(h));
grid on;
title('Real part of H(e^{j\omega})')
xlabel('\omega /\pi');
ylabel('Amplitude');
subplot(2,2,2)
plot(w/pi,imag(h));
grid on;
title('Imaginary part of H(e^{j\omega})')
xlabel('\omega /\pi');
ylabel('Amplitude');
subplot(2,2,3)
plot(w/pi,abs(h));
grid on;
title('Magnitude Spectrum |H(e^{j\omega})|')
xlabel('\omega /\pi');
ylabel('Amplitude');
subplot(2,2,4)
plot(w/pi,angle(h));
grid on;
title('Phase Spectrum arg[H(e^{j\omega})]')
xlabel('\omega /\pi');
ylabel('Phase, radians');

PROGRAM:
%% Computation of N point DFT of a given sequence and to plot magnitude and phase
spectrum.
N = input('Enter the the value of N(Value of N in N-Point DFT)');
x = input('Enter the sequence for which DFT is to be calculated');
n=[0:1:N-1];
k=[0:1:N-1];
WN=exp(-1j*2*pi/N); % twiddle factor
nk=n'*k;
WNnk=WN.^nk;
Xk=x*WNnk;
MagX=abs(Xk) % Magnitude of calculated DFT
PhaseX=angle(Xk)*180/pi % Phase of the calculated DFT figure(1);
subplot(2,1,1);
plot(k,MagX);
subplot(2,1,2);
plot(k,PhaseX);
-------------*******--------------
OUTPUT
Enter the the value of N(Value of N in N-Point DFT)4 Enter the sequence for which DFT is to be
calculated [1 2 3 4]
MagX = 10.0000 2.8284 2.0000 2.8284
PhaseX = 0 135.0000 -180.0000 -135.0000
DFT of the given sequence is
10.0000 -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 -
2.0000i
4. IMPLEMENTATION OF FFT OF GIVEN SEQUENCE
AIM: To implementation of Fast Fourier Transform.
EQUIPMENTS:
Operating System - Windows XP
Constructor - Simulator
Software - CCStudio 3 & MATLAB 7.5
THEORY: The Fast Fourier Transform is useful to map the time-domain sequence into a continuous
function of a frequency variable. The FFT of a sequence {x(n)} of length N is given by a complex-
valued sequence X(k).
M -j2𝜋𝑘𝑛
X(k)= ∈ x(n) e ;0 <k <N -1
K=0

The above equation is the mathematical representation of the DFT. As the number of computations
involved in transforming a N point time domain signal into its corresponding frequency domain signal
was found to be N2 complex multiplications, an alternative algorithm involving lesser number of
computations is opted.

Implementation of FFT of given sequence using inbuilt functions


%fft%
clc;
clear all;
close all;
xn=input('enter the input sequence');
N=input('enter the number of samples');
n=0:1:N-1;
xk=fft(xn,N);
k=0:1:N-1;
subplot(2,1,1);
stem(k,abs(xk));
xlabel('frq/w');
ylabel('magnitude');
title('magnitude response of fft');
subplot(2,1,2);
stem(k,angle(xk));
xlabel('frq/w');
ylabel('phase');
title('phase response of fft');

IMPLEMENTATION OF FFT OF GIVEN SEQUENCE IN MATLAB


N-POINT FFT WITHOUT USING INBUILT FUNCTION
PROGRAM:
clear all;
close all;
clc;
x=input('Enter the sequence x[n]= ');
N=input('Enter the value N point= ');
L=length(x);
x_n=[x,zeros(1,N-L)];
for i=1:N
for j=1:N
temp=-2*pi*(i-1)*(j-1)/N;
DFT_mat(i,j)=exp(complex(0,temp));
end
end
X_k=DFT_mat*x_n';
disp('N point DFT is X[k] = ');
disp(X_k);
mag=abs(X_k);
phase=angle(X_k)*180/pi;
subplot(2,1,1);
stem(mag);
xlabel('frequency index k');
ylabel('Magnitude of X[k]');
axis([0 N+1 -2 max(mag)+2]);
subplot(2,1,2);
stem(phase);
xlabel('frequency index k');
ylabel('Phase of X[k]');
axis([0 N+1 -180 180]);

PROGRAM: for user defined sequence


%fast fourier transform
clc;
clear all;
close all;
tic;
x=input('enter the sequence');
n=input('enter the length of fft'); %compute fft
disp('fourier transformed signal');
X=fft(x,n)
subplot(1,2,1);stem(x); title('i/p signal');
xlabel('n --->');
ylabel('x(n) -->');grid;
subplot(1,2,2);stem(X);
title('fft of i/p x(n) is:');
xlabel('Real axis --->');
ylabel('Imaginary axis -->');grid;
OUTPUT:-
enter the sequence[1 .25 .3 4]
enter the length of fft4
fourier transformed signal
X=
5.5500 0.7000 + 3.7500i -2.9500 0.7000 - 3.7500i

You might also like