-
Notifications
You must be signed in to change notification settings - Fork 0
/
fft.c
146 lines (127 loc) · 3.87 KB
/
fft.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "fftw3.h"
#include "fft.h"
/*
* Create an array of evenly spaced numbers.
* Input: malloced array `array`, min, max, n
* Output: array `array` filled with values
* Reference: https://gist.github.com/mortenpi/f20a93c8ed3ee7785e65
*/
static void
range (double *array, double min, double max, size_t n)
{
double delta = (max - min) / (double)(n - 1);
size_t i;
for (i = 0; i < n; i++)
{
array[i] = min + i * delta;
}
}
void
fftToFileHalf (double *data, uint64_t dataSamples, double sampleRate, FILE *fptr)
{
fftw_plan fft;
uint64_t i;
double min = 0.0, max = sampleRate;
uint64_t mid = (dataSamples % 2 == 0) ? (dataSamples / 2) : (dataSamples / 2 + 1);
/* allocate memory */
fftw_complex *in = (fftw_complex *)fftw_malloc (sizeof (fftw_complex) * dataSamples);
fftw_complex *out = (fftw_complex *)fftw_malloc (sizeof (fftw_complex) * dataSamples);
double *freq = (double *)malloc (sizeof (double) * dataSamples);
/* prepare input data */
for (i = 0; i < dataSamples; i++)
{
in[i][0] = data[i];
in[i][1] = 0;
}
/* prepare frequency index and data */
range (freq, min, max, dataSamples);
/* Fourier transform and save result to `out` */
fft = fftw_plan_dft_1d (dataSamples, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute (fft);
fftw_destroy_plan (fft);
for (i = 0; i < mid; i++)
{
fprintf (fptr, "%lf %lf %lf\n", freq[i], out[i][0], out[i][1]);
}
/* free allocated memory */
fftw_free (in);
fftw_free (out);
free (freq);
}
void
fftToFile (double *data, uint64_t dataSamples, double sampleRate, FILE *fptr)
{
fftw_plan fft;
uint64_t i;
double min = 0, max = sampleRate;
/* allocate memory */
fftw_complex *in = (fftw_complex *)fftw_malloc (sizeof (fftw_complex) * dataSamples);
fftw_complex *out = (fftw_complex *)fftw_malloc (sizeof (fftw_complex) * dataSamples);
double *freq = (double *)malloc (sizeof (double) * dataSamples);
/* prepare input data */
for (i = 0; i < dataSamples; i++)
{
in[i][0] = data[i];
in[i][1] = 0;
}
/* prepare frequency index and data */
range (freq, min, max, dataSamples);
/* Fourier transform and save result to `out` */
fft = fftw_plan_dft_1d (dataSamples, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute (fft);
fftw_destroy_plan (fft);
for (i = 0; i < dataSamples; i++)
{
fprintf (fptr, "%lf %lf %lf\n", freq[i], out[i][0], out[i][1]);
}
/* free allocated memory */
fftw_free (in);
fftw_free (out);
free (freq);
}
/* Usage: Unit test function of FFT utility.
* Return: None
*/
void
testFFT (double *data, fftw_complex *in, fftw_complex *out,
fftw_complex *ref, uint64_t totalSamples)
{
fftw_plan fft, ifft;
uint64_t i;
/* allocate memory */
in = (fftw_complex *)fftw_malloc (sizeof (fftw_complex) * totalSamples);
out = (fftw_complex *)fftw_malloc (sizeof (fftw_complex) * totalSamples);
ref = (fftw_complex *)fftw_malloc (sizeof (fftw_complex) * totalSamples);
/* prepare input data */
for (i = 0; i < totalSamples; i++)
{
in[i][0] = data[i];
in[i][1] = 0;
}
/* Fourier transform and save result to `out` */
fft = fftw_plan_dft_1d (totalSamples, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
fftw_execute (fft);
fftw_destroy_plan (fft);
/* inverse Fourier transform and save result to `ref` */
printf ("\ninverse transform:\n");
ifft = fftw_plan_dft_1d (totalSamples, out, ref, FFTW_BACKWARD, FFTW_ESTIMATE);
fftw_execute (ifft);
/* normalize */
for (i = 0; i < totalSamples; i++)
{
ref[i][0] *= 1. / totalSamples;
ref[i][1] *= 1. / totalSamples;
}
for (i = 0; i < totalSamples; i++)
{
printf ("recover: %" PRId64 " %+9.5f %+9.5f I v.s. %+9.5f %+9.5f I\n",
i, in[i][0], in[i][1], ref[i][0], ref[i][1]);
}
fftw_destroy_plan (ifft);
/* cleanup plan */
fftw_cleanup ();
}