Example #1
0
static void kf_bfly2(
    kiss_fft_cpx * Fout,
    const size_t fstride,
    const kiss_fft_cfg st,
    int m,
    int N,
    int mm
)
{
    kiss_fft_cpx * Fout2;
    kiss_fft_cpx * tw1;
    kiss_fft_cpx t;
    if (!st->inverse) {
        int i,j;
        kiss_fft_cpx * Fout_beg = Fout;
        for (i=0; i<N; i++)
        {
            Fout = Fout_beg + i*mm;
            Fout2 = Fout + m;
            tw1 = st->twiddles;
            for(j=0; j<m; j++)
            {
                /* Almost the same as the code path below, except that we divide the input by two
                 (while keeping the best accuracy possible) */
                ms_word32_t tr, ti;
                tr = SHR32(SUB32(MULT16_16(Fout2->r , tw1->r),MULT16_16(Fout2->i , tw1->i)), 1);
                ti = SHR32(ADD32(MULT16_16(Fout2->i , tw1->r),MULT16_16(Fout2->r , tw1->i)), 1);
                tw1 += fstride;
                Fout2->r = PSHR32(SUB32(SHL32(EXTEND32(Fout->r), 14), tr), 15);
                Fout2->i = PSHR32(SUB32(SHL32(EXTEND32(Fout->i), 14), ti), 15);
                Fout->r = PSHR32(ADD32(SHL32(EXTEND32(Fout->r), 14), tr), 15);
                Fout->i = PSHR32(ADD32(SHL32(EXTEND32(Fout->i), 14), ti), 15);
                ++Fout2;
                ++Fout;
            }
        }
    } else {
        int i,j;
        kiss_fft_cpx * Fout_beg = Fout;
        for (i=0; i<N; i++)
        {
            Fout = Fout_beg + i*mm;
            Fout2 = Fout + m;
            tw1 = st->twiddles;
            for(j=0; j<m; j++)
            {
                C_MUL (t,  *Fout2 , *tw1);
                tw1 += fstride;
                C_SUB( *Fout2 ,  *Fout , t );
                C_ADDTO( *Fout ,  t );
                ++Fout2;
                ++Fout;
            }
        }
    }
}
Example #2
0
/*
 *
 * User-callable function to allocate all necessary storage space for the fft.
 *
 * The return value is a contiguous block of memory, allocated with malloc.  As such,
 * It can be freed with free(), rather than a kiss_fft-specific function.
 * */
kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem )
{
    kiss_fft_cfg st=NULL;
    size_t memneeded = sizeof(struct kiss_fft_state)
                       + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/

    if ( lenmem==NULL ) {
        st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded );
    } else {
        if (mem != NULL && *lenmem >= memneeded)
            st = (kiss_fft_cfg)mem;
        *lenmem = memneeded;
    }
    if (st) {
        int i;
        st->nfft=nfft;
        st->inverse = inverse_fft;
#ifdef MS_FIXED_POINT
        for (i=0; i<nfft; ++i) {
            ms_word32_t phase = i;
            if (!st->inverse)
                phase = -phase;
            kf_cexp2(st->twiddles+i, DIV32(SHL32(phase,17),nfft));
        }
#else
        for (i=0; i<nfft; ++i) {
            const double pi=3.14159265358979323846264338327;
            double phase = ( -2*pi /nfft ) * i;
            if (st->inverse)
                phase *= -1;
            kf_cexp(st->twiddles+i, phase );
        }
#endif
        kf_factor(nfft,st->factors);
    }
    return st;
}
Example #3
0
File: ltp.c Project: e6/PhonoSDK
void pitch_unquant_3tap(
    spx_word16_t exc[],             /* Input excitation */
    spx_word32_t exc_out[],         /* Output excitation */
    int   start,                    /* Smallest pitch value allowed */
    int   end,                      /* Largest pitch value allowed */
    spx_word16_t pitch_coef,        /* Voicing (pitch) coefficient */
    const void *par,
    int   nsf,                      /* Number of samples in subframe */
    int *pitch_val,
    spx_word16_t *gain_val,
    SpeexBits *bits,
    char *stack,
    int count_lost,
    int subframe_offset,
    spx_word16_t last_pitch_gain,
    int cdbk_offset
)
{
    int i;
    int pitch;
    int gain_index;
    spx_word16_t gain[3];
    const signed char *gain_cdbk;
    int gain_cdbk_size;
    const ltp_params *params;

    params = (const ltp_params*) par;
    gain_cdbk_size = 1<<params->gain_bits;
    gain_cdbk = params->gain_cdbk + 4*gain_cdbk_size*cdbk_offset;

    pitch = speex_bits_unpack_unsigned(bits, params->pitch_bits);
    pitch += start;
    gain_index = speex_bits_unpack_unsigned(bits, params->gain_bits);
    /*printf ("decode pitch: %d %d\n", pitch, gain_index);*/
#ifdef FIXED_POINT
    gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4]);
    gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+1]);
    gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[gain_index*4+2]);
