DiscreteFourierTransform.hh 3.17 KB

#ifndef _DISCRETEFOURIERTRANSFORM_H
#define _DISCRETEFOURIERTRANSFORM_H

#include "plplot/plplot.h"

#include <complex>
#include <math.h>

#include <vector>

using namespace std;

template <class T, class E>
class DiscreteFourierTransform
{

public:
    /**
     * @brief Construct a new Discrete Fourier Transform object: https://docs.scipy.org/doc/scipy/tutorial/fft.html
     * @param signal_ the signal (data)
     * @param sampleSpacing  Sample spacing
     */
    DiscreteFourierTransform(vector<T> signal_, double sampleSpacing);
    /**
     * @brief Compute FFT or DFT
     *
     * @param computeFFT flag to indicate either compute FFT or DFT, default to false
     */
    void compute(bool computeFFT = false);
    /**
     * @brief Compute FFT for given signal
     *
     * @param sig signal (data)
     * @return vector<complex<E>> phasors
     */
    vector<complex<E>> fft(vector<T> sig);
    /**
     * @brief Compute DFT for given signal
     *
     * @param sig signal (data)
     * @return vector<complex<E>> phasors
     */
    vector<complex<E>> dft(vector<T> sig);
    /**
     * @brief Get only the values of Even indexes
     *
     * @param x  given array
     * @return vector<T>
     */
    vector<T> getEven(vector<T> x);
    /**
     * @brief Get only the values of Odd indexes
     *
     * @param x given array
     * @return vector<T>
     */
    vector<T> getOdd(vector<T> x);
    /**
     * @brief Compute DSP for given complex phasors
     *
     * @param x complex phasors = FFT/DFT output
     * @return vector<E>
     */
    vector<E> computeDSP(vector<complex<E>> x);
    /**
     * @brief Compute frequencies for given phasors
     *
     * @param x phasors
     * @param frequency_ sampling frequency
     * @return vector<E>
     */
    vector<E> getFreq(vector<complex<E>> x, double frequency_);
    /**
     * @brief Compute periods for given phasors
     *
     * @param x phasors
     * @param frequency_ sampling frequency
     * @return vector<E>
     */
    vector<E> getPeriods(std::vector<std::complex<E>> x, double frequency_);
    /**
     * @brief Create a vector of Points for test
     *
     * @param N size of the vector to be created
     * @param sampleSpacing_ sample spacing
     * @return vector<E>
     */
    vector<E> createTestPoints(int N, double sampleSpacing_);
    /**
     * @brief Create a Test Function
     *
     * @param x vector of points on which to compute this fucntion
     * @return vector<E>
     */
    vector<E> createTestFunction(vector<E> x);
    /**
     * @brief Check if is a power 2
     *
     * @param N integer number
     * @return true
     * @return false
     */
    bool isPowerOfTwo(int N);
    /**
     * @brief Get the smallest power of 2 number bigger than the given number
     *
     * @param n integer number
     * @return int
     */
    int highestPowerof2(int n);

    vector<complex<E>> getPhasors()
    {
        return phasors;
    }

private:
    /**
     * @brief the signal (data)
     *
     */
    vector<T> signal;
    /**
     * @brief Sample Spacing
     *
     */
    double sampleSpacing;
    /**
     * @brief a vector of a complex numbers representing a FFT result
     *
     */
    vector<complex<E>> phasors;
};

#endif