沙发
anshanguojiahao [专家分:0] 发布于 2010-05-27 17:42:00
#include<stdio.h>
#include<math.h>
#include<mem.h>
#include<ctype.h>
#define FRAME 300
#define BETWEEN 100
#define Fs 10000
int Pitch[300];
void Filter(int x[],float y[],int length);
int PitchDetect(float *sample);
int MaxIntAbs(int *Buffer,int length);
float Maxfloat(float *Buffer,int length,int *Maxindx);
int MaxIntAbs(int *Buffer,int length);
float MaxFloatAbs(float sample[],int length);
float minor(float x1,float x2);
void AutoCorrelate(float *Buffer,int length,float Max,float *R);
float MiddelShap(float *Buffer,int length,int *V,float *X,float Max);
void PitchesDetect(int *sample,int length,int *Pitch)
{
float y[FRAME];
int i,k=0;
for(i=0;i<length-FRAME;i+=BETWEEN)
{
Filter(&sample[i],y,FRAME);
Pitch[k]=PitchDetect(y);
}
printf("\n%d",k);
}
int PitchDetect(float *sample)
{
float Maxhead,Maxtail;
int N;
float Max,R[FRAME];
setmem(R,sizeof(float)*FRAME,0);
Maxhead=MaxFloatAbs(sample,100);
Maxtail=MaxFloatAbs(&sample[FRAME-100],100);
Max=0.68*minor(Maxhead,Maxtail);
AutoCorrelate(sample,FRAME,Max,R);
Max=Maxfloat(&R[20],FRAME-20,&N);
N+=20;
if (Max<0.25*R[0])
N=0;
return N;
}
float Maxfloat(float *Buffer,int length,int *Maxindx)
{
int i;
float Max;
Max=Buffer[0];
for (i=1;i<length;i++)
if (Max<Buffer[i])
{
Max=Buffer[i];
*Maxindx=i;
}
return Max;
}
void AutoCorrelate(float *Buffer,int length,float Max,float *R)
{
int i,j,V[FRAME];
float X[FRAME];
R[0]=MiddelShap(Buffer,length,V,X,Max);
for(j=20;j<160;j++)
{
for(i=j;i<length;i++)
if((i+j)<length)
if(V[j]<0)
R[j]=R[j]-X[i+j];
else
if(V[j]>0)
R[j]=R[j]+X[i+j];
}
}
float MiddelShap(float *Buffer,int length,int *V,float *X,float Max)
{
int i,RO=0;
for(i=0;i<length;i++)
{
if(abs(Buffer[i])<Max)
{V[i]=0;
X[i]=0;
}
else
if(Buffer[i]>0)
{
V[i]=1;
X[i]=Buffer[i]-Max;
RO+=X[i];
}
else
{
V[i]=-1;
X[i]=Buffer[i]+Max;
RO-=X[i];
}
}
return RO;
}
int MaxIntAbs(int *Buffer,int length)
{
int i,Max;
Max=abs(Buffer[0]);
for(i=1;i<length;i++)
{
if(Max<abs(Buffer[i]))
Max=abs(Buffer[i]);
}
return Max;
}
float MaxFloatAbs(float sample[],int length)
{int i;
float max;
max=sample[0];
for(i=0;i<length;i++)
if(max<sample[i])
max=sample[i];
return(max);
}
float minor(float x1,float x2)
{float x;
x=x1;
if(x1>x2)
x=x2;
return(x);
}
void Filter(int x[],float y[],int length)
{
int i,j;
double H[20]={
.0035270585,-.0075853243,-.022130724,
-.037701912,-.040792551,
-.017618544,.037134223,.1139423,.18955371,
.23657782,
.23657782,.18955371,.1139423,.037134223,
-.017618544,
-.040792551,-.037701912,-.022130724,
-.0075853243,.0035270585
};
setmem(y,sizeof(float)*length,0);
for(i=10;i<length-11;i++)
{ for(j=0;j<20;j++)
y[i]+=x[i-j+10]*H[j];
}
}
int main()
{
int length=900;
int temp;
FILE *fp1;
long frame;
int indata[10000];
int i,k=0,n,t=1;
fp1=fopen("insp.txt","rb");
if(fp1==NULL)
printf("%s","error.\n");
else
printf("%s","succeed.\n");
frame=1;
indata[0]=0;
i=0;
while(feof(fp1)==0)
{
indata[frame]=fgetc(fp1);
if(isspace(indata[frame]))
{
indata[i]=indata[frame-1];
indata[frame-1]=0;
i++;
while(isspace(indata[frame]))
indata[frame]=fgetc(fp1);
t=1;
}
if(indata[frame]=='-')
{
indata[frame]=fgetc(fp1);
t=-1;
goto L2;
}
indata[frame]=indata[frame]-48;
temp=(int)(t*(fabs(indata[frame-1]*10)+indata[frame]));
goto L1;
L2:indata[frame]=indata[frame]-48;
temp=(int)(t*(fabs(indata[frame-1])*10+indata[frame]));
t=-1;
L1:indata[frame]=temp;
frame++;
}
PitchesDetect(indata,length,Pitch);
putchar('\n');
fcloseall();
/*
for(n=0;n<i;n++)
printf("%d ",indata[n]);
printf("%s","end\n");
printf("%d\n%d",i,frame); */
return(0);
}