From 2e183b1d4429f42d7785a199828f83abe9def68e Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 5 Mar 2018 13:02:30 +0000 Subject: Added dft again --- Project/TestCode/dft.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Project/TestCode/dft.c (limited to 'Project/TestCode/dft.c') diff --git a/Project/TestCode/dft.c b/Project/TestCode/dft.c new file mode 100644 index 0000000..9ffcfbe --- /dev/null +++ b/Project/TestCode/dft.c @@ -0,0 +1,46 @@ +#include +#include + +#define E_PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062 +#define N 8 + +typedef struct { + float r; + float i; +} Complex; + +float x[] = {0, 0, 2, 3, 4, 0, 0, 0}; +Complex X[N]; +float x_out[N]; + +void dft() { + int n, k; + for(k = 0; k < N; ++k) { + X[k].r = 0.f; + X[k].i = 0.f; + for(n = 0; n < N; ++n) { + X[k].r += x[n] * cos(2 * E_PI * k * n / N); + X[k].i -= x[n] * sin(2 * E_PI * k * n / N); + } + } +} + +void idft() { + int n, k; + for(n = 0; n < N; ++n) { + x_out[n] = 0.f; + for(k = 0; k < N; ++k) { + x_out[n] += X[k].r * cos(2 * E_PI * k * n / N) - X[k].i * sin(2 * E_PI * k * n / N); + } + x_out[n] /= N; + } +} + +int main() { + int k; + dft(); + idft(); + for(k = 0; k < N; ++k) { + printf("x[%d] = %.2f, X[%d] = %.2f + %.2fi, x_out[%d] = %.2f\n", k, x[k], k, X[k].r, X[k].i, k, x_out[k]); + } +} \ No newline at end of file -- cgit From c394d751491782dfe485b0929c0e1343674872f4 Mon Sep 17 00:00:00 2001 From: ymherklotz Date: Mon, 5 Mar 2018 13:17:54 +0000 Subject: Modified dft --- Project/TestCode/dft.c | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to 'Project/TestCode/dft.c') diff --git a/Project/TestCode/dft.c b/Project/TestCode/dft.c index 9ffcfbe..d0dca12 100644 --- a/Project/TestCode/dft.c +++ b/Project/TestCode/dft.c @@ -13,26 +13,35 @@ float x[] = {0, 0, 2, 3, 4, 0, 0, 0}; Complex X[N]; float x_out[N]; -void dft() { - int n, k; +void dft(int N, Complex *X) { + int i, n, k; + Complex x[N]; + for(i = 0; i < N; ++i) { + x[i] = X[i]; + } for(k = 0; k < N; ++k) { X[k].r = 0.f; X[k].i = 0.f; for(n = 0; n < N; ++n) { - X[k].r += x[n] * cos(2 * E_PI * k * n / N); - X[k].i -= x[n] * sin(2 * E_PI * k * n / N); + X[k].r += x[n].r * cos(2 * E_PI * k * n / N); + X[k].i -= x[n].r * sin(2 * E_PI * k * n / N); } } } -void idft() { - int n, k; +void idft(int N, Complex *X) { + int i, n, k; + Complex x[N]; + for(i = 0; i < N; ++i) { + x[i] = X[i]; + } for(n = 0; n < N; ++n) { - x_out[n] = 0.f; + X[n].i = 0.f; + X[n].r = 0.f; for(k = 0; k < N; ++k) { - x_out[n] += X[k].r * cos(2 * E_PI * k * n / N) - X[k].i * sin(2 * E_PI * k * n / N); + X[n].r += x[k].r * cos(2 * E_PI * k * n / N) - x[k].i * sin(2 * E_PI * k * n / N); } - x_out[n] /= N; + X[n] /= N; } } -- cgit From 6d1622b38a4153c6fd0de24f57e3654b706e78d0 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Mon, 5 Mar 2018 13:39:46 +0000 Subject: [DFT] Fixed code --- Project/TestCode/dft.c | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) (limited to 'Project/TestCode/dft.c') diff --git a/Project/TestCode/dft.c b/Project/TestCode/dft.c index d0dca12..e82cb3e 100644 --- a/Project/TestCode/dft.c +++ b/Project/TestCode/dft.c @@ -1,8 +1,9 @@ #include #include +#include #define E_PI 3.1415926535897932384626433832795028841971693993751058209749445923078164062 -#define N 8 +#define N_DEF 8 typedef struct { float r; @@ -10,12 +11,10 @@ typedef struct { } Complex; float x[] = {0, 0, 2, 3, 4, 0, 0, 0}; -Complex X[N]; -float x_out[N]; void dft(int N, Complex *X) { int i, n, k; - Complex x[N]; + Complex *x = malloc(N * sizeof(X[0])); for(i = 0; i < N; ++i) { x[i] = X[i]; } @@ -27,11 +26,12 @@ void dft(int N, Complex *X) { X[k].i -= x[n].r * sin(2 * E_PI * k * n / N); } } + free(x); } void idft(int N, Complex *X) { int i, n, k; - Complex x[N]; + Complex *x = malloc(N * sizeof(X[0])); for(i = 0; i < N; ++i) { x[i] = X[i]; } @@ -41,15 +41,24 @@ void idft(int N, Complex *X) { for(k = 0; k < N; ++k) { X[n].r += x[k].r * cos(2 * E_PI * k * n / N) - x[k].i * sin(2 * E_PI * k * n / N); } - X[n] /= N; + X[n].r /= N; } + free(x); } int main() { int k; - dft(); - idft(); - for(k = 0; k < N; ++k) { - printf("x[%d] = %.2f, X[%d] = %.2f + %.2fi, x_out[%d] = %.2f\n", k, x[k], k, X[k].r, X[k].i, k, x_out[k]); + Complex X_C[8]; + for(k = 0; k < N_DEF; ++k) { + X_C[k].r = x[k]; + X_C[k].i = 0; + } + dft(N_DEF, X_C); + for(k = 0; k < N_DEF; ++k) { + printf("x[%d] = %.2f, X[%d] = %.2f + %.2fi\n", k, x[k], k, X_C[k].r, X_C[k].i); } -} \ No newline at end of file + idft(N_DEF, X_C); + for(k = 0; k < N_DEF; ++k) { + printf("x[%d] = %.2f, X[%d] = %.2f + %.2fi\n", k, x[k], k, X_C[k].r, X_C[k].i); + } +} -- cgit