Sampling Rate Conversion in Real Time(Using a C language)
Upsampling and DownSampling using Reconstruction
(Using sinc Function):
Code:
#include<stdio.h>#include<math.h>
#define PI 3.14
void main()
{
int m,n,samplingrate,chunksize,subchunk2size,newchunksize,newsamplingrate,newsubchunk2size;
float i;
//int noofsamples;
int factor;
unsigned char a,b,c,d,e;
FILE *fp,*fp2;
void writeheaderfile(FILE *fp,int newsamplingrate,int newchunksize,int newsubchunk2size,FILE *fp2);
void writedatafile(FILE *fp,int newsamplingrate,int newchunksize,int newsubchunk2size,float i,int subchunk2size,FILE *fp2);
printf("Original Song is in : song1.wav\nResult is stored in : Createdsong.wav\n");
fp=fopen("song1.wav","r");
fp2=fopen("Createdsong.wav","w+");
fseek(fp,24,SEEK_SET);
fread(&samplingrate,4,1,fp);
printf("Sampling Rate is : %d\n",samplingrate);
fseek(fp,4,SEEK_SET);
fread(&chunksize,4,1,fp);
printf("chunksize is : %d\n",chunksize);
fseek(fp,40,SEEK_SET);
fread(&subchunk2size,4,1,fp);
printf("subchunk2size is : %d\n",subchunk2size);
printf("Enter the New Sampling Rate : ");scanf("%d",&newsamplingrate);
i=(float)samplingrate/newsamplingrate;
//printf("I value is : %f\n",i);
//noofsamples=readnoofsamples(fp);
printf("No of Samples in Given File : %d\n",subchunk2size/4);
newsubchunk2size=subchunk2size/i;
newchunksize=newsubchunk2size+36;
//noofsamples=newsubchunk2size/4;
writeheaderfile(fp,newsamplingrate,newchunksize,newsubchunk2size,fp2);
//fclose(fp2);
// fp2=fopen("Createdsong.wav","w+");
writedatafile(fp,newsamplingrate,newchunksize,newsubchunk2size,i,subchunk2size,fp2);
}
/*int readnoofsamples(FILE *fp)
{
int count=1;
unsigned char c;
fp=fopen("song1.wav","r");
fseek(fp,44,SEEK_SET);
while(1)
{
if(feof(fp)) break;
fscanf(fp,"%c",&c);
count++;
//printf("%d\n",count);
}
//count=count/4;
return count;
}*/
void writedatafile(FILE *fp,int newsamplingrate,int newchunksize,int newsubchunk2size,float i,int subchunk2size,FILE *fp2)
{
//FILE *fp2;
int count=0,samco=1,center,samp=0,previous=0,x,temp;
short int e,r;
long int m,n,sumfr,sumfl;
float suml=0,sumr=0;
unsigned char z,s,v,w,c;
float sinc(int m,float i,int n);
printf("Writing the Samples in File....Please Wait....\n");
//fp=fopen("song1.wav","r");
fseek(fp,40,SEEK_SET);
fread(&subchunk2size,4,1,fp);
//printf("subchunk2size is : %d\n",subchunk2size);
//fclose(fp);
//fp=fopen("song1.wav","r");
//fp2=fopen("Createdsong.wav","a");
fseek(fp,44,SEEK_SET);
fseek(fp2,44,SEEK_SET);
//printf("%d\n",subchunk2size);
for(m=0;m<=subchunk2size;m++)
{
//if(feof(fp)) break;
sumfl=0;
sumr=0;
suml=0;
sumfr=0;
center=rint(m*i);
if(previous==center)
{
}
else
{
for(temp=0;temp<(center-previous);temp++)
{
count++;
}
}
previous=center;
fseek(fp,44+(4*count),SEEK_SET);
for(n=count-10;n<count+10;n++)
{
//printf("m value : %d\n",m);
if(n<0)
{
e=0;
r=0;
}
else
{
fread(&e,2,1,fp);
fread(&r,2,1,fp);
}
sumr=sumr+(e*sinc(m,i,n));
suml=suml+(r*sinc(m,i,n));
sumfl=suml;
sumfr=sumr;
//if(sumfl==0) printf("m=%d\t n=%d\n",m,n);
//printf("m*I=%d\t n=%d\t e=%d\n",m*i,n,e);
//printf("suml=%lf\t sumr=%lf\t n=%d\t count=%d\t e=%d\t r=%d\n",suml,sumr,n,count,e,r);
}
fwrite(&sumfr,2,1,fp2);
fwrite(&sumfl,2,1,fp2);
/*fscanf(fp,"%c",&t);
fscanf(fp,"%c",&s);
fscanf(fp,"%c",&v);
fscanf(fp,"%c",&w);
printf("%d %d %d %d\n",t,s,v,w);*/
//if(sumfr==0) printf("m=%d\t and n=%d\n",m,n);
//printf("m=%d\t and sumfl=%d\t sumfr=%d\n",m,sumfl,sumfr);
//getchar();
}
printf("Process Completed...Result is in Createdsong.wav\n");
fclose(fp);
fclose(fp2);
}
float sinc(int m,float i,int n)
{
float temp;
if(rint(m*i)==n)
{
//printf("sinc value is : %f\n",1.00);
return 1.00;
}
else
{
temp=PI*(m*i-n);
//printf("sinc value is : %f\n",(sin(temp)/temp));
return (sin(temp)/temp);
}
}
void writeheaderfile(FILE *fp,int newsamplingrate,int newchunksize,int newsubchunk2size,FILE *fp2)
{long int count=1;
unsigned char header;
//FILE *fp2;
//fp=fopen("song1.wav","r");
//fp2=fopen("Createdsong.wav","w+");
fseek(fp,0,SEEK_SET);
while(1)
{
fread(&header,4,1,fp);
fwrite(&header,4,1,fp2);
if(ftell(fp)==44) break;
}
fseek(fp2,4,SEEK_SET);
fwrite(&newchunksize,4,1,fp2);
fseek(fp2,24,SEEK_SET);
fwrite(&newsamplingrate,4,1,fp2);
fseek(fp2,40,SEEK_SET);
fwrite(&newsubchunk2size,4,1,fp2);
//fclose(fp2);
//fclose(fp);
//printf("Suc\n");
}
Down Sampling using Decimation(Without Using Sinc Function):
Code:#include<stdio.h>
void main()
{
FILE *fp,*fp2;
unsigned char down;
int samplingrate,ratio,i=4,chunksize,subchunk2size,newchunksize,newsubchunk2size;
int newsamplingrate;
long int count=1;
//Getting the Sampling Rate from the File
// while(1)
// {
fp=fopen("mono.wav","r");
fseek(fp,24,SEEK_SET);
fread(&samplingrate,4,1,fp);
rewind;
fseek(fp,4,SEEK_SET);
fread(&chunksize,4,1,fp);
rewind;
fseek(fp,40,SEEK_SET);
fread(&subchunk2size,4,1,fp);
//fscanf(fp,"%d",&sample);
//**fprintf(stdout,"samplingrate is %u\n chunksize is : %u\nsubchunk2size is : %u\n",samplingrate,chunksize,subchunk2size);
//count++;
//if(count>44) break;
//}
//printf("The count value is : %ld\n",count-1);
fclose(fp);
//printf("Enter the New Sampling Rate : ");scanf("%d",&newsamplingrate);
newsamplingrate=5000;
if(newsamplingrate<samplingrate)
{
ratio=samplingrate/newsamplingrate;
fp=fopen("song1.wav","r");
fseek(fp,44,SEEK_SET);
while(1)
{
fscanf(fp,"%c",&down);
//fprintf(stdout,"%d\t",down);
count++;
i--;
if(feof(fp)) break;
if(i<1)
{
i=4;
fseek(fp,SEEK_CUR+4*(ratio-1)-1,SEEK_CUR);
}
}
fclose(fp);
count=count/4;
// printf("Count is : %ld\n",count);
newsubchunk2size=chunksize/3;
newchunksize=newsubchunk2size+36;
writeheaderfile(fp,newsamplingrate,newchunksize,newsubchunk2size);
writedatafile(fp,newsamplingrate,newchunksize,newsubchunk2size,ratio);
}
/*if(newsamplingrate>samplingrate)
{
ratio=newsamplingrate/samplingrate;
fp=fopen("song1.wav","r");
fseek(fp,44,SEEK_SET);
newsubchunk2size=chunksize*3;
newchunksize=newsubchunk2size+36;
writeheaderfile(fp,newsamplingrate,newchunksize,newsubchunk2size);
writedataupfile(fp,newsamplingrate,newchunksize,newsubchunk2size,ratio);
}*/
}
//Writing the Header Section
void writeheaderfile(FILE *fp,int newsamplingrate,int newchunksize,int newsubchunk2size)
{long int count=1;
unsigned char header;
FILE *fp2;
fp=fopen("mono.wav","r");
fp2=fopen("song2.wav","w+");
//rewind;
while(1)
{
fread(&header,4,1,fp);
//if(ftell(fp)==5) fprintf(fp2,"%c",newchunksize);
//else if(ftell(fp)==17) fprintf(fp2,"%c",newsubchunk2size);
//else if(ftell(fp)==25) fprintf(fp2,"%c",newsamplingrate);
//else fprintf(fp2,"%c",header);
fwrite(&header,4,1,fp2);
if(ftell(fp)==44) {fclose(fp);break;}
}
//fp=fopen("song1.wav","r");
//fp2=fopen("song2.wav","w+");
//rewind(fp2);
fseek(fp2,4,SEEK_SET);
fwrite(&newchunksize,4,1,fp2);//fprintf(fp2,"%c",&newchunksize);
//rewind(fp2);
fseek(fp2,40,SEEK_SET);
fwrite(&newsubchunk2size,4,1,fp2);
//rewind(fp2);
fseek(fp2,24,SEEK_SET);
fwrite(&newsamplingrate,4,1,fp2);
//fclose(fp);
fclose(fp2);
}
//Write data to file
void writedatafile(FILE *fp,int newsamplingrate,int newchunksize,int newsubchunk2size,int ratio)
{unsigned char down;
long int count=1;
int i=4;
FILE *fp2;
fp=fopen("mono.wav","r");
fp2=fopen("song2.wav","a+");
fseek(fp,44,SEEK_SET);
while(1)
{
fscanf(fp,"%c",&down);
fprintf(fp2,"%c",down);
count++;
i--;
if(feof(fp)) break;
if(i<1)
{
i=4;
fseek(fp,SEEK_CUR+4*(ratio-1)-1,SEEK_CUR);
}
}
fclose(fp);
fclose(fp2);
}
0 Comments:
Post a Comment