aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorunknown <dm2515@eews303a-004.ic.ac.uk>2018-03-05 13:02:30 +0000
committerunknown <dm2515@eews303a-004.ic.ac.uk>2018-03-05 13:02:30 +0000
commit2e183b1d4429f42d7785a199828f83abe9def68e (patch)
treeea4dfb817643662fdff7941ffc2100812036868e
parentfb6696ea0232887f63a943b11e0f16b811b241b9 (diff)
downloadNoiseSilencer-2e183b1d4429f42d7785a199828f83abe9def68e.tar.gz
NoiseSilencer-2e183b1d4429f42d7785a199828f83abe9def68e.zip
Added dft again
-rw-r--r--Project/TestCode/dft.c46
1 files changed, 46 insertions, 0 deletions
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 <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() {
+ 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