Sunteți pe pagina 1din 28

1.a). Write a program to simulate BIT stuffing & destuffing using HDLC.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char ch, array[50]={"01111110"},recd_array[50]={"xx"};
int counter=0,i=8,j,k;
clrscr();

/*stuffing*/

printf("\n Enter the original stream data for bit stuffing: ");
while((ch=getche())!='\r')
{
if(ch=='1')
++counter;
else
counter=0;
array[i++]=ch;
if(counter==5)
{
array[i++]='0';
counter=0;
}
}
strcat(array,"01111110");
printf("\n The stuffed frame is:");
for(j=0;j<i+8;j++)
printf("%c",array[j]);

/* destuffing*/
counter=0;
printf("\nThe destuffed data stream is:");
for(j=8,k=0;j<i+8;j++)
{
if(array[j]=='1')
++counter;
else
counter=0;
recd_array[k++]=array[j];
if(counter==6)
break;
else if(counter==5&&array[j+1]=='0')
{
++j;
counter=0;
}
}
for(j=0;j<k-strlen("01111110");++j)
printf("%c",recd_array[j]);
getch();
}

SAMPLE RUN:
Enter the original data stream for bit stuffing : 011011111111110010
The stuffed stream is: 011111100110111110111110001001111110
The destuffed data stream is: 011011111111110010

1.b)Write a progarm to simulate CHARACTER stuffing & destuffing using HDLC.

#include<stdio.h>
#include<conio.h>
#define DLE 16
#define STX 2
#define ETX 3
void main()
{
char ch;
char array[100]={DLE,STX};
int i=2,j;
clrscr();

/* STUFFING*/

printf("\n Enter the data stream (ctrl+b->STX,Ctrl+c->ETX,Ctrl+p->DLE):\n");


while((ch=getch())!='\r')
{
if(ch==DLE)
{
array[i++]=DLE;
printf("DLE");
}
else if(ch==STX)
printf("STX");
else if(ch==ETX)
printf("ETX");
else printf("%c",ch);
array[i++]=ch;
}
array[i++]=DLE;
array[i++]=ETX;
printf("\n The stuffed frame is:\n");
for(j=0;j<i;++j)
{
if(array[j]==DLE)
printf("DLE");
else if(array[j]==STX)
printf("STX");
else if(array[j]==ETX)
printf("ETX");
else printf("%c",array[j]);
}
/* DESTUFFING */
printf("\n The destuffed data stream is : \n");
for(j=2;j<i-2;++j)
{
if(array[j]==DLE)
{
printf("DLE");
++j;
}
else if(array[j]==STX)
printf("STX");
else if(array[j]==ETX)
printf("ETX");
else
printf("%c",array[j]);
}
getch();
}

SAMPLE RUN:

Enter the data stream(Ctrl+B->STX,Ctrl+c->ETX,Ctrl+p->DLE):


a STX B ETX c DLE

The character stuffed stream is:


DLESTX a STX B ETX c DLEDLEDLEETX

The destuffed data stream is :


a STX B ETX c DLE.

2. Write a program to simulate the shortest path algorithm.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define NO_OF_NODES 10
#define PERMANENT 1
#define TENTATIVE 0

struct node
{
unsigned int weight;
int prev;
int state;
};
void main()
{
int table[NO_OF_NODES][NO_OF_NODES]={/* A B C D E F G H I J*/
/* A */ {0,1,0,0,0,4,0,0,0,0},
/* B */ {1,0,4,0,0,0,0,1,0,0},
/* C */ {0,4,0,3,2,0,0,0,3,0},
/* D */ {0,0,3,0,1,0,0,0,0,0},
/* E */ {0,0,2,1,0,3,0,0,0,1},
/* F */ {4,0,0,0,3,0,1,0,0,0},
/* G */ {0,0,0,0,0,1,0,2,0,2},
/* H */ {0,1,0,0,0,0,2,0,1,0},
/* I */ {0,0,3,0,0,0,0,1,0,2},
/* J */ {0,0,0,0,1,0,2,0,2,0},
};
int src,dest,i,working_node;
unsigned int min;
struct node nodes[NO_OF_NODES];
clrscr();
for(i=0;i<NO_OF_NODES;i++)
{
nodes[i].state=TENTATIVE;
nodes[i].weight=-1;
}
printf("\n Enter source: ");
src=getche();
working_node=src=toupper(src)-'A';
nodes[src].prev=-1;
nodes[src].weight=0;
printf("\n Enter destination: ");
dest=toupper(getche())-'A';
do
{
nodes[working_node].state=PERMANENT;
for(i=0;i<NO_OF_NODES;i++)
{
if(table[working_node][i]!=0 && nodes[i].state==TENTATIVE)
{
if(nodes[working_node].weight+table[working_node][i]<nodes[i].weight)
{
nodes[i].weight=nodes[working_node].weight+table[working_node][i];
nodes[i].prev=working_node;
}
}
}
min=-1;
for(i=0;i<NO_OF_NODES;i++)
{
if(nodes[i].state==TENTATIVE && nodes[i].weight<min)
{
min=nodes[i].weight;
working_node=i;
}
}
}while(working_node!=dest);
printf(" \n Shortest path got --> \n %c",dest+65);
do
{
working_node=nodes[working_node].prev;
printf("<-%c",working_node+65);
}while(nodes[working_node].prev!=-1);
printf("\n At a total weight of : %d",nodes[dest].weight);
getch();
}

