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

#ifndef _RIF_FILTER_H
#define _RIF_FILTER_H

#include "windowfunc.h"

namespace gctl
{
    class fir_filter
    {
    public:
        /**
         * @brief Construct a new fir filter object
         * 
         * @param fi_type Filting type
         * @param w_type Window function type
         * @param taps Tap size
         * @param fs Sampling frequency
         * @param f1 First filtering frequency. This is needed for low/high pass filters
         * @param f2 Second filtering frequency. This is needed for band pass or band stop filters
         */
        fir_filter(filting_type_e fi_type, window_type_e w_type, int taps, double fs, double f1, double f2 = 0);

        /**
         * @brief Default constructor
         * 
         */
        fir_filter();

        /**
         * @brief Default de-constructor
         * 
         */
        virtual ~fir_filter();
        
        /**
         * @brief Initiate a fir filter object
         * 
         * @param fi_type Filting type
         * @param w_type Window function type
         * @param taps Tap size
         * @param fs Sampling frequency
         * @param f1 First filtering frequency. This is needed for low/high pass filters
         * @param f2 Second filtering frequency. This is needed for band pass or band stop filters
         */
        void init(filting_type_e fi_type, window_type_e w_type, int taps, double fs, double f1, double f2 = 0);

        /**
         * @brief Filt the input data using the initiated filter. Phase shift is corrected automatically
         * 
         * @param in Input data
         * @param out OUtput data
         */
        void filter(const array<double> &in, array<double> &out);

        /**
         * @brief Get filter coefficients for external exams
         * 
         * @return Filter coefficients
         */
        array<double> &get_coefficients();

    private:
        void low_pass_coefficient(double f); ///< initiate low pass filter coefficients
        void high_pass_coefficient(double f); ///< initiate hig pass filter coefficients
        void band_pass_coefficient(double f1, double f2); ///< initiate band pass filter coefficients
        void band_stop_coefficient(double f1, double f2); ///< initiate band stop filter coefficients

        array<double> h_; ///< FIR coefficients
        array<double> w_; ///< Coefficient weights (window function coefficients)

        int taps_;       ///< Number of taps of the filter
    };
};

#endif // _RIF_FILTER_H