C语言完成窗口算法.docx
- 文档编号:15633428
- 上传时间:2023-07-06
- 格式:DOCX
- 页数:10
- 大小:17.20KB
C语言完成窗口算法.docx
《C语言完成窗口算法.docx》由会员分享,可在线阅读,更多相关《C语言完成窗口算法.docx(10页珍藏版)》请在冰点文库上搜索。
C语言完成窗口算法
C语言完成窗口算法
/*窗类型*/typedefenum{Bartlett=0,BartLettHann,BlackMan,BlackManHarris,Bohman,Chebyshev,FlatTop,Gaussian,Hamming,Hann,Kaiser,Nuttal,Parzen,Rectangular,Taylor,Triangular,Tukey}winType;
/**fileWindowFunction.h*authorVincentCui*e-mailwhcui1987@*version0.3*data31-Oct-2014*brief各种窗函数的C语言实现*/#ifndef_WINDOWFUNCTION_H_#define_WINDOWFUNCTION_H_
#include"GeneralConfig.h"
#defineBESSELI_K_LENGTH10
#defineFLATTOPWIN_A00.215578995#defineFLATTOPWIN_A10.41663158#defineFLATTOPWIN_A20.277263158#defineFLATTOPWIN_A30.083578947#defineFLATTOPWIN_A40.006947368
#defineNUTTALL_A00.3635819#defineNUTTALL_A10.4891775#defineNUTTALL_A20.1365995#defineNUTTALL_A30.0106411
#defineBLACKMANHARRIS_A00.35875#defineBLACKMANHARRIS_A10.48829#defineBLACKMANHARRIS_A20.14128#defineBLACKMANHARRIS_A30.01168dspErrorStatustaylorWin(dspUint_16N,dspUint_16nbar,dspDoublesll,dspDouble**w);dspErrorStatustriangularWin(dspUint_16N,dspDouble**w);dspErrorStatustukeyWin(dspUint_16N,dspDoubler,dspDouble**w);dspErrorStatusbartlettWin(dspUint_16N,dspDouble**w);dspErrorStatusbartLettHannWin(dspUint_16N,dspDouble**w);dspErrorStatusblackManWin(dspUint_16N,dspDouble**w);dspErrorStatusblackManHarrisWin(dspUint_16N,dspDouble**w);dspErrorStatusbohmanWin(dspUint_16N,dspDouble**w);dspErrorStatuschebyshevWin(dspUint_16N,dspDoubler,dspDouble**w);dspErrorStatusflatTopWin(dspUint_16N,dspDouble**w);dspErrorStatusgaussianWin(dspUint_16N,dspDoublealpha,dspDouble**w);dspErrorStatushammingWin(dspUint_16N,dspDouble**w);dspErrorStatushannWin(dspUint_16N,dspDouble**w);dspErrorStatuskaiserWin(dspUint_16N,dspDoublebeta,dspDouble**w);dspErrorStatusnuttalWin(dspUint_16N,dspDouble**w);dspErrorStatusparzenWin(dspUint_16N,dspDouble**w);dspErrorStatusrectangularWin(dspUint_16N,dspDouble**w);#endif
WindowFunction.h
------------------------------------------------------------------/**fileWindowFunction.c*authorVincentCui*e-mailwhcui1987@*version0.3*data31-Oct-2014*brief各种窗函数的C语言实现*/#include"WindowFunction.h"#include"GeneralConfig.h"#include"MathReplenish.h"#include"math.h"#include<stdlib.h>#include<stdio.h>#include<string.h>
/*函数名:
taylorWin*说明:
计算泰勒窗。
泰勒加权函数*输入:
*输出:
*返回:
*调用:
prod()连乘函数*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=taylorWin(99,4,40,&w);注意此处的40是正数表示-40dB*/dspErrorStatustaylorWin(dspUint_16N,dspUint_16nbar,dspDoublesll,dspDouble**w){dspDoubleA;dspDouble*retDspDouble;dspDouble*sf;dspDouble*result;dspDoublealpha,beta,theta;dspUint_16i,j;
/*A=Rcosh(PI,A)=R*/A=(dspDouble)acosh(pow((dspDouble)10.0,(dspDouble)sll/20.0))/PI;A=A*A;/*开出存放系数的空间*/retDspDouble=(dspDouble*)malloc(sizeof(dspDouble)*(nbar-1));if(retDspDouble==NULL)returnDSP_ERROR;sf=retDspDouble;
/*开出存放系数的空间*/retDspDouble=(dspDouble*)malloc(sizeof(dspDouble)*N);if(retDspDouble==NULL)returnDSP_ERROR;result=retDspDouble;
alpha=prod(1,1,(nbar-1));alpha*=alpha;beta=(dspDouble)nbar/sqrt(A+pow((nbar-0.5),2));for(i=1;i<=(nbar-1);i++){*(sf+i-1)=prod(1,1,(nbar-1+i))*prod(1,1,(nbar-1-i));theta=1;for(j=1;j<=(nbar-1);j++){theta*=1-(dspDouble)(i*i)/(beta*beta*(A+(j-0.5)*(j-0.5)));}*(sf+i-1)=alpha*(dspDouble)theta/(*(sf+i-1));}
/*奇数阶*/if((N%2)==1){for(i=0;i<N;i++){alpha=0;for(j=1;j<=(nbar-1);j++){alpha+=(*(sf+j-1))*cos(2*PI*j*(dspDouble)(i-((N-1)/2))/N);}*(result+i)=1+2*alpha;}}/*偶数阶*/else{for(i=0;i<N;i++){alpha=0;for(j=1;j<=(nbar-1);j++){alpha+=(*(sf+j-1))*cos(PI*j*(dspDouble)(2*(i-(N/2))+1)/N);}*(result+i)=1+2*alpha;}}*w=result;free(sf);
returnDSP_SUCESS;
}/**函数名:
triangularWin*说明:
计算三角窗函数*输入:
*输出:
*返回:
*调用:
*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=triangularWin(99,&w);*/dspErrorStatustriangularWin(dspUint_16N,dspDouble**w){dspDouble*ptr;dspUint_16i;
ptr=(dspDouble*)malloc(N*sizeof(dspDouble));if(ptr==NULL)returnDSP_ERROR;/*阶数为奇*/if((N%2)==1){for(i=0;i<((N-1)/2);i++){*(ptr+i)=2*(dspDouble)(i+1)/(N+1);}for(i=((N-1)/2);i<N;i++){*(ptr+i)=2*(dspDouble)(N-i)/(N+1);}}/*阶数为偶*/else{for(i=0;i<(N/2);i++){*(ptr+i)=(i+i+1)*(dspDouble)1/N;}for(i=(N/2);i<N;i++){*(ptr+i)=*(ptr+N-1-i);}}*w=ptr;
returnDSP_SUCESS;}
/**函数名:
tukeyWin*说明:
计算tukey窗函数*输入:
*输出:
*返回:
linSpace()*调用:
*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=tukeyWin(99,0.5,&w);*/dspErrorStatustukeyWin(dspUint_16N,dspDoubler,dspDouble**w){dspErrorStatusretErrorStatus;dspUint_16index;dspDouble*x,*result,*retPtr;dspDoublealpha;
retErrorStatus=linSpace(0,1,N,&x);if(retErrorStatus==DSP_ERROR)returnDSP_ERROR;
result=(dspDouble*)malloc(N*sizeof(dspDouble));if(result==NULL)returnDSP_ERROR;
/*r<=0就是矩形窗*/if(r<=0){retErrorStatus=rectangularWin(N,&retPtr);if(retErrorStatus==DSP_ERROR)returnDSP_ERROR;/*将数据拷出来以后,释放调用的窗函数的空间*/memcpy(result,retPtr,(N*sizeof(dspDouble)));free(retPtr);}/*r>=1就是汉宁窗*/elseif(r>=1){retErrorStatus=hannWin(N,&retPtr);if(retErrorStatus==DSP_ERROR)returnDSP_ERROR;/*将数据拷出来以后,释放调用的窗函数的空间*/memcpy(result,retPtr,(N*sizeof(dspDouble)));free(retPtr);}else{for(index=0;index<N;index++){alpha=*(x+index);if(alpha<(r/2)){*(result+index)=(dspDouble)(1+cos(2*PI*(dspDouble)(alpha-(dspDouble)r/2)/r))/2;}elseif((alpha>=(r/2))&&(alpha<(1-r/2))){*(result+index)=1;}else{*(result+index)=(dspDouble)(1+cos(2*PI*(dspDouble)(alpha-1+(dspDouble)r/2)/r))/2;}}}
free(x);
*w=result;
returnDSP_SUCESS;
}
/**函数名:
bartlettWin*说明:
计算bartlettWin窗函数*输入:
*输出:
*返回:
*调用:
*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=bartlettWin(99,&w);*/dspErrorStatusbartlettWin(dspUint_16N,dspDouble**w){dspDouble*ret;dspUint_16n;
ret=(dspDouble*)malloc(N*sizeof(dspDouble));if(ret==NULL)returnDSP_ERROR;
for(n=0;n<(N-1)/2;n++){*(ret+n)=2*(dspDouble)n/(N-1);}
for(n=(N-1)/2;n<N;n++){*(ret+n)=2-2*(dspDouble)n/((N-1));}
*w=ret;
returnDSP_SUCESS;}
/**函数名:
bartLettHannWin*说明:
计算bartLettHannWin窗函数*输入:
*输出:
*返回:
*调用:
*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=bartLettHannWin(99,&w);*/dspErrorStatusbartLettHannWin(dspUint_16N,dspDouble**w){dspUint_16n;dspDouble*ret;
ret=(dspDouble*)malloc(N*sizeof(dspDouble));if(ret==NULL)returnDSP_ERROR;/*奇*/if((N%2)==1){for(n=0;n<N;n++){*(ret+n)=0.62-0.48*myAbs(((dspDouble)n/(N-1))-0.5)+0.38*cos(2*PI*(((dspDouble)n/(N-1))-0.5));}for(n=0;n<(N-1)/2;n++){*(ret+n)=*(ret+N-1-n);}}/*偶*/else{for(n=0;n<N;n++){*(ret+n)=0.62-0.48*myAbs(((dspDouble)n/(N-1))-0.5)+0.38*cos(2*PI*(((dspDouble)n/(N-1))-0.5));}for(n=0;n<N/2;n++){*(ret+n)=*(ret+N-1-n);}}
*w=ret;
returnDSP_SUCESS;
}
/**函数名:
blackManWin*说明:
计算blackManWin窗函数*输入:
*输出:
*返回:
*调用:
*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=blackManWin(99,&w);*/dspErrorStatusblackManWin(dspUint_16N,dspDouble**w){dspUint_16n;dspDouble*ret;ret=(dspDouble*)malloc(N*sizeof(dspDouble));if(ret==NULL)returnDSP_ERROR;
for(n=0;n<N;n++){*(ret+n)=0.42-0.5*cos(2*PI*(dspDouble)n/(N-1))+0.08*cos(4*PI*(dspDouble)n/(N-1));}
*w=ret;
returnDSP_SUCESS;}
/**函数名:
blackManHarrisWin*说明:
计算blackManHarrisWin窗函数*输入:
*输出:
*返回:
*调用:
*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=blackManHarrisWin(99,&w);*minimum4-termBlackman-harriswindow--FromMatlab*/dspErrorStatusblackManHarrisWin(dspUint_16N,dspDouble**w){dspUint_16n;dspDouble*ret;ret=(dspDouble*)malloc(N*sizeof(dspDouble));if(ret==NULL)returnDSP_ERROR;
for(n=0;n<N;n++){*(ret+n)=BLACKMANHARRIS_A0-BLACKMANHARRIS_A1*cos(2*PI*(dspDouble)n/(N))+\BLACKMANHARRIS_A2*cos(4*PI*(dspDouble)n/(N))-\BLACKMANHARRIS_A3*cos(6*PI*(dspDouble)n/(N));}
*w=ret;
returnDSP_SUCESS;}
/**函数名:
bohmanWin*说明:
计算bohmanWin窗函数*输入:
*输出:
*返回:
*调用:
*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=bohmanWin(99,&w);*/dspErrorStatusbohmanWin(dspUint_16N,dspDouble**w){dspUint_16n;dspDouble*ret;dspDoublex;ret=(dspDouble*)malloc(N*sizeof(dspDouble));if(ret==NULL)returnDSP_ERROR;
for(n=0;n<N;n++){x=-1+n*(dspDouble)2/(N-1);/*取绝对值*/x=x>=0?
x:
(x*(-1));*(ret+n)=(1-x)*cos(PI*x)+(dspDouble)(1/PI)*sin(PI*x);}
*w=ret;
returnDSP_SUCESS;}
/**函数名:
chebyshevWin*说明:
计算chebyshevWin窗函数*输入:
*输出:
*返回:
*调用:
*其它:
用过以后,需要手动释放掉*w的内存空间*调用示例:
ret=chebyshevWin(99,100,&w);*/dspErrorStatuschebyshevWin(dspUint_16N,dspDoubler,dspDouble**w){dspUint_16n,index;dspDouble*ret;dspDoublex,alpha,beta,theta,gama;
ret=(dspDouble*)malloc(N*sizeof(dspDouble));if(ret==NULL)returnDSP_ERROR;/*10^(r/20)*/theta=pow((dspDouble)10,(dspDouble)(myAbs(r)/20));beta=pow(cosh(acosh(theta)/(N-1)),2);alpha=1-(dspDouble)1/beta;
if((N%2)==1){/*计算一半的区间*/for(n=1;n<(N+1)/2;n++){gama=1;for(index=1;index<n;index++){x=index*(dspDouble)(N-1-2*n+index)/((n-index)*(n+1-index));gama=gama*alpha*x+1;}*(ret+n)=(N-1)*alpha*gama;}
theta=*(ret+(N-1)/2);*ret=1;
for(n=0;n<(N+1)/2;n++){*(ret+n)=(dspDouble)(*(ret+n))/theta;}
/*填充另一半*/for(;n<N;n++){*(ret+n)=ret[N-n-1];
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 完成 窗口 算法