SAMPLE RUN:
Enter source: a
Enter Destination: d
Shortest path got -->
D<-E<-J<-I<-H<-B<-A
At a total weight of: 7

Enter source: a
Enter destination: g
Shortest oath got -->
G<-H<-B<-A
At a total weight of: 4

3.a)write a program to compute Encryption and Decryption of a given message by


substitution method.

#include<stdio.h>
#include<conio.h>
#include<string.h>
char data[100];
int key;
void two_in_one();
void main()
{
clrscr();
printf("Enter the message:\n");
gets(data);
printf("Enter the key:");
scanf("%d",&key);
two_in_one();
printf("message after encrytion:\n");
puts(data);
printf("\n\nEnter the key for decryption:");
scanf("%d",&key);
key=key * -1;
two_in_one();
printf("message after decryption:\n");
puts(data);
getch();
}
void two_in_one()
{
int i=0,temp;
while(data[i]!='\0')
{
if(data[i]>=65&&data[i]<=90)
{
temp=data[i]+key;
if(temp<65)
temp=temp+26;
if(temp>90)
temp=temp-26;
}
else if(data[i]>=97&&data[i]<=122)
{
temp=data[i]+key;
if(temp<=97)
temp=temp+26;
if(temp>=122)
temp=temp-26;
}
else
temp=data[i];
data[i]=temp;
i++;
}
}

SAMPLE RUN:
1.Enter the message: friend
Enter the key: 2
message after encryption:htkgpf
Enter the key for decryption: 2
message after decryption: friend

3.b) Write a program to compute encryption and decryption of a given message by


using transposition method.

#include<stdio.h>
#include<conio.h>
#include<string.h>
char data[100];
char key[10];
void two_in_one();
void main()
{
clrscr();
printf("\n ENTER THE MESSAGE:\n");
gets(data);
printf("\n ENTER THE KEY(WITHOUT LETTER REPITITION):");
gets(key);
two_in_one(0);
printf("\n MESSAGE AFTER ENCRYPTION:\n");
puts(data);
printf("\n ENTER THE KEY FOR DECRYPTION:\n ");
gets(key);
two_in_one(1);
printf("\n MESSAGE AFTER DECRYPTION:\n");
puts(data);
getch();
}
void two_in_one(logic)
int logic;
{
char dummy[100];
int i=0,j,len,place,row;
strcpy(dummy,data);
len=strlen(data);
strupr(key);
place=0;
for(i=65;i<=90;i++)
{
for(j=0;j<strlen(key);j++)
{
if(key[j]==i)
{
row=0;
while((j+row)<len)
{
if(logic==0)
data[place++]=dummy[j+row];
else
data[j+row]=dummy[place++];
row=row+strlen(key);
}
}
}
}
}

SAMPLE RUN:

1.ENTER THE MESSAGE: bangalore is our capital city


ENTER THE KEY(WITHOUT LETTER REPITITION): word
MESSAGE AFTER ENCRYPTION: grsrpltal octcnoiuaaibae i y
ENTER THE KEY FOR DECRYPTION: word
MESSAGE AFTER DECRYPTION: bangalore is our capital city

4. Write a program to find minimum spanning tree of a subset.

#include<stdio.h>
#include<conio.h>

struct node
{
int set;
}node[100];

struct edge
{
int first_node,second_node;
int distance;
int selected;
}e[100];
int edge_count=0;
void getdata(int index,int total)
{
int i;
for(i=index;i<total;i++)
{
if(i!=index)
{
printf("\n Enter the distance between the vertex %c and %c:",
index+65,i+65);
scanf("%d",&e[edge_count].distance);
e[edge_count].first_node=index;
e[edge_count].second_node=i;
++edge_count;
}
}
}

void initialise(int total_nodes)


{
int i;
for(i=0;i<total_nodes;++i)
node[i].set=i;
for(i=0;i<edge_count;++i)
e[i].selected=-1;
}

void sort_edges()
{
int i,j;
struct edge temp;
for(i=0;i<edge_count-1;++i)
for(j=0;j<edge_count-1;++j)
if(e[j].distance>e[j+1].distance)
{
temp=e[j];
e[j]=e[j+1];
e[j+1]=temp;
}
}

