/*
 * Copyright (c) 2018, Linz Center of Mechatronics GmbH (LCM), web: www.lcm.at
 * All rights reserved.
 */
/*
 * This file is licensed according to the BSD 3-clause license as follows:
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the "Linz Center of Mechatronics GmbH" and "LCM" nor
 *       the names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL "Linz Center of Mechatronics GmbH" BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
/*
 * This file is part of X2C. web: x2c.lcm.at
 * $LastChangedRevision: 3474 $
 * $LastChangedDate:: 2024-11-06 18:00:53 +0100#$
 */
// USERCODE-BEGIN:Description                                                                                           
/* Description: */
// USERCODE-END:Description                                                                                             

package at.lcm.x2c.library.filter;

import at.lcm.x2c.core.structure.*;
import at.lcm.bu21.general.dtypes.*;

// USERCODE-BEGIN:Imports                                                                                               
// USERCODE-END:Imports                                                                                                 

@SuppressWarnings("unused")
public class ConvFnc_NotchBiQ_Float64 extends JavaConversionFunction {
    private static final long serialVersionUID = 1L;

    @Override
    public void convertMaskToImplementation() throws Exception {

        // Mask Parameter: method                                                                                       
        MaskComboBox methodMaskVal = 
            (MaskComboBox)this.getMaskParameter("method").getMaskDataType();
        TString methodMaskData = 
            (TString)methodMaskVal.getData();
        // Mask Parameter: fn                                                                                           
        MaskDouble fnMaskVal = 
            (MaskDouble)this.getMaskParameter("fn").getMaskDataType();
        TNumeric fnMaskData = 
            (TNumeric)fnMaskVal.getData();
        // Mask Parameter: Q                                                                                            
        MaskDouble QMaskVal = 
            (MaskDouble)this.getMaskParameter("Q").getMaskDataType();
        TNumeric QMaskData = 
            (TNumeric)QMaskVal.getData();
        // Mask Parameter: ts_fact                                                                                      
        MaskDouble ts_factMaskVal = 
            (MaskDouble)this.getMaskParameter("ts_fact").getMaskDataType();
        TNumeric ts_factMaskData = 
            (TNumeric)ts_factMaskVal.getData();

        // Implementation Parameter: b0                                                                                 
        TNumeric b0CtrVal = this.getImplementationParameterData("b0");
        // Implementation Parameter: b1                                                                                 
        TNumeric b1CtrVal = this.getImplementationParameterData("b1");
        // Implementation Parameter: b2                                                                                 
        TNumeric b2CtrVal = this.getImplementationParameterData("b2");
        // Implementation Parameter: a1                                                                                 
        TNumeric a1CtrVal = this.getImplementationParameterData("a1");
        // Implementation Parameter: a2                                                                                 
        TNumeric a2CtrVal = this.getImplementationParameterData("a2");
        // Implementation Parameter: in_old                                                                             
        TNumeric in_oldCtrVal = this.getImplementationParameterData("in_old");
        // Implementation Parameter: in_veryold                                                                         
        TNumeric in_veryoldCtrVal = this.getImplementationParameterData("in_veryold");
        // Implementation Parameter: out_old                                                                            
        TNumeric out_oldCtrVal = this.getImplementationParameterData("out_old");
        // Implementation Parameter: out_veryold                                                                        
        TNumeric out_veryoldCtrVal = this.getImplementationParameterData("out_veryold");

// USERCODE-BEGIN:ConvMaskToImplementation                                                                              
        /* get mask parameter values */
        double f_n = Double.valueOf(fnMaskVal.getValue()); /* the notch frequency */
        double Q = Double.valueOf(QMaskVal.getValue()); /* Q-factor */
        double ts_fact = Double.valueOf(ts_factMaskVal.getValue()).intValue();

        /* validation */
        if (ts_fact < 1) {
            throw new Exception("Sample time factor must not be smaller than one!");
        }
        /* calculate multiplication factors for filter coefficients */
        double Ts = ts_fact * this.getDedicatedBlock().getModel().getSampleTime();
        double fs = 1 / Ts;

        if (Q < 0.5) {
            throw new Exception("The quality factor must be grater than 0.5.");
        }
        if (f_n > (fs / 2)) {
            throw new Exception("The notch frequency must be lower than fs/2.");
        }
        if (f_n < 0) {
            throw new Exception("The notch frequency must be grater than zero.");
        }

        // Create and initialize the coefficients
        double a0 = 1, a1 = 1, a2 = 1;
        double b0 = 1, b1 = 1, b2 = 1;
        // Calculate angular velocity
        double w0 = f_n * 2.0 * Math.PI;

            if (methodMaskVal.getValue().equals("bilinear")) {
            // warping of freq-axis
            w0 = (2.0 / Ts) * Math.tan((w0 * Ts) / 2.0);
            // Calculate coefficients using bilinear transformation s -> z
            // s = 2/Ts*((z-1)/((z+1)))
            a1 = (((2 * Q * Ts * Ts * w0 * w0) - (8 * Q))) / (((Q * Ts * Ts * w0 * w0) + (2 * Ts * w0) + (4 * Q)));
            b1 = a1;
            a2 = (((Q * Ts * Ts * w0 * w0) - (2 * Ts * w0)) + (4 * Q))
                    / (((Q * Ts * Ts * w0 * w0) + (2 * Ts * w0) + (4 * Q)));
            b0 = (((Q * Ts * Ts * w0 * w0) + (4 * Q))) / (((Q * Ts * Ts * w0 * w0) + (2 * Ts * w0) + (4 * Q)));
            b2 = b0;
        } else if (methodMaskVal.getValue().equals("matched Z-transform")) {
            // Calculate the coefficients using zeros and poles transform
            a0 = 1;
            a1 = -2 * Math.exp((-w0*Ts)/(2*Q)) * Math.cos(((-w0*Ts)/(2*Q)) * Math.sqrt((4*Q*Q) - 1));
            a2 = Math.exp((-2*w0*Ts)/(2*Q));
            b0 = 1;
            b1 = -2 * Math.cos(w0*Ts);
            b2 = 1;
            // Adjust gain:
            double K = (a0 + a1 + a2) / (b0 + b1 + b2);
            b0 *= K;
            b1 *= K;
            b2 *= K;
        }
        else {
            throw new Exception("You must choose one of possible method.");
        }
        /* set implementation parameter values */
        b0CtrVal.setReal(0, 0, b0);
        b1CtrVal.setReal(0, 0, b1);
        b2CtrVal.setReal(0, 0, b2);
        a1CtrVal.setReal(0, 0, a1);
        a2CtrVal.setReal(0, 0, a2);

// USERCODE-END:ConvMaskToImplementation                                                                                
        this.setImplementationParameterData("b0", b0CtrVal);
        this.setImplementationParameterData("b1", b1CtrVal);
        this.setImplementationParameterData("b2", b2CtrVal);
        this.setImplementationParameterData("a1", a1CtrVal);
        this.setImplementationParameterData("a2", a2CtrVal);
        this.setImplementationParameterData("in_old", in_oldCtrVal);
        this.setImplementationParameterData("in_veryold", in_veryoldCtrVal);
        this.setImplementationParameterData("out_old", out_oldCtrVal);
        this.setImplementationParameterData("out_veryold", out_veryoldCtrVal);
    }

