forked from ycshu/110_Fast_Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_practice.c
More file actions
69 lines (57 loc) · 1.39 KB
/
5_practice.c
File metadata and controls
69 lines (57 loc) · 1.39 KB
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
#include <stdio.h> // for printf function
#include <stdlib.h> // for memory allocation
#include <time.h> // for time calculation
#include <math.h> // for sine and cosine functions
int main() {
// Declare all the variables
int k, n, N;
double *x, *yr, *yi;
time_t t;
// Input the number N
printf("Please input a number N\n");
scanf("%d",&N);
// Locate the memory for x, yr, yi;
x = (double *)malloc(N * sizeof(double));
yr = (double *)malloc(N * sizeof(double));
yi = (double *)malloc(N * sizeof(double));
// Initial setting for x, for example, x[k] = k
for (k=0;k<N;k++){
x[k] = k;
yr[k] = 0;
yi[k] = 0;
}
t = clock();
// yr[n]+i*yi[n] = sum(exp(-i 2 Pi k n / N)*x[k], k=0..N-1), n=0..N-1
for ( n = 0; n < N; n++){
for ( k = 0; k < N; k++){
yr[n] += cos(2 * M_PI * k * n / N ) * x[k];
yi[n] -= sin(2 * M_PI * k * n / N ) * x[k];
}
}
// output the results
t = clock() - t;
printf("Result \n");
printf("x = (");
for (k = 0; k < N; k++)
{
if (k % 10 == 0 && k > 1)
printf("\n ");
printf(" %.0f ", x[k]);
}
printf(" )\n");
printf("F(x) = \n");
printf("( ");
for (n = 0; n < N; n++)
{
if (n % 5 == 0 && n > 1)
printf("\n ");
printf(" %.2f %+.2fi ", yr[n], yi[n]);
}
printf(" )\n");
printf("%d ms for discrete Fourier Transform of %d elements\n", t, N);
// free the memory located by x, yr, yi
free(x);
free(yr);
free(yi);
return 100;
}