void main()
{
int total_vertices,i,j,k,m,n,edges_selected=0,nodel,noder;
clrscr();
printf("\n Enter the number of vertices:");
scanf("%d",&total_vertices);
for(i=0;i<total_vertices;++i)
getdata(i,total_vertices);
initialise(total_vertices);
sort_edges();
clrscr();
printf("\n Sorted order of edges:");
for(i=0;i<edge_count;++i)
printf("\n Edge:%d First node: %c Second node: %c Distance: %d"
,i,e[i].first_node+65,e[i].second_node+65,e[i].distance);
i=0;
do
{
e[i].selected=1;
nodel=e[i].first_node;
noder=e[i].second_node;
if(node[nodel].set==node[noder].set)
e[i].selected=-1;
else
{
edges_selected++;
m=node[nodel].set;
k=node[noder].set;
for(n=0;n<total_vertices;++n)
{
if(node[n].set==k)
node[n].set=m;
}
}
++i;
}while(edges_selected<(total_vertices-1));
printf("\n\n Minimal spanning tree:\n");
for(i=0;i<edge_count;++i)
if(e[i].selected==1)
printf("\n %c <--> %c Distance: %d",
e[i].first_node+65,e[i].second_node+65,e[i].distance);
getch();
}

SAMPLE RUN:
Enter the number of vertices: 6
Enter the distance between vertex A and B:2
Enter the distance between vertex A and C:5
Enter the distance between vertex A and D:6
Enter the distance between vertex A and E:3
Enter the distance between vertex A and F:4
Enter the distance between vertex B and C:3
Enter the distance between vertex B and D:6
Enter the distance between vertex B and E:3
Enter the distance between vertex B and F:6
Enter the distance between vertex C and D:4
Enter the distance between vertex C and E:5
Enter the distance between vertex C and F:7
Enter the distance between vertex D and E:4
Enter the distance between vertex D and F:6
Enter the distance between vertex E and F:4
Sorted order of edges:
Edge: 0 First nide: A Second node: B Distance:2
Edge: 1 First nide: A Second node: B Distance:3
Edge: 2 First nide: B Second node: B Distance:3
Edge: 3 First nide: B Second node: B Distance:3
Edge: 4 First nide: A Second node: B Distance:4
Edge: 5 First nide: C Second node: B Distance:4
Edge: 6 First nide: D Second node: B Distance:4
Edge: 7 First nide: E Second node: B Distance:4
Edge: 8 First nide: A Second node: B Distance:5
Edge: 9 First nide: C Second node: B Distance:5
Edge: 10 First nide: A Second node: B Distance:6
Edge: 11 First nide: B Second node: B Distance:6
Edge: 12 First nide: B Second node: B Distance:6
Edge: 13 First nide: D Second node: B Distance:6
Edge: 14 First nide: C Second node: B Distance:7
Minimal spanning tree:

A <-->B Distance: 2
A <-->e Distance: 3
B <-->c Distance: 3
A <-->F Distance: 4
C <-->D Distance: 4

5.write a program to compute polynomial code checksum for CRC-CCITT.


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int i,j,m[20],m1[20],m2[20],g[20],flag=1;
char ch='n';
int r[20],lm,lg;
clrscr();
printf("\n Enter the order of message:");
scanf("%d",&lm);
printf("\n Enter the message:");
for(i=0;i<lm;i++)
scanf("%d",& m[i]);
printf("\n Enter the order of generator:");
scanf("%d",&lg);
printf("\n Enter the generator:");
for(i=0;i<lg;i++)
scanf("%d",&g[i]);
for(i=lm;i<lm+lg-1;i++)
m[i]=0;
for(i=0;i<lm+lg-1;i++)
m1[i]=m[i];
for(i=0;i<lm;i++)
{
if(m[i]==1)
{
for(j=0;j<lg;j++)
m[i+j]^=g[j];
}
}
printf("\n CRC code:");
for(i=lm;i<lm+lg-1;i++)
printf("%d",m[i]);
for(i=0;i<lm+lg-1;i++)
m2[i]=m[i]^m1[i];
printf("\n The transmitted message:");
for(i=0;i<lm+lg-1;i++)
printf("%d",m2[i]);
oncemore:
printf("\n Enter the recieved message:");
for(i=0;i<lm+lg-1;i++)
scanf("%d",&r[i]);
for(i=0;i<lm;i++)
{
if(r[i]==1)
{
for(j=0;j<lg;j++)
r[i+j]^=g[j];
}
}
for(i=0;i<lm+lg-1;i++)
if(r[i]==1)
{
flag=0;
break;
}
if(flag==0)
printf("\n errror in transmission:");
else
printf("\n No error:");
flag=1;
printf("\n Do you want to continue:");
ch=getche();
if(ch=='y'||ch=='Y')
goto oncemore;
getch();
}

SAMPLE RUN:

1. Enter the order of message: 6