    @Override
    public void convertImplementationToMask() throws Exception {

        // Mask Parameter: method                                                                                       
        MaskComboBox methodMaskVal = 
            (MaskComboBox)this.getMaskParameter("method").getMaskDataType();
        TString methodMaskData = 
            (TString)methodMaskVal.getData();
        // Mask Parameter: fn                                                                                           
        MaskDouble fnMaskVal = 
            (MaskDouble)this.getMaskParameter("fn").getMaskDataType();
        TNumeric fnMaskData = 
            (TNumeric)fnMaskVal.getData();
        // Mask Parameter: Q                                                                                            
        MaskDouble QMaskVal = 
            (MaskDouble)this.getMaskParameter("Q").getMaskDataType();
        TNumeric QMaskData = 
            (TNumeric)QMaskVal.getData();
        // Mask Parameter: ts_fact                                                                                      
        MaskDouble ts_factMaskVal = 
            (MaskDouble)this.getMaskParameter("ts_fact").getMaskDataType();
        TNumeric ts_factMaskData = 
            (TNumeric)ts_factMaskVal.getData();

        // Implementation Parameter: b0                                                                                 
        TNumeric b0CtrVal = this.getImplementationParameterData("b0");
        // Implementation Parameter: b1                                                                                 
        TNumeric b1CtrVal = this.getImplementationParameterData("b1");
        // Implementation Parameter: b2                                                                                 
        TNumeric b2CtrVal = this.getImplementationParameterData("b2");
        // Implementation Parameter: a1                                                                                 
        TNumeric a1CtrVal = this.getImplementationParameterData("a1");
        // Implementation Parameter: a2                                                                                 
        TNumeric a2CtrVal = this.getImplementationParameterData("a2");
        // Implementation Parameter: in_old                                                                             
        TNumeric in_oldCtrVal = this.getImplementationParameterData("in_old");
        // Implementation Parameter: in_veryold                                                                         
        TNumeric in_veryoldCtrVal = this.getImplementationParameterData("in_veryold");
        // Implementation Parameter: out_old                                                                            
        TNumeric out_oldCtrVal = this.getImplementationParameterData("out_old");
        // Implementation Parameter: out_veryold                                                                        
        TNumeric out_veryoldCtrVal = this.getImplementationParameterData("out_veryold");

// USERCODE-BEGIN:ConvImplementationToMask                                                                              
        if (true) {
            throw new Exception(
                    "Implementation-to-Mask conversion not implemented for Block 'NotchBiQ', Implementation 'Float64' in Library 'Filter'");
        }
// USERCODE-END:ConvImplementationToMask                                                                                
        this.getMaskParameter("method").setValue(methodMaskData.getString());
        this.getMaskParameter("fn").setValue(fnMaskData.toFullString());
        this.getMaskParameter("Q").setValue(QMaskData.toFullString());
        this.getMaskParameter("ts_fact").setValue(ts_factMaskData.toFullString());
    }
}
