Ejemplo n.º 1
0
int main(int argc, char *argv[]) {
  char c;
  int opt_idx;
  Vector *newfreqs = vec_new(4);
  TreeModel *mod;

  struct option long_opts[] = {
    {"help", 0, 0, 'h'},
    {0, 0, 0, 0}
  };

  while ((c = getopt_long(argc, argv, "h", long_opts, &opt_idx)) != -1) {
    switch (c) {
    case 'h':
      printf("%s", HELP);
      exit(0);
    case '?':
      die("Bad argument.  Try 'modFreqs -h'.\n");
    }
  }

  if (optind != argc - 2 && optind != argc - 5) 
    die("ERROR: Wrong number of arguments.  Try 'modFreqs -h'.\n");

  set_seed(-1);

  mod = tm_new_from_file(phast_fopen(argv[optind], "r"), 1);

  if (!tm_is_reversible(mod)) 
    die("ERROR: reversible input model required.\n");
  if (mod->order != 0)
    die("ERROR: single nucleotide model required.\n");
  if (strcmp(mod->rate_matrix->states, DEFAULT_ALPHABET) != 0)
    die("ERROR: default nucleotide alphabet required.\n");

  if (optind == argc - 2) {
    double gc = get_arg_dbl_bounds(argv[optind+1], 0, 1);
    vec_set(newfreqs, 0, (1-gc)/2);
    vec_set(newfreqs, 1, gc/2);
    vec_set(newfreqs, 2, gc/2);
    vec_set(newfreqs, 3, (1-gc)/2);
  }
  else {
    vec_set(newfreqs, 0, get_arg_dbl_bounds(argv[optind+1], 0, 1));
    vec_set(newfreqs, 1, get_arg_dbl_bounds(argv[optind+2], 0, 1));
    vec_set(newfreqs, 2, get_arg_dbl_bounds(argv[optind+3], 0, 1));
    vec_set(newfreqs, 3, get_arg_dbl_bounds(argv[optind+4], 0, 1));
    pv_normalize(newfreqs);
  }

  tm_mod_freqs(mod, newfreqs);

  tm_print(stdout, mod);

  return 0;
}
Ejemplo n.º 2
0
Int inv_short_complex_rot(
    Int32 *Data_in,
    Int32 *Data_out,
    Int32  max)