#else
    gain[0] = 0.015625*gain_cdbk[gain_index*4]+.5;
    gain[1] = 0.015625*gain_cdbk[gain_index*4+1]+.5;
    gain[2] = 0.015625*gain_cdbk[gain_index*4+2]+.5;
#endif

    if (count_lost && pitch > subframe_offset)
    {
        spx_word16_t gain_sum;
        if (1) {
#ifdef FIXED_POINT
            spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : SHR16(last_pitch_gain,1);
            if (tmp>62)
                tmp=62;
#else
            spx_word16_t tmp = count_lost < 4 ? last_pitch_gain : 0.5 * last_pitch_gain;
            if (tmp>.95)
                tmp=.95;
#endif
            gain_sum = gain_3tap_to_1tap(gain);

            if (gain_sum > tmp)
            {
                spx_word16_t fact = DIV32_16(SHL32(EXTEND32(tmp),14),gain_sum);
                for (i=0; i<3; i++)
                    gain[i]=MULT16_16_Q14(fact,gain[i]);
            }

        }

    }

    *pitch_val = pitch;
    gain_val[0]=gain[0];
    gain_val[1]=gain[1];
    gain_val[2]=gain[2];
    gain[0] = SHL16(gain[0],7);
    gain[1] = SHL16(gain[1],7);
    gain[2] = SHL16(gain[2],7);
    for (i=0; i<nsf; i++)
        exc_out[i]=0;
    for (i=0; i<3; i++)
    {
        int j;
        int tmp1, tmp3;
        int pp=pitch+1-i;
        tmp1=nsf;
        if (tmp1>pp)
            tmp1=pp;
        for (j=0; j<tmp1; j++)
            exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp]);
        tmp3=nsf;
        if (tmp3>pp+pitch)
            tmp3=pp+pitch;
        for (j=tmp1; j<tmp3; j++)
            exc_out[j]=MAC16_16(exc_out[j],gain[2-i],exc[j-pp-pitch]);
    }
    /*for (i=0;i<nsf;i++)
    exc[i]=PSHR32(exc32[i],13);*/
}
Example #4
0
File: ltp.c Project: e6/PhonoSDK
/** Finds the best quantized 3-tap pitch predictor by analysis by synthesis */
static spx_word32_t pitch_gain_search_3tap(
    const spx_word16_t target[],       /* Target vector */
    const spx_coef_t ak[],          /* LPCs for this subframe */
    const spx_coef_t awk1[],        /* Weighted LPCs #1 for this subframe */
    const spx_coef_t awk2[],        /* Weighted LPCs #2 for this subframe */
    spx_sig_t exc[],                /* Excitation */
    const signed char *gain_cdbk,
    int gain_cdbk_size,
    int   pitch,                    /* Pitch value */
    int   p,                        /* Number of LPC coeffs */
    int   nsf,                      /* Number of samples in subframe */
    SpeexBits *bits,
    char *stack,
    const spx_word16_t *exc2,
    const spx_word16_t *r,
    spx_word16_t *new_target,
    int  *cdbk_index,
    int plc_tuning,
    spx_word32_t cumul_gain
)
{
    int i,j;
    VARDECL(spx_word16_t *tmp1);
    VARDECL(spx_word16_t *e);
    spx_word16_t *x[3];
    spx_word32_t corr[3];
    spx_word32_t A[3][3];
    spx_word16_t gain[3];
    spx_word32_t err;
    spx_word16_t max_gain=128;
    int          best_cdbk=0;

    ALLOC(tmp1, 3*nsf, spx_word16_t);
    ALLOC(e, nsf, spx_word16_t);

    if (cumul_gain > 262144)
        max_gain = 31;

    x[0]=tmp1;
    x[1]=tmp1+nsf;
    x[2]=tmp1+2*nsf;

    {
        VARDECL(spx_mem_t *mm);
        int pp=pitch-1;
        ALLOC(mm, p, spx_mem_t);
        for (j=0; j<nsf; j++)
        {
            if (j-pp<0)
                e[j]=exc2[j-pp];
            else if (j-pp-pitch<0)
                e[j]=exc2[j-pp-pitch];
            else
                e[j]=0;
        }
        for (j=0; j<p; j++)
            mm[j] = 0;
        iir_mem16(e, ak, e, nsf, p, mm, stack);
        for (j=0; j<p; j++)
            mm[j] = 0;
        filter_mem16(e, awk1, awk2, e, nsf, p, mm, stack);
        for (j=0; j<nsf; j++)
            x[2][j] = e[j];
    }
    for (i=1; i>=0; i--)
    {
        spx_word16_t e0=exc2[-pitch-1+i];
        x[i][0]=MULT16_16_Q14(r[0], e0);
        for (j=0; j<nsf-1; j++)
            x[i][j+1]=ADD32(x[i+1][j],MULT16_16_P14(r[j+1], e0));
    }

    for (i=0; i<3; i++)
        corr[i]=inner_prod(x[i],target,nsf);
    for (i=0; i<3; i++)
        for (j=0; j<=i; j++)
            A[i][j]=A[j][i]=inner_prod(x[i],x[j],nsf);

    {
        spx_word32_t C[9];
#ifdef FIXED_POINT
        spx_word16_t C16[9];
#else
        spx_word16_t *C16=C;
#endif
        C[0]=corr[2];
        C[1]=corr[1];
        C[2]=corr[0];
        C[3]=A[1][2];
        C[4]=A[0][1];
        C[5]=A[0][2];
        C[6]=A[2][2];
        C[7]=A[1][1];
        C[8]=A[0][0];

        /*plc_tuning *= 2;*/
        if (plc_tuning<2)
            plc_tuning=2;
        if (plc_tuning>30)
            plc_tuning=30;
#ifdef FIXED_POINT
        C[0] = SHL32(C[0],1);
        C[1] = SHL32(C[1],1);
        C[2] = SHL32(C[2],1);
        C[3] = SHL32(C[3],1);
        C[4] = SHL32(C[4],1);
        C[5] = SHL32(C[5],1);
        C[6] = MAC16_32_Q15(C[6],MULT16_16_16(plc_tuning,655),C[6]);
        C[7] = MAC16_32_Q15(C[7],MULT16_16_16(plc_tuning,655),C[7]);
        C[8] = MAC16_32_Q15(C[8],MULT16_16_16(plc_tuning,655),C[8]);
        normalize16(C, C16, 32767, 9);
#else
        C[6]*=.5*(1+.02*plc_tuning);
        C[7]*=.5*(1+.02*plc_tuning);
        C[8]*=.5*(1+.02*plc_tuning);
#endif

        best_cdbk = pitch_gain_search_3tap_vq(gain_cdbk, gain_cdbk_size, C16, max_gain);

#ifdef FIXED_POINT
        gain[0] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4]);
        gain[1] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+1]);
        gain[2] = ADD16(32,(spx_word16_t)gain_cdbk[best_cdbk*4+2]);
        /*printf ("%d %d %d %d\n",gain[0],gain[1],gain[2], best_cdbk);*/
