/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * Geophysical Computational Tools & Library (GCTL)
 *
 * Copyright (c) 2023  Yi Zhang (yizhang-geo@zju.edu.cn)
 *
 * GCTL is distributed under a dual licensing scheme. You can redistribute 
 * it and/or modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation, either version 2 
 * of the License, or (at your option) any later version. You should have 
 * received a copy of the GNU Lesser General Public License along with this 
 * program. If not, see <http://www.gnu.org/licenses/>.
 * 
 * If the terms and conditions of the LGPL v.2. would prevent you from using 
 * the GCTL, please consider the option to obtain a commercial license for a 
 * fee. These licenses are offered by the GCTL's original author. As a rule, 
 * licenses are provided "as-is", unlimited in time for a one time fee. Please 
 * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget 
 * to include some description of your company and the realm of its activities. 
 * Also add information on how to contact you by electronic and paper mail.
 ******************************************************/

#include "../lib/core.h"
#include "../lib/io.h"
#include "../lib/algorithm.h"

using namespace gctl;

double module(std::complex<double> a)
{
    return sqrt(a.real()*a.real() + a.imag()*a.imag());
}

int main(int argc, char const *argv[]) try
{
	int fs = 100;
    double T = 5;
    int n = round(fs*T);
    double t;

    gctl::array<double> in(n), out;
    gctl::array<double> freq_index;
    gctl::_1cd_array spectrum;
 
    for (size_t i = 0; i < n; i++)
    {
        t = 1.0*T*i/n;
        in[i] = 2.0*sin(2.0*GCTL_Pi*t*10) + 1.0*sin(2.0*GCTL_Pi*t*5) + 3.0*sin(2.0*GCTL_Pi*t*2);
    }

    // Run the r2c transform
    gctl::dft_r2c_1d(in, spectrum, 1.0*fs, &freq_index);

    std::ofstream ofile;    
    open_outfile(ofile, "fft_example_spec.out");
    for (size_t i = 0; i < spectrum.size(); i++)
    {
        ofile << freq_index[i] << ' ' << module(spectrum[i]) << " ";

        if (9.5 < freq_index[i] && freq_index[i] < 10.5) {spectrum[i].real(0.0); spectrum[i].imag(0.0);}
        //if (1.5 < freq_index[i] && freq_index[i] < 2.5) {spectrum[i].real(0.0); = spectrum[i].imag(0.0);}
        if (4.5 < freq_index[i] && freq_index[i] < 5.5) {spectrum[i].real(0.0); spectrum[i].imag(0.0);}

        ofile << module(spectrum[i]) << "\n";
    }
    ofile.close();

    // Run the c2r transform
    gctl::dft_c2r_1d(spectrum, out);
    
    open_outfile(ofile, "fft_example_sig.out");
    for (size_t i = 0; i < n; i++)
    {
        t = 1.0*T*i/n;
        ofile << t << " " << in[i] << " " << out[i] << "\n";
    }
    ofile.close();
}
catch(std::exception &e)
{
    GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}