I am doing a project related with real-time feedback.
I need to acquire and analyze the data in a few tens of microseconds. However, using the build-in mathematical operations, it takes too much.
I am not going to put all my code because of its extension, just the part that I need to optimize.
int ADC1After ; float A_Fourier; float B_Fourier; float Amplitude; float Phase; A_Fourier=2.0/3.0*(ADC1After*cos(0.0*PI/3.0)+ADC1After*cos(2.0*PI/3.0)+ADC1After*cos(4.0*PI/3.0)); B_Fourier=2.0/3.0*(ADC1After*sin(0.0*PI/3.0)+ADC1After*sin(2.0*PI/3.0)+ADC1After*sin(4.0*PI/3.0)); Amplitude=sqrt(pow(A_Fourier,2)+pow(B_Fourier,2))/1023*3.3; Phase=atan2(B_Fourier,A_Fourier) * 180 / PI;
the ADC1After vector is some data vector I had previously acquired (in uint32_t datatype).
As you can see, it involves sum, multiplication, division,sqrt, pow, and a trigonometric function.
This whole 4 lines of code takes 120 microseconds ( I have estimated it with an oscilloscope ), and breaking it down, each line code takes:
1st line (10 uS), 2nd line (10 uS), 3r line (25 us), and 4th line (75 us).
As expected, the phase, as it involves a division and trigonometric functions takes the longest.
My objective is to reduce all the computation time up to 25-30 uS.
After some research I found that in general there are methods to substitute the division by multiplication and shift, and the trig. function to Taylor expansions. However, I don't think I can use Taylor, as my phase can take values from 0 to 2pi.
I have also read that programming in assembly language can improve the speed of performance. However, as it is not an easy task I'll try to avoid it if possible.