Continue to Site

### Welcome to EDAboard.com

#### Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Status
Not open for further replies.

#### ezyneab

##### Newbie level 4
My program

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "dsk6713_aic23.h" //codec-DSK support file
Uint32 fs=DSK6713_AIC23_FREQ_8KHZ; //set sampling rate
#define BIT_PER_SAMPLE 16
int input_dec;

int m=5, n=31, k=16, t=3, d=7;
int length=31;
int p[6]; // irreducible polynomial
int alpha_to[32], index_of[32], g[16];
int recd[31], data[16], bb[16];
int numerr, errpos[32], decerror=0;

typedef struct list
{
short data;

struct list *previous,*next;
}node;

node *first=NULL;
node *back=NULL;
//===========Decimal to Binary==================
void de2bi(char bin[BIT_PER_SAMPLE],int dec)
{
int j, temp1, temp2=dec;

for(j=0; j<BIT_PER_SAMPLE; j++)
{
temp1=(int)floor((float)temp2/2);
bin[j]=temp2-temp1*2;
temp2=temp1;

}
return;
}

//Primitive polynomial of degree 5
{
register int i;
p[0]=p[2]=p[5]=1; // x^5 + x^2 + 1
p[1]=p[3]=p[4]=0;
}

// generate GF(2**m) from the irreducible polynomial p(X) in p[0]..p[m]
// lookup tables: index->polynomial form alpha_to[] contains j=alpha**i;
// polynomial form -> index form index_of[j=alpha**i] = i alpha=2 is the
// primitive element of GF(2**m)
void generate_gf()
{
alpha_to[m]=0;
for(i=0; i<m; i++)
{
index_of[alpha_to]=i;
if(p!=0)
}
index_of[alpha_to[m]]=m;
for(i=m+1; i<n; i++)
{
else
alpha_to=alpha_to[i - 1]<<1;
index_of[alpha_to]=i;
}
index_of[0]=-1;
}

// Compute generator polynomial of BCH code of length = 31, redundancy = 15
void gen_poly()
{
register int ii, jj, ll, kaux;
int test, aux, nocycles, root, noterms, rdncy;
int cycle[16][6], size[16], min[16], zeros[16];
// Generate cycle sets modulo 31
cycle[0][0]=0; size[0]=1;
cycle[1][0]=1; size[1]=1;
jj=1; // cycle set index
do
{
// Generate the jj-th cycle set
ii=0;
do
{
ii++;
cycle[jj][ii]=(cycle[jj][ii-1]*2)%n;
size[jj]++;
aux=(cycle[jj][ii]*2)%n;
}
while(aux!=cycle[jj][0]);
// Next cycle set representative
ll=0;
do
{
ll++;
test=0;
for(ii=1; ((ii<=jj)&&(!test)); ii++)
// Examine previous cycle sets
for(kaux=0; ((kaux<size[ii])&&(!test)); kaux++)
if(ll==cycle[ii][kaux])
test=1;
}
while((test)&&(ll<(n - 1)));
if(!(test))
{
jj++; // next cycle set index
cycle[jj][0]=ll;
size[jj]=1;
}
}
while(ll<(n-1));
nocycles=jj; // number of cycle sets modulo n
// Search for roots 1, 2, ..., d-1 in cycle sets
kaux=0;
rdncy=0;
for(ii=1; ii<=nocycles; ii++)
{
min[kaux]=0;
for(jj=0; jj<size[ii]; jj++)
for(root=1; root<d; root++)
if(root==cycle[ii][jj])
min[kaux]=ii;
if(min[kaux])
{
rdncy+=size[min[kaux]];
kaux++;
}
}
noterms=kaux;
kaux=1;
for(ii=0; ii<noterms; ii++)
for(jj=0; jj<size[min[ii]]; jj++)
{
zeros[kaux]=cycle[min[ii]][jj];
kaux++;
}
// Compute generator polynomial
g[0]=alpha_to[zeros[1]];
g[1]=1; // g(x) = (X + zeros[1]) initially
for(ii=2; ii<=rdncy; ii++)
{
g[ii]=1;
for(jj=ii-1; jj>0; jj--)
if(g[jj]!=0)
g[jj]=g[jj-1]^alpha_to[(index_of[g[jj]]+zeros[ii])%n];
else
g[jj]=g[jj-1];
g[0]=alpha_to[(index_of[g[0]]+zeros[ii])%n];
}
}
//===============Encode_bch==============================
// Calculate redundant bits bb[], codeword is c(X) = data(X)*X**(n-k)+ bb(X)
void encode_bch(int data[16], int bb[16])
{
register int i, j;
register int feedback;
for(i=0; i<length-k; i++)
bb=0;
for(i=k-1; i>=0; i--)
{
feedback=data^bb[length-k-1];
if(feedback!=0)
{
for(j=length-k-1; j>0; j--)
if(g[j]!=0)
bb[j]=bb[j-1]^feedback;
else
bb[j]=bb[j-1];
bb[0]=g[0]&&feedback;
}
else
{
for(j=length-k-1; j>0; j--)
bb[j]=bb[j-1];
bb[0]=0;
};
};
}
//==============decode_bch================
void decode_bch()
{
register int i, j, q;
int elp[3], s[5], s3;
int count=0, syn_error=0;
int loc[3], reg[3];
int aux;
// first form the syndromes
for(i=1; i<=4; i++)
{
s=0;
for(j=0; j<length; j++)
if(recd[j]!=0)
s^=alpha_to[(i*j)%n];
if(s!=0)
syn_error=1; // set flag if non-zero syndrome NOTE: If only error detection is needed, then exit the program here...
s=index_of[s]; // convert syndrome from polynomial form to index form
};
if(syn_error) // If there are errors, try to correct them
{
if(s[1]!=-1)
{
s3=(s[1]*3)%n;
if(s[3]==s3) // Was it a single error ?
{
recd[s[1]]^=1; // Yes: Correct it
}
else // Assume two errors occurred and solve for the coefficients of sigma(x), the error locator polynomail
{
if(s[3]!=-1)
aux=alpha_to[s3]^alpha_to[s[3]];
else
aux=alpha_to[s3];
elp[0]=0;
elp[1]=(s[2]-index_of[aux]+n)%n;
elp[2]=(s[1]-index_of[aux]+n)%n;
for(i=1; i<=2; i++) // find roots of the error location polynomial
reg=elp;
count=0;
for(i=1; i<=n; i++) // Chien search
{
q=1;
for(j=1; j<=2; j++)
if(reg[j]!=-1)
{
reg[j]=(reg[j]+j)%n;
q^=alpha_to[reg[j]];
}
if(!q) // store error location number indices
{
loc[count]=i%n;
count++;
}
}
if(count==2) // no. roots = degree of elp hence 2 errors
for(i=0; i<2; i++)
recd[loc]^=1;
}
}
}
}
//=========== binary 16 bits to decimal==============
int bi2de(char bin[BIT_PER_SAMPLE])
{
int op=0;
long int power_vector[BIT_PER_SAMPLE];
int j;

int i, c=0;
int flag=0;

power_vector[0]=1;
//====================1's complement==================

if(bin[15]==1)
{ flag=1;
for (i=15; i>=0; i--)
{
if(bin==0)
{
bin=1;
}
else
{
bin=0;
}
// printf("%d",bin);
}

//=================2's complement=======================
for(i=0; i<16; i++)
{
if(i==0)
{
if(bin==0)
{
bin=1;
}
else
{
bin=0;
c=1;
}
}
else
{
if(c==1 && bin==0)
{
bin=1;
c=0;
}
else if (c==1 && bin==1)
{
bin=0;
c=1;
}
}
// printf("%d",bin);
}
//==============================
}

for(j=1; j<BIT_PER_SAMPLE; j++)
{
power_vector[j]=power_vector[j-1]*2;
}

//================= convert bit to decimal digit===============

for(j=0; j<BIT_PER_SAMPLE; j++)
{
op+=power_vector[j]*bin[j];
}
if(flag==1)
{
op=(-2*op)+op;
flag=0;
}
return(op);
}

