aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2018-03-05 13:39:46 +0000
committerYann Herklotz <ymherklotz@gmail.com>2018-03-05 13:39:46 +0000
commit6d1622b38a4153c6fd0de24f57e3654b706e78d0 (patch)
tree865e608e584965cae3c164a1897e0896b798be8d
parentc394d751491782dfe485b0929c0e1343674872f4 (diff)
downloadNoiseSilencer-6d1622b38a4153c6fd0de24f57e3654b706e78d0.tar.gz
NoiseSilencer-6d1622b38a4153c6fd0de24f57e3654b706e78d0.zip
[DFT] Fixed code
-rw-r--r--Project/TestCode/dft.c31
1 files changed, 20 insertions, 11 deletions
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 <math.h>
#include <stdio.h>
+#include <stdlib.h>
#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);
+ }
+}