DiscreteFourierTransform.cc 1.28 KB
#include "DiscreteFourierTransform.hh"
#include <bits/stdc++.h>

template <class T, class E>
DiscreteFourierTransform<T, E>::DiscreteFourierTransform(int nbEchantillons_, vector<T> signal_, double frequenceEchantillonnage_)
{
    nbEchantillons = nbEchantillons_;
    frequenceEchantillonnage = frequenceEchantillonnage_;
    signal = signal_;
}

template <class T, class E>
void DiscreteFourierTransform<T, E>::compute()
{
    for (int k = 0; k < nbEchantillons; k++)
    {
        E xR = 0;
        E yI = 0;
        E fk = (E)(k * frequenceEchantillonnage) / (E)nbEchantillons;
        // E fk = (E)(k * 1.0 / nbEchantillons);
        for (int n = 0; n < signal.size(); n++)
        {
            const T val = signal[n];
            if (!isNAN(val))
            {
                const double angle = 2 * PI * n * fk;
                xR += (E)cos(angle) * val;
                yI += (E)sin(angle) * val;
            }
        }

        E amplitude = (E)sqrt(xR * xR + yI * yI);
        E phase = (E)atan2(yI, xR);

        amplitudes.push_back(amplitude);
        phases.push_back(phase);
        frequences.push_back(fk);
        if (fk == (E)0)
            periodes.push_back((E)0);
        else
            periodes.push_back((E)(1.0 / fk));
        sort(periodes.begin(), periodes.end());
    }
}