``````

In [9]:

%pylab inline
pylab.rcParams['figure.figsize'] = (15, 3)

``````
``````

Populating the interactive namespace from numpy and matplotlib

``````
``````

In [10]:

phase_inc = 2 * pi / 100;
l = 400

``````

Calculate a buffer of sine values.

``````

In [11]:

p = 0
v = []
for i in range(l):
v.append(sin(p))
p += phase_inc

plot(v)
grid(True)
show()
print(v[0], v[-1])

``````
``````

0.0 -0.06279051952942746

``````

Fast version that caclulates followup values using one multiplication and one subtraction

``````

In [12]:

si0 = sin(-phase_inc)
si1 = 0.0
fc = 2.0 * cos(phase_inc)
v = []
for i in range(l//2):
v.append(si1)
si0 = fc * si1 - si0
v.append(si0)
si1 = fc * si0 - si1

plot(v)
grid(True)
show()
print(v[0], v[-1])

``````
``````

0.0 -0.0627905195293102

``````
``````

In [18]:

sk = 0.0
ck = 1.0
s1 = sin(phase_inc)
c1 = cos(phase_inc)
v = []
for i in range(l):
v.append(sk)
skk = c1 * sk + s1 * ck
ck = c1 * ck - s1 * sk
sk = skk

plot(v)
grid(True)
show()
print(v[0], v[-1])

``````
``````

0.0 -0.0627905195293153

``````

Same as before but demonstrating how initializie the series with a phase != 0

``````

In [22]:

phase = pi / 2.0
si0 = sin(phase - phase_inc)
si1 = sin(phase)
fc = 2.0 * cos(phase_inc)
v = []
for i in range(l//2):
v.append(si1)
si0 = fc * si1 - si0
v.append(si0)
si1 = fc * si0 - si1

plot(v)
grid(True)
show()

``````
``````

``````
``````

In [21]:

phase = pi / 2.0
sk = sin(-phase)
ck = cos(-phase)
s1 = sin(phase_inc)
c1 = cos(phase_inc)
v = []
for i in range(l):
v.append(sk)
skk = c1 * sk + s1 * ck
ck = c1 * ck - s1 * sk
sk = skk

plot(v)
grid(True)
show()

``````
``````

``````

Variant that demonstrates changing the frequency without reseting the phase.

``````

In [16]:

sk = 0.0
ck = 1.0
s1 = sin(phase_inc)
c1 = cos(phase_inc)
v = []
l1 = l // 3
l2 = l - l1
for i in range(l1):
v.append(sk)
skk = c1 * sk + s1 * ck
ckk = c1 * ck - s1 * sk
sk = skk
ck = ckk
s1 = sin(phase_inc*2.0)
c1 = cos(phase_inc*2.0)
for i in range(l2):
v.append(sk)
skk = c1 * sk + s1 * ck
ckk = c1 * ck - s1 * sk
sk = skk
ck = ckk

plot(v)
grid(True)
show()

``````
``````

``````
``````

In [ ]:

``````