摘要
在学习数字信号处理算法程序中用vc编写的几个通用算法程序。 关键词 离散卷积 fir 在学习信号处理的过程中,看到书上的大部分算法都是用fortan或者basic实现,于是自己试验着用vc实现了一下。 1、卷积计算void cintervolvedlg::calthenumbyarray() { this->updatedata(true);
ffuncs funcs[2] = {funch1,funch2};
int n = this->m_valuen; double* x = new double[2*(n+1)];
//x(n) double* y = new double[2*(n+1)];
//y(n) double* h = new double[2*(n+1)];
//h(n)
//1.init x(n),h(n),y(n) cbutton* pbtn = (cbutton*) this->getdlgitem(idc_radio1);
int nchoseitem = 0;
//函数选择 if(pbtn->getcheck()) { nchoseitem = 0; } e
lse { nchoseitem = 1; } for(int i= 0;i<2*(n+1);i++) { if(i< n+1) { x[i] = 1; h[i] = funcs[nchoseitem](i); }
else { x[i] = 0; h[i] = 0; } }
//2.y(i)=sum(x(m)*h(i-m)) m=0..i for(i=0;i<2*(n+1);i++) { y[i] = calcy(x,h,i); }
//显示结果 delete[] x; delete[] y; delete[] h;} 1.2 各个子函数实现typedef double (* ffuncs)(int);
//h1(x) doublefunch1(intn) { doublefbase = (double)4/(double)5; double fr = std::pow(fbase, n); return fr; }
//h2(x)doublefunch2(intn) { doublefpi = 3.1415927; return 0.5*sin((double)0.5*n); }
//y(n)//y(n)=sum(x(m)*y(n-m))m=0..n doublecalcy(double x[],double h[],int n) {double yvalue = 0;
for(int m= 0;m<=n;m++) { yvalue += x[m]*h[n-m]; }
return yvalue;} 2、dft与fft实现 程序界面,具体实现见注释及代码: void cfftconversiondlg::onbnclickedbtncal() { this->updatedata(true);
int nn = this->m_numn;
float ff = this->m_numf;
float ft = this->m_numt;
bool bistimesof2 = false;
for(int i= 0;i<100;i++) { if(nn==(2 < < i)) { bistimesof2 = true; break; } }
if(!bistimesof2) { afxmessagebox("n请输入一个以2为底的幂级数!");
this->getdlgitem(idc_edtn)->setfocus();
return; } comp* x = new comp[nn];
//x(n) comp* x = new comp[nn];//x(k) initx(nn,x,ff,ft);
cbutton* pradio = (cbutton*)this->getdlgitem(idc_radiodft);
if(pradio->getcheck()) { dft(nn,x,x); }
else { fft(nn,x,x); }
char buffer[256];
comp source = x[nn-1];
sprintf(buffer,"%f+%fi",source.real(),source.imag());
cwnd* pwnd = this->getdlgitem(idc_edtret);
pwnd->setwindowtext(buffer);
clistctrl* plist=(clistctrl*) this->getdlgitem(idc_list1);
clistoper oper;
oper.filllist(*plist,nn,x,x);
delete[] x;
delete[] x;} 2.2 子函数代码 说明:其中comp为复数类型/*******************************************
name :dft* function
:disperse fuliye transformation* params
:n -- total count of sampling points* x -- input sequence* return
:xn(k)=sum[x(n)*pow(e,j2*pi/n)]
* k,n
:0..n-1
*******************************************
/void dft(int n,comp x[],comp xk[]){ double c = (2*pi)/n;
comp t(0,0),ret(0,0);
for(int k=0;k < n;k++)
{ ret = comp(0,0);
for(int i=0;i< n;i++) { t = comp(cos(c*k*i),-sin(c*k*i));
ret += x[i]*t; } xk[k] = ret; } }/
*******************************************
name
:fft* function
:fast fuliye transformation* params
:n -- total count of sampling points* x -- input sequence* return
:xn(k)=sum[x(n)*pow(e,j2*pi/n)] * k,n
:0..n-1
*******************************************
/void fft(int n,comp x[],comp xk[]){ int j=0; comp u=0,w=0;
comp* a = xk;
//adjust sequence for(int i=0;i< n;i++) { if(i==0) { a[0] = x[0]; }
else { j=getinverse(n,j);
a[i] = x[j]; } }
//确定级别数
for(int m=0;m< n;m++) { if((1<< m)==n) break; }
for(int l=1;l<=m;l++)//1-m级依次确定 { int le = (int)pow(2,l);
//间隔 int le1 = le/2;
//w级数,如w0,w1,w2... w=comp(cos(pi/le1),-sin(pi/le1));
u=comp(1,0); for(j=0;j< le1;j++)
// { i=j; while(i< n) { int ip = i+le1; comp t=a[ip]*u;
a[ip]=a[i]-t;//蝶形计算 a[i]=a[i]+t; i+=le; } u=u*w;
//不同的w次幂 } }}void initx(int n,comp x[],float f,float t){
for(int i=0;i< n;i++) { x[i] = comp(cos(2*pi*f*t*i),0);
}} 3.2 子函数代码实现/*********************************************************************
name :
funchd* function:
hd()--required frequency response function
***********************************************************************
/comp funchd(double lowlimit,double upperlimit,comp x){
if(x.real()>upperlimitx.real() < lowlimit) return 0;
else return 1;
}void fir(double lowlimit,double upperlimit,int n,comp hn[])
{ int m = 2*n;
for(int i=0;i < n;i++) { hn[i] = comp(0,0);
for(int k=0;k < m;k++) { comp c = comp(cos(2*pi*i*k/(double)m),sin(2*pi*i*k/(double)m));
hn[i] += c*funchd(lowlimit,upperlimit,comp(cos(2*pi*k/(double)m),sin(2*pi*k/(double)m)));
} hn[i] = hn[i]*comp(1/(double)m,0);
}} 4、结束语 基本算法参考《数字信号处理基础及试验》--王树勋主编。虽然现在dsp算法都有很好c语言实现。但是能够通过自己动手编写代码加深对基础知识的掌握,对自己进行数据采集器件的控制还是有很多益处的。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器