Skip to content

Commit

Permalink
focs: add docs to new fft and ifft
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Elfimov committed Jun 27, 2024
1 parent a716012 commit 48a7fee
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 14 deletions.
44 changes: 37 additions & 7 deletions numerical_methods/fast_fourier_transform2.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
/**
* @file
* @brief [A fast Fourier transform
* (FFT)](https://www.oreilly.com/library/view/c-cookbook/0596007612/ch11s18.html)
*/

#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
* (FFT)](https://www.oreilly.com/library/view/c-cookbook/0596007612/ch11s18.html)
* is an algorithm that computes the discrete Fats Fourier transform (FFT)
* @details
* Function in this example, was written to be as simple as possible rather
* than focusing on efficiency. Time complexity this algorithm computes
* the DFT in O(nlogn) time in comparison to traditional O(n^2).
* @author [Alexey Elfimov](https://github.com/ikocs)
*/

#include <complex> /// for storing points and coefficients
#include <iostream> /// for IO operations
#include <vector> /// for std::vector
#include <cstdint> /// for uint8_t and other
#include <cassert> /// for assert

using namespace std;

/**
* @namespace numerical_methods
* @brief Numerical algorithms/methods
*/
namespace numerical_methods {
/**
* @brief Performs bit inversion in a number
* @param x input number
* @param log2n number size
* @return result revert number
*/
unsigned int bit_reverse(unsigned int x, const unsigned int log2n) {
unsigned int n = 0;
for (int i = 0; i < log2n; i++) {
Expand All @@ -26,6 +41,14 @@ unsigned int bit_reverse(unsigned int x, const unsigned int log2n) {

const double PI = 3.1415926536;

/**
* @brief Fast Fourier Transform is a recursive function which
* returns Container of complex numbers
* @tparam Container container for storing data. For example std::vector
* @param in_data input data
* @param log2n size fft in power of two format
* @return calculation result
*/
template <typename Container>
Container fft(const Container& in_data, const unsigned int log2n) {
using Complex = typename Container::value_type;
Expand Down Expand Up @@ -58,6 +81,13 @@ Container fft(const Container& in_data, const unsigned int log2n) {
}
} // namespace numerical_methods

/**
* @brief Self-test implementations
* @details
* Declaring three test cases and checking for the error
* in predicted and true value is less than 1e-6.
* @returns void
*/
static void test() {
using ComplVec = std::vector<std::complex<double>>;

Expand Down
46 changes: 39 additions & 7 deletions numerical_methods/inverse_fast_fourier_transform2.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
/**
* @file
* @brief [A fast Fourier transform
* (FFT)](https://www.oreilly.com/library/view/c-cookbook/0596007612/ch11s18.html)
*/

#include <cmath>
#include <complex>
#include <iostream>
#include <vector>
* (FFT)](https://www.oreilly.com/library/view/c-cookbook/0596007612/ch11s18.html)
* is an algorithm that computes the discrete Inverse Fast Fourier transform (FFT)
* @details
* The difference between an inverse transformation and a direct one
* is the presence of normalization and a change in the sign under the exponent.
* Function in this example, was written to be as simple as possible rather
* than focusing on efficiency. Time complexity this algorithm computes
* the DFT in O(nlogn) time in comparison to traditional O(n^2).
* @author [Alexey Elfimov](https://github.com/ikocs)
*/

#include <complex> /// for storing points and coefficients
#include <iostream> /// for IO operations
#include <vector> /// for std::vector
#include <cstdint> /// for uint8_t and other
#include <cassert> /// for assert

using namespace std;

/**
* @namespace numerical_methods
* @brief Numerical algorithms/methods
*/
namespace numerical_methods {
/**
* @brief Performs bit inversion in a number
* @param x input number
* @param log2n number size
* @return result revert number
*/
unsigned int bit_reverse(unsigned int x, const unsigned int log2n) {
unsigned int n = 0;
for (int i = 0; i < log2n; i++) {
Expand All @@ -26,6 +43,14 @@ unsigned int bit_reverse(unsigned int x, const unsigned int log2n) {

const double PI = 3.1415926536;

/**
* @brief Inverse Fast Fourier Transform is a recursive function which
* returns Container of complex numbers
* @tparam Container container for storing data. For example std::vector
* @param in_data input data
* @param log2n size ifft in power of two format
* @return calculation result
*/
template <typename Container>
Container ifft(const Container& in_data, const unsigned int log2n) {
using Complex = typename Container::value_type;
Expand Down Expand Up @@ -63,6 +88,13 @@ Container ifft(const Container& in_data, const unsigned int log2n) {
}
}// namespace numerical_methods

/**
* @brief Self-test implementations
* @details
* Declaring three test cases and checking for the error
* in predicted and true value is less than 1e-6.
* @returns void
*/
static void test() {
using ComplVec = std::vector<std::complex<double>>;

Expand Down

0 comments on commit 48a7fee

Please sign in to comment.