#include "FIRF32.h"
#include <stdio.h>
#include "Error.h"

#define SNR_THRESHOLD 120

/* 

Reference patterns are generated with
a double precision computation.

*/
#define REL_ERROR (3.0e-5)

#if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
static __ALIGNED(8) float32_t coeffArray[32];
#endif 

    void FIRF32::test_fir_f32()
    {
        

        const int16_t *configp = configs.ptr();
        float32_t *statep = state.ptr();
        const float32_t *orgcoefsp = coefs.ptr();
        
        const float32_t *coefsp;
        const float32_t *inputp = inputs.ptr();
        float32_t *outp = output.ptr();

        int i;
#if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
        int j;
#endif
        int blockSize;
        int numTaps;

        

        /*

        Python script is generating different tests with
        different blockSize and numTaps.

        We loop on those configs.

        */
        for(i=0; i < configs.nbSamples() ; i += 2)
        {
           blockSize = configp[0];
           numTaps = configp[1];

#if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
           /* Copy coefficients and pad to zero 
           */
           memset(coeffArray,0,32);
           for(j=0;j < numTaps; j++)
           {
              coeffArray[j] = orgcoefsp[j];
           }
   
           coefsp = coeffArray;
#else
           coefsp = orgcoefsp;
#endif

           /*

           The filter is initialized with the coefs, blockSize and numTaps.

           */
           arm_fir_init_f32(&this->S,numTaps,coefsp,statep,blockSize);

           /*

           Input pointer is reset since the same input pattern is used

           */
           inputp = inputs.ptr();

           /*
           
           Python script is filtering a 2*blockSize number of samples.
           We do the same filtering in two pass to check (indirectly that
           the state management of the fir is working.)

           */

           arm_fir_f32(&this->S,inputp,outp,blockSize);
           outp += blockSize;
           
           inputp += blockSize;
           arm_fir_f32(&this->S,inputp,outp,blockSize);
           outp += blockSize;

           configp += 2;
           orgcoefsp += numTaps;

        }


        ASSERT_EMPTY_TAIL(output);

        ASSERT_SNR(output,ref,(float32_t)SNR_THRESHOLD);

        ASSERT_REL_ERROR(output,ref,REL_ERROR);

    } 

 
    void FIRF32::setUp(Testing::testID_t id,std::vector<Testing::param_t>& params,Client::PatternMgr *mgr)
    {
      

       
       switch(id)
       {
        case FIRF32::TEST_FIR_F32_1:
        break;

       }
      

       inputs.reload(FIRF32::FIRINPUTS_F32_ID,mgr);
       coefs.reload(FIRF32::FIRCOEFS_F32_ID,mgr);
       configs.reload(FIRF32::FIRCONFIGS_S16_ID,mgr);
       ref.reload(FIRF32::FIRREFS_F32_ID,mgr);

       output.create(ref.nbSamples(),FIRF32::OUT_F32_ID,mgr);
       /* Max blockSize + numTaps - 1 as generated by Python script */
       state.create(47,FIRF32::OUT_F32_ID,mgr);
    }

    void FIRF32::tearDown(Testing::testID_t id,Client::PatternMgr *mgr)
    {
        output.dump(mgr);
    }
