在时域中采集的正弦波形,利用傅里叶将其转换成频域上的函数

代码如下

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <math.h>             
 4 #define PI 3.1415926    
 5 #define N 72
 6 double Input_Squence[100];    //输入的原始数据序列 
 7 double Ampl[100];       //存储幅值计算结果
 8 double phase[100];
 9 typedef struct                //定义复数结构,下面通过欧拉公式运算
10 {
11     double real, imag;
12 }complex;
13 complex Result_Point[100];
14 
15 void DFT_Calculate_Point(int k)
16 {
17     int n = 0;
18     complex Sum_Point;
19     complex One_Point[N];
20     Sum_Point.real = 0;
21     Sum_Point.imag = 0;
22     for (n = 0; n<N; n++)
23     {
24         One_Point[n].real = cos(2 * PI / N*2 *k * n)*Input_Squence[n];  //复数的实部
25         One_Point[n].imag = -sin(2 * PI / N*2 *k * n)*Input_Squence[n]; //复数的虚部
26 
27         Sum_Point.real += One_Point[n].real;    //对实部求和
28         Sum_Point.imag += One_Point[n].imag;    //对虚部求和        
29     }
30     Result_Point[k].real = Sum_Point.real;
31     Result_Point[k].imag = Sum_Point.imag;
32 }
33 
34 void DFT_Calculate()
35 {
36     int i = 0;
37     for (i = 0; i<N; i++)
38     {
39         DFT_Calculate_Point(i);
40         Ampl[i] = (2 * sqrt(Result_Point[i].real * Result_Point[i].real + Result_Point[i].imag * Result_Point[i].imag)) / N;  //计算幅值
41         if (Result_Point[i].imag ==0)
42             phase[i] = 0;
43         else
44             phase[i] = atan(-Result_Point[i].imag / Result_Point[i].real);
45     }
46 }
47 
48 int main(int argc, char *argv[])
49 {
50     int i = 0;
51     for (i = 0; i<N; i++)//产生输入序列 
52     {
53         Input_Squence[i] = 3.45*sin(10 * i*PI / 180);
54     }
55     DFT_Calculate(); //进行DFT计算 
56     for (i = 0; i<N; i++)
57     {
58         printf("%d\t%lf\t%lf\n", i, Ampl[i], phase[i] * 180 / PI); //输出计算结果
59     }
60     getchar();
61     return 0;
62 }
posted on 2020-01-10 10:11  永夜无昼  阅读(250)  评论(0)    收藏  举报