From 1be3b38c6b8b034449ba9c2048814cc88bf45a9e Mon Sep 17 00:00:00 2001 From: YanXun-Zhou Date: Tue, 8 Mar 2022 12:56:12 +0800 Subject: [PATCH] Finish 5_practice.c Do Discrete Fourier Transform and show the result. --- 5_practice.c | 68 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 49 insertions(+), 19 deletions(-) diff --git a/5_practice.c b/5_practice.c index 70d6651..e194ed0 100644 --- a/5_practice.c +++ b/5_practice.c @@ -1,40 +1,70 @@ #include // for printf function -#include // for memory allocation +#include // for memory allocation #include // for time calculation #include // for sine and cosine functions -int main() { +int main() +{ // Declare all the variables int k, n, N; double *x, *yr, *yi; time_t t; // Input the number N - - + printf("Input the number 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 - - - - - 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 (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("%d ms for discrete Fourier Transform of %d elements\n", t, N); + printf("x = \n"); + printf("[ "); + for (k = 0; k < N; k++) + { + if (k % 10 == 0 && k > 1) + printf("...\n "); + printf(" %.0f ", x[k]); + } + printf(" ]\n"); + + printf("y = 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"); + // free the memory located by x, yr, yi - - - + free(x); + free(yr); + free(yi); + return 100; } -