C语言实现数字信号处理算法

  • 格式:pdf
  • 大小:195.13 KB
  • 文档页数:12

下载文档原格式

  / 12
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

for(i=1;i<=256;i+=2){
xxx[i]=1.0; //xxx[1,3,5,7...] is real part
xxx[i+1]=0.0; //xxx[2,4,6,8...] is image part
}
FFT(xxx,128,-1);
*/
#define SWAP(x,y) {double temp=(x);(x)=(y);(y)=temp;} void FFT(double xxx[],int N,int FFTorIFFT)/*FFT 1 IFFT -1*/ {
}
}
3、FFT 变换函数
/* 参数说明: xxx : 需要进行变换的数组; N
调用示例:
: 长度; FFtorIFFT : 进行正变换或反变换的指示参数
如果您在阅读过程中发现疏漏和错误,请您尽快和编者取得联系 network@ustc.edu.cn cxh@ustc.edu.cn
http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛 中国科学技术大学电子工程与信息科学系 多媒体通信实验室 (Copyright 2000)
result : 傅立叶变换的结果(复数序列) 调用示例:
M=100;
DSFT(f,0,N,M,F);
*/
void DSFT(double *f,int N,int M,complex *result)
{int k,n;
double omega,delta_omega;
delta_omega=2*M_PI/M;
1、傅立叶变换数值计算函数
/* 输入参数: f : 需要进行傅立叶变换的数值序列 N : 输入序列的长度 M : 频谱分析的频点数
如果您在阅读过程中发现疏漏和错误,请您尽快和编者取得联系 network@ustc.edu.cn cxh@ustc.edu.cn
http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛 中国科学技术大学电子工程与信息科学系 多媒体通信实验室 (Copyright 2000)
2、定义复数类,填写相应成员函数
//C 中的复数类型调用时可能不是非常好用,可自己定义复数类(ComplexUse.Cpp 文件) class Complex{ public:
Complex(){} Complex( float re, float im ); float r(){return real;}; float i(){return imag;}; float mod(){return sqrt(real*real+imag*imag);}; Complex operator+( Complex &other ); Complex operator-( Complex &other ); Complex operator*( Complex &other ); Complex operator/( Complex &other ); private: float real, imag; };// Operator overloaded using a member function Complex::Complex(float re,float i来自百度文库){
if(max<fabs(f[n])) max=fabs(f[n]);
Left=left+5;Top=top+5; Right=right-5;Bottom=bottom-5; Mid=(Bottom+Top)/2; x_unit_length=(Right-Left)/(N+2); y_unit_length=(Bottom-Mid)/max; int gdriver = DETECT, gmode; initgraph(&gdriver, &gmode, ""); rectangle(left,top,right,bottom); setcolor(YELLOW); setcolor(GREEN); int x; x=Left+x_unit_length/2; n=0; while(n<length) {
*f_end){
int m,n,i,begin,end;
*f_begin=(f1_begin<f2_begin)?f1_begin:f2_begin;
*f_end=*f_begin+f1_end-f1_begin+f2_end-f2_begin;
for(i=0,n=*f_begin;n<=*f_end;n++,i++){
f
: 需要绘制图形的数组
length : f 数组的长度
(2)函数调用示例
//在以(10,5)为左上角坐标,(200,240)为右下角坐标的区域内绘制数组 x 的图形,数组长度为 10
Plot(10,5,200,240,x,10);
(3)BC 图形程序需要“BC 安装目录\bgi\egavga.bgi”文件支持
附录 A2 BC 下的绘图
本部分内容可以在http://202.38.75.33/dsp/C/PlotByBC.cpp获得。
1、通用绘图函数的有关说明
(1)函数输入参数说明
left : 绘图区的左上角横坐标
top
: 绘图区的左上角纵坐标
right : 绘图区的右下角横坐标
bottom : 绘图区的右下角纵坐标
omega=0.0;
for(k=0;k<M;k++)
{result[k]=complex(0.0,0.0);
for(n=0;n<N;n++)
result[k]+=f[n]*exp(complex(0,-1)*omega*n);
omega+=delta_omega;
}
}
2、卷积计算函数
/* 参数说明 f1 : 序列一; f1_begin : 序列一开始下标; f1_end f2 : 序列二; f2_begin : 序列二开始下标; f2_end f : 存放结果序列; begin: 存放结果序列开始下标; end : 存放结果序列结束下标
begin=(n-f2_end>f1_begin)?(n-f2_end):f1_begin;
end=(n-f2_begin<f1_end)?(n-f2_begin):f1_end;
f[i]=0;
for(m=begin;m<=end;m++) f[i]+=f1[m-f1_begin]*f2[n-m-f2_begin];
1、利用 BC 提供的复数支持
//BC 中使用复数类型使用示例(ComplexUse.Cpp 文件) #include <iostream.h> #include <complex.h> int main(void) {
double x = 3.1, y = 4.2; complex z = complex(x,y); cout << "z = "<< z << "\n"; cout << " and imaginary real part = " << imag(z) << "\n"; cout << "z has complex conjugate = " << conj(z) << " \n"; return 0; }
如果您在阅读过程中发现疏漏和错误,请您尽快和编者取得联系 network@ustc.edu.cn cxh@ustc.edu.cn
http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛 中国科学技术大学电子工程与信息科学系 多媒体通信实验室 (Copyright 2000)
调用示例:
: 序列一结束下标; : 序列二结束下标
JuanJi(f1,0,10,f2,0,3,f3,&begin,&end);
*/
void JuanJi(double *f1,int f1_begin,int f1_end,double *f2,int f2_begin,int f2_end,double *f,int *f_begin,int
如果您在阅读过程中发现疏漏和错误,请您尽快和编者取得联系 network@ustc.edu.cn cxh@ustc.edu.cn
http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛 中国科学技术大学电子工程与信息科学系 多媒体通信实验室 (Copyright 2000)
double temp,tempr,tempi; double wtemp,wr,wpr,wpi,wi,theta; int n,mmax,m,q,istep,p; n=N<<1; q=1; for(p=1;p<n;p+=2){
if(q>p){ SWAP(xxx[q],xxx[p]); SWAP(xxx[q+1],xxx[p+1]);
2、通用绘图函数
void Plot(int left,int top,int right,int bottom,double *f,int length) {
int n; int Left,Top,Right,Bottom,Mid; double x_unit_length,y_unit_length,max=-1.0; for(n=begin;n<=end;n++) //begin 和 end 表示数组下标的起始值和结束值
line(x,Mid,x,Mid-f[n]*y_unit_length); x+=x_unit_length; n++; } setcolor(LIGHTGRAY); getch(); closegraph(); }
附录 A3 傅立叶变换有关算法
本部分内容可以在http://202.38.75.33/dsp/C/DFT-FFT.cpp获得。
http://www.elecfans.com 电子发烧友 http://bbs.elecfans.com 电子技术论坛 中国科学技术大学电子工程与信息科学系 多媒体通信实验室 (Copyright 2000)
附录 A C 语言实现数字信号处理算法
附录 A1 BC 下复数类型的实现
本部分内容可以在http://202.38.75.33/dsp/C/ComplexUse.cpp获得。
real=re; imag=im; }; Complex Complex::operator+( Complex &other ){ return Complex( real + other.real, imag + other.imag ); }; Complex Complex::operator-( Complex &other ){ return Complex( real - other.real, imag - other.imag ); }; Complex Complex::operator*( Complex &other ){ float x,y; x=real*other.real-imag*other.imag; y=real*other.imag+imag*other.real; return Complex( x,y ); }; Complex Complex::operator/( Complex &other ){ float x,y,l; l=other.real*other.real+other.imag*other.imag; x=real*other.real+imag*other.imag; y=other.real*imag-real*other.imag; x=x/l; y=y/l; return Complex(x,y); };
} m=n>>1; while(m>=2&&q>m){q-=m;m>>=1;} q+=m; } mmax=2; while(n>mmax){ istep=2*mmax; theta=2.0*M_PI/(FFTorIFFT*mmax); wtemp=sin((double)(0.5*theta)); wpr=-2.0*wtemp*wtemp; wpi=sin((double)theta); for(wr=1.0,wi=0.0,m=1;m<mmax;m+=2){