Skip to content

Instantly share code, notes, and snippets.

@pichenettes
Created February 22, 2012 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pichenettes/1884491 to your computer and use it in GitHub Desktop.
Save pichenettes/1884491 to your computer and use it in GitHub Desktop.
Hertz Google Doodle, correctly done
import numpy
import pylab
points = [
(3, 3.0),
(3, 3.0),
(3, 3.0),
(2, 2.0),
(2, 2.0),
(2, 2.0),
(2, 2.0),
(2, 3.0),
(2, 3.0),
(1, 3.0),
(2, 2.0),
(2, 2.0),
]
points = points * 3
N = 50
frequency = []
amplitude_modulation = []
for pitch, amplitude in points:
frequency += [1.0 / pitch / (2 * N)] * N
amplitude_modulation += [amplitude] * N
smoothing = numpy.hanning(N)
smoothing /= smoothing.sum()
frequency = numpy.convolve(smoothing, frequency, 'same')
amplitude_modulation = numpy.convolve(smoothing, amplitude_modulation, 'same')
phase = numpy.cumsum(frequency)
y = numpy.sin(2 * numpy.pi * phase) * amplitude_modulation
y = y[len(y) / 3 + 4:-len(y) / 3 + 5]
pylab.figure(figsize=(20, 6))
colors = 'brybgr'
zeros_crossings = numpy.where(numpy.abs(numpy.diff(numpy.sign(y))))[0]
zeros_crossings = [0] + list(zeros_crossings)
for c, x_from, x_to in zip(colors, zeros_crossings[:-1], zeros_crossings[1:]):
x = range(x_from, x_to + 1)
pylab.plot(x, y[x], c)
pylab.savefig('doodle.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment