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

using namespace gctl;

const size_t ensemble_size = 500;
const unsigned int S_number = 4;
const unsigned int num_siftings = 50;
const double noise_strength = 0.02;
const unsigned long int rng_seed = 0;
const std::string outfile = "ceemdan_example.out";

const size_t N = 512;

int main(int argc, char const *argv[]) try
{
	libeemd_error_code err;
	// As an example decompose a Dirac signal as in the original CEEMDAN paper
	array<double> inp(N, 0.0);
	inp[N/2] = 1.0;

	// Allocate memory for output data
	size_t M = emd_num_imfs(N); // This function is provided by the libeemd
	matrix<double> outp;

	// Run CEEMDAN
	// This is just a warpper of the function ceemdan() from libeemd
	// IMFs are stored in rows of the output matrix in the shape of M*N
	ceemdan1d(inp, outp, M, ensemble_size, noise_strength, S_number, num_siftings, rng_seed);
	
	std::ofstream ofile;
    open_outfile(ofile, outfile);

    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();
}
catch(std::exception &e)
{
    GCTL_ShowWhatError(e.what(), GCTL_ERROR_ERROR, 0, 0, 0);
}