位置:51电子网 » 技术资料 » EDA/PLD

vc中几个数字信号处理算法程序

发布时间:2008/8/23 0:00:00 访问次数:450

  摘要:在学习数字信号处理算法程序中用vc编写的几个通用算法程序。

  在学习信号处理的过程中,看到书上的大部分算法都是用fortan或者basic实现,于是自己试验着用vc实现了一下。

1、卷积计算

  离散卷积公式的算法实现


  图1 卷积计算界面

1.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实现

程序界面,具体实现见注释及代码:


图2 dft与fft实现界面

2.1 主程序代码

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

  摘要:在学习数字信号处理算法程序中用vc编写的几个通用算法程序。

  在学习信号处理的过程中,看到书上的大部分算法都是用fortan或者basic实现,于是自己试验着用vc实现了一下。

1、卷积计算

  离散卷积公式的算法实现


  图1 卷积计算界面

1.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实现

程序界面,具体实现见注释及代码:


图2 dft与fft实现界面

2.1 主程序代码

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

相关IC型号

热门点击

 

推荐技术资料

声道前级设计特点
    与通常的Hi-Fi前级不同,EP9307-CRZ这台分... [详细]
版权所有:51dzw.COM
深圳服务热线:13751165337  13692101218
粤ICP备09112631号-6(miitbeian.gov.cn)
公网安备44030402000607
深圳市碧威特网络技术有限公司
付款方式


 复制成功!