#include "wabor_t_array_functions.h"


void wabor_t_simplefft_array(pac_type ** const ppcIn, pac_type ** ppcOut, t_int FFTW_FLAG){
	//fftwPAC_plan_.... ueberschreibt *in und *out. deshalb muss die folgende Reihenfolge eingehalten werden:
	//  fftwPAC_complex *in, *out;
	//  fftwPAC_plan p;
	//  ...
	//  in = (fftwPAC_complex*)wabor_t_malloc0(sizeof(fftwPAC_complex)* N);
	//	out = (fftwPAC_complex*)wabor_t_malloc0(sizeof(fftwPAC_complex)* N);
	//	p = fftwPAC_plan_dft_1d(N, in, out, FFTW_FORWARd, FFTW_ESTIMATE);
	//  ...
	//	in mit daten fuellen
	//  ...
	//	fftwPAC_execute(p); /* repeat as needed */
	//	...
	//	fftwPAC_destroy_plan(p);
	//  wabor_t_free(in); wabor_t_free(out);
    pac_type *in, *out;
    fftwPAC_plan p;
    t_int iLength;
    pac_type *cIn;
    
    if(*ppcIn==NULL) { if(DEBUG) error("wabor_t_simpleftt_array: call with NULL-Pointer");  return; }
    pac_type placeholder=(*ppcIn)[WABOR_T_PLACE_HOLDER];
    iLength = (t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    cIn = *ppcIn;
    in = wabor_t_malloc0(iLength * sizeof *in);
    if(in==NULL) return;
    out = wabor_t_malloc0(iLength * sizeof *out);
    if(out==NULL){
        wabor_t_free(in);
        return;
    }
    
    p = fftwPAC_plan_dft_1d(iLength, in, out, FFTW_FLAG, FFTW_ESTIMATE);
    
    memcpy(in, cIn, iLength * sizeof *in);
    fftwPAC_execute(p);

    wabor_t_free(*ppcOut);
    *ppcOut=out; //Ich verwende gleiche out als Array der Rueckgabe
    (*ppcOut)[WABOR_T_LENGTH_BIN] = iLength + 0*I;
    (*ppcOut)[WABOR_T_PLACE_HOLDER] = placeholder;

    fftwPAC_destroy_plan(p);
    wabor_t_free(in); 
    
    return;
}



t_int wabor_t_get_first_zero_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return -1;
    t_int iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    if(iLength==0) return -1;
    if(crealPAC((*ppcIn)[0])<0) return -1;
    for(t_int i=0;i<iLength;++i){
        if(crealPAC((*ppcIn)[i])<0) return i;
    }
    return iLength-1;
}
t_int wabor_t_get_window_length_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return 0;
    return (t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
}
pac_type wabor_t_last_value_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return 0;
    t_int iLastBin=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN-1]);
    return (*ppcIn)[iLastBin];
}
pac_type wabor_t_first_value_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return 0;
    return (*ppcIn)[0];
}