Enter the message:
1
1
0
0
1
0
Enter the order of generator: 4
Enter the generator:
1
0
0
1
CRC code: 100
The transmitted message: 110010100

Enter the received message:

1
1
0
0
1
0
1
0
1
0
0
No error:
Do you want to continue:
% Matlab program to find the Impulse response of the system

% 1 program to find the impulse response of the system


clc
clear
close all
n = input('Desird impule response length=');
p = input('Type in the vector p=');
d = input('Type in the vector d=');
x = [1 zeros(1,n-1)];
y = filter(p,d,x);
k = 0:1:n-1;
stem(k,y);
xlabel('time index n');
ylabel('Amplitude');
sprintf('%d',y)

% 2 program to find the impulse response of the system


clc
close
clear
y = input('type in the numerator coefcients=');
x = input('Type in the denominator coefcients=');
[r,p,k]=residuez(y,x);
[h,t]=impz(y,x,10)
stem(t,h);
xlabel('Time');
ylabel('Amplitude');
title('Impulse response');

% verification of Sampling theorem by using matlab


clc
clear
close all
t=0:0.01:0.1;
f0 = input('enter the input signal frequency');
y = 2*cos(2*pi*f0*t);
fs=input('enter the sampling frequency in Hz=');
ts=1/fs;
tx=0:ts:1;
ys=2*cos(2*pi*f0*tx);
subplot(2,2,1);
stem(t,y)
xlabel('time index');
ylabel('Amplitude');
title('Input sine wave');
subplot(2,2,2);
stem(tx,ys);
xlabel('time index');
ylabel('Amplitude');
title('sine wave sampled at fs Hz');
x=abs(fft(y,512));
k=0:length(y)-1;
subplot(2,2,3);
stem(x);
xlabel('time index');
ylabel('Amplitude');
title('spectrum of input signal');
xs=abs(fft(ys));
k1=0:length(ys)-1;
subplot(2,2,4);
stem(xs);
xlabel('time index');
ylabel('Amplitude');
title('spectrum of sampled sine signal');

%program to compute diffrence equation by suing mat lab


clc
close all
clear
b=input('type in the numerator coeficients');
a=input('type in the denominator coefcients');
[r,p,k]=residuez(b,a);
[h,t]=impz(b,a,10)
x=input('enter the input sequence');
y=conv(x,h);
subplot(2,1,1);
stem(t,h);
xlabel('Time index');
ylabel('Amplitude');
title('Impulse response');
n=0:1:length(y)-1;
subplot(2,1,2);
stem(n,y);
xlabel('time');
ylabel('Amplitude');
title('Output sequence');

%Linear Convolution via the DFT

clc
close all
x = input('type in the first sequence = ');
h = input('type in the second sequence = ');
%determine the length of the result of convolution
L = length(x)+length(h)-1;
%compute the DFT by zero padding
XE = fft(x,L);
HE = fft(h,L);
%Determine the IDFT of the Product
y1 = ifft(XE.*HE);
%plot the sequnce generated by DFT Based
%convolution and the error from direct linear convolution
k = 0:1:L-1;
subplot(2,1,1)
stem(k,y1)
xlabel('time index n'); ylabel('amplitude');
title('result of the DFT-based linear convolution')
y2=conv(x,h);
sprintf(' the linear convolution dft based method')
sprintf(' %d ',y1)
sprintf(' the linear convolution direct method')
sprintf(' %d ',y2)

% program to compute the circular convolution


clc
clear
close all
x1=input(' enter the first input sequence');
x2=input('enter the second sequence');
L1=length(x1);
L2=length(x2);
N=max(L1,L2);
if(N<max(L1,L2))
error('N must be >= max(L,Lo)')
end
x1=[x1,zeros(1,N-L1)];
x2=[x2,zeros(1,N-L2)];
m=[0:1:N-1];
M=mod(-m,N);
x2=x2(M+1);

H=zeros(N,N);
for n=1:1:N
m=n-1;
p=0:1:N-1;
q=mod(p-m,N);
xm=x2(q+1);
H(n,:)=xm;
end
y=x1*H';
n2=0:1:length(y)-1;
stem(n2,y);
xlabel('Time index');
ylabel('Amplitude');
title('Circular convolution');

% to design a FIR LPF using Kaiser window


clc
close all
clear
fpts=input('Type in the bandedges= ');
mag=input('Type in the desired magnitude frequency=');
dev=input('Type in the riples in each band= ');
[N,wn,beta,ftype]=kaiserord(fpts,mag,dev);
w=kaiser(N+1,beta);
b = fir1(N,wn,w);
% b = fir1(N,wn,'high',w); % this is to generate high pass filter
[h,omega]=freqz(b,1,512);
plot(omega/pi,20*log10(abs(h)));
grid;
sprintf(' %d \n ',w)
xlabel('freq in radians omega/pi');
ylabel('gain in dB');
title('response of FIR filter using Kaiser window');
% to realize IIR butterworth filter using BLT
clc
close all
clear
Ap=input('enter the pass band attenuation= ');
As=input('Enter stop band attenuation= ');
fp=input('enter the pasband frequency= ');
fs=input('enter the stop band frequency= ');
Fs=input('enter the 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');

