Skip to content

Commit

Permalink
Corrected rescaling issue in the implementation of the MFCCs
Browse files Browse the repository at this point in the history
  • Loading branch information
christophe0606 committed Jul 19, 2023
1 parent b16db78 commit 60a8c88
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
23 changes: 11 additions & 12 deletions Source/ComplexMathFunctions/arm_cmplx_mag_q15.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void arm_cmplx_mag_q15(
acc0 = __SMUAD(in, in);

/* store result in 2.14 format in destination buffer. */
arm_sqrt_q31(acc0 >> 1 , &res);
arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
*pDst++ = res >> 16;


Expand Down Expand Up @@ -170,22 +170,22 @@ void arm_cmplx_mag_q15(
in = read_q15x2_ia (&pSrc);
acc0 = __SMUAD(in, in);
/* store result in 2.14 format in destination buffer. */
arm_sqrt_q31(acc0 >> 1 , &res);
arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
*pDst++ = res >> 16;

in = read_q15x2_ia (&pSrc);
acc0 = __SMUAD(in, in);
arm_sqrt_q31(acc0 >> 1 , &res);
arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
*pDst++ = res >> 16;

in = read_q15x2_ia (&pSrc);
acc0 = __SMUAD(in, in);
arm_sqrt_q31(acc0 >> 1 , &res);
arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
*pDst++ = res >> 16;

in = read_q15x2_ia (&pSrc);
acc0 = __SMUAD(in, in);
arm_sqrt_q31(acc0 >> 1 , &res);
arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
*pDst++ = res >> 16;
#else
real = *pSrc++;
Expand All @@ -194,28 +194,28 @@ void arm_cmplx_mag_q15(
acc1 = ((q31_t) imag * imag);

/* store result in 2.14 format in destination buffer. */
arm_sqrt_q31((acc0 + acc1) >> 1 , &res);
arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
*pDst++ = res >> 16;

real = *pSrc++;
imag = *pSrc++;
acc0 = ((q31_t) real * real);
acc1 = ((q31_t) imag * imag);
arm_sqrt_q31((acc0 + acc1) >> 1 , &res);
arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
*pDst++ = res >> 16;

real = *pSrc++;
imag = *pSrc++;
acc0 = ((q31_t) real * real);
acc1 = ((q31_t) imag * imag);
arm_sqrt_q31((acc0 + acc1) >> 1 , &res);
arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
*pDst++ = res >> 16;

real = *pSrc++;
imag = *pSrc++;
acc0 = ((q31_t) real * real);
acc1 = ((q31_t) imag * imag);
arm_sqrt_q31((acc0 + acc1) >> 1 , &res);
arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
*pDst++ = res >> 16;
#endif /* #if defined (ARM_MATH_DSP) */

Expand All @@ -240,9 +240,8 @@ void arm_cmplx_mag_q15(
#if defined (ARM_MATH_DSP)
in = read_q15x2_ia (&pSrc);
acc0 = __SMUAD(in, in);

/* store result in 2.14 format in destination buffer. */
arm_sqrt_q31(acc0 >> 1 , &res);
arm_sqrt_q31((uint32_t)acc0 >> 1 , &res);
*pDst++ = res >> 16;
#else
real = *pSrc++;
Expand All @@ -251,7 +250,7 @@ void arm_cmplx_mag_q15(
acc1 = ((q31_t) imag * imag);

/* store result in 2.14 format in destination buffer. */
arm_sqrt_q31((acc0 + acc1) >> 1 , &res);
arm_sqrt_q31(((uint32_t)acc0 + (uint32_t)acc1) >> 1 , &res);
*pDst++ = res >> 16;

#endif
Expand Down
9 changes: 8 additions & 1 deletion Source/TransformFunctions/arm_mfcc_f16.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ void arm_mfcc_f16(
/* Normalize */
arm_absmax_f16(pSrc,S->fftLen,&maxValue,&index);

arm_scale_f16(pSrc,1.0f16/(_Float16)maxValue,pSrc,S->fftLen);
if ((_Float16)maxValue != 0.0f16)
{
arm_scale_f16(pSrc,1.0f16/(_Float16)maxValue,pSrc,S->fftLen);
}

/* Multiply by window */
arm_mult_f16(pSrc,S->windowCoefs,pSrc,S->fftLen);
Expand Down Expand Up @@ -125,6 +128,10 @@ void arm_mfcc_f16(
pTmp[1]=0.0f;
#endif
arm_cmplx_mag_f16(pTmp,pSrc,S->fftLen);
if ((_Float16)maxValue != 0.0f16)
{
arm_scale_f16(pSrc,maxValue,pSrc,S->fftLen);
}

/* Apply MEL filters */
for(i=0; i<S->nbMelFilters; i++)
Expand Down
9 changes: 8 additions & 1 deletion Source/TransformFunctions/arm_mfcc_f32.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ void arm_mfcc_f32(
/* Normalize */
arm_absmax_f32(pSrc,S->fftLen,&maxValue,&index);

arm_scale_f32(pSrc,1.0f/maxValue,pSrc,S->fftLen);
if (maxValue != 0.0f)
{
arm_scale_f32(pSrc,1.0f/maxValue,pSrc,S->fftLen);
}

/* Multiply by window */
arm_mult_f32(pSrc,S->windowCoefs,pSrc,S->fftLen);
Expand Down Expand Up @@ -115,6 +118,10 @@ void arm_mfcc_f32(
pTmp[1]=0.0f;
#endif
arm_cmplx_mag_f32(pTmp,pSrc,S->fftLen);
if (maxValue != 0.0f)
{
arm_scale_f32(pSrc,maxValue,pSrc,S->fftLen);
}

/* Apply MEL filters */
for(i=0; i<S->nbMelFilters; i++)
Expand Down
7 changes: 5 additions & 2 deletions Source/TransformFunctions/arm_mfcc_q15.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@
point computations may saturate.
*/

arm_status arm_mfcc_q15(
const arm_mfcc_instance_q15 * S,
q15_t *pSrc,
Expand Down Expand Up @@ -163,7 +162,11 @@ arm_status arm_mfcc_q15(

}


if (m != 0)
{
arm_scale_q31(pTmp,m<<16,0,pTmp,S->nbMelFilters);
}

// q34.29 - fftShift - satShift
/* Compute the log */
arm_vlog_q31(pTmp,pTmp,S->nbMelFilters);
Expand Down
8 changes: 6 additions & 2 deletions Source/TransformFunctions/arm_mfcc_q31.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
*/


arm_status arm_mfcc_q31(
const arm_mfcc_instance_q31 * S,
q31_t *pSrc,
Expand Down Expand Up @@ -155,6 +154,7 @@ arm_status arm_mfcc_q31(
S->filterLengths[i],
&result);


coefsPos += S->filterLengths[i];

// q16.48 - fftShift
Expand All @@ -165,7 +165,11 @@ arm_status arm_mfcc_q31(

}


if (m != 0)
{
arm_scale_q31(pTmp,m,0,pTmp,S->nbMelFilters);
}

// q16.29 - fftShift - satShift
/* Compute the log */
arm_vlog_q31(pTmp,pTmp,S->nbMelFilters);
Expand Down

0 comments on commit 60a8c88

Please sign in to comment.