Documente Academic
Documente Profesional
Documente Cultură
#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
#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*/
SAMPLE RUN:
#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
#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
#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:
#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 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
SAMPLE RUN:
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
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)
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');
#include <stdio.h>
#include <math.h>
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];
}
#include <stdio.h>
#define Order 2
#define Len 10
main()
{
int j,k;
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>
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]);
}
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}
order=30;
cf=[500/4000,1000/4000,1500/4000]; %cf--> contains set of cutoff frequencies
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');
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');
#include �filtercfg.h�
#include �dsk6713.h�
#include �dsk6713_aic23.h�
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;
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;
int I=0;
signed long output=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];
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);
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);
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]);
#include �dsk6713.h�
#include �dsk6713_aic23.h�
/*
* main() -Main code routine, initializes BSL and generates tone
*/
void main()
{
DSK6713_AIC23_CodecHandle hCodec;
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;
int temp=0;
temp=(short int)x1; /* copy input to temp*/
temp>= 15;
if ( temp >32767 )
{
temp=32767;
}
else if (temp < -32767)
{
temp = -32767;
}
y[0]= temp;
/* shuffle values along one place for next time*/
return(temp<<2);
}
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);