void wabor_t_badfilter_array(pac_type ** const ppcIn, t_int iBin0, t_int iBin1){
    //Bei iBin0 = 0, Bei iBin1 = 1,
    if((*ppcIn)==NULL) return;
    t_int iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    pac_type_real ldFactor=0L;
    
    if(iLength==0) return;
    if(iBin0<0) iBin0=0;
    if(iBin0>=iLength) iBin0=iLength-1;
    if(iBin1<0) iBin1=0;
    if(iBin1>=iLength) iBin1=iLength-1;
    
    if(iBin0<iBin1){
        for(t_int i=iBin0; i<iBin1; ++i){
            ldFactor=-0.5L*cosPAC(((pac_type_real)i-(pac_type_real)iBin0)/((pac_type_real)iBin1-(pac_type_real)iBin0)*PI)+0.5L;
            (*ppcIn)[i] = (*ppcIn)[i]*ldFactor;
        }
        return;
    }
    if(iBin1<iBin0){
        for(t_int i=iBin1; i<iBin0; ++i){
            ldFactor=-0.5L*cosPAC(((pac_type_real)i-(pac_type_real)iBin0)/((pac_type_real)iBin1-(pac_type_real)iBin0)*PI)+0.5L;
            (*ppcIn)[i]*=ldFactor;
        }
        return;
    }
}
void wabor_t_abs_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return;
    //if(DEBUG) {post("Adresse: *%p, Wert: %f",*ppcIn,**ppcIn);}
    t_int iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    for(t_int i=0;i<iLength;++i){
        (*ppcIn)[i] = cabsPAC((*ppcIn)[i]) + 0*I;   
    }
}
void wabor_t_componentabs_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return;
    //if(DEBUG) {post("Adresse: *%p, Wert: %f",*ppcIn,**ppcIn);}
    t_int iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    for(t_int i=0;i<iLength;++i){
        (*ppcIn)[i] = fabsPAC(crealPAC((*ppcIn)[i]) ) + I*fabsPAC(cimagPAC((*ppcIn)[i]) );   
    }
}
void wabor_t_ln_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return;
    t_int iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    for(t_int i=0;i<iLength;++i){
        (*ppcIn)[i] = clogl((*ppcIn)[i]);
    }
}
void wabor_t_mult_array(pac_type ** const ppcIn, enum _eMultType eType, pac_type cVal){
    t_int iLength;
    if((*ppcIn)==NULL) return;
    iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    
    switch (eType)
    {    
        //enum _eMultType { TIMES_CONJ, SQUARE, TIMES_N, dIV_N, TIMES_VAL, RECIPROCAL, CONJUGATE };
        case TIMES_VAL:     for(t_int i=0;i<iLength;++i) { (*ppcIn)[i] *= cVal; }         break;
        case TIMES_CONJ:    for(t_int i=0;i<iLength;++i) { (*ppcIn)[i] *= conjl((*ppcIn)[i]); }    break;
        //case SQUARE:        for(t_int i=0;i<iLength;++i) { (*ppcIn)[i] *= (*ppcIn)[i]; }  break;
        case TIMES_N:       for(t_int i=0;i<iLength;++i) { (*ppcIn)[i] *= iLength; }      break;
        case DIV_N:         for(t_int i=0;i<iLength;++i) { (*ppcIn)[i] /= iLength; }      break;
        case RECIPROCAL:    for(t_int i=0;i<iLength;++i) { (*ppcIn)[i] = 1.0L/(*ppcIn)[i]; } break;
        case CONJUGATE:     for(t_int i=0;i<iLength;++i) { (*ppcIn)[i] = conjl((*ppcIn)[i]); }       break;
        case NORMALIZE:     for(t_int i=0;i<iLength;++i) 
            { 
                if(cabsPAC((*ppcIn)[i])==0) {
                    (*ppcIn)[i] = 1L+0*I;
                } else { 
                    (*ppcIn)[i] = ((*ppcIn)[i])/cabsPAC((*ppcIn)[i]); 
                } 
            }       
            break;
    }

}
void wabor_t_cut_array(pac_type ** const ppcIn, pac_type_real dCutLow, pac_type_real dCutHigh, pac_type cSetValueLow, pac_type cSetValueHigh){
    t_int iLength;
    if((*ppcIn)==NULL) return;
    iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    for(t_int i=0;i<iLength;++i){
        if(cabsPAC((*ppcIn)[i])<=dCutLow)
            (*ppcIn)[i]=((*ppcIn)[i])/cabsPAC((*ppcIn)[i])*cSetValueLow;
        if(cabsPAC((*ppcIn)[i])>=dCutHigh) 
            (*ppcIn)[i]=((*ppcIn)[i])/cabsPAC((*ppcIn)[i])*cSetValueHigh;
    }
}
void wabor_t_set_nan_to_zero_array(pac_type ** const ppcIn){
    t_int iCount=0;
    if((*ppcIn)==NULL) return;
    t_int iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    for(t_int i=0;i<iLength;++i){
        if(isnan((*ppcIn)[i])){
            if(DEBUG) ++iCount;
            (*ppcIn)[i]=0+0*I;
        }
    }
    if(iCount) {post("Anzahl Nans: %i",*ppcIn, iCount);}
}
void wabor_t_set_inf_to_zero_array(pac_type ** const ppcIn){
    t_int iCount=0;
    if((*ppcIn)==NULL) return;
    t_int iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    for(t_int i=0;i<iLength;++i){
        if(isinf((*ppcIn)[i])){
            if(DEBUG) ++iCount;
            (*ppcIn)[i]=0+0*I;
        }
    }
    if(iCount) {post("*%p, Anzahl Infs: %i",*ppcIn, iCount);}
}
void wabor_t_normalize_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return;
    pac_type_real ldMax=0L+0L*I;
    t_int iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    for(t_int i=0;i<iLength;++i){
        if(cabsPAC((*ppcIn)[i])>ldMax) ldMax=cabsPAC((*ppcIn)[i]);
    }
    if(ldMax==0) return;
    for(t_int i=0;i<iLength;++i){
        (*ppcIn)[i]=((*ppcIn)[i])/ldMax;
    }
    //if(DEBUG) {post("wabor_t_normalize_array fuer Adresse: *%p, Maximumwert: %f",*ppcIn, dMax);}   
}
void wabor_t_cycle_array(pac_type ** const ppcIn, t_int iBins){
    //NULL
    //Ziel und Source duerfen gleich sein
    pac_type *pc=NULL;
    t_int iLength;
    if(*ppcIn==NULL) return;
    iLength = (t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    pac_type placeholder=(*ppcIn)[WABOR_T_PLACE_HOLDER];
    if(iLength==0) return;
    while(iBins<0) iBins+=iLength;
    while(iBins>=iLength) iBins-=iLength;

    if(iBins==0) return;
    pc=wabor_t_malloc0(iLength * sizeof *pc); 
    if(pc==NULL){
        error("wabor_t_cycle_array: Out of Memory");
        return;
    }
    memcpy(pc+iBins, *ppcIn, (iLength-iBins) * sizeof *pc);
    memcpy(pc, &((*ppcIn)[iLength-iBins]),iBins * sizeof *pc);
    wabor_t_free(*ppcIn);
    *ppcIn=pc;
    
    (*ppcIn)[WABOR_T_LENGTH_BIN] = iLength + 0*I;
    (*ppcIn)[WABOR_T_PLACE_HOLDER] = placeholder;
    for(t_int i=1;i<=4;++i){ 
        (*ppcIn)[-i]=0L+0L*I;(*ppcIn)[iLength+i-1]=0L+0L*I; 
    } //Anfangs und End Nullen setzen
}
void wabor_t_fft_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return;
    t_int iLength=(*ppcIn)[WABOR_T_LENGTH_BIN];
    wabor_t_simplefft_array(ppcIn,ppcIn,FFTW_FORWARD);
    for(t_int i=1;i<=4;++i){ (*ppcIn)[-i]=0L+0L*I;(*ppcIn)[iLength+i-1]=0L+0L*I; } //Anfangs und End Nullen setzen
    return;
}
void wabor_t_floating_average_array(pac_type ** const ppcIn, t_int iWL){
    //Rechnet Gleitenden Durchschnitt über iWL Bins
    
    pac_type *pc=NULL;
    if((*ppcIn)==NULL) return;
    t_int const iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    pac_type placeholder = (*ppcIn)[WABOR_T_PLACE_HOLDER];
    if(iWL>iLength) iWL=iLength;
    if(iWL%2){;} else { --iWL;} //Nur ungerader floating Average
    if(iWL<=1) return;
    pc=wabor_t_malloc0(iLength * sizeof *pc);
    if(pc==NULL){
        error("wabor_t_floating_average_array: Out of Memory");
        return;
    }
    pac_type val;
    t_int WL_half=iWL/2;
    t_int iBin;
    for(t_int i=0;i<iLength;++i){
        val=0;
        for(t_int j=-WL_half;j<WL_half;++j){
            iBin=i+j;
            if(iBin<0) iBin+=iLength;
            if(iBin>iLength) iBin-=iLength;
            val+=(*ppcIn)[iBin];
        }
        pc[i]=val/iWL;
    }
    
    wabor_t_free(*ppcIn);
    *ppcIn=pc;
    
    (*ppcIn)[WABOR_T_LENGTH_BIN] = iLength + 0*I;
    (*ppcIn)[WABOR_T_PLACE_HOLDER] = placeholder;
    for(t_int i=1;i<=4;++i){ 
        (*ppcIn)[-i]=0L+0L*I;(*ppcIn)[iLength+i-1]=0L+0L*I; 
    } //Anfangs und End Nullen setzen
    
    return;
    
}
void wabor_t_differentiate_array(pac_type ** const ppcIn){
    //NULL
    pac_type *pc=NULL;
    t_int iLength;
    if((*ppcIn)==NULL) return;
    iLength = (t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    pac_type placeholder = (*ppcIn)[WABOR_T_PLACE_HOLDER];
    pc=wabor_t_malloc0(iLength * sizeof *pc);
    if(pc==NULL){
        error("wabor_t_differntiate_array: Out of Memory");
        return;
    }
    pc[0]=(*ppcIn)[0]-(*ppcIn)[iLength-1];
    for(t_int i=1; i<iLength; ++i) pc[i]=(*ppcIn)[i]-(*ppcIn)[i-1];
    wabor_t_free(*ppcIn);
    *ppcIn=pc;
    (*ppcIn)[WABOR_T_LENGTH_BIN] = iLength + 0*I;
    (*ppcIn)[WABOR_T_PLACE_HOLDER] = placeholder;
    for(t_int i=1;i<=4;++i){ 
        (*ppcIn)[-i]=0L+0L*I;(*ppcIn)[iLength+i-1]=0L+0L*I; 
    } //Anfangs und End Nullen setzen
        
    return;
}
void wabor_t_integrate_array(pac_type ** const ppcIn){
    pac_type cSum=0;
    if((*ppcIn)==NULL) return;
    t_int iLength=(*ppcIn)[WABOR_T_LENGTH_BIN];
    for(t_int i=1; i<iLength; ++i) { (*ppcIn)[i]+=(*ppcIn)[i-1]; }    
    for(t_int i=0; i<iLength; ++i) { cSum+=(*ppcIn)[i]; } //Wieder auf Gleichspannungsanteil 0 bringen
    cSum/=(pac_type_real)iLength;
    for(t_int i=0; i<iLength; ++i) { (*ppcIn)[i]-=cSum; }    
    //for(t_int i=1; i<iLength; ++i) { (*ppcIn)[i]/=WABOR_T_SAMPLING_RATE; }
    return;
}
void wabor_t_imag_plus_real_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return;   
    t_int iLength=(*ppcIn)[WABOR_T_LENGTH_BIN];
    for(t_int i=0; i<iLength; ++i){
        (*ppcIn)[i]=(crealPAC((*ppcIn)[i])+cimagPAC((*ppcIn)[i]))+I*(cimagPAC((*ppcIn)[i])-crealPAC((*ppcIn)[i]));
    }
    return;
}
pac_type wabor_t_make_dc_offset_array(pac_type ** const ppcIn, t_int iChangeOffset, pac_type cNewMean){
    if((*ppcIn)==NULL) return NAN+I*NAN;   
    t_int iLength=(*ppcIn)[WABOR_T_LENGTH_BIN];
    pac_type cMean=0+0*I;
    for(t_int i=0; i<iLength; ++i){
        cMean+=(*ppcIn)[i];
    }
    cMean/=(pac_type_real)iLength;
    if(iChangeOffset){
        for(t_int i=0; i<iLength; ++i){
            (*ppcIn)[i]=(*ppcIn)[i]-cMean+cNewMean;
        }
    }
    return cMean;
}
pac_type wabor_t_inner_product_array(pac_type  ** const ppcIn1, pac_type  ** const ppcIn2){
    if((*ppcIn1)==NULL) {
        return NAN+I*NAN;}
    if((*ppcIn2)==NULL) {
        return NAN+I*NAN;}
    t_int iLength1=(*ppcIn1)[WABOR_T_LENGTH_BIN];    
    t_int iLength2=(*ppcIn2)[WABOR_T_LENGTH_BIN];    
    if(iLength1!=iLength2) {
        return NAN+I*NAN;}
    pac_type cDotProd=0+0*I;
    for(t_int i=0; i<iLength1; ++i){
        cDotProd+=(*ppcIn1)[i] * conjl((*ppcIn2)[i]);
    }
    return cDotProd;
}
pac_type_real wabor_t_length_of_vector_array(pac_type  ** const ppcIn){
    pac_type cReturn;
    if((*ppcIn)==NULL) {
        return NAN;}   
    cReturn = csqrtPAC(wabor_t_inner_product_array(ppcIn,ppcIn));
    if(DEBUG) if(cimagPAC(cReturn)) post("length_of_vector: Fehler in Berechnung.");
    return cReturn;
}
void wabor_t_prolong_array(pac_type  ** const ppcIn, t_int _iN, t_int iWhere, t_int iRandom){
    //Verlaengert Fenster um Nuller
            //fWhere=0: am Anfang, 1: in der Mitte, 2: am Ende
    //Ist fRandom gesetzt werden zufaellig viele Nuller zwischen Null und fN geschriben
    if(_iN<=0) return;
    if((*ppcIn)==NULL) return;
    t_int iLength=(*ppcIn)[WABOR_T_LENGTH_BIN];  
    pac_type placeholder=(*ppcIn)[WABOR_T_PLACE_HOLDER];  
    t_int iInsertBin= (iWhere==0 ? iLength/2 : iLength);
    t_int iN=(iRandom == 1 ? rand()%_iN+1 : _iN);
    
    pac_type *out = wabor_t_malloc0((iLength+iN) * sizeof *out); 
    if(iWhere==PROLONG_END) { //am Ende
        memcpy(out, *ppcIn, iLength * sizeof *out);
        memset(out+iLength,0,iN * sizeof *out);
    } else if (iWhere==PROLONG_MIDDLE) { // in der Mitte
        memcpy(out, *ppcIn, iInsertBin * sizeof *out);
        memset(out+iInsertBin,0,iN * sizeof *out);
        memcpy(out+iInsertBin+iN, *ppcIn+iInsertBin,(iLength-iInsertBin) * sizeof *out);
    } else if (iWhere==PROLONG_START) {
        memset(out, 0, iN * sizeof *out);
        memcpy(out+iN, *ppcIn, iLength * sizeof *out);
    } else {
        post("wabor_t_prolong_array: Falscher Aufruf: fWhere=0: am Anfang, 1: in der Mitte, 2: am Ende");
    }
    wabor_t_free(*ppcIn);
    *ppcIn=out; //Ich verwende gleiche out als Array der Rueckgabe
    
    (*ppcIn)[WABOR_T_LENGTH_BIN] = iLength+iN + 0*I;
    (*ppcIn)[WABOR_T_PLACE_HOLDER] = placeholder;
    for(t_int i=1;i<=4;++i){ 
        (*ppcIn)[-i]=0+0*I;(*ppcIn)[iLength+iN+i-1]=0+0*I; } //Anfangs und End Nullen setzen 
    return;
}
pac_type wabor_t_abs_sum_coarse_array(pac_type ** const ppcIn){
    if((*ppcIn)==NULL) return NAN;
    t_int iLength=(*ppcIn)[WABOR_T_LENGTH_BIN];  
    pac_type_real sum=0;
    for(t_int i=0; i<iLength; ++i){
        sum+=cabsPAC((*ppcIn)[i]);
    }
    return sum;
}
pac_type wabor_t_abs_mu_coarse_array(pac_type  ** const ppcIn){
    if((*ppcIn)==NULL) return NAN;
    pac_type_real sum=wabor_t_abs_sum_coarse_array(ppcIn);
    if(sum==0) {return INFINITY; }
    t_int iLength=(*ppcIn)[WABOR_T_LENGTH_BIN];  
    pac_type_real mu=0;
    for(t_int i=0; i<iLength; ++i){
        mu+=cabsPAC((*ppcIn)[i])*(i+1);
    }
    return mu/sum -1;
}
pac_type wabor_t_abs_sigma_coarse_array(pac_type  ** const ppcIn){
    if((*ppcIn)==NULL) return NAN;
    pac_type_real sum=wabor_t_abs_sum_coarse_array(ppcIn);
    if(sum==0) {return INFINITY; }
    pac_type_real mu=wabor_t_abs_mu_coarse_array(ppcIn);
    t_int iLength=(*ppcIn)[WABOR_T_LENGTH_BIN];  
    pac_type_real sigma=0;
    {
        pac_type_real j=0;
        t_int i=0;
        for( ; i<iLength; ++i, ++j){
            sigma+=cabsPAC((*ppcIn)[i])*(j-mu)*(j-mu);
        }
    }
    return sqrtPAC(sigma/sum);
}


