-
Notifications
You must be signed in to change notification settings - Fork 0
/
alias.c
120 lines (113 loc) · 2.98 KB
/
alias.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <stdio.h>
#include <math.h>
# define dt 0.001
# define fm1 10
# define fm2 35
# define fm3 70
# define PI 2*asin(1.0)
#define N 3000
#define N1 30
#define N2 300
#define TS1 0.1
#define TS2 0.01
void dft(float xreal[N], float ximag[N]); //DFT 离散傅里叶正变换
int main()
{
FILE *fp1;
fp1 = fopen("STtest_in_P98.xls", "w");
float S1[N], S2[N]; //定义两个数组,f1为0.1s抽样,f2为0.01s抽样
float fss1[N1], fss2[N2];
float fs1[N], fs2[N];//存储抽样后的fs时域函数值
float fw1[N1], fw2[N2];//存储抽样后的fw频域函数值
float t; t=0.0;
for(int i = 0; i < N; i++) //创建抽样函数
{
t += dt;
if (i%100 == 0)
{S1[i] = 1.0;
//printf("%d\n",S1[i]);
}
else
S1[i] = 0.0;
if(i%10 == 0)
S2[i] = 1.0;
else
S2[i] = 0.0;
}
fprintf(fp1, "t\t fs1\t fw1\n");
t=0.0;
for(int i = 0; i < N; i++) //计算时域数据
{
t += dt;
float f;
f = sin(2*PI*fm1*t)+sin(2*PI*fm2*t)+sin(2*PI*fm3*t);
printf("%f\n", f);
fs1[i] = f * S1[i];
fs2[i] = f * S2[i];
}
int j, k; j=0;k=0;
for(int i = 0; i < N; i++) //抽样后的函数值
{
if (i%100 == 0)
{
fss1[j] = fs1[i];
j++;
}
if(i%10 == 0)
{
fss2[k] = fs2[i];
k++;
}
}
float fs1R[N], fs2R[N], fs1I[N], fs2I[N];//存储抽样后的fw频域函数值
for(int i = 0; i < N1; i++) //时域数据分为实部和虚部
{
fs1R[i] = fss1[i];
fs1I[i] = 0.0;
}
for(int i = 0; i < N2; i++) //时域数据分为实部和虚部
{
fs2R[i] = fss2[i];
fs2I[i] = 0.0;
}
dft(fs1R, fs1I); //离散傅里叶变换
dft(fs2R, fs2I); //离散傅里叶变换
t=0.0;
for(int i = 0; i < N1; i++) //输出fs1序列
{
t+=TS1;
fprintf(fp1, "%10.6f\t %10.6f\t %10.6f\n", t, fss1[i], sqrt(pow(fs1R[i],2)+pow(fs1I[i], 2))); //t输出单位为ms
}
t=0.0;
fprintf(fp1, "\n\nt\t fs2\t fw2\n");
for(int i = 0; i < N2; i++) //输出fs2序列
{
t+= TS2;
fprintf(fp1, "%10.6f\t %10.6f\t %10.6f\n", t, fss2[i], sqrt(pow(fs2R[i],2)+pow(fs2I[i], 2))); //t输出单位为ms
}
fclose(fp1);
return 0;
}
void dft(float xreal[N], float ximag[N]) //DFT 离散傅里叶正变换
//float xreal[N],ximag[N];
{
float Xreal[N], Ximag[N];
float sumreal, sumimag, rad; //定义实部计算加和、虚部计算加和、角度
for(int k = 0; k < N; k++)
{
sumreal = 0.0; sumimag = 0.0;
for(int n=0; n<N; n++)
{
rad = 2*PI/N*n*k;
sumreal += xreal[n]*cos(rad) + ximag[n]*sin(rad);
sumimag+= -xreal[n]*sin(rad) + ximag[n]*cos(rad);
}
Xreal[k] = sumreal;
Ximag[k] = sumimag;
}
for(int k = 0; k < N; k++)
{
xreal[k] = Xreal[k];
ximag[k] = Ximag[k];
}
}