aboutsummaryrefslogtreecommitdiff
path: root/fft.c
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2011-03-12 01:53:00 +0000
committerDimitri Sokolyuk <demon@dim13.org>2011-03-12 01:53:00 +0000
commitfce59d949c0a4767009a9db94f0af0903ff31be1 (patch)
treebfcd4b47fa07cc29e6256f3fbda802901215ec0c /fft.c
sio spectrogram
Diffstat (limited to 'fft.c')
-rw-r--r--fft.c58
1 files changed, 58 insertions, 0 deletions
diff --git a/fft.c b/fft.c
new file mode 100644
index 0000000..5372b89
--- /dev/null
+++ b/fft.c
@@ -0,0 +1,58 @@
+/* $Id$ */
+/*
+ * Copyright (c) 2010 Dimitri Sokolyuk <demon@dim13.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <fftw3.h>
+
+struct fft {
+ fftw_plan plan;
+ double *in;
+ double *out;
+ int n;
+};
+
+struct fft *
+init_fft(int n)
+{
+ struct fft *p;
+
+ p = malloc(sizeof(struct fft));
+ p->in = fftw_malloc(n * sizeof(double));
+ p->out = fftw_malloc(n * sizeof(double));
+ p->plan = fftw_plan_r2r_1d(n, p->in, p->out, FFTW_R2HC, FFTW_MEASURE);
+ p->n = n;
+
+ return p;
+}
+
+int
+dofft(struct fft *p, double *data)
+{
+ int i, n;
+
+ memset(p->out, 0, p->n * sizeof(double));
+ memcpy(p->in, data, p->n * sizeof(double));
+ fftw_execute(p->plan);
+
+ n = p->n / 2;
+ for (i = 1; i < n; i++)
+ data[i - 1] = sqrt(i * (p->out[i] * p->out[i] + p->out[p->n - i] * p->out[p->n - i]));
+
+ return 0;
+}