频谱缺启分析需要用FFT变换,给你提供一下我的代码(c++)
/*==============================================================
快速傅立叶变换 模块
================================================================*/
inline unsigned int NumberOfBitsNeeded( unsigned int p_nSamples )
{
int i
if( p_nSamples <2 )
{
return 0
}
for ( i=0i++ )
{
if( p_nSamples &(1 <<i) ) return i
}
}
inline unsigned int ReverseBits(unsigned int p_nIndex, unsigned int p_nBits)
{
unsigned int i, rev
for(i=rev=0i <p_nBitsi++)
{
rev = (rev <<1) | (p_nIndex &1)
p_nIndex >>= 1
}
return rev
}
inline double Index_to_frequency(unsigned int p_nBaseFreq, unsigned int p_nSamples, unsigned int p_nIndex)
{
if(p_nIndex >= p_nSamples)
{
return 0.0
}
else if(p_nIndex <= p_nSamples/2)
{
return ( (double)p_nIndex / (double)p_nSamples * p_nBaseFreq )
}
else
{
return ( -(double)(p_nSamples-p_nIndex) /乎扮茄岁察 (double)p_nSamples * p_nBaseFreq )
}
}
void FFT (unsigned int p_nSamples, bool p_bInverseTransform, double *p_lpRealIn, double *p_lpImagIn, double *p_lpRealOut, double *p_lpImagOut)
{
if(!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return
unsigned int NumBits
unsigned int i, j, k, n
unsigned int BlockSize, BlockEnd
double angle_numerator = 2.0 * PI
double tr, ti
if( p_bInverseTransform ) angle_numerator = -angle_numerator
NumBits = NumberOfBitsNeeded ( p_nSamples )
for( i=0i <p_nSamplesi++ )
{
j = ReverseBits ( i, NumBits )
p_lpRealOut[j] = p_lpRealIn[i]
p_lpImagOut[j] = (p_lpImagIn == NULL) ? 0.0 : p_lpImagIn[i]
}
BlockEnd = 1
for( BlockSize = 2BlockSize <= p_nSamplesBlockSize <<= 1 )
{
double delta_angle = angle_numerator / (double)BlockSize
double sm2 = sin ( -2 * delta_angle )
double sm1 = sin ( -delta_angle )
double cm2 = cos ( -2 * delta_angle )
double cm1 = cos ( -delta_angle )
double w = 2 * cm1
double ar[3], ai[3]
for( i=0i <p_nSamplesi += BlockSize )
{
ar[2] = cm2
ar[1] = cm1
ai[2] = sm2
ai[1] = sm1
for ( j=i, n=0n <BlockEndj++, n++ )
{
ar[0] = w*ar[1] - ar[2]
ar[2] = ar[1]
ar[1] = ar[0]
ai[0] = w*ai[1] - ai[2]
ai[2] = ai[1]
ai[1] = ai[0]
k = j + BlockEnd
tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k]
ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k]
p_lpRealOut[k] = p_lpRealOut[j] - tr
p_lpImagOut[k] = p_lpImagOut[j] - ti
p_lpRealOut[j] += tr
p_lpImagOut[j] += ti
}
}
BlockEnd = BlockSize
}
if( p_bInverseTransform )
{
double denom = (double)p_nSamples
for ( i=0i <p_nSamplesi++ )
{
p_lpRealOut[i] /= denom
p_lpImagOut[i] /= denom
}
}
}
/*===================================================================
=====================================================================*/
如果你是做软件的,这些应该看得懂,
还有个使用的实例,我做的声音变调函数:
//*************************音调变换*****************************************
void __stdcall Sd72_Pitch(char *in_fn,//输入文件名
char *in_outfn,//输出文件名
long in_s,//开始位置(单位:采样)
long in_l,//处理长度(单位:采样)
double in_v)//变调幅度((1,1.8]升调,[0.6,1)降调)
{
jFile fp,dp
fp=in_fn
dp=in_outfn
dp.toend()
unsigned long filelength=dp.len()+in_l*2
long l,i,j=0,k,m=2
double a=in_v,ca=(abs(a-1)>0.3)?0.85:0.75,w,winpower=(a>1)?1:2,at=(a>1)?0.5:2
l=pow(2,13)
long cl,el,jl=1
cl=(long)(ca*l)
el=l-cl
short *e=(short*)malloc(sizeof(short)*l*m)
short *be=(short*)malloc(sizeof(short)*l*m)
short *f=(short*)malloc(sizeof(short)*l*m)
short *bf=(short*)malloc(sizeof(short)*l*m)
double *rin=(double*)malloc(sizeof(double)*l*m)
double *iin=(double*)malloc(sizeof(double)*l*m)
double *rout=(double*)malloc(sizeof(double)*l*m)
double *iout=(double*)malloc(sizeof(double)*l*m)
fp.pointto(in_s*2-1)
fp.in(0,e,sizeof(short)*l)
memcpy(be,e,sizeof(short)*l)
////
ZeroMemory(rin,sizeof(double)*l*m)
ZeroMemory(iin,sizeof(double)*l*m)
ZeroMemory(rout,sizeof(double)*l*m)
ZeroMemory(iout,sizeof(double)*l*m)
w=2*PI/l
for(i=0i<li++)
rin[i]=(double)e[i]*pow(0.5*(cos(w*(i-l*0.5+0.5))+1),winpower)//余弦窗
C_DoEvents()
FFT(l,false,rin,iin,rout,iout)
C_DoEvents()
ZeroMemory(&rout[(long)(l*0.5)],sizeof(double)*l*0.5)
ZeroMemory(&iout[(long)(l*0.5)],sizeof(double)*l*0.5)
ZeroMemory(rin,sizeof(double)*l*m)
ZeroMemory(iin,sizeof(double)*l*m)
if(a>1 &&a<=1.8)
{
FcZero(rout,l*0.5,rin,(l*0.5)*a)
FcZero(iout,l*0.5,iin,(l*0.5)*a)
}
if(a>=0.6 &&a<1)
{
FcTo(rout,l*0.5,rin,(l*0.5)*a)
FcTo(iout,l*0.5,iin,(l*0.5)*a)
}
ZeroMemory(rout,sizeof(double)*l*m)
ZeroMemory(iout,sizeof(double)*l*m)
C_DoEvents()
FFT(l*m,true,rin,iin,rout,iout)
C_DoEvents()
FcTo(rout,l*m,iout,l)//iout出
for(i=0i<li++)
f[i]=(short)(iout[i]*at)
////
memcpy(bf,f,sizeof(short)*l)
while(jl==1)
{
fp.in(0,&e[cl],sizeof(short)*el)
memcpy(e,&be[el],sizeof(short)*cl)
memcpy(be,e,sizeof(short)*l)
////
ZeroMemory(rin,sizeof(double)*l*m)
ZeroMemory(iin,sizeof(double)*l*m)
ZeroMemory(rout,sizeof(double)*l*m)
ZeroMemory(iout,sizeof(double)*l*m)
w=2*PI/l
for(i=0i<li++)
rin[i]=(double)e[i]*pow(0.5*(cos(w*(i-l*0.5+0.5))+1),winpower)//余弦窗
C_DoEvents()
FFT(l,false,rin,iin,rout,iout)
C_DoEvents()
ZeroMemory(&rout[(long)(l*0.5)],sizeof(double)*l*0.5)
ZeroMemory(&iout[(long)(l*0.5)],sizeof(double)*l*0.5)
ZeroMemory(rin,sizeof(double)*l*m)
ZeroMemory(iin,sizeof(double)*l*m)
if(a>1 &&a<=1.8)
{
FcZero(rout,l*0.5,rin,(l*0.5)*a)
FcZero(iout,l*0.5,iin,(l*0.5)*a)
}
if(a>=0.6 &&a<1)
{
FcTo(rout,l*0.5,rin,(l*0.5)*a)
FcTo(iout,l*0.5,iin,(l*0.5)*a)
}
ZeroMemory(rout,sizeof(double)*l*m)
ZeroMemory(iout,sizeof(double)*l*m)
C_DoEvents()
FFT(l,true,rin,iin,rout,iout)
C_DoEvents()
for(i=0i<li++)
f[i]=(short)(rout[i]*at)
////
for(k=0k<clk++)
{
f[k]=f[k]+bf[k+el]// ^_^ 重叠叠加,使声音变平滑
}
dp.out(0,bf,sizeof(short)*el)
in_l-=el
if(in_l<=0)
jl=0
memcpy(bf,f,sizeof(short)*l)
}
fp.leave(NULL,JFILE_NORMAL)
dp.leave(NULL,filelength)
}
//*********************************************************************************
如果不懂,等我高考完了可以和我探讨探讨,QQ:994373259
%mfccfunction mfc=mfcc(x)
%%%%%%%%%%%%%%%%%%%%%%%%%
%对输入的语音序列x进行mfcc参数提取,返回mfcc参数和一阶差分mfcc参数,mel滤波器的阶数为24
%fft变换长度为256,采样频率为8000HZ,对x 256点分为一帧
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bank=melbankm(24,256,8000,0,0.5,'m')
%归一化mel滤波器组参数
bank=full(bank)
bank=bank/颤滑max(bank((:))
%DCT系数,12*24
for k=1:12
n=0:23
dctcoef(:,k)=cos((2*n+1)*k*pi/(2*24))
end
%归穗银一化倒谱提升窗口
w=1+6*sin(pi*[1:12]./12)
w=w/max(w)
%预加重滤波器
xx=double(x)
xx=filter([1 -0.9375],1,xx)
%语音信号分茄族腊帧
xx=enframe(xx,256,80)
%计算每帧的mfcc参数
for i=1:size(xx,1) %
y=xx(i,:)
s=y'.*hamming(256)
t=abs(fft(s))
t=t.^2%计算能量
c1=dctcoef*log(bank*t(1:129))%dctcoef为dct系数,bank归一化mel滤波器组系数
c2=c1.*w'%w为归一化倒谱提升窗口
m(i,:)=c2'
end
%差分系数
dtm=zeros(size(m))
for i=3:size(m,1)-2
dtm(i,:)=-2*(i-2,:)-m(i-1,1)+2*m(i+2,:)
end
dtm=dtm/3
%合并mfcc参数和一阶差分mfcc参数
mfc=[m dtm]
%去除首尾两帧,因为这两帧的一阶差分参数为0
mfc=mfc(3:size(m,1)-2,:)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)