# [music-dsp] fast sin(x) function

Denis Sbragion d.sbragion at infotecna.it
Wed Jun 11 02:49:35 EDT 2008

```Hello Stephen,

On Tue, June 10, 2008 21:13, Stephen Blinkhorn wrote:
> I'm looking for a fast sin(x) type function.  So far I haven't found
> anything with the necessary precision for audio work.  Any ideas?

don't know if it has been already pointed out, but there's a fast and quite
accurate method to generate sin(x) sequences, i.e. not arbitrary values but
uniformly spaced values. Here it is a not so fast but easy to understand
Octave snippet:

## usage: st = sintable(sv,sd,sn)
##
## Generate a sin table by recurrence
##
## sv: sine start value
## sd: sine delta
## sn: sine number of values

function st = sintable(sv,sd,sn);

# Initializes the sin array
st = zeros(1,sn);

# Initializes the sine constants
R = sin(sd/2);
R = -4 * R * R;
S = sin(sv);
S = sin(sd) * cos(sv) + (S * (1 - cos(sd)));

# Set the first value
V = sin(sv);
st(1) = V;

# Sine generation loop
for I = 2:sn
S = R * V + S;
V = V + S;
st(I) = V;
endfor

endfunction

I use this mehtod, translated to C, to generate a huge amount of Blackman
windowed lowpass FIR filters, and it turned out to be noticeably faster than
direct sin computation. Probably it could be also translated to vector code
(SSE or similar) making it even faster. If you need arbitrary values it's
quite difficult to beat the standard sin function unless you're ready to
accept a lower accuracy.

Bye,

--
Denis Sbragion
InfoTecna
Tel: +39 0362 805396, Fax: +39 0362 805404
URL: http://www.infotecna.it

```