diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2018-03-05 11:13:04 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2018-03-05 11:13:04 +0000 |
commit | b864b45734e8ba68eb3c7b0a60a4a5b98e6b4571 (patch) | |
tree | fae1740430a9099b13426673e6f7a82f5065846e /Project/RTDSP | |
parent | 8cdda67f13ccf90e60033a630dcd8f5b4079d7b9 (diff) | |
download | NoiseSilencer-b864b45734e8ba68eb3c7b0a60a4a5b98e6b4571.tar.gz NoiseSilencer-b864b45734e8ba68eb3c7b0a60a4a5b98e6b4571.zip |
Adding dft
Diffstat (limited to 'Project/RTDSP')
-rw-r--r-- | Project/RTDSP/dft.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/Project/RTDSP/dft.c b/Project/RTDSP/dft.c new file mode 100644 index 0000000..4d97085 --- /dev/null +++ b/Project/RTDSP/dft.c @@ -0,0 +1,43 @@ +#include <math.h> +#include <stdio.h> + +#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() { + for(int k = 0; k < N; ++k) { + X[k].r = 0.f; + X[k].i = 0.f; + for(int 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() { + for(int n = 0; n < N; ++n) { + x_out[n] = 0.f; + for(int 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() { + dft(); + idft(); + for(int 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]); + } +} |