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

/* Copyright 2013 Perttu Luukko

 * This file is part of libeemd.

 * libeemd is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * libeemd is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with libeemd.  If not, see <http://www.gnu.org/licenses/>.
 */

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

using namespace gctl;

const size_t ensemble_size = 250;
const unsigned int S_number = 4;
const unsigned int num_siftings = 50;
const double noise_strength = 0.2;
const unsigned long int rng_seed = 0;
std::string outfile = "eemd_example.out";

// An example signal to decompose
const size_t N = 1024;
static inline double input_signal(double x) {
	const double omega = 2*GCTL_Pi/(N-1);
	return sin(17*omega*x)+0.5*(1.0-exp(-0.002*x))*sin(51*omega*x+1);
}

int main(int argc, char const *argv[]) try
{
	// Define input data
    array<double> inp(N);
	for (size_t i=0; i<N; i++)
    {
		inp[i] = input_signal((double)i);
	}

    // Run eemd
	matrix<double> outp;
    // This is just a warpper of the function eemd() from libeemd
    // IMFs are stored in rows of the output matrix in the shape of M*N
    eemd1d(inp, outp, 0, ensemble_size, noise_strength, S_number, num_siftings, rng_seed);

    std::ofstream ofile;
    open_outfile(ofile, outfile);

    size_t M = outp.row_size();
    for (size_t j=0; j<N; j++)
    {
        ofile << inp[j];
		for (size_t i=0; i<M; i++)
        {
            ofile << " " << outp[i][j];
		}
		ofile << "\n";
	}
    ofile.close();
    return 0;
}
catch(std::exception &e)
{
    GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}