#include "wabor_t_window_functions.h"

pac_type wabor_t_wf_win_cos(pac_type_real t, pac_type_real h, pac_type_real wl, pac_type_real UNUSED wl_c){
    if(t<-wl/2.0L) return 0;
    if(t<wl/2.0L) return h*cos(PI*t/wl);
    return 0;
}
pac_type wabor_t_wf_WIN_COS(pac_type_real f, pac_type_real h, pac_type_real wl, pac_type_real UNUSED wl_c){
   pac_type_real w = f*2.0L*PI;
   pac_type_real l = wl;
    if(w==PI/l || w==-PI/l) {
        return (0.5L*l*h)+0*I; 
    }
    //if( fabsPAC(PI_L-ldFrequ*dLength)<1e-3 || fabsPAC(PI_L+dFrequ*dLength)<1e-3 ){
    if( fabsPAC(PI-w*l)<1e-3 || fabsPAC(PI+w*l)<1e-3 ){
       pac_type_real *aw=wabor_t_malloc0(11 * sizeof *aw );
       pac_type_real *al=wabor_t_malloc0(11 * sizeof *al );
        if(al==NULL || aw ==NULL){ wabor_t_free(al); wabor_t_free(aw); error("win_function: Out of Memory"); return NAN+I*NAN; }
        aw[0] = 1;
        aw[1] = w;
        aw[2] = aw[1] * aw[1];
        aw[3] = aw[2] * aw[1];
        aw[4] = aw[2] * aw[2];
        aw[5] = aw[3] * aw[2];
        aw[6] = aw[3] * aw[3];
        aw[7] = aw[4] * aw[3];
        aw[8] = aw[4] * aw[4];
        aw[9] = aw[5] * aw[4];
        aw[10]= aw[5] * aw[5];

        al[0] = 1;
        al[1] = l;
        al[2] = al[1] * al[1];
        al[3] = al[2] * al[1];
        al[4] = al[2] * al[2];
        al[5] = al[3] * al[2];
        al[6] = al[3] * al[3];
        al[7] = al[4] * al[3];
        al[8] = al[4] * al[4];
        al[9] = al[5] * al[4];
        al[10]= al[5] * al[5];
        
        return (-.130621781524094697806897105157e-15L*al[1]*h*(115403559438020.691977198724147L*aw[2]*al[2]+890682886.60146732177537L*aw[1]*al[1]+5656.1668414453185215008369L*aw[10]*al[10]+375175.5695890276238862764096L*aw[9]*al[9]+25624930.115360487641742938L*aw[7]*al[7]-16593705.3403553964836842101L*aw[8]*al[8]+4418848345.455752659483273201L*aw[6]*al[6]-999969556207.65548304317027618L*aw[4]*al[4]+370983117.1359132982065017L*aw[5]*al[5]+1336824385.932056980518354L*aw[3]*al[3]-4873764530791584.78509326671219L))+0L*I;
    }
    return (2.0L*l*PI*h*cosPAC(0.5L*w*l)/(PI2-w*w*l*l))+0L*I;
}

//#define C0 (0.8932496851384835500842199045142)
//pac_type wabor_t_wf_win_sqrt_gauss(pac_type_real t, pac_type_real h, pac_type_real wl, pac_type_real UNUSED wl_c){
//    return C0*h*expPAC(-1/4*t*t/wl/wl);
//}
//pac_type wabor_t_wf_WIN_SQRT_GAUSS(pac_type_real f, pac_type_real h, pac_type_real wl, pac_type_real UNUSED wl_c){
//    return C0*SQRT2*h*expPAC(-f*f*wl*wl)*wl;
//}
//#undef C0

//#define C (1)
#define   C (0.89325169867176854980281661636193L)

pac_type wabor_t_wf_win_sqrt_gauss(pac_type_real t, pac_type_real h, pac_type_real wl, pac_type_real UNUSED wl_c){
    return C*h*expPAC(-1/4*t*t/wl/wl);
}
pac_type wabor_t_wf_WIN_SQRT_GAUSS(pac_type_real f, pac_type_real h, pac_type_real wl, pac_type_real UNUSED wl_c){
    pac_type_real val=C*SQRT2*h*expPAC(-f*f*wl*wl)*wl;

    return  val;
}
#undef C0

pac_type wabor_t_WF_win_bspline2(pac_type_real t, pac_type_real h, pac_type_real wl, pac_type_real UNUSED wl_c){
    pac_type n=2;
    if(t==0) return powPAC(n/(n+1),n+1);
    return h*wl*n/(2*PI*t*(n+1))*sinPAC(2*PI*t/wl);
}

pac_type wabor_t_WF_WIN_BSPLINE2(pac_type_real f, pac_type_real h, pac_type_real wl, pac_type_real UNUSED wl_c){
    pac_type n=2;
    return h*wl/(2.0*(n+1))*centered_bspline(f*wl/(2*(n+1)),n);
}

pac_type centered_bspline(pac_type_real f, int n){
    //n.. order
    //f.. f-value
    switch(n){
        case 0:
            if(f< (-1.0/2.0L)) return 0;
            if(f<=1/2.0L) return 1;
            return 0;
            break;
        case 1:
            if(f<-1/2.0L) return 0;
            if(f<0) return 1 + 2*f;
            if(f<=1/2.0L) return 1-2*f;
            return 0;
            break;
        case 2:
            if(f<-1/2.0L) return 0;
            if(f<-1/6.0L) return 9.0L/8.0L*(1 + 4*f*(1+f));
            if(f< 1/6.0L) return -(3.0L/4.0L)*(-1 + 12*f*f);
            if(f< 1/2.0L) return 9.0/8.0*(1 + 4*f*(-1+f));
            return 0;
            break;
        case 3:
            if(f<(-1/2.0L)) return 0;
            if(f<-1/4.0L) return 4/3.0L*(1 + 6*f + 12*f*f + 8*f*f*f);
            if(f<0) return -(2.0L/3.0L)*(-1 + 24*f*f*(1 + 2*f));
            if(f<1/4.0L) return 2.0/3.0*(1 +24*f*f*(-1+2*f));
            if(f<1/2.0L) return -(4/3.0L)*(-1 + 6*f - 12*f*f + 8*f*f*f);
            return 0;
            break;
        default:
            post("wabor_t: centered_bspline: Order of bspline too high");
            return 0;
            break;
    }
    error("wabor_t: centered_bspline: Very strange error");
    return 0;
    
    
}