DiscreteFourierTransform.hh 1.86 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);
    vector<T> getEven(vector<T> x);
    vector<T> getOdd(vector<T> x);
    vector<E> computeDSP(vector<complex<E>> x);
    vector<E> getFreq(vector<complex<E>> x, double sampleSpacing_);
    vector<E> getPeriods(std::vector<std::complex<E>> x, double sampleSpacing_);
    vector<E> createTestPoints(int N, double sampleSpacing_);
    vector<E> createTestFunction(vector<E> x);
    bool isPowerOfTwo(int N);
    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