% to realize IIR chebyshev filter using BLT


clc
close all
clear
Ap=input('enter the pass band attenuation= ');
As=input('Enter stop band attenuation= ');
fp=input('enter the pasband frequency= ');
fs=input('enter the stop band frequency= ');
Fs=input('enter the sampling frequency= ');
w1=2*fp/Fs;
w2=2*fs/Fs;
[N,wn]=cheb1ord(w1,w2,Ap,As)
[b,a]=cheby1(N,Ap,wn)
freqz(b,a);
title('chebyshev frequency response');
�C� program to implement Difference Equation

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

#define FREQ 400

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;
float a[3] = {0.072231,0.144462,0.072231};
float b[3] = {1.000000,-1.109229,0.398152};

for (i=0;i<128;i++)
{
m[i] = sin(2*3.14*FREQ*i/24000);
}

for(j=0;j<128;j++)
{
x[0] = m[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];
y[2] = y[1];
y[1] = y[0];
x[2] = x[1];
x[1] = x[0];
}

�C� program to implement Impulse response

#include <stdio.h>

#define Order 2
#define Len 10

float y[Len] = {0,0,0}, sum;

main()
{
int j,k;

float a[order+1] = {0.1311, 0.2622, 0.1311};


float b[order+1] = {1, -0.7478, 0.2722};

for(j=0; j<Len;j++)
{
sum=0;
for( k=1;k<=order;k++)
{
if((j-k)>=0)
sum=sum+(b[k]*y[j-k]);
}
if (j<=order)
{
y[j]=a[j]-sum;
}
else
{
y[j]=-sum;
}
printf(�Response[%d] = %f\n�,j,y[j]);
}
}
�C� program to implement Linear Convolution

#include <stdio.h>

#define LENGHT1 6 /*Length of i/p samples sequences*/


#define LENGHT2 4 /*Length of impulse response Co-efficients*/

int x[2*LENGHT1-1]={1,2,3,4,5,6,0,0,0,0,0} /*Input Signal Samples*/


int h[2*LENGHT1-1]={1,2,3,4,0,0,0,0,0,0,0} /*Impulse Response Co-efficients*/

int y[LENGHT1+LENGHT2-1];

main()
{
int i=0,j;

for(i=0;i<( LENGHT1+LENGHT2-1);i++)
{
y[i]=0;
for(j=0;j<=i;j++)
y[i]+=x[j]*h[i-j];

}
for(I=0;i<( LENGHT1+LENGHT2-1);i++)
printf(�%d\n�,y[i]);
}

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 length 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++)
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<;i++)
printf(�%d \t�,y[i]);
}

IN PUT:
Eg: x[4]={3, 2, 1, 0}
h[4]={1, 1, 0, 0}

OUT PUT y[4]={3, 5, 3, 0}

Matlab Program to generate �FIR Filter-Low Pass�, Coefficients using FIR1

% FIR Low pass filters using rectangular,triangular and kaiser windows

%sampling rate= 8000

order=30;
cf=[500/4000,1000/4000,1500/4000]; %cf--> contains set of cutoff frequencies

%cutoff frequency -500


b_rect1=fir1(order,cf(1),boxcar(31)); %Rectangular
b_tri1=fir1(order,cf(1),bartlett(31)); %Trinagular
b_kai1=fir1(order,cf(1),Kaiser(31,8)); %kaiser [where 8-->Beta Co-efficient]

%cutoff frequency -1000


b_rect2=fir1(order,cf(2),boxcar(31)); %Rectangular
b_tri2=fir1(order,cf(2),bartlett(31)); %Trinagular
b_kai2=fir1(order,cf(2),Kaiser(31,8)); %kaiser [where 8-->Beta Co-efficient]

%cutoff frequency -1500


b_rect3=fir1(order,cf(3),boxcar(31)); %Rectangular
b_tri3=fir1(order,cf(3),bartlett(31)); %Trinagular
b_kai3=fir1(order,cf(3),Kaiser(31,8)); %kaiser [where 8-->Beta Co-efficient]

