CMSIS-DSP icon indicating copy to clipboard operation
CMSIS-DSP copied to clipboard

Issue in arm_fir_init_q15 when ARM_MATH_DSP is enabled

Open BhaskarsarmaP opened this issue 1 year ago • 6 comments

arm_fir_init_q15 has wrong logic in ARM_MATH_DSP which is resulting in output mismatch. https://github.com/ARM-software/CMSIS-DSP/blob/v1.16.2/Source/FilteringFunctions/arm_fir_init_q15.c .

When I removed ARM_MATH_DSP code, I was getting proper outputs on M4 and M7 targets.

BhaskarsarmaP avatar Nov 12 '25 18:11 BhaskarsarmaP

@BhaskarsarmaP Do you have a reproducer ? (filter coef + input and expected output and block size). Which compiler is used ?

christophe0606 avatar Nov 13 '25 07:11 christophe0606

Compiler used - GNU Tools for ARM Embedded Processors V13.2 rel 1 Inputs

0.2785
0.5469
0.9575
0.9649
0.1576
0.9706
0.9572
0.4854
0.8003
0.1419

Coefficients - fcof

fcof =

-0.0018 -0.0016 0.0037 0.0081 0.0085 -0.0174 outCRL_M4 =

-0.0005 -0.0015 -0.0016 0.0010 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163 0.0143 0.0097 0.0064 0.0022 0.0085 0.0139 0.0132 0.0107 0.0105 0.0163

      DataTypeMode: Fixed-point: binary point scaling
        Signedness: Signed
        WordLength: 16
    FractionLength: 15

outExp =

-0.0005 -0.0015 -0.0016 0.0010 0.0085 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135 -0.0026 -0.0070 -0.0021 -0.0117 0.0060 0.0091 0.0038 -0.0060 -0.0063 0.0135

      DataTypeMode: Fixed-point: binary point scaling
        Signedness: Signed
        WordLength: 16
    FractionLength: 15

BhaskarsarmaP avatar Nov 13 '25 09:11 BhaskarsarmaP

I tried on M3 board, I got same results as outExp . But on SIMD Supported boards, It seems there is an issue

BhaskarsarmaP avatar Nov 13 '25 10:11 BhaskarsarmaP

I am not able to reproduce the issue.

arm_float_to_q15(input_f32, input_q15, NB);
arm_float_to_q15(coef_f32, coef_q15, NB_COEFS);

arm_fir_instance_q15 S;
    
arm_fir_init_q15(&S, NB_COEFS, coef_q15, state_q15, NB);

arm_fir_q15(&S, input_q15, output_q15, NB); 

arm_q15_to_float(output_q15, output_f32, NB);
for(int i=0; i<NB; i++) {
        printf("%f \n", output_f32[i]);
}   

Python code to compare (use of reverse in Python is because the coefs are stored in time reversed order in CMSIS-DSP):

from scipy.signal import fftconvolve, lfilter, firwin
import numpy as np
input=[0.2785,
0.5469,
0.9575,
0.9649,
0.1576,
0.9706,
0.9572,
0.4854,
0.8003,
0.1419]
coefs=[-0.0018, -0.0016, 0.0037, 0.0081, 0.0085, -0.0174]
coefs.reverse()
output=lfilter(coefs, 1.0, input)
print(output)

I tested on M0 and M3/M4. Results are the same as the Python (with small differences due to q15 accuracy being less than f32).

christophe0606 avatar Nov 14 '25 08:11 christophe0606

/* Clear state buffer. The size is always (blockSize + numTaps - 1) */ memset(pState, 0, (numTaps + (blockSize - 1U)) * sizeof(q15_t));

/* Clear the state buffer. The size is always (blockSize + numTaps ) */ memset(pState, 0, (numTaps + (blockSize)) * sizeof(q15_t)); , this is different for both M3 and M4. I will be sharing wrapper to reproduce

BhaskarsarmaP avatar Nov 17 '25 11:11 BhaskarsarmaP

I added two calls to arm_fir_q15 to process 2*NB samples by blocks of NB and use the state buffer. I have initialized the state buffer with garbage before the arm_fir_init_q15. I can't reproduce the issue (on latest CMSIS-DSP version - github HEAD).

christophe0606 avatar Nov 25 '25 09:11 christophe0606