//======================main_code==========================
void main()
{
char temp_binary[BIT_PER_SAMPLE],temp_decimal[BIT_PER_SAMPLE];

int *binary_buffer;

int *out_buffer;

int j,i=0;

int count=0,count_bit;

char temp[BIT_PER_SAMPLE];

int c=0;

int z=0;

generate_gf(); // generate the Galois Field GF(2**m)
gen_poly(); // Compute the generator polynomial of BCH code

comm_poll(); //init DSK, codec, McBSP
DSK6713_LED_init(); //init LED from BSL
DSK6713_DIP_init(); //init DIP from BSL
{
short voice ;
node *currptr;

while(DSK6713_DIP_get(3) == 0)
{ c++;
count++; //count data

DSK6713_LED_on(3); //turn on LED#3
{
voice=input_sample();//store input data into buffer
printf("%d\n",voice);

// scanf("%d",&voice);

if(back==NULL)
{
currptr=(node*)malloc(sizeof(node));
back=currptr;
first=currptr;
currptr->data=voice;
currptr->previous=NULL;
currptr->next=NULL;
}
else
{
currptr=(node*)malloc(sizeof(node));
back->next=currptr;
currptr->previous=back;
back=currptr;
currptr->data=voice;
back->next=NULL;
}
}
}
DSK6713_LED_off(3);

count_bit=count*BIT_PER_SAMPLE;//count bit
// printf("Bit all=%d\n",count_bit);

//===========================================

binary_buffer = (int *) malloc(count_bit*sizeof(int)); //binary_buffer dynamic array

//===========================================

while(first!=NULL) //while first data
{
input_dec=first->data;
de2bi(temp_binary, input_dec); //Goto func de2bi

//=================define_binary_16bit=====================
for(j=15; j>=0; j--) //for 16 bit
{
binary_buffer=temp_binary[j]; //tranfer temp_binary to dynamic array
// printf("%d",binary_buffer);
i++;
}

first=first->next;
}
/* for(i=0;i<=(count_bit-1);i++)
{
printf("%d",binary_buffer);
} */

//====================dividing_binary_data================

for(j=0; j<count; j++)
{
for(i=0; i<16; i++)
{
data=binary_buffer[16*j+i];
// printf("%d",data);
}
// BCH encoding
encode_bch(data, bb); // encode data
for(i=0; i<length-k; i++)
{
recd=bb; // first (length-k) bits are redundancy
// printf("%d",recd);
}
for(i=0; i<k; i++)
{
recd[i+length-k]=data; // last k bits are data
// printf("%d",recd[i+length-k]);
}

for (i = 0; i < length; i++)
{
printf("%d", recd); //print codeword
}

printf("codeword\n");

//Input Error

//---------------------------------------------------------
// BCH decoding
decode_bch(); //decoded data
for(i=length-k; i<length; i++) // decoding errors: compare only the data portion
{ if(data[i-length+k]!=recd)
decerror++;
}

for(i=0; i<16; i++)
{
binary_buffer[16*j+i]=recd[i+length-k];
printf("%d",binary_buffer[16*j+i]);
}
printf("decode\n");
}

//==============================================
out_buffer = (int *) malloc(count*sizeof(int));

// Convert 16-bit binary 'binary_buffer' to decimal 'out_buffer'

for(i=0; i<count; i++)
{
for(j=0; j<BIT_PER_SAMPLE; j++)
{
temp_decimal[j]=binary_buffer[16*i+j];
// printf("%d",temp_decimal[j]);
}
z=0;
for(j=15; j>=0; j--) //for 16 bit
{
temp[z]=temp_decimal[j];
// printf("%d",temp[z]);
z++;
}

out_buffer=bi2de(temp);

// output_sample(out_buffer*10);
printf("%d\n",out_buffer);
}
free(binary_buffer) ;
}
}

***************************************************
Help me to do.
Program to voice out.

Status
Not open for further replies.