#else
        gain[0] = 0.015625*gain_cdbk[best_cdbk*4]  + .5;
        gain[1] = 0.015625*gain_cdbk[best_cdbk*4+1]+ .5;
        gain[2] = 0.015625*gain_cdbk[best_cdbk*4+2]+ .5;
#endif
        *cdbk_index=best_cdbk;
    }

    for (i=0; i<nsf; i++)
        exc[i]=0;
    for (i=0; i<3; i++)
    {
        int j;
        int tmp1, tmp3;
        int pp=pitch+1-i;
        tmp1=nsf;
        if (tmp1>pp)
            tmp1=pp;
        for (j=0; j<tmp1; j++)
            exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp]);
        tmp3=nsf;
        if (tmp3>pp+pitch)
            tmp3=pp+pitch;
        for (j=tmp1; j<tmp3; j++)
            exc[j]=MAC16_16(exc[j],SHL16(gain[2-i],7),exc2[j-pp-pitch]);
    }
    for (i=0; i<nsf; i++)
    {
        spx_word32_t tmp = ADD32(ADD32(MULT16_16(gain[0],x[2][i]),MULT16_16(gain[1],x[1][i])),
                                 MULT16_16(gain[2],x[0][i]));
        new_target[i] = SUB16(target[i], EXTRACT16(PSHR32(tmp,6)));
    }
    err = inner_prod(new_target, new_target, nsf);

    return err;
}