{
    Int     i;
    Int16     I;
    const   Int16 *pTable;
    const   Int32 *p_rotate;

    Int32   *pData_in_1;
    Int     exp;
    Int32   temp_re;
    Int32   temp_im;

    Int32   exp_jw;
    Int16   *pData_re;
    Int16   *pData_im;
    Int32   *pData_in_ref;

    Int16   temp_re_0;
    Int16   temp_im_0;
    Int16   temp_re_1;
    Int16   temp_im_1;
    Int16   *p_data_1;
    Int16   *p_data_2;
    Int16   *p_Data_Int_precision;
    Int16   *p_Data_Int_precision_1;
    Int16   *p_Data_Int_precision_2;

    Int     n     = 256;
    Int     n_2   = n >> 1;
    Int     n_4   = n >> 2;
    Int     n_8   = n >> 3;
    Int     n_3_4 = n_2 + n_4;


    p_data_1 = (Int16 *)Data_out;
    p_data_1 += n;
    pData_re  = p_data_1;
    pData_im  = p_data_1 + n_4;


    p_rotate  =  exp_rotation_N_256;
    pTable    =  digit_reverse_64;

    pData_in_ref  =  Data_in;

    exp = 16 - pv_normalize(max);


    if (exp < 0)
    {
        exp = 0;
    }

    exp -= 1;

    for (i = INV_SHORT_CX_ROT_LENGTH; i != 0; i--)
    {

        /*
         * cos_n + j*sin_n == exp(j(2pi/N)(k+1/8))
         */

        /*
         *   Perform digit reversal by accessing index I from table
         */

        I = *pTable++;
        pData_in_1 = pData_in_ref + I;
        /*
         *  Use auxiliary variables to avoid double accesses to memory.
         *  Data in is scaled to use only lower 16 bits.
         */

        temp_im =  *(pData_in_1++);
        temp_re =  *(pData_in_1);

        exp_jw = *p_rotate++;

        /*
         *   Post-rotation
         */

        *(pData_re++)  = (Int16)(cmplx_mul32_by_16(temp_re, -temp_im, exp_jw) >> exp);
        *(pData_im++)  = (Int16)(cmplx_mul32_by_16(temp_im,  temp_re, exp_jw) >> exp);
    }


    p_data_2 = pData_im -  1;


    p_Data_Int_precision = (Int16 *)Data_out;
    p_Data_Int_precision_1 = p_Data_Int_precision + n_3_4 - 1;
    p_Data_Int_precision_2 = p_Data_Int_precision + n_3_4;

    for (i = n_8 >> 1; i != 0; i--)
    {
        temp_re_0 = (*(p_data_1++));
        temp_re_1 = (*(p_data_1++));
        temp_im_0 = (*(p_data_2--));
        temp_im_1 = (*(p_data_2--));

        *(p_Data_Int_precision_1--) =  temp_re_0;
        *(p_Data_Int_precision_1--) =  temp_im_0;
        *(p_Data_Int_precision_1--) =  temp_re_1;
        *(p_Data_Int_precision_1--) =  temp_im_1;

        *(p_Data_Int_precision_2++) =  temp_re_0;
        *(p_Data_Int_precision_2++) =  temp_im_0;
        *(p_Data_Int_precision_2++) =  temp_re_1;
        *(p_Data_Int_precision_2++) =  temp_im_1;

    }


    /*
     *  loop is split to avoid conditional testing inside loop
     */

    p_Data_Int_precision_2 = p_Data_Int_precision;

    for (i = n_8 >> 1; i != 0; i--)
    {

        temp_re_0 = (*(p_data_1++));
        temp_re_1 = (*(p_data_1++));
        temp_im_0 = (*(p_data_2--));
        temp_im_1 = (*(p_data_2--));

        *(p_Data_Int_precision_1--) =   temp_re_0;
        *(p_Data_Int_precision_1--) =   temp_im_0;
        *(p_Data_Int_precision_1--) =   temp_re_1;
        *(p_Data_Int_precision_1--) =   temp_im_1;

        *(p_Data_Int_precision_2++) = (Int16)(-temp_re_0);
        *(p_Data_Int_precision_2++) = (Int16)(-temp_im_0);
        *(p_Data_Int_precision_2++) = (Int16)(-temp_re_1);
        *(p_Data_Int_precision_2++) = (Int16)(-temp_im_1);

    }

    return (exp + 1);
}
Int fwd_long_complex_rot(
    Int32 *Data_in,
    Int32 *Data_out,
    Int32  max)
{
    Int     i;
    const   Int32 *p_rotate;
    Int32   temp_re;
    Int32   temp_im;
    Int32   *pData_in_ref1;
    Int32   *pData_in_ref2;
    Int32   exp_jw;
    Int32   temp_re_32;
    Int32   temp_im_32;

    Int32   *pData_out_1;
    Int32   *pData_out_2;
    Int32   *pData_out_3;
    Int32   *pData_out_4;

    Int32 *pData_in_1;
    Int32 *pData_in_2;

    Int     exp;

    p_rotate       =  exp_rotation_N_2048;

    pData_in_ref1  =  Data_in;
    pData_in_ref2  = &Data_in[TWICE_FWD_LONG_CX_ROT_LENGTH];

    pData_out_1 = Data_out;
    pData_out_2 = &Data_out[LONG_WINDOW_LENGTH_m_1];
    pData_out_3 = &Data_out[LONG_WINDOW_LENGTH];
    pData_out_4 = &Data_out[TWICE_LONG_WINDOW_LENGTH_m_1];

    /*
     *  Data_out
     *                                   >>>>                   <<<<
     *                                pData_out_3             pData_out_4
     *      |             |             |             |             |
     * pData_out_1               pData_out_2
     *      >>>>                     <<<<
     */


    exp = 16 - pv_normalize(max);

    if (exp < 0)
    {
        exp = 0;
    }

    /*
     *  Apply  A/2^(diff) + B
     */


    pData_in_1 = pData_in_ref1;
    pData_in_2 = pData_in_ref2;

    for (i = FWD_LONG_CX_ROT_LENGTH; i != 0; i--)
    {

        /*
         * cos_n + j*sin_n == exp(j(2pi/N)(k+1/8))
         */

        exp_jw = *p_rotate++;

        /*
         *  Use auxiliary variables to avoid double accesses to memory.
         *  Data in is scaled to use only lower 16 bits.
         */

        temp_re =  *(pData_in_1++) >> exp;
        temp_im =  *(pData_in_1++) >> exp;

        /*
         *   Pre-rotation
         */

        temp_re_32  = (cmplx_mul32_by_16(temp_re,   temp_im,  exp_jw));
        temp_im_32  = (cmplx_mul32_by_16(temp_im,  -temp_re,  exp_jw));

        *(pData_out_1++) = - temp_re_32;
        *(pData_out_2--) =   temp_im_32;
        *(pData_out_3++) = - temp_im_32;
        *(pData_out_4--) =   temp_re_32;

        /*
         *   Pointer increment to jump over imag (1 & 4) or real parts
         *   (2 & 3)
         */
        pData_out_1++;
        pData_out_2--;
        pData_out_3++;
        pData_out_4--;

        /*
         *   Repeat procedure for odd index at the output
         */

        exp_jw = *p_rotate++;

        temp_re =  *(pData_in_2++) >> exp;
        temp_im =  *(pData_in_2++) >> exp;

        temp_re_32  = (cmplx_mul32_by_16(temp_re,   temp_im,  exp_jw));
        temp_im_32  = (cmplx_mul32_by_16(temp_im,  -temp_re,  exp_jw));

        *(pData_out_1++) = - temp_re_32;
        *(pData_out_2--) =   temp_im_32;
        *(pData_out_3++) = - temp_im_32;
        *(pData_out_4--) =   temp_re_32;

        pData_out_1++;
        pData_out_2--;
        pData_out_3++;
        pData_out_4--;

    }

    return (exp + 1);
}
Ejemplo n.º 4
0
Int huffspec_fxp(
    FrameInfo *pFrameInfo,
    BITS      *pInputStream,
    Int       nsect,
    SectInfo  *pSectInfo,
    Int       factors[],
    Int32     coef[],
    Int16     quantSpec[],
    Int16     tmp_spec[],
    const FrameInfo  *pLongFrameInfo,
    PulseInfo  *pPulseInfo,
    Int         qFormat[])
{
    /*----------------------------------------------------------------------------
    ; Define all local variables
    ----------------------------------------------------------------------------*/
    const Hcb       *pHcb;
    Int     i;
    Int     sfb;
    Int     idx_count;
    Int     sect_cb;    /* section codebook */
    Int     dim;
    Int     idx;
    Int     stop_idx;     /* index of 1st coef in next sfb */
    Int     sect_start;   /* start index of sfb in one section*/
    Int     sect_end;     /* index of 1st sfb in next section */
    Int     *pSfbStart;
    Int     *pSfb;
    Int16     *pQuantSpec;        /* probably could be short */
    Int     max = 0;
    /* rescaling parameters */
    Int     nsfb;
    Int     tot_sfb;
    Int     fac;

    Int32   *pCoef; /* ptr to coef[], inverse quantized coefs */
    UInt16     scale;

    Int     power_scale_div_4;
    Int     sfbWidth;

    void (*pUnpack_idx)(
        Int16  quant_spec[],
        Int  codeword_indx,
        const Hcb *pHuffCodebook,
        BITS  *pInputStream,
        Int *max);

    Int(*pDec_huff_tab)(BITS *) = NULL;

    UInt32 temp;
    Int    binaryDigits, QFormat;

    /*----------------------------------------------------------------------------
    ; Function body here
    ----------------------------------------------------------------------------*/

    sect_start = 0;
    stop_idx   = 0;

    /* pSfb: ptr to array that holds stop index of each sfb */
    pSfbStart = pFrameInfo->frame_sfb_top;
// 2010.01.11 :
// nsect can be 0
    if (pSfbStart == NULL)
    {
        return (-1);   /*  error condition */
    }

    pSfb      = pSfbStart;

    /* decoding spectral values section by section */
    for (i = nsect; i > 0; i--)
    {
        /* read the codebook and section length */
        sect_cb  =  pSectInfo->sect_cb;     /* codebook */
        if ((sect_cb > 15) || (sect_cb < 0))
        {
            return (-1);   /*  error condition */
        }
        sect_end =  pSectInfo->sect_end;    /* # of sfbs */

        if (sect_end < 0)
        {
            return (-1);   /*  error condition */
        }

        pSectInfo++;

        /*  sect_cb       sect_cb - 1
         *  ZERO_HCB        1111b
         *    1             0000b
         *    2             0001b
         *    3             0010b
         *    4             0011b
         *    5             0100b
         *    6             0101b
         *    7             0110b
         *    8             0111b
         *    9             1000b
         *    10            1001b
         *    11            1010b
         *    12            1011b
         * NOISE_HCB        1100b
         * INTENSITY_HCB2   1101b
         * INTENSITY_HCB    1110b
         * if ( ((sect_cb - 1) & 0xC) == 0xC ) is identical to
         * if !((sect_cb == ZERO_HCB) || (sect_cb == NOISE_HCB) ||
         *      (sec_cb == INTENSITY_HCB) || (sect_cb==INTENSITY_HCB2) )
         * use this compare scheme to speed up the execution
         */

        if (((sect_cb - 1) & 0xC) != 0xC)
        {
            /* decode spec in one section */
            if (sect_cb > BY4BOOKS)
            {
                dim = DIMENSION_2; /* set codebook dimension */
            }
            else
            {
                dim = DIMENSION_4;
            }

            pHcb        = &hcbbook_binary[sect_cb];

            if (sect_cb == ESCBOOK)
            {
                pUnpack_idx = &unpack_idx_esc;
            }
            else if (pHcb->signed_cb == FALSE)
            {
                pUnpack_idx = &unpack_idx_sgn;
            }
            else
            {
                pUnpack_idx = &unpack_idx;
            }


            switch (sect_cb)
            {
                case 1:
                    pDec_huff_tab = decode_huff_cw_tab1;
                    break;
                case 2:
                    pDec_huff_tab = decode_huff_cw_tab2;
                    break;
                case 3:
                    pDec_huff_tab = decode_huff_cw_tab3;
                    break;
                case 4:
                    pDec_huff_tab = decode_huff_cw_tab4;
                    break;
                case 5:
                    pDec_huff_tab = decode_huff_cw_tab5;
                    break;
                case 6:
                    pDec_huff_tab = decode_huff_cw_tab6;
                    break;
                case 7:
                    pDec_huff_tab = decode_huff_cw_tab7;
                    break;
                case 8:
                    pDec_huff_tab = decode_huff_cw_tab8;
                    break;
                case 9:
                    pDec_huff_tab = decode_huff_cw_tab9;
                    break;
                case 10:
                    pDec_huff_tab = decode_huff_cw_tab10;
                    break;
                case 11:
                    pDec_huff_tab = decode_huff_cw_tab11;
                    break;
                default:
                    return (-1); /* error condition */
            }

            /* move ptr to first sfb of current section */
            pQuantSpec  = quantSpec + stop_idx;

            /* step through all sfbs in current section */
            for (sfb = sect_start; sfb < sect_end; sfb++)
            {
                idx_count = *pSfb - stop_idx;
                stop_idx  = *pSfb++;

                /* decode all coefs for one sfb */
                while ((idx_count > 0) && (idx_count < 1024))
                {

                    idx = (*pDec_huff_tab)(pInputStream);

                    (*pUnpack_idx)(pQuantSpec,
                                   idx,
                                   pHcb,
                                   pInputStream,
                                   &max);      /* unpack idx -> coefs */

                    pQuantSpec += dim;
                    idx_count  -= dim;

                } /* while(idx_count) */

            } /* for (sfb=sect_start) */
        }
        else
        {

            /* current section uses ZERO_HCB, NOISE_HCB, etc */

            /* move sfb pointer to the start sfb of next section */
            pSfb        = pSfbStart + sect_end;
            /* number of coefs in current section */
            idx_count   = *(pSfb - 1) - stop_idx;

            if ((idx_count > 1024) || (idx_count < 0))
            {
                return (-1);   /*  error condition */
            }

            /*
             * This memset is necessary in terms of (1) net savings in total
             * MIPS and (2) accurate Q-Formats for fft_rx2
             * In case a scalefactor band uses ZERO_HCB, all coefficients of
             * that sfb should be zeros. Without this call to memset, the
             * coefficients for a ZERO_HCB sfb are the "leftovers" of the
             * previous frame, which may not have all zero values. This leads
             * to a drastical increase in the cycles consumed by esc_iquant_fxp
             * and fft_rx2, which is the most "expensive" function of the
             * library.
             * This memset also guarantees the Q_Format for sfbs with all zero
             * coefficients will be set properly.
             * Profiling data on ARM and TMS320C55x proves that there is a net
             * gain in total MIPS if a memset is called here.
             */
            pv_memset(&quantSpec[stop_idx],
                      0,
                      idx_count * sizeof(quantSpec[0]));

            /*
             * This memset is called because pQuantSpec points to tmp_spec
             * after deinterleaving
             */

            pv_memset(&tmp_spec[stop_idx],
                      0,
                      idx_count * sizeof(tmp_spec[0]));


            /* stop_idx is the index of the 1st coef of next section */
            stop_idx    = *(pSfb - 1);

        }/* if (sect_cb) */

        sect_start = sect_end;

    } /* for (i=nsect) */

    /* noisless coding reconstruction */
    if (pFrameInfo->islong != FALSE)
    {
        if (pPulseInfo->pulse_data_present == 1)
        {
            pulse_nc(quantSpec,
                     pPulseInfo,
                     pLongFrameInfo,
                     &max);    /* add pulse data */
        }

        pQuantSpec = quantSpec;

    }
    else
    {
        deinterleave(quantSpec,
                     tmp_spec,
                     pFrameInfo);

        pQuantSpec = tmp_spec;
    }


    /* inverse quantization, Q_format: Int32 */
    /* rescaling */

    /* what we can do here is assuming that we already know maxInput for each band, we have to go
    though each one of them for re-quant and scaling, and pick the right qFormat to apply to
    all spectral coeffs.*/

    if ((max < 0) || (max > 8192))    /* (8192>>ORDER) == 1024 is the inverseQuantTable size */
    {
        return (-1);   /*  error condition */
    }
    else
    {
        /* Get  (max/SPACING) ^ (1/3), in Q Format  */
        temp = inverseQuantTable[(max >> ORDER) + 1];
    }


    /* Round up before shifting down to Q0 */
    temp += ROUND_UP;

    /* shift down to Q0 and multiply by 2 (FACTOR) in one step */
    temp >>= (QTABLE - 1);

    /* Now get max ^ (4/3) in Q0 */
    temp *= max;


    binaryDigits = 31 - pv_normalize(temp);


    /* Prevent negative shifts caused by low maximums. */
    if (binaryDigits < (SIGNED32BITS - QTABLE))
    {
        binaryDigits = SIGNED32BITS - QTABLE;
    }

    QFormat = SIGNED32BITS - binaryDigits;

    /********************/
    tot_sfb = 0;
    nsfb = pFrameInfo->sfb_per_win[0];
    pCoef = coef;

    for (i = pFrameInfo->num_win; i > 0; i--)
    {
        stop_idx  = 0;

        for (sfb = 0; sfb < nsfb; sfb++)
        {
            sfbWidth   =  pFrameInfo->win_sfb_top[0][sfb] - stop_idx;

            if ((sfbWidth < 0) || (sfbWidth > 1024))
            {
                return (-1);   /*  error condition */
            }

            stop_idx  += sfbWidth;

            fac   = factors[tot_sfb] - SF_OFFSET;
            scale = exptable[fac & 0x3];

            power_scale_div_4 = fac >> 2;

            power_scale_div_4++;

            qFormat[tot_sfb] = QFormat;

            esc_iquant_scaling(pQuantSpec,
                               pCoef,
                               sfbWidth,
                               QFormat,
                               scale,
                               max);

            pQuantSpec += sfbWidth;
            qFormat[tot_sfb] -= power_scale_div_4;
            pCoef += sfbWidth;

            tot_sfb++;

        } /* for (sfb) */
    } /* for (i) */


    /*----------------------------------------------------------------------------
    ; Return status
    ----------------------------------------------------------------------------*/
    return SUCCESS;

} /* huffspec_fxp */
Ejemplo n.º 5
0
void calc_auto_corr_LC(struct ACORR_COEFS *ac,
                       Int32  realBuf[][32],
                       Int32  bd,
                       Int32  len)
{
    Int32 j;
    Int32 temp1;
    Int32 temp3;
    Int32 temp5;

    int64 temp_r01r;
    int64 temp_r02r;
    int64 temp_r11r;
    int64 temp_r12r;
    int64 temp_r22r;
    int64 max = 0;


    temp1 = (realBuf[ 0][bd]) >> N;
    temp3 = (realBuf[-1][bd]) >> N;
    temp5 = (realBuf[-2][bd]) >> N;


    temp_r11r = fxp_mac64_Q31(0, temp3, temp3);   /* [j-1]*[j-1]  */
    temp_r12r = fxp_mac64_Q31(0, temp3, temp5);   /* [j-1]*[j-2]  */
    temp_r22r = fxp_mac64_Q31(0, temp5, temp5);   /* [j-2]*[j-2]  */

    temp_r01r = 0;
    temp_r02r = 0;

    for (j = 1; j < len; j++)
    {
        temp_r01r = fxp_mac64_Q31(temp_r01r, temp1, temp3);    /* [j  ]*[j-1]  */
        temp_r02r = fxp_mac64_Q31(temp_r02r, temp1, temp5);    /* [j  ]*[j-2]  */
        temp_r11r = fxp_mac64_Q31(temp_r11r, temp1, temp1);    /* [j-1]*[j-1]  */

        temp5 = temp3;
        temp3 = temp1;
        temp1 = (realBuf[j][bd]) >> N;
    }


    temp_r22r += temp_r11r;
    temp_r12r += temp_r01r;          /* [j-1]*[j-2]  */

    temp_r22r  = fxp_mac64_Q31(temp_r22r, -temp3, temp3);

    temp_r01r = fxp_mac64_Q31(temp_r01r, temp1, temp3);
    temp_r02r = fxp_mac64_Q31(temp_r02r, temp1, temp5);

    max  |= temp_r01r ^(temp_r01r >> 63);
    max  |= temp_r02r ^(temp_r02r >> 63);
    max  |= temp_r11r;
    max  |= temp_r12r ^(temp_r12r >> 63);
    max  |= temp_r22r;

    if (max)
    {
        temp1 = (UInt32)(max >> 32);
        if (temp1)
        {
            temp3 = 33 - pv_normalize(temp1);
            ac->r01r = (Int32)(temp_r01r >> temp3);
            ac->r02r = (Int32)(temp_r02r >> temp3);
            ac->r11r = (Int32)(temp_r11r >> temp3);
            ac->r12r = (Int32)(temp_r12r >> temp3);
            ac->r22r = (Int32)(temp_r22r >> temp3);

        }
        else
        {
            temp3 = pv_normalize(((UInt32)max) >> 1) - 2;

            if (temp3 > 0)
            {
                ac->r01r = (Int32)(temp_r01r << temp3);
                ac->r02r = (Int32)(temp_r02r << temp3);
                ac->r11r = (Int32)(temp_r11r << temp3);
                ac->r12r = (Int32)(temp_r12r << temp3);
                ac->r22r = (Int32)(temp_r22r << temp3);
            }
            else
            {
                temp3 = -temp3;
                ac->r01r = (Int32)(temp_r01r >> temp3);
                ac->r02r = (Int32)(temp_r02r >> temp3);
                ac->r11r = (Int32)(temp_r11r >> temp3);
                ac->r12r = (Int32)(temp_r12r >> temp3);
                ac->r22r = (Int32)(temp_r22r >> temp3);
            }

        }

        /*
         *  ac->det = ac->r11r*ac->r22r - rel*(ac->r12r*ac->r12r);
         */
        /* 1/(1 + 1e-6) == 1 - 1e-6 */
        /* 2^-20 == 1e-6 */
        ac->det  = fxp_mul32_Q30(ac->r12r, ac->r12r);

        ac->det -= ac->det >> 20;

        ac->det  = fxp_mul32_Q30(ac->r11r, ac->r22r) - ac->det;
    }