"Fossies" - the Fresh Open Source Software Archive 
Member "HTTPing-2.9/fft.c" (29 Oct 2022, 1323 Bytes) of package /linux/www/HTTPing-2.9.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "fft.c" see the
Fossies "Dox" file reference documentation.
1 #include <libintl.h>
2 #include <math.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <fftw3.h>
6
7 #include "error.h"
8
9 double *pin = NULL;
10 fftw_complex *pout = NULL;
11 fftw_plan plan;
12 int sample_rate = 44100;
13
14 void fft_init(int sample_rate_in)
15 {
16 sample_rate = sample_rate_in;
17
18 pin = (double *)malloc(sizeof(double) * sample_rate_in);
19 if (!pin)
20 error_exit(gettext("failed allocating memory for fft (1)"));
21
22 pout = (fftw_complex *)fftw_malloc(sizeof(fftw_complex) * sample_rate_in + 1);
23 if (!pout)
24 error_exit(gettext("failed allocating memory for fft (2)"));
25
26 /* init fftw */
27 plan = fftw_plan_dft_r2c_1d(sample_rate_in, pin, pout, FFTW_ESTIMATE);
28 if (!plan)
29 error_exit(gettext("failed calculating plan for fft"));
30 }
31
32 void fft_free(void)
33 {
34 if (pin)
35 {
36 fftw_free(pout);
37 free(pin);
38 }
39
40 fftw_destroy_plan(plan);
41 }
42
43 void fft_stop(void)
44 {
45 fftw_cleanup();
46 }
47
48 void fft_do(double *in, double *output_mag, double *output_phase)
49 {
50 int loop = 0;
51
52 memcpy(pin, in, sizeof(double) * sample_rate);
53
54 /* calc fft */
55 fftw_execute(plan);
56
57 for(loop=0; loop<(sample_rate / 2) + 1; loop++)
58 {
59 double real = pout[loop][0];
60 double img = pout[loop][1];
61
62 /* magnitude */
63 output_mag[loop] = sqrt(pow(real, 2.0) + pow(img, 2.0));
64
65 /* phase */
66 output_phase[loop] = (real == 0 && img == 0) ? 0 : atan2(real, img);
67 }
68 }