void wabor_t_array_synopsis_array(pac_type  ** const ppcIn){
    t_int iLength;
    t_int iIsNan=0;
    t_int iIsInf=0;
    pac_type_real ldRealMax=0, ldImagMax=0;
    pac_type_real ldRealMin, ldImagMin;
    pac_type cMaxdiff=0L+0L*I;
    pac_type cdiff=0;
    post("wabor_t_array_synopsis:");
    if((*ppcIn)==NULL) {
        post("Leeres Array");
    } 
    else {
        iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
        post("Laenge: %i",iLength);
        post("Adresse: %p",*ppcIn);
        ldRealMin=crealPAC((*ppcIn)[0]);
        ldImagMin=cimagPAC((*ppcIn)[0]);
        for( t_int i = 0; i<iLength; ++i){
            if(crealPAC((*ppcIn)[i])<ldRealMin)
                ldRealMin=crealPAC((*ppcIn)[i]);
            if(crealPAC((*ppcIn)[i])>ldRealMax)
                ldRealMax=crealPAC((*ppcIn)[i]);
            if(cimagPAC((*ppcIn)[i])<ldImagMin) 
                ldImagMin=cimagPAC((*ppcIn)[i]);
            if(cimagPAC((*ppcIn)[i])>ldImagMax)
                ldImagMax=cimagPAC((*ppcIn)[i]);
        }        
        for( t_int i=1; i<=iLength; ++i){
            cdiff=(*ppcIn)[i]-(*ppcIn)[i-1];
            if(cabsPAC(cMaxdiff)<cabsPAC(cdiff)) cMaxdiff=cdiff;
        }
        
        for( t_int i=0;i<iLength; ++i) { 
            if(isnan(crealPAC((*ppcIn)[i]))) ++iIsNan; 
            if(isnan(cimagPAC((*ppcIn)[i]))) ++iIsNan; 
            if(isinf(crealPAC((*ppcIn)[i]))) ++iIsInf; 
            if(isinf(cimagPAC((*ppcIn)[i]))) ++iIsInf; 
        }
        
        post("Real Min: %f, Max: %f",(double)ldRealMin, (double)ldRealMax);
        post("Imag Min: %f, Max: %f",(double)ldImagMin, (double)ldImagMax);
        if(ldImagMax) {
            post("Max diff: %f + I* %f",(double)crealPAC(cMaxdiff),(double)cimagPAC(cMaxdiff)); 
        } else {
            post("Max diff: %f ",(double)crealPAC(cMaxdiff)); 
        }
        if(iIsNan) post("Anzahl Nan: %i",iIsNan);
        if(iIsInf) post("Anzahl Inf: %i",iIsInf);
        
        //Anfangs und Endnullen Ueberpruefen
        t_int iZeros=0;
        for(t_int i=1;i<=4;++i){ 
            if( crealPAC((*ppcIn)[-i])!=0 || cimagPAC((*ppcIn)[-i])!=0) ++iZeros;
            if( crealPAC((*ppcIn)[iLength+i-1])!=0 || cimagPAC((*ppcIn)[iLength+i-1])!=0) ++iZeros;
        }
        if(iZeros) {
            post("Anfangs und Endnullen falsch:");
            post("(%f,%f)(%f,%f)(%f,%f)(%f,%f)[daten](%f,%f)(%f,%f)(%f,%f)(%f,%f)",
                    (double)crealPAC((*ppcIn)[-1]),       (double)cimagPAC((*ppcIn)[-1]),
                    (double)crealPAC((*ppcIn)[-2]),       (double)cimagPAC((*ppcIn)[-2]),
                    (double)crealPAC((*ppcIn)[-3]),       (double)cimagPAC((*ppcIn)[-3]),
                    (double)crealPAC((*ppcIn)[-4]),       (double)cimagPAC((*ppcIn)[-4]),
                    (double)crealPAC((*ppcIn)[iLength+0]),(double)cimagPAC((*ppcIn)[iLength+0]),
                    (double)crealPAC((*ppcIn)[iLength+1]),(double)cimagPAC((*ppcIn)[iLength+1]),
                    (double)crealPAC((*ppcIn)[iLength+2]),(double)cimagPAC((*ppcIn)[iLength+2]),
                    (double)crealPAC((*ppcIn)[iLength+3]),(double)cimagPAC((*ppcIn)[iLength+3])
                );
        }                
    }
    post("-----------------------------------------------");
    return;
}

