Question

How does Matlab/C generates Sine wave, I mean do they store the values for every angle ? but if they do then there are infinite values that are needed to be stored.

Was it helpful?

Solution

There are many, many routines for calculating sines of angles using only the basic arithmetic operators available on any modern digital computer. One such, but only one example, is the CORDIC algorithm. I don't know what algorithm(s) Matlab uses for trigonometric functions.

Computers don't simply look up the value of a trigonometric function in a stored table of values, though some of the algorithms in current use do look up critical values in stored tables. What those tables contain is algorithm-specific and would probably not accord with a naive expectation of a trig table.

Note that this question has been asked many times on SO, this answer seems to be the pick of them.

OTHER TIPS

No, they generally don't. And even if they did, no, there are not "infinite values". Digital finite (real) computers don't deal well with the infinite, but that applies just as well to the angle (the input to the sine function). You can't ask for "every" angle, since the angle itself must be expressed in a finite set of bits.

I think using Taylor series is common, or other solutions based on interpolation.

Also, modern CPU:s have instructions for computing sine, but that of course just pushes the question of how it's implemented down a level or three.

See also this very similar question for more details.

Before the 90s computers would numerically calculate sine and other trigonometric functions using the basic functions addition, subtraction, multiplication and division. The algorithm for calculating them was usually included in basic compiler libraries. At some point, it become possible to add an optional hardware processor called the floating point processor (FPU). My understanding of the FPU is that it did have hard values of the trig functions stored on it. The speed to calculate trig functions would increase dramatically with the inclusion of the FPU. Since the 90s however, the FPU has been bundled with the CPU.

I can't seem to find any explicit description on precisely how sine and other trig functions are implemented by the FPU and generally that is an implementation detail left to the electrical engineer designing the chip. However it really is only necessary to know the values from 0 to pi/2. Everything else can be easily calculated from those values.

EDIT: Ok here is the implementation used on the FPU: CORDIC, which I found at this answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top