libdspl-2.0
Digital Signal Processing Algorithm Library
|
Data Structures | |
struct | fft_t |
Fast Fourier Transform Object Data Structure. More... | |
Functions | |
int DSPL_API | dft (double *x, int n, complex_t *y) |
Discrete Fourier transform of a real signal. More... | |
int DSPL_API | dft_cmplx (complex_t *x, int n, complex_t *y) |
Discrete Fourier transform of a complex signal. More... | |
int DSPL_API | fft (double *x, int n, fft_t *pfft, complex_t *y) |
Fast Fourier transform for the real vector. More... | |
int DSPL_API | fft_cmplx (complex_t *x, int n, fft_t *pfft, complex_t *y) |
Fast Fourier transform for the complex vector. More... | |
int DSPL_API | fft_create (fft_t *pfft, int n) |
Function creates and fill fft_t structure. More... | |
void DSPL_API | fft_free (fft_t *pfft) |
Free fft_t structure. More... | |
int DSPL_API | fft_shift (double *x, int n, double *y) |
Perform a shift of the vector x , for use with the fft and ifft functions, in order to move the frequency 0 to the center of the vector y . More... | |
int DSPL_API | fourier_series_dec (double *t, double *s, int nt, double period, int nw, double *w, complex_t *y) |
Fourier series coefficient calculation for periodic signal. More... | |
int DSPL_API | fourier_series_rec (double *w, complex_t *s, int nw, double *t, int nt, complex_t *y) |
Time signal reconstruction from Fourier series coefficients. More... | |
int DSPL_API | goertzel (double *x, int n, int *ind, int k, complex_t *y) |
Goertzel algorithm individual DFT samples calculation for the real input vector x . More... | |
int DSPL_API | goertzel_cmplx (complex_t *x, int n, int *ind, int k, complex_t *y) |
Goertzel algorithm individual DFT samples calculation for the complex input vector x . More... | |
int DSPL_API | idft_cmplx (complex_t *x, int n, complex_t *y) |
Inverse discrete Fourier transform of the complex spectrum. More... | |
int DSPL_API | ifft_cmplx (complex_t *x, int n, fft_t *pfft, complex_t *y) |
Inverse fast Fourier transform. More... | |
Detailed Description
Function Documentation
◆ dft()
int dft | ( | double * | x, |
int | n, | ||
complex_t * | y | ||
) |
Discrete Fourier transform of a real signal.
The function calculates the \( n \) -point discrete Fourier transform real signal \( x (m) \), \( m = 0 \ldots n-1 \).
\[ Y (k) = \sum_ {m = 0} ^ {n-1} x (m) \exp \left (-j \frac {2 \pi} {n} m k \right), \]
where \( k = 0 \ldots n-1 \).
- Parameters
-
[in] x Pointer to the vector of the real input signal \( x (m) \), \( m = 0 \ldots n-1 \).
The size of the vector is[n x 1]
.
[in] n The size of the DFT \( n \) (the size of the vectors of the input signal and the result of the DFT).
[out] y Pointer to the complex vector of the DFT result \( Y (k) \), \( k = 0 \ldots n-1 \). The size of the vector is [n x 1]
.
Memory must be allocated.
- Returns
RES_OK
if the DFT is calculated successfully.
Otherwise, error code.
An example of using the dft
function:
The result of the program:
y [0] = 120.000 0.000 y [1] = -8.000 40.219 y [2] = -8.000 19.314 y [3] = -8.000 11.973 y [4] = -8.000 8.000 y [5] = -8.000 5.345 y [6] = -8.000 3.314 y [7] = -8.000 1.591 y [8] = -8.000 0.000 y [9] = -8.000 -1.591 y [10] = -8.000 -3.314 y [11] = -8.000 -5.345 y [12] = -8.000 -8.000 y [13] = -8.000 -11.973 y [14] = -8.000 -19.314 y [15] = -8.000 -40.219
- Note
- This function performs the DFT calculation using the naive method and requires \( n ^ 2 \) complex multiplications.
To increase the calculation speed, it is recommended to use fast Fourier transform algorithms.
◆ dft_cmplx()
Discrete Fourier transform of a complex signal.
The function calculates the \( n \) -point discrete Fourier transform complex signal \( x (m) \), \( m = 0 \ldots n-1 \).
\[ Y (k) = \sum_ {m = 0} ^ {n-1} x (m) \exp \left (-j \frac {2 \pi} {n} m k \right), \]
where \( k = 0 \ldots n-1 \).
- Parameters
-
[in] x Pointer to a vector of complex input signal \( x (m) \), \( m = 0 \ldots n-1 \).
The size of the vector is[n x 1]
.
[in] n The size of the DFT \( n \) (the size of the vectors of the input signal and the result of the DFT).
[out] y Integrated Vector Pointer DFT result \( Y (k) \), \( k = 0 \ldots n-1 \).
The size of the vector is[n x 1]
.
Memory must be allocated.
- Returns
RES_OK
if the DFT is calculated successfully.
Otherwise, error code.
An example of using the dft_cmplx
function:
The result of the program:
y [0] = 120.000 0.000 y [1] = -8.000 40.219 y [2] = -8.000 19.314 y [3] = -8.000 11.973 y [4] = -8.000 8.000 y [5] = -8.000 5.345 y [6] = -8.000 3.314 y [7] = -8.000 1.591 y [8] = -8.000 0.000 y [9] = -8.000 -1.591 y [10] = -8.000 -3.314 y [11] = -8.000 -5.345 y [12] = -8.000 -8.000 y [13] = -8.000 -11.973 y [14] = -8.000 -19.314 y [15] = -8.000 -40.219
- Note
- This function performs the calculation of the DFT by the naive method and requires \( n ^ 2 \) complex multiplications.
To increase the calculation speed, it is recommended use fast Fourier transform algorithms.
Definition at line 163 of file dft_cmplx.c.
◆ fft()
Fast Fourier transform for the real vector.
Function calculated \( n \)-points FFT for the real vector \( x(m) \), \( m = 0 \ldots n-1 \).
\[ Y(k) = \sum_{m = 0}^{n-1} x(m) \exp \left( -j \frac{2\pi}{n} m k \right), \]
here \( k = 0 \ldots n-1 \).
- Parameters
-
[in] x Pointer to the input real vector \(x(m)\), \( m = 0 \ldots n-1 \).
Vector size is[n x 1]
.
[in] n FFT size \(n\).
FFT size can be composite: \(n = n_0 \times n_1 \times n_2 \times \ldots \times n_p \times m\), here \(n_i = 2,3,5,7\), а \(m \) – simple number less than 46340 (see fft_create function).
[in] pfft Pointer to the fft_t
object.
This pointer cannot beNULL
.
Structure fft_t should be previously once filled with the fft_create function, and the memory should be cleared before exiting by the fft_free function.
[out] y Pointer to the FFT result complex vector \(Y(k)\), \( k = 0 \ldots n-1 \).
Vector size is[n x 1]
.
Memory must be allocated.
- Returns
RES_OK
if FFT is calculated successfully.
Else code error.
Example:
Result:
y[ 0] = 91.000 0.000 y[ 1] = -7.000 30.669 y[ 2] = -7.000 14.536 y[ 3] = -7.000 8.778 y[ 4] = -7.000 5.582 y[ 5] = -7.000 3.371 y[ 6] = -7.000 1.598 y[ 7] = -7.000 0.000 y[ 8] = -7.000 -1.598 y[ 9] = -7.000 -3.371 y[10] = -7.000 -5.582 y[11] = -7.000 -8.778 y[12] = -7.000 -14.536 y[13] = -7.000 -30.669
Definition at line 173 of file fft.c.
Referenced by xcorr(), and xcorr_cmplx().
◆ fft_cmplx()
Fast Fourier transform for the complex vector.
Function calculated \( n \)-points FFT for the complex vector \( x(m) \), \( m = 0 \ldots n-1 \).
\[ Y(k) = \sum_{m = 0}^{n-1} x(m) \exp \left( -j \frac{2\pi}{n} m k \right), \]
here \( k = 0 \ldots n-1 \).
- Parameters
-
[in] x Pointer to the input complex vector \(x(m)\), \( m = 0 \ldots n-1 \).
Vector size is[n x 1]
.
[in] n FFT size \(n\).
FFT size can be composite: \(n = n_0 \times n_1 \times n_2 \times \ldots \times n_p \times m\), here \(n_i = 2,3,5,7\), а \(m \) – simple number less than 46340 (see fft_create function).
[in] pfft Pointer to the fft_t
object.
This pointer cannot beNULL
.
Structure fft_t should be previously once filled with the fft_create function, and the memory should be cleared before exiting by the fft_free function.
[out] y Pointer to the FFT result complex vector \(Y(k)\), \( k = 0 \ldots n-1 \).
Vector size is[n x 1]
.
Memory must be allocated.
- Returns
RES_OK
if FFT is calculated successfully.
Else code error.
Example:
Result:
y[ 0] = -0.517 0.686 y[ 1] = -0.943 0.879 y[ 2] = -2.299 1.492 y[ 3] = 16.078 -6.820 y[ 4] = 2.040 -0.470 y[ 5] = 1.130 -0.059 y[ 6] = 0.786 0.097 y[ 7] = 0.596 0.183 y[ 8] = 0.470 0.240 y[ 9] = 0.375 0.283 y[10] = 0.297 0.318 y[11] = 0.227 0.350 y[12] = 0.161 0.380 y[13] = 0.094 0.410 y[14] = 0.023 0.442 y[15] = -0.059 0.479 y[16] = -0.161 0.525 y[17] = -0.300 0.588
Definition at line 179 of file fft_cmplx.c.
Referenced by conv_fft_cmplx().
◆ fft_create()
int fft_create | ( | fft_t * | pfft, |
int | n | ||
) |
Function creates and fill fft_t
structure.
The function allocates memory and calculates twiddle factors of the n
-point FFT for the structurefft_t
.
- Parameters
-
[in,out] pfft Pointer to the fft_t
object.
Pointer cannot beNULL
.
[in] n FFT size \(n\).
FFT size can be composite \(n = n_0 \times n_1 \times n_2 \ldots \times n_p \times m\), here \(n_i = 2,3,5,7\), and \(m \) – arbitrary prime factor not exceeding 46340.
Thus, the FFT algorithm supports arbitrary integer lengths. degrees of numbers 2,3,5,7, as well as their various combinations.
For example, with \( n = 725760 \) the structure will be successfully filled, because \( 725760 = 2 \cdot 3 \cdot 4 \cdot 5 \cdot 6 \cdot 7 \cdot 9 \cdot 16 \).
If \( n = 172804 = 43201 \cdot 4 \) then the structure will also be successfully filled, because the simple factor in \( n \) does not exceed 46340.
For size \( n = 13 \cdot 17 \cdot 23 \cdot 13 = 66079 \) the function will return an error since 66079 is greater than 46340 and is not the result of the product of numbers 2,3,5,7.
- Returns
RES_OK
if FFT structure is created and filled successfully.
Else code error.
- Note
- Some compilers do not nullify its contents when creating a structure. Therefore, it is recommended to reset the structure after its declaration: fft_t pfft = {0}; // fill and fields of fft_t as zerosint n = 64; // FFT sizeint err;// Create fft_t object for 64-points FFTerr = fft_create(&pfft, n);// ...................................// Clear fft_t structurefft_free(&pfft);
Before exiting the program, the memory allocated in the structure need to clear by fft_free function.
- Note
- The "magic number" 46340 because \(\sqrt{2^{31}} = 46340.95\).
Definition at line 161 of file fft_create.c.
Referenced by fft(), fft_cmplx(), and ifft_cmplx().
◆ fft_free()
void fft_free | ( | fft_t * | pfft | ) |
Free fft_t
structure.
The function clears the intermediate data memory and vectors of FFT twiddle factors of the structure fft_t
.
- Parameters
-
[in] pfft Pointer to the fft_t
object.
Definition at line 63 of file fft_free.c.
Referenced by xcorr(), and xcorr_cmplx().
◆ fft_shift()
int fft_shift | ( | double * | x, |
int | n, | ||
double * | y | ||
) |
Perform a shift of the vector x
, for use with the fft
and ifft
functions, in order to move the frequency 0 to the center of the vector y
.
- Parameters
-
[in] x Pointer to the input vector (FFT or IFFT result).
Vector size is[n x 1]
.
[in] n Input and output vector size.
[out] y Pointer to the output vector with frequency 0 in the center.
Vector size is[n x 1]
.
Memory must be allocated.
- Returns
RES_OK
if function is calculated successfully.
Else code error.
Definition at line 95 of file fft_shift.c.
◆ fourier_series_dec()
int fourier_series_dec | ( | double * | t, |
double * | s, | ||
int | nt, | ||
double | period, | ||
int | nw, | ||
double * | w, | ||
complex_t * | y | ||
) |
Fourier series coefficient calculation for periodic signal.
- Parameters
-
[in] t Pointer to the time vector.
Vector size is[nt x 1]
.
[in] s Pointer to the signal corresponds to time t
.
Vector size is[nt x 1]
.
[in] nt Size of time and signal vectors.
This value must be positive.
[in] period Signal time period.
[in] nw Number of Fourie series coefficients.
[out] w Pointer to the frequency vector (rad/s).
Vector size is[nw x 1]
.
Memory must be allocated.
[out] y Pointer to the complex Fourier series coefficients vector.
Vector size is[nw x 1]
.
Memory must be allocated.
- Returns
RES_OK
if function is calculated successfully.
Else code error.
- Note
- Numerical integration is used for Fourier series coefficients calculation. This function is not effective. To increase the speed of calculation of the signal spectrum it is more expedient to use fast Fourier transform algorithms.
Definition at line 152 of file fourier_series_dec.c.
◆ fourier_series_rec()
Time signal reconstruction from Fourier series coefficients.
Function reconstructs the time signal:
\[ s(t) = \sum\limits_{n = 0}^{n_{\omega}-1} S(\omega_n) \exp(j\omega_n t) \]
- Parameters
-
[in] w Pointer to the Fourier series spectrum frequency vector \(\omega_n\).
Vector size is[nw x 1]
.
[in] s Pointer to the Fourier series coefficients vector \(S(\omega_n)\).
Vector size is[nw x 1]
.
[in] nw Number of Fourier series coefficients.
This value must be positive.
[in] t Pointer to the reconstructed signal time vector.
Vector size is[nt x 1]
.
[in] nt Size of time vector and reconstructed signal vector .
[out] y Pointer to the reconstructed signal vector.
Vector size is[nt x 1]
.
Memory must be allocated.
- Returns
RES_OK
if function is calculated successfully.
Else code error.
- Note
- The output reconstructed signal is generally complex. However, subject to the symmetry properties of the vectors
w
ands
with respect to zero frequency we get the imaginary part of the vectory
at the EPS level. The negligible imaginary part in this case can be ignored.
Definition at line 158 of file fourier_series_rec.c.
◆ goertzel()
int goertzel | ( | double * | x, |
int | n, | ||
int * | ind, | ||
int | k, | ||
complex_t * | y | ||
) |
Goertzel algorithm individual DFT samples calculation for the real input vector x
.
Goertzel algorithm calculates k
samples of n
-point DFT, according to ind
indexes vector.
- Parameters
-
[in] x Pointer to the real input vector x
Vector size is[n x 1]
.
[in] n Size of vector x
.
[in] ind Pointer to the DFT samples indexes which need to calculate by Goertzel algorithm.
Vector size is[k x 1]
.
[in] k Size of vector ind
.
[out] y Pointer to the DFT samples vector corresponds to indexes ind
.
Vector size is[k x 1]
.
Memory must be allocated.
- Returns
RES_OK
if function is calculated successfully.
Else code error.
- Note
- Goertzel's algorithm is effective when it is necessary to calculate several DFT samples of a signal of long duration.
However, the sizek
of the vector of indicesind
can be arbitrary, including more than the length of the signaln
. In this case, some DFT samples will be repeated, but this will not entail a runtime error.
The values of the indices of the DFT spectral samplesind
can also be arbitrary integers, including negative ones. In this case, the DFT samples will be calculated. with indices modulon
.
Definition at line 125 of file goertzel.c.
◆ goertzel_cmplx()
Goertzel algorithm individual DFT samples calculation for the complex input vector x
.
Goertzel algorithm calculates k
samples of n
-point DFT, according to ind
indexes vector.
- Parameters
-
[in] x Pointer to the complex input vector x
Vector size is[n x 1]
.
[in] n Size of vector x
.
[in] ind Pointer to the DFT samples indexes which need to calculate by Goertzel algorithm.
Vector size is[k x 1]
.
[in] k Size of vector ind
.
[out] y Pointer to the DFT samples vector corresponds to indexes ind
.
Vector size is[k x 1]
.
Memory must be allocated.
- Returns
RES_OK
if function is calculated successfully.
Else code error.
- Note
- Goertzel's algorithm is effective when it is necessary to calculate several DFT samples of a signal of long duration.
However, the sizek
of the vector of indicesind
can be arbitrary, including more than the length of the signaln
. In this case, some DFT samples will be repeated, but this will not entail a runtime error.
The values of the indices of the DFT spectral samplesind
can also be arbitrary integers, including negative ones. In this case, the DFT samples will be calculated. with indices modulon
.
Definition at line 127 of file goertzel_cmplx.c.
◆ idft_cmplx()
Inverse discrete Fourier transform of the complex spectrum.
The function calculates the \( n \) -point inverse discrete transform Fourier complex spectrum \( x (m) \), \( m = 0 \ldots n-1 \).
\[ y (k) = \sum_ {m = 0} ^ {n-1} x (m) \exp \left (j \frac {2 \pi} {n} m k \right), \]
where \( k = 0 \ldots n-1 \).
- Parameters
-
[in] x Pointer to the vector of the input complex signal spectrum \( x (m) \), \( m = 0 \ldots n-1 \).
The size of the vector is[n x 1]
.
[in] n The size of the ODPF \( n \) (the size of the vectors of the input spectrum and the result of the ODPF).
[out] y Pointer to the complex vector of the ODPF result \( y (k) \), \( k = 0 \ldots n-1 \). The size of the vector is [n x 1]
.
Memory must be allocated.
- Returns
RES_OK
if the ODPF is calculated successfully.
Otherwise, error code.
An example of using the dft_cmplx
function:
The result of the program:
x [0] = 0.000 + 0.000j, z [0] = 0.000 -0.000 x [1] = 1.000 + 0.000j, z [1] = 1.000 -0.000 x [2] = 2.000 + 0.000j, z [2] = 2.000 -0.000 x [3] = 3.000 + 0.000j, z [3] = 3.000 -0.000 x [4] = 4.000 + 0.000j, z [4] = 4.000 -0.000 x [5] = 5.000 + 0.000j, z [5] = 5.000 -0.000 x [6] = 6.000 + 0.000j, z [6] = 6.000 -0.000 x [7] = 7.000 + 0.000j, z [7] = 7.000 -0.000 x [8] = 8.000 + 0.000j, z [8] = 8.000 -0.000 x [9] = 9.000 + 0.000j, z [9] = 9.000 -0.000 x [10] = 10.000 + 0.000j, z [10] = 10.000 -0.000 x [11] = 11.000 + 0.000j, z [11] = 11.000 +0.000 x [12] = 12.000 + 0.000j, z [12] = 12.000 +0.000 x [13] = 13.000 + 0.000j, z [13] = 13.000 +0.000 x [14] = 14.000 + 0.000j, z [14] = 14.000 +0.000 x [15] = 15.000 + 0.000j, z [15] = 15.000 -0.000
- Note
- This function performs the calculation of the DFT using the naive method. and requires \( n ^ 2 \) complex multiplications.
To increase the calculation speed, it is recommended use fast Fourier transform algorithms.
Definition at line 163 of file idft_cmplx.c.
◆ ifft_cmplx()
Inverse fast Fourier transform.
Function calculates \( n \)-point IFFT of complex data \( x(m) \), \( m = 0 \ldots n-1 \).
\[ Y(k) = \frac{1}{N} \sum_{m = 0}^{n-1} x(m) \exp \left( j \frac{2\pi}{n} m k \right), \]
here \( k = 0 \ldots n-1 \).
- Parameters
-
[in] x Pointer to the input vector \(x(m)\), \( m = 0 \ldots n-1 \).
Vector size is[n x 1]
.
[in] n IFFT size \(n\).
IFFT size can be composite: \(n = n_0 \times n_1 \times n_2 \times \ldots \times n_p \times m\), here \(n_i = 2,3,5,7\), а \(m \) – simple number less than 46340 (see fft_create function).
[in] pfft Pointer to the fft_t
object.
This pointer cannot beNULL
.
Structure fft_t should be previously once filled with the fft_create function, and the memory should be cleared before exiting by the fft_free function.
[out] y Pointer to the IFFT result vector \(Y(k)\), \( k = 0 \ldots n-1 \).
Vector size is[n x 1]
.
Memory must be allocated.
- Returns
RES_OK
if IFFT is calculated successfully.
Else code error.
IFFT example:
Result:
| x[ 0] = 1.000 0.000 | y[ 0] = -0.517 0.686 | z[ 0] = 1.000 0.000 | | x[ 1] = 0.540 0.841 | y[ 1] = -0.943 0.879 | z[ 1] = 0.540 0.841 | | x[ 2] = -0.416 0.909 | y[ 2] = -2.299 1.492 | z[ 2] = -0.416 0.909 | | x[ 3] = -0.990 0.141 | y[ 3] = 16.078 -6.820 | z[ 3] = -0.990 0.141 | | x[ 4] = -0.654 -0.757 | y[ 4] = 2.040 -0.470 | z[ 4] = -0.654 -0.757 | | x[ 5] = 0.284 -0.959 | y[ 5] = 1.130 -0.059 | z[ 5] = 0.284 -0.959 | | x[ 6] = 0.960 -0.279 | y[ 6] = 0.786 0.097 | z[ 6] = 0.960 -0.279 | | x[ 7] = 0.754 0.657 | y[ 7] = 0.596 0.183 | z[ 7] = 0.754 0.657 | | x[ 8] = -0.146 0.989 | y[ 8] = 0.470 0.240 | z[ 8] = -0.146 0.989 | | x[ 9] = -0.911 0.412 | y[ 9] = 0.375 0.283 | z[ 9] = -0.911 0.412 | | x[10] = -0.839 -0.544 | y[10] = 0.297 0.318 | z[10] = -0.839 -0.544 | | x[11] = 0.004 -1.000 | y[11] = 0.227 0.350 | z[11] = 0.004 -1.000 | | x[12] = 0.844 -0.537 | y[12] = 0.161 0.380 | z[12] = 0.844 -0.537 | | x[13] = 0.907 0.420 | y[13] = 0.094 0.410 | z[13] = 0.907 0.420 | | x[14] = 0.137 0.991 | y[14] = 0.023 0.442 | z[14] = 0.137 0.991 | | x[15] = -0.760 0.650 | y[15] = -0.059 0.479 | z[15] = -0.760 0.650 | | x[16] = -0.958 -0.288 | y[16] = -0.161 0.525 | z[16] = -0.958 -0.288 | | x[17] = -0.275 -0.961 | y[17] = -0.300 0.588 | z[17] = -0.275 -0.961 |
Definition at line 179 of file ifft_cmplx.c.
Referenced by conv_fft_cmplx().
Generated on Wed Jan 5 2022 12:44:37 for libdspl-2.0 by 1.9.2