2015-03-15 23:30:07 +08:00
|
|
|
from copy import copy
|
|
|
|
from math import cos, pi
|
|
|
|
|
|
|
|
import cairoplot
|
|
|
|
|
|
|
|
|
|
|
|
class Spline:
|
2015-03-15 23:48:24 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.c = [0.0]
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def set_coefficients(self, c):
|
|
|
|
self.c = copy(c)
|
|
|
|
|
|
|
|
def next(self):
|
|
|
|
r = self.c[0]
|
|
|
|
for i in range(len(self.c)-1):
|
|
|
|
self.c[i] += self.c[i+1]
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
class SplinePhase:
|
2015-03-15 23:48:24 +08:00
|
|
|
def __init__(self):
|
2015-03-15 23:30:07 +08:00
|
|
|
self.c = [0.0]
|
2015-03-15 23:48:24 +08:00
|
|
|
self.c0 = 0.0
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def set_coefficients(self, c):
|
|
|
|
self.c = self.c[0:1] + c[1:]
|
|
|
|
self.c0 = self.c[0]
|
|
|
|
|
2015-03-15 23:48:24 +08:00
|
|
|
def clear(self):
|
|
|
|
self.c[0] = 0.0
|
|
|
|
|
2015-03-15 23:30:07 +08:00
|
|
|
def next(self):
|
|
|
|
r = self.c[0] + self.c0
|
|
|
|
for i in range(len(self.c)-1):
|
|
|
|
self.c[i] += self.c[i+1] % 1.0
|
|
|
|
return r
|
|
|
|
|
|
|
|
|
|
|
|
class DDS:
|
2015-03-15 23:48:24 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.amplitude = Spline()
|
|
|
|
self.phase = SplinePhase()
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def next(self):
|
|
|
|
return self.amplitude.next()*cos(2*pi*self.phase.next())
|
|
|
|
|
|
|
|
|
|
|
|
class Wave:
|
2015-03-15 23:48:24 +08:00
|
|
|
def __init__(self):
|
|
|
|
self.bias = Spline()
|
|
|
|
self.dds = DDS()
|
2015-03-15 23:30:07 +08:00
|
|
|
|
|
|
|
def next(self):
|
|
|
|
return self.bias.next() + self.dds.next()
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
x = list(range(400))
|
2015-03-15 23:48:24 +08:00
|
|
|
w = Wave()
|
|
|
|
|
|
|
|
w.dds.amplitude.set_coefficients([0.0, 0.0, 0.01])
|
|
|
|
w.dds.phase.set_coefficients([0.0, 0.0, 0.0005])
|
2015-03-15 23:30:07 +08:00
|
|
|
y = [w.next() for i in range(100)]
|
2015-03-15 23:48:24 +08:00
|
|
|
|
|
|
|
w.dds.amplitude.set_coefficients([49.5, 1.0, -0.01])
|
2015-03-15 23:30:07 +08:00
|
|
|
y += [w.next() for i in range(100)]
|
2015-03-15 23:48:24 +08:00
|
|
|
|
|
|
|
w.dds.phase.set_coefficients([0.0, 0.1, -0.0005])
|
2015-03-15 23:30:07 +08:00
|
|
|
y += [w.next() for i in range(100)]
|
2015-03-15 23:48:24 +08:00
|
|
|
|
|
|
|
w.dds.amplitude.set_coefficients([50.5, -1.0, 0.01])
|
2015-03-15 23:30:07 +08:00
|
|
|
y += [w.next() for i in range(100)]
|
2015-03-15 23:48:24 +08:00
|
|
|
|
2015-03-15 23:30:07 +08:00
|
|
|
cairoplot.scatter_plot("plot.png", [x, y])
|