Ccn-Matlab Soft
Ccn-Matlab Soft
ENGINEERING
LABORATORY CERTIFICATE
This
is
to
certify
that
Mr/Ms_________________________
bearing USN
___________has satisfactorily completed the
course
of
experiments
in
VLSI
&
MICROCONTROLLER Laboratory prescribed
by the VTU for 7th semester BE electronics &
communication course in the laboratory of this
college in the year 2005 2006.
DATE :
(Mrs.Indumathi)
2
CCN
& DSP Question Bank
1. Using MATLAB design an IIR low pass filter which has
maximally flat frequency response with the following
specifications:
Pass band ripple=0.5db
Pass band edge frequency=0.25
Stop band attenuation=15db
Stop band edge frequency=0.55
Use T=1 sec and impulse invariance transformation.
2. Using MATLAB design an IIR filter with pass band edge
frequency at 1500Hz and stop band edge at 2000Hz for a
sampling frequency of 8000Hz.passband attenuation is 1dB
and stop band attenuation is 15dB. Use butter worth prototype
design and bilinear transformation.
3. Using MATLAB design an IIR filter with pass band edge
frequency at 1500Hz and stop band edge at 2000Hz for a
sampling frequency of 8000Hz.passband attenuation is 1dB
and stop band attenuation is 15dB. Use chebysher prototype
design and bilinear transformation.
4. Using MATLAB design an IIR filter to meet the following
specifications by choosing hamming window.
Window length =227
Stop band attenuation =50dB
Cut-off frequency=100Hz
Sampling frequency =1000Hz
5. Using MATLAB compute the linear convolution of the given
two sequences. Also
compute the linear convolution, circular convolution using
function files.
X1 (n)=[1 2 2 1]
X2 (n) =[1 2 2 1]
6. Using MATLAB compute the circular convolution of the
given two sequences
using function files.
3
X1 (n)=[1 2 2]
X2 (n) =[1 2 2 1]
7
Using matlab for a given sequence x(n), compute the Ninput DFT using FFT
algorithms. Plot the magnitude and phase spectrum
from the DFT calculated, obtain the results of IDFT.
8. Using MATLAB sample a band limited continuous time signal
band limited to
fm= 400Hz under the following conditions:
1. Nyquist rate
2. twice the Nyquist rate
3. half the Nyquist rate
9. Using MATLAB compute the impulse response of a system
described by the
difference equation
y(n) 3/4 y(n-1) -1/8 y(n-2)= x(n)-1/3x(n-1)
10 .Write a C Program to simulate Bit-Stuffing and De-Stuffing for
a HLDC frame.
11. Write a C Program to simulate Character-Stuffing and DeStuffing for a HLDC
Frame.
12. Write a c program to simulate the shortest Algorithm for the
given network in the
graph form (the number of nodes should be greater than 6).
13. Write a C Program to encrypt a given text message using
substitution method and
decrypt it
14. Write a C Program to encrypt a given text message using
transposition method and
decrypt it
15. Write a C Program to simulate the Minimum Spanning Tree
using suitable algorithm
for the given network in the graph form (the number of nodes
should be at least 6).
16. Write a C Program to compute the Polynomial Code Checksum
for CRC-CCITT.
Verify the program for the cases a. with out error b. with error
introduced
4
Computer Communication
Networks
Contents
1. WRITE A C PROGRAM TO SIMULATE BIT-STUFFING AND
DE-STUFFING FOR A HLDC FRAME AND DISPLAY THE
5
STUFFED FRAME.
AND
AND
INTRODUCTION TO CCN
COMMUNICATION NETWORKS
Computer network
the Internet
the Public switched telephone network
the global Telex network
Data
communications,
application
of
telecommunications
technology to the problem of transmitting data, especially to, from, or
between computers. In popular usage, it is said that data
communications make it possible for one computer to talk with
another. Telephone circuits are often used to transfer data, although
their relatively limited bandwidth makes them relatively slow paths
for data. Recent techniques, however, have made it possible to send
data over phone lines at rates of 28,800 bits per second and higher. A
modem is required for such telephone communications when they
occur over standard (analog) telephone circuits. Where cost can be
justified, high speed data links are constructed; these are often fiberoptic or coaxial cables designed for wide frequency range, or
microwave, radio links. Local-area and wide-area networks link
computers together so that they can transfer and share data. Because
many computers can be on the network at any given time, techniques
such as time-division multiplexing are used; each computer is
assigned a short time slot during which it can use the full bandwidth
of the network. Packet switching allows a single channel to be used
for multiple concurrent transmissions. Data packets contain addresses
that indicate the intended destination. To minimize datacommunication errors, special codes are used.
Network Topologies
HDLC
THEORY
Digital communication has its advantages of speed, reliability,
etc but with it comes the problem of control signals. In analog
communication the modulation technique used can be used to
generate control signals. Like in FDM 3 frequencies can be
used: one frequency for a 'low', another one for a 'high' and
finally a third frequency for a control signal. But it isn't possible
in digital communication.
Bit patterns or bytes can be reserved for control signals. These
control signals can not be present in normal data. This can be
achieved for ASCII text or if a few bit patterns are used as data.
But when the whole set of bit patterns or ASCII values are
used, the special control singnals can not be present in data and
if present should not be interpreted as control signals but be
interpreted as data. This is the reason we use stuffing.
Stuffing is a process of adding redundant information or
increasing the amount of data transmitted in order to
acomodate control signals.
This is used when a bit oriented communication syatem is used.
Let us consider the control signals of frame started/end. We give
them a representation of 0111 1110. So now if this is present in
the data we are transmitting we change this pattern so that it no
longer matches the control signals of frame start/end. Each time
we see five ones (1s) at the transmitter we append 0 instantly so
that six ones (1s) can never exist. An argument can be raised
about the fact why should we add the 'zero' after 5 ones if the
6th bit was a 0 and not a 1. The reason is simlpe enough - the
receiver cannot differentiatebetween that 0 which is data and a
stuffed 0.
For example,
0111110 can now be interpreted as
0111 1110
(assuming 0 is stuffed 0)
0111 1101 (assuming 0
as data).
So once its settled that every 5 ones has a zero following it
irrespective of the next bit being a 0 or 1 lets see what the
receiver has to do. The receiver keeps a count of the ones and
each time it receives 5 ones it discards the next bit if its a 0 bit
9
which is the stuffed 0 bit and knows this isn't a control signal
but if the bit after 5 ones is also 1 its a frame end/start.
HDLC Frame
The high level data link control protocol is a bit oriented
protocol which uses bit stuffing for data transparency. HDLC
frame is as shown
CHECKSU 011111
M
10
ALGORITHM
1. Input data sequence.
2. Add start to frame to output sequence.
3. For every bit in input
a). Append bit to output sequence.
b). Is bit a 1?
Yes
Increment count.
If count is 5, append 0 to output
sequence and reset count.
No
Set count to zero.
4. Add stop to frame bits to output sequence.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define MAX 1000
void main()
{
int si=0,di=0,count=0;
char src[MAX],dest[MAX];
10
char flag[MAX]="01111110";
clrscr();
printf("Enter the msg bits\n");
scanf("%s",src);
di=strlen(flag);
strcpy(dest,flag);
while(src[si]!='\0')
{
if (src[si]=='1')
count++;
else
count=0;
dest[di++]=src[si++];
if (count==5)
{
count=0;
dest[di++]='0';
}
}
dest[di++]='\0';
printf("Stuffed message is %s",strcat(dest,flag));
si=strlen(flag);
for (di=0;si<strlen(dest)-2*si-1;si++,di++)
src[di]=dest[si];
count=0;
si=0;di=0;
while(src[si]!='\0')
{
if (src[si]=='1')
count++;
else
count=0;
dest[di++]=src[si++];
if (count==5)
{
count=0;
dest[di++]=src[si++];
}
}
dest[di]='\0';
printf("\nDestuffed msg is %s",dest);
getch();
}
OUTPUT:
1.
Enter the msg bits 01111111
Stuffed message is 0111111001111101101111110
Destuffed msg is
01111111
2.
Enter the msg bits 0110111111
Stuffed message is 011111100110111110101111110
Destuffed msg is
0110111111
11
12
ALGORITHM
1. Input data sequence
2. Add start of frame to output sequence (DLE STX)
3. For every character in input
a. Append character to output sequence
b. Is character DLE?
Yes:
Add DLE to output sequence
4. Add stop of frame chars to output sequence
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 100
void main()
{
int si=0,di=0;
char src[max],des[max];
char flag1[max]="DLESTX",flag2[max]="DLEETX";
clrscr();
printf("Enter the string data\n");
scanf("%s",&src);
/*STUFFING PROCESS*/
strcpy(des,flag1);
di=strlen(flag1);
while(src[si]!='\0')
{
if(src[si]=='D'&&src[si+1]=='L'&&src[si+2]=='E')
{
des[di+0]='D',des[di+1]='L',des[di+2]='E',des[di+3]='D
',des[di+4]='L',des[di+5]='E';
di+=6;
si+=3;
}
else
des[di++]=src[si++];
}
des[di]='\0';
strcat(des,flag2);
printf("The stuffed string is %s\n",des);
/*DE-STUFFING PROCESS*/
di=strlen(des)-strlen(flag2);
des[di]='\0';
di=strlen(flag1);
for(si=0;des[di]!='\0';si++,di++)
src[si]=des[di];
13
si=di=0;
while(src[si]!='\0')
{
if(src[si+0]=='D'&&src[si+1]=='L'&&src[di+2]=='E'&&src
[si+3]=='D'&&src[si+4]=='L'&&src[di+5]=='E')
{
des[di]='D',des[di+1]='L',des[di+2]='E';
di+=3;
si+=6;
}
else
des[di++]=src[si++];
}
des[di]='\0';
printf("The destuffed string is %s",des);
getch();
}
OUTPUT:
1.
ABCDLE
DLESTXABCDLEDLEDLEETX
ABCDEFGH
DLESTXABCDEFGHDLEETX
THEORY
All packets need to be routed from the source to thedestination and in most
cases multiple hops are required.Routing algorithms are required to decide on
which output line and incoming packet should be forwarded onto.
Two types of routing algorithms now come in-one where the decision is fixed
and one where the route is constantly being recalculated for the best route in
other words static and dynamic routing algorithms or non-adaptive and
adaptive algorithms. Routing algorithms can be may to adapt to average
traffic/loss/delay/distance/queue length etc.
Dijkstras method of computing the shortest path is a static routing algorithm.
It involves building a graph of the subnet, with each node of the graph
representing a router and each arc representing a communication line or a link.
14
To find a route between a pair of routers the algorithm just finds the shortest
path between them on the graph.
In Dijkstras algorithm the metric used for calculation is distance. Each node
is labeled with its distance from the source node along with the previous node
in the path. Initially, no paths are known so all nodes are labeled with infinity.
As the algorithm proceeds and paths are found, the labels may change,
reflecting better paths.
A label may be either tentative or permanent. Initially, all nodes are tentative
and once it is discovered that the shortest possible path to a node is got it is
made permanent and never changed after that.
ALGORITHM
1.
2.
3.
4.
5.
6.
7.
8.
9.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int p[10][10];
void main()
{
int i,j,k,n,pn,t;
int m[10][10],w[10][10];
void path(int i,int j);
clrscr();
printf("Enter no of nodes:");
scanf("%d",&n);
printf("\n Enter the node connection matrix");
printf("\n To indicate no connection between two nodes,enter weight as 100\n\n");
\* Prints the outline of the matrix*\
for (i=1;i<=n;i++)
printf("\t %d",i);
for (i=1;i<=n;i++)
{
15
printf("\n %d \t",i);
\* Takes the entered values as input*/
for (j=1;j<=n;j++)
{
scanf("%d",&w[i][j]);
m[i][j]=w[i][j];
p[i][j]=0;
}
}
for (i=1;i<=n;i++)
m[i][j]=0;
for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
if (m[i][k]+m[k][j]<m[i][j])
{
m[i][j]=m[i][k]+m[k][j];
p[i][j]=k;
}
do
{
printf("\n Enter the source and destination nodes\n");
scanf("%d %d",&i,&j);
printf("\n The weight is %d",m[i][j]);
printf("\n The path is ");
printf("%d--->",i);
path(i,j);
printf("%d",j);
printf("\n to repeat press R");
}while (getch()=='R');
void path(int i,int j)
{
int k;
k=p[i][j];
if (k!=0)
{
path(i,k);
printf("%d-->",k);
path(k,j);
}}
OUTPUT:
16
Enter no of nodes 7
Enter the node connection matrix
1
1
100
2
2
3
1
4
100
5
100
6
4
7
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
GIVEN MESSAGE
17
THEORY
In cryptography, the messages to be encrypted, known as plaintext, are
transformed function that is parameterized by a key. The output of the encryption
process, known as ciphertext, is then transmitted, often by messenger or radio.
The art of breaking ciphers is called cryptanalysis. The art of devising ciphers
(cryptography) and breaking them (cryptanalysis) is collectively known as
cryptology.
Encryption methods are historically divided into two categories: substitution
ciphers and transposition ciphers.
Substitution ciphers
In a substitution ciphers, each letter or group of letters is replaced by another
letter or group of letters to disguise it . A way to do this is to have each of the
symbols in the plaintext, say the 26 letters for simplicity, map onto some other
letter. For example,
Plaintext: a b c d e f g h i j k l m n o p q r s t u v w x y z
Ciphertext: Q W E R T Y U I O P A S D F G H J K L Z X C V B N M
This general system is called a mono-alphabetic substitution with the key being
the 26-letter string corresponding to the full alphabet. For the key above, the
plaintext, attack would be transformed into the Ciphertext QZZQEA.
Transposition Ciphers
Substitution ciphers preserve the order of the plaintext symbols but disguise the.
Transposition Ciphers, in contrast, reorder the letters, but do not disguise them.
The figure below depicts a common transposition cipher, the columnar
transposition. The cipher is keyed by a word of phrase not containing any
repeated letters. In this example, MEGABUCK is the key. The purpose of the
key is to number the columns, column one being under the letter closest to the
start of the alphabet and so on. The plaintext is written horizontally in rows. The
cipher is readout by columns starting with the column whose key letter is the
lowest.
Plain text:
Pleasetransferonemilliondollarstomyswissbankaccountsixtwotwo
18
MEGA B U CK
p l e a s e t r
a n s f e r o n
e m i l l i o n
d o l l a r s t
o m y s w i s s
b a n k a c c o
u n t s i x t w
o t w o a b c d
Ciphertext:
AFLLSKSOSELAWAIATOOSSCTCLNMOMANTESILYNTWRNNTSOWDP
AEDBUOERIRICXB.
ALGORITHM
Encryption by substitution method
1. Read data to be encoded
2. For every character of data
a. If data is between a and z
Set encoded data to uppercase character from key
b. If data is between A and Z.
Set encoded data to lowercase character from key
c. If data between 0 and 9.
Set encoded data to digit from key
d. Else copy data into encoded datas array.
3. Print encoded data.
Decryption by substitution method
1. Read encrypted data.
2. For every character of data.
a. Scan through key to see if data is present.
(i)
If present get index of character add it to a if data is uppercase
else A if it is in lowercase else 0 if it is a digit.
(ii) If not present copy data into decoded datas array.
3. Print decoded data
19
2.
3.
4.
5.
SOURCE CODE:
/*Encryption and De-cryption using Substitution method*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
void main()
{
int encry,decry,ci;
char plain[100]=" ",cipher[100]="
clrscr();
printf("Enter the text\n");
printf("--------------\n");
gets(plain);
";
/*Encryption Process*/
while(1)
{
printf("Enter the key for encryption(between
1 and 25)\n");
scanf("%d",&encry);
if(encry<1||encry>25)
printf("\nInvalid key");
else
break;
}
for(ci=0;plain[ci]!='\0';ci++)
{
if(isalpha(plain[ci]))
{
20
if(isupper(plain[ci]))
cipher[ci]=(plain[ci]'A'+encry)%26+'A';
else
cipher[ci]=(plain[ci]'a'+encry)%26+'a';
}
else
cipher[ci]=plain[ci];
}
printf("Text after encryption is %s",cipher);
/*Decryption process*/
while(1)
{
printf("\nEnter the decryption key(between 1
and 25)\n");
scanf("%d",&decry);
if(decry==encry)
break;
else
printf("\nKey is wrong. Re-enter
key\n");
}
for(ci=0;cipher[ci]!='\0';ci++)
{
if(isalpha(cipher[ci]))
{
if(isupper(cipher[ci]))
plain[ci]=(cipher[ci]-'A'decry+26)%26+'A';
else
plain[ci]=(cipher[ci]-'a'decry+26)%26+'a';
}
else
plain[ci]=cipher[ci];
}
printf("Text after decryption is %s",plain);
getch();
}
OUTPUT:
Enter the text CMRIT
Enter the key for encryption(between 1 and 25)=5
Text after encryption is HRWNY
Enter the decryption key(between 1 and 25)=5
Text after decryption is CMRIT
SOURCE CODE:
21
22
int len;
char plain[MAX],cipher[MAX],key[MAX];
clrscr();
printf("Encryption & decryption using
transposition\n");
printf("Enter the plain text\n");
gets(plain);
len=strlen(plain);
getkey("\nEnter key for encryption\n",key);
transpose(plain,key,cipher,1);
printf("\nnAfter encryption %s\n",cipher);
getkey("\nEnter key for decryption\n",key);
transpose(cipher,key,plain,0);
plain[len]='\0';
printf("\nAfter decryption %s\n",plain);
getch();
}
OUTPUT:
Encryption & decryption using transposition
Enter the plain text TRANSFER ONE CRORE RUPEES
Enter key for encryption MEGABUCK
After extending TRANSFER ONE CRORE RUPEESABCDEFG
After encryption NERCS UDEREFRDEAAN BROEGT RSFCPE
Enter key for decryption MEGABUCK
After decryption TRANSFER ONE CRORE RUPEES
SUBNET
23
THEORY
A spanning tree is a graph is just a sub-graph that contains all
the vertices and is a tree. A graph may have many spanning
trees. For instance, a complete graph 4 nodes can have 16
spanning trees. Suppose, the edges of the graphs have weights
or lengths, the weight of the trees is the some of its edges.
Obviously, different trees have different lengths.
A spanning tree includes all the routers, but contains no loops.
If each router knows which of its lines belong to the spanning
trees, it can copy and incoming broadcast packet onto all the
spanning trees lines except the one it arrived on. This method
makes excellent use of bandwidth, generative the absolute
minimum number of packets necessary to do the job. The only
problem is that each router must have knowledge of some
spanning tree for it to be applicable. Sometime this information
is available (e.g. with link state routing), but sometimes it is not
(e.g. with distance vector routing).
ALGORITHM
1.
2.
3.
4.
5.
SOURCE CODE:
#include<conio.h>
#include<stdio.h>
int p[10][10];
void main()
{
int i,j,k,z=900,x,y,n,flag,wt=0;
int m[10][10],w[10][10];
int chkcl(int,int,int,int);
clrscr();
printf("Enter the number of nodes\n");
scanf("%d",&n);
printf("\nEnter the node connection matrix\n");
for (i=1;i<=n;i++)
printf("\t%d",i);
for (i=1;i<=n;i++)
24
{
printf("\n%d\t",i);
for (j=1;j<=n;j++)
{
scanf("%d",&w[i][j]);
p[i][j]=w[i][j];
m[i][j]=0;
}
}
for (i=1;i<=n;i++)
{
x=100;
for (j=1;j<=n;j++)
{
if (p[i][j]<x)
{
flag=chkcl (i,j,n,z);
if (flag==1)
continue;
m[i][j]=1;
for (y=1;y<j;y++)
m[i][y]=0;
k=j;
x=p[i][j];
}
}
if (p[i][k]<100)
wt=wt+p[i][k];
p[i][k]=150;
p[k][i]=p[i][k];
}
printf("\nThe tree is \n");
for (i=1;i<=n;i++)
for (j=1;j<=n;j++)
if (m[i][j]!=0)
printf("%d--->%d\n",i,j);
printf("The weight is %d\n",wt);
getch();
}
int chkcl(int i,int j,int n,int k)
{
int t,flag;
for (t=1;t<=n;t++)
{
if ((p[i][t]==150)&&t!=k)
{
if (t==j)
return (1);
else
{
k=i;
flag=chkcl(t,j,n,k);
if (flag==1)
return (1);
}
}
25
}
return (0);
}
OUTPUT:
1
2
100 2
3
3
4
5
100 4
100 100 5
100
100 100 7
100 5
100 100
100 8
100 100
The tree is
1-->2
2-->4
3-->1
5-->1
26
The weight is 14
ALGORITHM
27
1. Let r be the degree of G(x). Append r zero bits to the low order
end of the frame, so it now contains m+r bits and corresponds
to the polynomial xr M(x).
2. divide the bit string corresponding to G(x) into the bit string
corresponding to xr M (x) using modulo-2 division.
3. Subtract the remainder (which is always r or fewer bits) from
the bit string corresponding to xr M(x) using modulo-2
subtraction. The result is the checksummed frame to be
transmitted. Call its polynomial T(x).
The CRC-CCITT polynomial is x16 + x12+x5+1
SOURCE CODE:
#include<stdio.h>
#include <conio.h>
#define degree 16
int res[30];
int getnext(int array[],int);
void crc(int len)
{
int cp[]={1,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,1};
int i=0,pos=0,newpos;
while(pos<len-degree)
{
for(i=pos;i<pos+degree+1;++i)
res[i]=res[i]^cp[i-pos];
newpos=getnext(res,pos);
if(newpos>pos+1)
pos=newpos-1;
++pos;
}
}
int getnext(int array[],int pos)
{
int i=pos;
while(array[i]==0)
++i;
return i;
}
void main()
{
int array[30];
char ch;
int len,i=0;
clrscr();
puts("Enter the data stream:");
while((ch=getche())!='\r')
array[i++]=ch-'0';
len=i;
for(i=0;i<degree;++i)
array[i+len]=0;
len+=degree;
28
for(i=0;i<len;i++)
res[i]=array[i];
crc(len);
printf("\nThe transmitted frame is: ");
for(i=0;i<len-degree;++i)
printf("%d",array[i]);
for(i=len-degree;i<len;++i)
printf("%d",res[i]);
printf("\nEnter the stream for which crc is to be checked");
i=0;
while((ch=getche())!='\r')
array[i++]=ch-'0';
len=i;
for(i=0;i<len;i++)
res[i]=array[i];
crc(len);
printf("\ncheck sum:");
for(i=len-degree;i<len;i++)
printf("%d",res[i]);
getch();
}
OUTPUT:
(Without Error)
Enter data stream 10011
Transmitted frame is: 100110000001000010000
Enter the stream for which crc to be checked 100110000001000010000
Check sum: 0000000000000000
(With Error)
Enter data stream 10011
Transmitted frame is: 100110000001000010000
Enter the stream for which crc to be checked 100110000001000010001
Check sum: 0000000000000001
29
30
CONTENTS
I. Introduction to MATLAB...3
II. Verification of Sampling
Theorem..8
31
INTRODUCTION TO MATLAB
MATLAB stands for matrix laboratory .It is a technical computing environment
for high performance numeric computation and visualization.
MATLAB integrated numerical analysis, matrix computations, signal processing
and graphics in an easy-to-use environment.
32
There are also several optional TOOLBOXES available from the MATLAB.
These toolboxes are collection of functions written for special applications such
as symbolic computations, image processing, statistics, and control system design,
neural networks etc.
ACTUAL EXAMPLES:
>>
>>2+2
ans=
4
>>area=pi*2.15^2
area= 14.5220
MATLAB prompt
Command
MATLAB response
ON-LINE HELP:
ON LINE DOCUMENTATION :
MATLAB provides online help for all its built in functions and programming
language constructs.
DEMO :
MATLAB has a demonstration program that shows many of its features. The
program includes a tutorial introduction .Type demo at the matlab prompt to
invoke the demonstration program and follow the instruction on the screen.
Type help category in MATLAB with the appropriate category name, provides a list
of functions and commands in that category.
Example:
>> help fft
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.
FFT(X,N) is the N-point FFT, padded with zeros if X has less
than N points and truncated if it has more.
FFT(X,[],DIM) or FFT(X,N,DIM) applies the FFT operation across the
dimension DIM.
For length N input vector x, the DFT is a length N vector X, with elements
N
X(k) =
x(n)*exp(-j*2*pi*(k-1)*(n-1)/N); 1kN.
n=1
33
34
35
MATLAB WINDOWS
MATLAB works through three basic windows,
1. COMMAND window
2. GRAPHICS or FIGURE window
3. EDITOR window
1. COMMAND WINDOW:
This is the main window .It is characterized by the MATLAB command prompt
>>.
When you launch an application program, MATLAB puts you in this window. All
commands including those for running user written programs are typed in this
window at the MATLAB prompt.
The output of all the graphics commands typed in the command window are
flushed to the graphics or figure window.
The user can create as many figure windows as the system memory will allow.
36
3. EDITOR WINDOW:
This is where you write, edit, create, and save your own programs in files
called m-Files .
MATLAB provides its own built in editor .
37
PROGRAM 1 :
THEORY:
Sampling Theorem can be stated in two forms If a finite energy signal g(t) contains no frequencies higher than
w Hz, it is completely determined by specifying its ordinates at
a sequence of points spaced 1/2w sec apart.
If a finite energy signal g(t) contains no frequencies higher than
w Hz, it may be completely recovered from its ordinates at a
sequence of points spaced 1/2w sec apart.
The minimum sampling rate of 2w samples per second, for a signal
bandwidth of wHz is called NYQUIST RATE.
s = 2m
where,
s=Sampling Frequency
m=Message frequency
We encounter these conditions in Sampling Theorem, they are Right
sampling, Under sampling and Over sampling
.
Right Sampling s = 2m
Under Sampling s 2m
Right Sampling s 2m
PROBLEM:
Using MATLAB, sample a band limited continuous time signal,
band limited to fm=400Hz under the following conditions:
1.
Nyquist rate
2.
Under sampling
3.
Over sampling
38
MATLAB CODE:
%Nyquist Rate Sampling
s=800;
t=0:1/ s:256/ s;
X=sin(2*pi*400*t);
Xm=abs(fft(X));
K=0:length(X)-1;
Subplot(2,2,1);
Stem(k,Xm);
xlabel(Hz);
ylabel(Magnitude);
title(Nyquist Rate Sampling);
%Under Sampling
s=200;
t=0:1/ s:256/ s;
X=sin(2*pi*400*t);
Xm=abs(fft(X));
K=0:length(X)-1;
Subplot(2,2,2);
Stem(k,Xm);
xlabel(Hz);
ylabel(Magnitude);
title(Under Sampling);
%Over Sampling
s=1000;
t=0:1/ s:256/ s;
X=sin(2*pi*400*t);
Xm=abs(fft(X));
K=0:length(X)-1;
Subplot(2,2,3);
Stem(k,Xm);
xlabel(Hz);
ylabel(Magnitude);
title(Over Sampling);
39
OUTPUT:
40
PROGRAM 2:
THEORY:
A complete characterization of any LTI system can be represented in
terms of its response to an Unit Impulse, which is referred to as
Impulse response of the system. Alternatively, the impulse response is
the output of a LTI system due to an impulse input applied at t=0, or
n=0.
PROBLEM:
Obtain the Impulse response of a system described by the
difference equationy(n)-3/4 y(n-1)+1/8 y(n-2) = x(n)-1/3 x(n-1)
Solution:
Y(z) - 3/4 Y(z)z-1 + 1/8 Y(z)z-2 = X(z) - 1/3 X(z) z-1
H(z) = Y(z) / X (z) = (1-1/3 z-1) / (1-3/4 z-1+1/8 z-2)
Applying partial fractions ,
-1
z )
A=2/3;
B=1/3;
H(z) = (2/3) / (1-1/2 z-1) + (1/3) / (1-1/4 z-1)
h(n) = (2/3) (1/2)n u(n) + (1/3) (1/4)n u(n)
h(0)=1
h(4)=0.0429
h(1)=0.416
h(5)=0.02116
h(2)=0.187 h(6)=0.0105
h(3)=0.088 h(7)=0.0052
h(8)=0.0026
h(9)=0.0013
41
MATLAB CODE:
b=input('type in numerator coeff=');
a=input('type in denominator coeff=');
[h,t]=impz(b,a,6)
stem(t,h);
grid;
xlabel('time index');
ylabel('amplitude');
title('impulse response');
INPUT:
Type in numerator coeff=[1 -1/3]
Type in denominator coeff=[1 -3/4 1/8]
OUTPUT:
42
PROGRAM 3:
THEORY:
DEFINITION OF CONVOLUTION:
The convolution a * b of two functions a and b is defined as:
The asterisk operator is used to denote convolution. Many computer systems, and people
who frequently write mathematics on a computer will often use an asterisk to denote
simple multiplication (the asterisk is the multiplication operator in many programming
languages), however an important distinction must be made here: The asterisk operator
means convolution.
The output using the convolution operation, is: y(t) = x(t) * h(t)
x(t) = input
h(t) = impulse response
y(t) = output
Where x(t) is the input to the circuit, h(t) is the circuit's impulse response, and y(t) is the
output. Here, we can find the output by convoluting the impulse response with the input
to the circuit. Hence we see that the impulse response of a circuit is not just the ratio of
43
the output over the input. In the frequency domain however, component in the output
with frequency is the product of the input component with the same frequency and the
transition function at that frequency. The moral of the story is this: the output to a circuit
is the input convoluted with the impulse response.
LINEAR CONVOLUTION:
Let x1(n) be the input sequence and h(n) be the impulse response of the system as shown
below.i.e
y(n)=x1(n)*h(n)
:where y(n) is the output sequence..
X1(n)
h(n)
y(n)
CIRCULAR CONVOLUTION:
Let x(n) and h(n) be the two sequences of length N.
Then,
44
xlabel('time index');
ylabel('amplitude');
title('circular with linear');
INPUT:
first sequence=[1 2 2 1]
second sequence=[1 2 2 1]
OUTPUT:
y1 =
1
10
10
10
y2 =
9
y3 =
1
45
PROGRAM 4:
THEORY:
DECIMATION
IN
TIME FFT:
46
47
48
49
50
DECIMATION
IN FREQUENCY
FFT:
PROBLEM:
For a given sequence x(n) compute the N-point DFT using
decimation in Time or decimation in Frequency FFT .Plot the
amplitude and phase spectrum. From the DFT calculated obtain
the IDFT,with and without function files.
MATLAB CODE :
51
1.WITHOUT
INPUT:
Enter the sequence x1= [ 1 1 1]
Enter the value N= 8
OUTPUT:
52
2.USING
FUNCTION FILES OF
DFT
AND
IDFT :
INPUT:
Enter the sequence x1=[ 1 1 1]
Enter the value N= 8
OUTPUT:
53
PROGRAM 5:
Realization of IIR Filters
THEORY:
A desired frequency response is approximated by a transfer
function expressed as a ratio of polynomials. This type of transfer
function yields an impulse response of infinite duration. Therefore, the
analog filters are commonly referred to as infinite impulse response
(IIR) filters The main classes of analog filters are 54
1.Butterworth Filter.
2.Chebyshev Filter.
These filters differ in the nature of their magnitude responses as well as
in their design and implementation.
BUTTERWORTH FILTERS:
Butterworth filters have very smooth passband,which we pay for with a
relatively wide transistion region.A butterworth filter is characterized
by its magnitude frequency response,
| H(j) | = 1 / [1+(/c)2N]1/2
where N is the order of the filter and c is defined as the cutoff
frequency where the filter magnitude is 1/2 times the dc gain (=0).
10 K 110 1
log10
Order
10
K2
10
2 log10 1
2
H j
1
N1 N 2
N3
0
N 2 N 3
N=1 (s + 1)
N=2 (s^2 +0.5 s^1 +1)
N=3 (s^2 +s+1)(s+1)
N=4 (s^2+0.76536s+1)(s^2+1.864776s+2)
N=5 (s+1)(s^2+0.6180s+1)(s^2+1.6180s+1)
55
CHEBYSHEV FILTERS:
Chebyshev filters are equiripple in either the passband or
stopband.Hence the magnitude response oscillates between the
permitted minimum and maximum values in the band a number of
times depending upon the order of filters.There are two types of
chebyshev filters.The chebyshev I filter is equiripple in passband and
monotonic in the stopband,where as chebyshev II is just the opposite.
The Chebyshev low-pass filter has a magnitude response given by
H j
A
2
C c
2
N
0. 5
where
CN x
H j
N odd
K1
N even
N1 N 2
N 2 N3
K2
N3
The magnitude response has equiripple pass band and maximally flat stop
band.
It can be seen that by increasing the filter order N, the Chebyshev
response approximates the ideal response.
56
The phase response of the Chebyshev filter is more non-linear than the
Butter worth filter for a given filter length N.
PROBLEM:
Using MATLAB design an IIR filter with passband edge
frequency 1500Hz and stop band edge at 2600Hz for a sampling
frequency of 8000Hz, variation of gain within pass band 1 db and
stopband attenuation of 15 db. Use Butterworth prototype
design and Bilinear Transformation.
Design:
W1=(2*pi* F1 )/ Fs = 2*pi*250)/1000 =0.5 rad
W2=(2*pi* F2 )/ Fs = 2*pi*375)/1000 =0.75 rad
Prewarp:
1 = 2/T tan (w1/2)
= 2 rad/sec
T=1sec
57
Hn(s) =
1/[ s2 + 1.4142s + 1]
MATLAB CODE:
%Butterworth Prototype
Ap=input(enter passband attenuation =);
As=input(enter stopband attenuation =);
fp=input(enter passband frequency =);
fs= input(enter stopband frequency =);
Fs=input(enter sampling frequency=);
W1=2* fp / Fs ;
W2=2* fs / Fs ;
[N,Wn]=buttord(W1,W2,Ap,As)
[b,a]=butter(N,Wn)
freqz(b,a);
title(Butterworth frequency response)
INPUT:
enter
enter
enter
enter
enter
passband attenuation =1
stopband attenuation =15
passband frequency =1500
stopband frequency =2000
sampling frequency=8000
58
OUTPUT:
N=
6
Wn =
0.4104
b=
0.0117
0.0117
0.0699
0.1748
0.2331
0.1748
0.0699
-1.0635
1.2005
-0.5836
0.2318
-0.0437
a=
1.0000
0.0043
59
Problem:
Using MATLAB design an IIR filter with passband edge
frequency 1500Hz and stop band edge at 2600Hz for a sampling
frequency of 8000Hz, variation of gain within pass band 1 db and
stopband attenuation of 15 db. Use Chebyshew prototype design
and Bilinear Transformation.
Design:
W1=(2*pi* F1 )/ Fs = 2*pi*100)/4000 =0.05 rad
W2=(2*pi* F2 )/ Fs = 2*pi*500)/4000 =0.25 rad
Prewarp:
1 = 2/T tan (w1/2)
T=1sec
= 0.157 rad/sec
2 = 2/T tan (w2/2)
= 0.828 rad/sec
Order:
=
10-Ap/10 - 1
= 0.765
A= 10-As/20
g=
, A = 1020/20
(A2 - 1) /
r= 2 / 1
n=log10
g+
A=10
, g = 13.01
log10{r+
(r2 1) }
60
n= 1.388
Therefore n= 2.
Cut-off Frequency:
c = p = 1 = 0.157 rad\sec
Normalized Transfer Function:
H(s)=[bo /
1+ 2
] / [ s2+b1s+b0]
= 0.505/[ s2+0.8s+0.036]
Denormalized Transfer Function:
H(s)= Hn(s) |s-s/c
61
INPUT:
enter
enter
enter
enter
enter
passband attenuation =1
stopband attenuation =15
passband frequency =1500
stopband frequency =2000
sampling frequency=8000
OUTPUT:
N=
4
Wn =
0.3750
b=
0.0191
a=
1.0000
0.0764
0.1147
0.0764
0.0191
-1.7994
1.9637
-1.1513
0.3301
62
PROGRAM 6:
THEORY:
A linear-phase is required throughout the passband of the filter to
preserve the shape of the given signal in the passband. A causal IIR
filter cannot give a linear-phase characteristics and only special types
of FIR filters that exhibit center symmetry in its impulse response give
the linear-space. An FIR filter with impulse response h(n) can be
obtained as follows:
h(n) = hd(n)
0nN-1
=0
otherwise
.(a)
The impulse response hd(n) is truncated at n = 0, since we are
interested in causal FIR
Filter. It is possible to write above equation alternatively as
h(n) = hd(n)w(n)
.(b)
where w(n) is said to be a rectangular window defined by
w(n) = 1
0nN-1
=0
otherwise
Taking DTFT on both the sides of equation(b), we get
H() = Hd()*W()
HAMMING WINDOW:
The impulse response of an N-term Hamming window is defined
as follows:
wHam(n) = 0.54 0.46cos(2n / (N-1)) 0nN-1
=0
otherwise
PROBLEM:
63
MATLAB CODE:
N=input(Enter the window length=);
fc=input(Enter cut-off frequency=);
Fs=input(Enter sampling frequency=);
Wc=2*fc/ Fs;
Wh = (hamming(N))
b = fir1(N-1, Wc ,Wh)
[h,omega] = freqz(b,1,256);
mag = 20*log10(abs(h));
plot(omega/pi,mag);
grid;
xlabel(Frequncy in radians in terms of pi);
ylabel(gain in dB);
title(Response of FIR filter using Hamming window);
INPUT:
Enter the window length=27
Enter cut-off frequency=100
Enter sampling frequency=1000
OUTPUT:
Wh =
0.0800
0.0934
0.1327
0.1957
0.2787
0.3769
0.4846
0.5954
0.7031
0.8013
0.8843
0.9473
0.9866
1.0000
0.9866
64
0.9473
0.8843
0.8013
0.7031
0.5954
0.4846
0.3769
0.2787
0.1957
0.1327
0.0934
0.0800
b=
Columns 1 through 6
0.0019 0.0023 0.0022 -0.0000 -0.0058 -0.0142
Columns 7 through 12
-0.0209 -0.0185 0.0000
0.0374
0.0890
0.1429
Columns 13 through 18
0.1840 0.1994 0.1840
0.1429
0.0890
0.0374
Columns 19 through 24
0.0000 -0.0185 -0.0209 -0.0142 -0.0058 -0.0000
Columns 25 through 27
0.0022 0.0023 0.0019
65
Transmitter
Channel
Receiver
Demodulator
67
or
Source
Noise
Noise Attenuation
Distortion
Noise
Modulator
DATA TRANSMISSION:
There is always a need to exchange the data, commands and other control information
between the computer and its terminals, or between to computers. This information is in
form of bits. Data transmission refers to the movement of the bits over some physical
medium connecting two or more digital devices. The two options of transmitting the bits
are outlined below:
1) PARALLEL TRANSMISSION
If more than one bit of information is transmitted over the data transmission medium at a
time then it is considered a parallel communication.
68
2) SERIAL TRANSMISSION
The physical connection determines how many bits (1s and 0s) can be transmitted in a
single instance of time. If only one bit of information can be transmitted over the data
transmission medium at a time then it is considered a serial communication.
ANALYSIS
Mode of
communication
Disadvantages
Advantages
Parallel
Serial
Long distances
MODES
OF
DATA TRANSMISSION:
There are two methods of timing control for reception of bits. The transmission modes
corresponding to these two timing methods are given below.
ASYNCHRONOUS TRANSMISSION:
69
70
71
Advantages
Simple and
inexpensive
Synchronous
Efficient
Disadvantages
High overhead
Complex and
expensive
AND
BAUD-RATE:
73
OF
RS232C:
PAIR:
75
Experiment 1
Objective:
80c51 main
controller card
Communication
module
80c51 main
controller card
Communication
module
76
Menu 2
Menu 4
Menu 3
Menu 5
Procedure:
*Note: The Baud Rate shall be changed in Menu 2 and all the steps as
explained in the experimental setup and procedure needs to be
repeated.
Observation:
In this experiment the students shall observe and analyze the Principles of Synchronous mode of serial communication.
Conversion of signals: Signal level are changed from TTL to
RS232 levels (i.e., Logical Low signal is represented +3 to +12
V and Logical High signals are represented as -3 to -12V).
78
Experiment 2
Objective:
79
80c51 main
controller card
Communication
module
80c51 main
controller card
Communication
module
Menu 2
80
The LCD screen will be cleared and the following screen will
appear on LCD.
Enter Your Choice
1 -> RS-232 medium
2 -> FSK/FO medium
3 -> PC-to-PC commn
Menu 3
Menu 4
Menu 5
81
Procedure:
*Note: The Baud Rate shall be changed in Menu 2 and all the steps as
explained in the experimental setup and procedure needs to be
repeated.
Observation:
In this experiment the students shall observe and analyze the
82
83
GENERAL LAYOUT
OF
CODE COMPOSER STUDIO
84
85
86
3. Now we need to write C source code . Go to File - - > New - > Source file.
87
88
89
90
Now dialog box will open ,here browse for the source code file in
projects directory and and click open .(usually the common paths
are ..\ti\MyProjects\projectname\...)
91
7. After the addition of all three files ,the expanded project tree on left
hand side will
92
93
94
95
96
11. To see the output of the program we have to run the program.
Go to Debug - - > Run .
97
If the program needs any user input, a pop-up window will open.
98
Select
99
*We can select different elements by using left and right keys
of keyboard.
100
CONTENTS
1. Find out the Discrete Time Convolution of the sequences
x(n)={1 2 3 4 5 6} and h(n)={1 2 3 4}, using Code Composer
Studio. Verify the results graphically.
2. Perform the Circular Convolution of the given sequence using
Code Composer Studio.
3. Compute the response of the system whose co-efficients are
a0=0.1311, a1=0.2622, a2=0.1311 and b0=1, b1= -0.7478,
b2=0.2722, when the input is a unit impulse signal.
4. Using Code Composer Studio, compute the system whose coefficients are a0=0.0051477, a1=0.010295, a3=0.005147,
b0=1, b1= -0.844881, b2=0.873965 and
when the input is two sine waves of frequencies 3000Hz and
400Hz .Take sampling frequency as 24000Hz.
101
Program 1
Problem: Find out the Discrete Time Convolution of the sequences
x(n)={1 2 3 4 5}
and h(n)={1 2 3 4}, using Code Composer Studio. Verify
the results
graphically.
/* program to implement linear convolution */
#include<stdio.h>
int y[20];
main()
{
int m=6;
int n=6;
int i=0,j;
int x[15]={1,2,3,4,5,6,0,0,0,0,0,0};
int h[15]={1,2,3,4,5,6,0,0,0,0,0,0};
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]);
}
Inputs:
x(n)=[1,2,3,4,5,6]
h(n)=[1,2,3,4,5,6]
Output:
y(n)=[1,4,10,20,35,56,70,76,73,60,36]
102
Program 2
Problem: Perform the Circular Convolution of the given sequence
using Code Composer
Studio.
/* program to implement circular convolution */
#include<stdio.h>
int m,n,x[30],h[30],y[30],i,j,temp[30],k,x2[30],a[30];
void main()
{
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 lenght of both sequences are not equal*/
{
if(m>n)
/* Pad the smaller sequence with zero*/
{
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++)
/*folding h(n) to h(-n)*/
103
a[j]=h[n-j];
/*Circular convolution*/
for(i=0;i<n;i++)
y[0]+=x[i]*a[i];
for(k=1;k<n;k++)
{
y[k]=0;
/*circular shift*/
for(j=1;j<n;j++)
x2[j]=a[j-1];
x2[0]=a[n-1];
for(i=0;i<n;i++)
{
a[i]=x2[i];
y[k]+=x[i]*x2[i];
}
}
/*displaying the result*/
printf(" the circular convolution is\n");
for(i=0;i<n;i++)
printf("%d \t",y[i]);
}
Inputs:
Length of first sequence = 4
Length of second sequence = 4
First sequence =[1 2 2 1 ]
Second sequence =[1 2 2 1]
Outputs:
The circular convolution is [9 8 9 10]
104
Program 3
Problem: Compute the response of the system whose co-efficients
are a0=0.1311,
a1=0.2622, a2=0.1311 and b0=1, b1= -0.7478, b2=0.2722,
when the input is a unit
impulse signal.
/*program to implement impulse response of the given sequence*/
#include <stdio.h>
float y[3]={0,0,0};
float x[3]={0,0,0};
float z[10];
float impulse[10]={1,0,0,0,0,0,0,0,0,0};
main()
{
int j;
float a[3]={0.1311, 0.2622, 0.1311};
float b[3]={1, -0.7478, 0.2722};
for(j=0;j<10;j++)
{
x[0]=impulse[j];
y[0] = (a[0] *x[0]) +(a[1]* x[1] ) +(x[2]*a[2]) - (y[1]*b[1])-(y[2]*b[2]);
printf("%f\n",y[0]);
z[j]=y[0];
y[2]=y[1];
y[1]=y[0];
x[2]=x[1];
x[1] = x[0];
}
}
105
Inputs:
Coefficient of x(n)={0.1311,0.2622,0.1311}
Coefficient of y(n)={1,-0.7478,0.2722}
Outputs:
0.131100
0.360237
0.364799
0.174741
0.031373
-0.024104
-0.026565
-0.013304
-0.002718
0.001589
Graph:
106
Program 4
Problem: Using Code Composer Studio, compute the system whose
co-efficients are
a0=0.0051477, a1=0.010295, a3=0.005147, b0=1, b1=
-0.844881, b2=0.873965 and
when the input is two sine waves of frequencies 3000Hz
and 400Hz .Take
sampling frequency as 24000Hz.
/*program to implement differenceequation*/
#include <stdio.h>
#include<math.h>
float y[3]={0,0,0};
float x[3]={0,0,0};
float z[128],m[128],n[128],p[128];
main()
{
int i=0,j;
107
float a[3]={0.005147,0.010295,0.005147};
float b[3]={1.000000,-0.844881,0.873965};
for(i=0;i<10;i++)
{
m[i]=sin(2*3.14*3000*i/24000);
n[i]=sin(2*3.14*400*i/24000);
p[i]=m[i]+n[i];
}
for(j=0;j<10;j++)
{
x[0]=p[j];
y[0] = (a[0] *x[0]) +(a[1]* x[1] ) +(x[2]*a[2]) - (y[1]*b[1])-(y[2]*b[2]);
z[j]=y[0];
printf("%f\n",z[j]);
/*z[j]=y[0];*/
y[2]=y[1];
y[1]=y[0];
x[2]=x[1];
x[1] = x[0];
}
}
OUTPUTS
0.000000
0.004176
0.018097
0.033484
0.031259
0.005522
-0.024796
-0.031289
-0.003494
108
0.039590
Graph:
109