CORDIC COS implementation
https://github.com/Sushmitabaga/CORDIC/tree/main
I was trying to Enable CORDIC IN STM32G4 series for checking the time taken by normal cos function and cos CORDIC function. But i found that both are taking almost the same time. which is not possible according to cordic .
| Time Taken with CORDIC |
| 1.61E-05 |
| Time taken without CORDIC |
| 2.50E-06 |
please the attached github code and below:
inline int f32_to_q31( float input )
{
const float Q_31_MAX_F = 1.0f ;// 0x0.FFFFFFp0F;//
const float Q_31_MIN_F = -1.0f;
return (int)roundf(scalbnf(fmaxf(fminf(input,Q_31_MAX_F),Q_31_MIN_F),31));
}
#define pi 3.14159265359
/*****convert notation integer to 32 bits float*********/
#define q31_to_f32(x) ldexp((int32_t)x, -31) //q31 represent no in range -1,1
float cordic_q31_cosf(float x)
{
CORDIC_ConfigTypeDef sConfig;
int32_t input_q31 = f32_to_q31(fmod(x,2.0f*pi)/(2.0f*pi))<<1;
int32_t output_q31;
sConfig.Function = CORDIC_FUNCTION_COSINE;
sConfig.Precision = CORDIC_PRECISION_6CYCLES;
sConfig.Scale = CORDIC_SCALE_0;
sConfig.NbWrite = CORDIC_NBWRITE_1;
sConfig.NbRead = CORDIC_NBREAD_1;
sConfig.InSize = CORDIC_INSIZE_32BITS;
sConfig.OutSize = CORDIC_OUTSIZE_32BITS;
HAL_CORDIC_Configure(&hcordic, &sConfig);
HAL_CORDIC_CalculateZO(&hcordic, &input_q31, &output_q31,1 ,0);
return q31_to_f32(output_q31);
}
int i;
for(i=0;i<361;i++)
{
radian = (float)(i)*2.0*pi/360.0; //degree to radain (2pi/360)
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
start_cyc = DWT->CYCCNT;// degree into radian
start_tick= SysTick->VAL;
cordic_cos[i] = cordic_q31_cosf(radian);
stop_tick = SysTick->VAL;
elasped = (float)(start_tick - stop_tick)/170000000.0;
end_cyc = DWT->CYCCNT;
if(end_cyc > start_cyc)
{
diff_cyc = end_cyc - start_cyc;
}
else
{
diff_cyc = end_cyc + (0xFFFFFFFF - start_cyc);
}
timeTaken = (float)diff_cyc/170000000.0;
}