pac_type wabor_t_maximum_array(pac_type   *  * const ppcIn){
    t_int iLength;
    pac_type cMax=0;
    pac_type cTemp=0;
    if((*ppcIn)==NULL) return 0;
    iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    for( t_int i = 0; i<iLength; ++i){
        cTemp=cabsPAC((*ppcIn)[i]);
        if(cabsPAC(cTemp)>cabsPAC(cMax)) cMax=cTemp;
    }
    return cMax;
}

t_int wabor_t_get_first_small_value_array(pac_type   *  * const ppcIn, pac_type_real ldSmallVal){
    t_int iLength;
    t_int i=0;
    if((*ppcIn)==NULL) return 0;
    iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    while( i < iLength && cabsPAC((*ppcIn)[i])<ldSmallVal )  { ++i; }    
    //while( i < iLength && !((*ppcIn)[i]) ) { ++i; }
    return i;
}
t_int wabor_t_get_last_small_value_array(pac_type   *  * const ppcIn, pac_type_real ldSmallVal){
    //sucht von rechts nach links, iStartFrom
    t_int iLength;
    t_int i;
    if((*ppcIn)==NULL) return 0;
    iLength=(t_int)crealPAC((*ppcIn)[WABOR_T_LENGTH_BIN]);
    i=iLength-1;
    while( i >= 0 && cabsPAC((*ppcIn)[i])<ldSmallVal ) { --i; }
    return i;
}


