/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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;

int main(int argc, char const *argv[]) try
{
	int fs = 200;
    double f1 = 4.0;
    double f2 = 10.0;

    // Create filters
    fft_filter filter_lp(LowPass, Butterworth, 5, (double) fs, f1);
    fft_filter filter_hp(HighPass, Butterworth, 5, (double) fs, f2);
    fft_filter filter_bp(BandPass, Butterworth, 5, (double) fs, f1, f2);
    fft_filter filter_bs(BandStop, Butterworth, 5, (double) fs, f1, f2);
    
    // Demo
    double f1_demo = 1.0; // Hz
    double f2_demo = 7.5;
    double f3_demo = 15.0;

    // Create input signal
    int snum = int(10.13*fs);
    gctl::array<double> t(snum), signal(snum);
    t.sequence(0.0, 10.13/(snum - 1));

    for (size_t i = 0; i < t.size(); i++)
    {
        signal[i] = 2.0*sin(2.0*GCTL_Pi*f1_demo*t[i]) + sin(2.0*GCTL_Pi*f2_demo*t[i]) + 0.5*sin(2.0*GCTL_Pi*f3_demo*t[i]);
    }

    gctl::array<double> filtered_lp, filtered_hp, filtered_bp, filtered_bs;

    // Run the filtersc。
    filter_lp.filter(signal, filtered_lp);
    filter_hp.filter(signal, filtered_hp);
    filter_bp.filter(signal, filtered_bp);
    filter_bs.filter(signal, filtered_bs);

    std::ofstream ofile;
    open_outfile(ofile, "fft_filter_ex.out");
    for (size_t i = 0; i < filtered_lp.size(); i++)
    {
        ofile << t[i] << " " << signal[i] << " " << filtered_lp[i] << " " << filtered_hp[i] << " " << filtered_bp[i] << " " << filtered_bs[i] << "\n";
    }
    ofile.close();
    return 0;
}
catch(std::exception &e)
{
    GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}