fid=fopen('FIR_lowpass_rectnagular.txt','wt');
fprintf(fid,'\t\t\t\t\t %s\n','Cutoff-500Hz');
fprintf(fid,'\nfloat b_kai1[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_kai1);
fseek(fid,-1,0);
fprintf(fid,'};');

fprintf(fid,'\n\n\n\n\n');
fprintf(fid,'\t\t\t\t\t %s\n','Cutoff-1000Hz');
fprintf(fid,'\nfloat b_kai2[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_kai2);
fseek(fid,-1,0);
fprintf(fid,'};');

fprintf(fid,'\n\n\n\n\n');
fprintf(fid,'\t\t\t\t\t %s\n','Cutoff-1500 Hz');
fprintf(fid,'\nfloat b_kai3[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_kai3);
fseek(fid,-1,0);
fprintf(fid,'};');
fclose(fid);
winopen('FIR_lowpass_rectnagular.txt');

Matlab Generated Coefficients for FIR Low pass Kaiser Filter:

Matlab generated Coefficients for FIR Low pass Triangular Filter

Matlab Generated Coefficients for FIR Low pass Triangular Filter:


Matlab generated Coefficients for FIR Low pass Rectangular filter

% FIR High pass filters using rectangular,triangular and kaiser windows

%sampling rate= 8000


clc
order=30;
cf=[400/4000,800/4000,1200/4000]; %cf--> contains set of cutoff frequencies

%cutoff frequency -400


b_rect1=fir1(order,cf(1),'high',boxcar(31)); %Rectangular
b_tri1=fir1(order,cf(1),'high',bartlett(31)); %Trinagular
b_kai1=fir1(order,cf(1),'high',Kaiser(31,8)); %kaiser [where 8-->Beta Co-efficient]

%cutoff frequency -1000


b_rect2=fir1(order,cf(2),'high',boxcar(31)); %Rectangular
b_tri2=fir1(order,cf(2),'high',bartlett(31)); %Trinagular
b_kai2=fir1(order,cf(2),'high',Kaiser(31,8)); %kaiser [where 8-->Beta Co-efficient]

%cutoff frequency -1500


b_rect3=fir1(order,cf(3),'high',boxcar(31)); %Rectangular
b_tri3=fir1(order,cf(3),'high',bartlett(31)); %Trinagular
b_kai3=fir1(order,cf(3),'high',Kaiser(31,8)); %kaiser [where 8-->Beta Co-efficient]

fid=fopen('FIR_highpass_rectnagular.txt','wt');
fprintf(fid,'\t\t\t\t\t %s\n','Cutoff-400Hz');
fprintf(fid,'\nfloat b_rect1[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect1);
fseek(fid,-1,0);
fprintf(fid,'};');

fprintf(fid,'\n\n\n\n\n');
fprintf(fid,'\t\t\t\t\t %s\n','Cutoff-800Hz');
fprintf(fid,'\nfloat b_rect2[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect2);
fseek(fid,-1,0);
fprintf(fid,'};');

fprintf(fid,'\n\n\n\n\n');
fprintf(fid,'\t\t\t\t\t %s\n','Cutoff-1200 Hz');
fprintf(fid,'\nfloat b_rect3[31]={');
fprintf(fid,'%f,%f,%f,%f,%f,%f,%f,%f,%f,\n',b_rect3);
fseek(fid,-1,0);
fprintf(fid,'};');

fclose(fid);
winopen('FIR_highpass_rectnagular.txt');

Matlab generated Coefficients for FIR High pass Rectangular filter

Matlab generated Coefficients for FIR High pass Kaiser filter


-*atlab generated Coefficients for FIR High pass Triangular filter

C program to Implement FIR filter

#include �filtercfg.h�

#include �dsk6713.h�
#include �dsk6713_aic23.h�

float filter_Coeff[]={0.000000,-0.001591,-0.002423,0.000000, 0.005728,0.011139,


0.010502,-0.000000,-0.018003,-0.033416,-0.031505, ,0.000000, 0.063010, 0.144802,
0.220534,0.262448,0.220534,0.144802,0.063010,0.000000,-0.031505,-0.033416,-
0.018003,-0.000000,0.010502,0.011139,0.005728,0.000000,-0.002423,-
0.001591,0.000000};

static short in_buffer[100];

DSK6713_AIC23_config ={\
0x0017, /*0 DSK6713_AIC23_LEFTINVOL Left line input channel volume */\
0x0017, /*1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume*/\
0x00d8, /*2 DSK6713_AIC23_LEFTPVOL Left channel headphone volume */\
0x00d8, /*3 DSK6713_AIC23_RIGHTPVOL Right channel headphone volume */\
0x0011, /*4 DSK6713_AIC23_ANAPATH Analog audio path control */\
0x0000, /*5 DSK6713_AIC23_DIGPATH Digital audio path channel */\
0x0000, /*6 DSK6713_AIC23_POWERDOWN power down control */\
0x0043, /*7 DSK6713_AIC23_DIGIF Digital audio interface format */\
0x0081, /*8 DSK6713_AIC23_SAMPLERATE sample rate control */\
0x0001 /*9 DSK6713_AIC23_DIGACT Digital interface activation */\
};

/*
* main() -Main code routine, initializes BSL and generates tone
*/

void main()
{
DSK6713_AIC23_CodecHandle hCodec;

Uint32 l_input, r_input, l_output, r_ output;

/* Intializes the board support library, must be called first*/


DSK6713_int();

/* Start the Codec */


hCodec = DSK6713_AIC23_openCOdec (0, &config);

DSK6713_AIC23_setFreq (hCodec, 1);

while(1)
{ /* Read a sample to the left channel */
while (!DSK6713_AIC23_read(hCodec, &l_input));
/* Read a sample to the right channel */
while (!DSK6713_AIC23_read(hCodec, &r_input));
l_output=(Int16)FIR_FILTER(&filter_Coeff ,l_input);
r_output=l_output;

/* send a sample to the left channel */


while (!DSK6713_AIC23_write(hCodec, &l_output));

/* send a sample to the right channel */


while (!DSK6713_AIC23_write(hCodec, &r_output));
}
/* Close the codec*/
DSK6713_AIC23_closeCodec(hCodec);
}

signed int FIR_FILTER(float *h, signed int x)


{

int I=0;
signed long output=0;

in_buffer[0] = x; /* new input at buffer[0]*/

for(i=30; i>0;i--)
in_buffer[i] = in_buffer[i-1]; /* shuffle the buffer */

for(i=0; I<32;i++)
output = output + h[i]* in_buffer[i];

return(output);

}
MATLAB PROGRAM TO GENERATE FILTER CO_EFFICIENTS
%IIR LOW Pass Butterworth and Chebysev filters
% smpling rate-24000
clc
close all
order = 2;
cf=[2500/12000,8000/12000];

% cutoff frequency -2500


[num_bw1,den_bw1]=butter(order,cf(1));
[num_cb1,den_cb1]=cheby1(order,3,cf(1));

% cutoff frequency -8000


[num_bw2,den_bw2]=butter(order,cf(2));
[num_cb2,den_cb2]=cheby1(order,3,cf(2));

fid=fopen('IIR_LP_BW.txt','wt');
fprintf(fid,'\t\t----------Pass band range: 0-2500Hz------\n');
fprintf(fid,'\t\t----------Magnitude response monotonic------\n\n');
fprintf(fid,'\n folat num_bw1[9]={');
fprintf(fid,'%f%f%f%f%f\n%f%f%f%f%f};\n',num_bw1);
fprintf(fid,'\n float den_bw1[9]={');
fprintf(fid,'%f%f%f%f%f\n%f%f%f%f%f};\n',den_bw1);

fprintf(fid,'\t\t----------Pass band range: 0-8000Hz------\n');


fprintf(fid,'\t\t----------Magnitude response monotonic------\n\n');
fprintf(fid,'\n folat num_bw2[9]={');
fprintf(fid,'%f%f%f%f%f\n%f%f%f%f%f};\n',num_bw2);
fprintf(fid,'\n float den_bw2[9]={');
fprintf(fid,'%f%f%f%f%f\n%f%f%f%f%f};\n',den_bw2);

fclose(fid);
winopen('IIR_LP_BW.txt');

fid=fopen('IIR_LP_CHEB_ type1.txt','wt');
fprintf(fid,'\t\t----------Pass band range: 0-2500Hz------\n');
fprintf(fid,'\t\t----------Magnitude response monotonic------\n\n');
fprintf(fid,'\n folat num_cb1[9]={');
fprintf(fid,'%f%f%f%f%f\n%f%f%f%f%f};\n',num_cb1);
fprintf(fid,'\n float den_cb1[9]={');
fprintf(fid,'%f%f%f%f%f\n%f%f%f%f%f};\n',den_cb1);

fprintf(fid,'\t\t----------Pass band range: 0-2500Hz------\n');


fprintf(fid,'\t\t----------Magnitude response monotonic------\n\n');
fprintf(fid,'\n folat num_cb2[9]={');
fprintf(fid,'%f%f%f%f%f\n%f%f%f%f%f};\n',num_cb2);
fprintf(fid,'\n float den_cb2[9]={');
fprintf(fid,'%f%f%f%f%f\n%f%f%f%f%f};\n',den_cb2);

fclose(fid);
winopen('IIR_LP_CHEB_ type1.txt');
%%%% %%%%%%%%%%%%%%%%%
figure(1);
[h,w]=freqz(num_bw1,den_bw1);
w=(w/max(w))*12000;
plot(w,20*log10(abs(h)),'linewidth',2)
hold on
[h,w]=freqz(num_cb1,den_cb1);
w=(w/max(w))*12000;
plot(w,20*log10(abs(h)),'linewidth',2,'color','r')
grid on
legend('Butterworth','Chebsyev Type_1');
xlabel('Frequency in Hertz');
ylabel('Magnitude in Decibels');
title('Magnitude response of low pass IIR filters (FC=2500Hz)');

figure(2);
[h,w]=freqz(num_bw2,den_bw2);
w=(w/max(w))*12000;
plot(w,20*log10(abs(h)),'linewidth',2)
hold on
[h,w]=freqz(num_cb2,den_cb2);
w=(w/max(w))*12000;
plot(w,20*log10(abs(h)),'linewidth',2,'color','r')
grid on
legend('Butterworth','Chebsyev Type_1(ripple 3dB)');
xlabel('Frequency in Hertz');
ylabel('Magnitude in Decibels');
title('Magnitude response in pasband');
axis([0 12000 -20 20]);

� C� Program to Implement IIR filter


include �xyzcfg.h�

#include �dsk6713.h�
#include �dsk6713_aic23.h�

const signed int filter_Coeff[]=


{
7215,-7215,32767,039,6171, /*HP 7000*/
};

/* Codec Configuration settings */


DSK6713_AIC23_config ={\
0x0017, /*0 DSK6713_AIC23_LEFTINVOL Left line input channel volume */\
0x0017, /*1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume*/\
0x00d8, /*2 DSK6713_AIC23_LEFTPVOL Left channel headphone volume */\
0x00d8, /*3 DSK6713_AIC23_RIGHTPVOL Right channel headphone volume */\
0x0011, /*4 DSK6713_AIC23_ANAPATH Analog audio path control */\
0x0000, /*5 DSK6713_AIC23_DIGPATH Digital audio path channel */\
0x0000, /*6 DSK6713_AIC23_POWERDOWN power down control */\
0x0043, /*7 DSK6713_AIC23_DIGIF Digital audio interface format */\
0x0081, /*8 DSK6713_AIC23_SAMPLERATE sample rate control */\
0x0001 /*9 DSK6713_AIC23_DIGACT Digital interface activation */\
};

/*
* main() -Main code routine, initializes BSL and generates tone
*/

void main()
{
DSK6713_AIC23_CodecHandle hCodec;

int l_input, r_input, l_output, r_ output;

/* Intializes the board support library, must be called first*/


DSK6713_int();

/* Start the Codec */


hCodec = DSK6713_AIC23_openCOdec (0, &config);

DSK6713_AIC23_setFreq (hCodec, 3);

while(1)
{ /* Read a sample to the left channel */
while (!DSK6713_AIC23_read(hCodec, &l_input));
/* Read a sample to the right channel */
while (!DSK6713_AIC23_read(hCodec, &r_input));
l_output=IIR_FILTER(&filter_Coeff ,l_input);
r_output=l_output;

/* send a sample to the left channel */


while (!DSK6713_AIC23_write(hCodec, &l_output));

/* send a sample to the right channel */


while (!DSK6713_AIC23_write(hCodec, &r_output));
}
/* Close the codec*/
DSK6713_AIC23_closeCodec(hCodec);
}

signed int IIR_FILTER(const signed int * h,signed int x1)


{

static signed int x[6]={ 0, 0, 0, 0, 0, 0}; /*x(n),x(n-1),x(n-2) must be static*/


static signed int y[6]={ 0, 0, 0, 0, 0, 0}; /*y(n),y(n-1),y(n-2) must be static*/

int temp=0;
temp=(short int)x1; /* copy input to temp*/

x[0] = (signed int) temp; /* Copy inpu to temp*/


temp = (signed int) temp; /* copy input to x[stages][0]*/

temp = ( (int)h[0] x[0]); /* B0 * x(n) */


temp += ( (int)h[1] x[1]); /* B0 * x(n) /* B1/2 * x(n-1) */
temp += ( (int)h[1] x[1]); /* B0 * x(n) */ B1/2 * x(n-1) */
temp += ( (int)h[2] x[2]); /* B0 * x(n) */ B2 * x(n-2) */

temp -= ( (int)h[4] y[1]); /* B0 * x(n) */ A1/2 * y(n-1) */


temp -= ( (int)h[4] y[1]); /* B0 * x(n) */ A1/2 * y(n-1) */
temp -= ( (int)h[5] y[2]); /* B0 * x(n) */ A2 * y(n-2) */

/* divide temp by coefficients [A0] */

temp>= 15;

if ( temp >32767 )
{
temp=32767;
}
else if (temp < -32767)
{
temp = -32767;
}
y[0]= temp;
/* shuffle values along one place for next time*/

y[2] = y[1]; /* y(n-2) = y(n-1) */


y[2] = y[1]; /* y(n-2) = y(n) */

x[2] = x[1]; /* x(n-2) = x(n-1) */


x[1] = x[0]; /* x(n-2) = x(n) */

/* temp is used as input next time through

return(temp<<2);
}

in_buffer[0] = x; /* new input at buffer[0]*/

for(i=30; i>0;i--)
in_buffer[i] = in_buffer[i-1]; /* shuffle the buffer */

for(i=0; I<32;i++)
output = output + h[i]* in_buffer[i];

return(output);

S-ar putea să vă placă și