Instrument Neutral Distributed Interface INDI  1.9.5
fft.c
Go to the documentation of this file.
1 /*
2  * libDSPAU - a digital signal processing library for astronoms usage
3  * Copyright (C) 2017 Ilia Platone <info@iliaplatone.com>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "dsp.h"
20 #include <fftw3.h>
21 
23 {
24  return sqrt (n.real * n.real + n.imaginary * n.imaginary);
25 }
26 
28 {
29  double out = 0;
30  if (n.real != 0) {
31  out = atan (n.imaginary / n.real);
32  }
33  return out;
34 }
35 
37 {
38  int i;
39  double* out = (double*)malloc(sizeof(double) * len);
40  for(i = 0; i < len; i++) {
41  out [i] = (double)dsp_fourier_complex_get_magnitude(in [i]);
42  }
43  return out;
44 }
45 
47 {
48  int i;
49  double* out = (double*)malloc(sizeof(double) * len);
50  for(i = 0; i < len; i++) {
51  out [i] = (double)dsp_fourier_complex_get_phase(in [i]);
52  }
53  return out;
54 }
55 
57 {
58  double* dft = (double*)malloc(sizeof(double) * stream->len);
59  dsp_complex* out = (dsp_complex*)malloc(sizeof(dsp_complex) * stream->len);
60  dsp_buffer_copy(stream->buf, dft, stream->len);
61  int *sizes = (int*)malloc(sizeof(int)*stream->dims);
62  memcpy (sizes, stream->sizes, sizeof(int)*stream->dims);
63  dsp_buffer_reverse(sizes, stream->dims);
64  fftw_plan plan = fftw_plan_dft_r2c(stream->dims, sizes, dft, (fftw_complex*)out, FFTW_ESTIMATE);
65  fftw_execute(plan);
66  fftw_free(plan);
67  fftw_free(dft);
68  return out;
69 }
70 
72 {
73  dsp_complex* dft = (dsp_complex*)malloc(sizeof(dsp_complex) * stream->len);
74  double* out = (double*)malloc(sizeof(double) * stream->len);
75  int x;
76  for (x=0; x<stream->len; x++) {
77  dft[x].real = stream->buf[x];
78  dft[x].imaginary = stream->buf[x];
79  }
80  int *sizes = (int*)malloc(sizeof(int)*stream->dims);
81  memcpy (sizes, stream->sizes, sizeof(int)*stream->dims);
82  dsp_buffer_reverse(sizes, stream->dims);
83  fftw_plan plan = fftw_plan_dft_c2r(stream->dims, sizes, (fftw_complex*)dft, out, FFTW_ESTIMATE);
84  fftw_execute(plan);
85  dsp_buffer_copy(out, stream->buf, stream->len);
86  fftw_free(plan);
87  free(dft);
88  free(out);
89  return stream->buf;
90 }
dsp.h
dsp_complex_t::real
double real
Real part of the complex number.
Definition: dsp.h:136
dsp_fourier_dft
dsp_complex * dsp_fourier_dft(dsp_stream_p stream)
Perform a discrete Fourier Transform of a dsp_stream.
Definition: fft.c:56
dsp_stream_t
Contains a set of informations and data relative to a buffer and how to use it.
Definition: dsp.h:175
dsp_stream_t::len
int len
The buffers length.
Definition: dsp.h:178
dsp_buffer_copy
#define dsp_buffer_copy(in, out, len)
Fill the output buffer with the values of the elements of the input stream by casting them to the out...
Definition: dsp.h:791
dsp_fourier_complex_array_get_phase
double * dsp_fourier_complex_array_get_phase(dsp_complex *in, int len)
Obtain a complex number's array phases.
Definition: fft.c:46
dsp_complex_t
Complex number, used in Fourier Transform functions.
Definition: dsp.h:133
dsp_stream_t::sizes
int * sizes
Sizes of each dimension.
Definition: dsp.h:182
dsp_fourier_idft
dsp_t * dsp_fourier_idft(dsp_stream_p stream)
Perform an inverse discrete Fourier Transform of a dsp_stream.
Definition: fft.c:71
dsp_fourier_complex_get_phase
double dsp_fourier_complex_get_phase(dsp_complex n)
Obtain a complex number's phase.
Definition: fft.c:27
dsp_fourier_complex_get_magnitude
double dsp_fourier_complex_get_magnitude(dsp_complex n)
Obtain a complex number's magnitude.
Definition: fft.c:22
dsp_complex_t::imaginary
double imaginary
Imaginary part of the complex number.
Definition: dsp.h:138
dsp_stream_t::dims
int dims
Number of dimensions of the buffers.
Definition: dsp.h:180
dsp_t
#define dsp_t
Definition: dsp.h:57
dsp_buffer_reverse
#define dsp_buffer_reverse(buf, len)
Reverse the order of the buffer elements.
Definition: dsp.h:745
dsp_fourier_complex_array_get_magnitude
double * dsp_fourier_complex_array_get_magnitude(dsp_complex *in, int len)
Obtain a complex number's array magnitudes.
Definition: fft.c:36
dsp_stream_t::buf
dsp_t * buf
buffer
Definition: dsp.h:184