I am working on a project which incorporates computing a sine wave as input for a control loop.
The sine wave has a frequency of 280 Hz, and the control loop runs every 30 µs and everything is written in C for an Arm Cortex-M7.
At the moment we are simply doing:
double time;void control_loop() { time += 30e-6; double sine = sin(2 * M_PI * 280 * time); ...}
Two problems/questions arise:
- When running for a long time,
time
becomes bigger. Suddenly there is a point where the computation time for the sine function increases drastically (see image). Why is this? How are these functions usually implemented? Is there a way to circumvent this (without noticeable precision loss) as speed is a huge factor for us? We are using sin from math.h (Arm GCC). - How can I deal with time in general? When running for a long time, the variable
time
will inevitably reach the limits of double precision. Even using a countertime = counter++ * 30e-6;
only improves this, but it does not solve it. As I am certainly not the first person who wants to generate a sine wave for a long time, there must be some ideas/papers/... on how to implement this fast and precise.