Spectral Harmonograph

Spectral Harmonographs

By Monty, 5th October 2015

Spectral Harmonograph

Spectral Harmonograph

This Python + Pygame program draws the trace of 4 decaying sine waves, 2 per axis, with rainbow colours. It generates a sequence of random harmonographs.

A harmonograph is a mechanical device typically seen in science museums, that has two or more pendulae with attached pens, that can draw on a sheet of paper. The pendulae are set in motion and the pen draws pretty patterns on the paper. This is easily simulated in a computer program, by plotting orthogonal sine waves acting together on a drawing point. This generates Lissajous figures, which are decayed to make a pleasing nesting of ‘parallel’ lines, like you might see on bank notes.

It’s fast, and can be set to go much faster (or slower) if you want. Tip: set the display window to fullscreen. MIT license; download from GitHub

'''    Spectral Harmonographs   Copyright 2014 Alan Richmond (Tuxar.uk)
print("Quit: q key, Screenshot: spacebar")

import pygame, sys, random as r
from pygame.locals import *
from math import pi, sin, cos, exp
#                        EDIT THESE:
width,height=1280,720       # YouTube HD
width,height=1920,1080      # my left monitor
width,height=1280,1024      # my right monitor
width,height=1680,1050      # Lucy's monitor
#width,height=2560,1440      # YT channel art
dd=0.99995                  # decay factor
dt=0.02                     # time increment
speed=200                   # yes, speed
hui=57*2                    # Hue increment
sd=0.005                    # frequency spread (from integer)
mx=4                        # max range for amplitudes & frequencies
print("Hit SPACE to save")

def check_event():
    global save
    for event in pygame.event.get():
        if event.type == QUIT:
        elif event.type == KEYDOWN and event.key == K_q:
        elif event.type == KEYDOWN and event.key == K_SPACE:
            print("Saving when finished...")

def scale(length):
    while True:
        if max>0: break
    return a1,a2,length/(2*max)
pygame.event.set_allowed([QUIT, KEYDOWN])
screen = pygame.display.set_mode((width,height),DOUBLEBUF)
while True:
#   Amplitudes & scales
    fx1, fx2 =  r.randint(1,mx) + r.gauss(0,sd), r.randint(1,mx) + r.gauss(0,sd)
    fy1, fy2 =  r.randint(1,mx) + r.gauss(0,sd), r.randint(1,mx) + r.gauss(0,sd)
    px1, px2 =  r.uniform(0,2*pi), r.uniform(0,2*pi)
    py1, py2 =  r.uniform(0,2*pi), r.uniform(0,2*pi)

    t=0.0                       # angle for sin
    while dec>0.015:
                                # calculate next x,y point along line
        x = xscale * dec * (ax1*sin(t * fx1 + px1) + ax2*sin(t * fx2 + px2)) + width/2
        y = yscale * dec * (ay1*sin(t * fy1 + py1) + ay2*sin(t * fy2 + py2)) + height/2
        dec*=dd                 # decay
        if not first:           # ignore any complaint about prev_x,y being undefined
#            fg.hsva=(hue,sat,val,aaa)
#            hue = (hue + dt*hui) % 360      # cycle hue
            pygame.draw.aaline(screen, fg, (x, y), (prev_x, prev_y), 1)
        prev_x = x              # save x,y for next line segment start
        prev_y = y
        if steps%speed==0: pygame.display.update()
        t+=dt                   # increment angle for sin
    if save:                    # parameters are encoded into filename
        pygame.image.save(screen, pars+'.jpg')
        print("Saved as "+pars+'.jpg')

#    screen.fill((255,255,255))

You may need to install python and/or pygame, e.g. on Ubuntu/Debian style linuxes:

sudo apt-get install pygame
sudo apt-get install python
Go to the directory you saved it in and make it executable, then run it:

chmod +x harmonograph.py
Set the display window to fullscreen (right click on title bar, maybe it’s under More Actions?).


A harmonograph is a mechanical apparatus that employs pendulums to create a geometric image. The drawings created typically are Lissajous curves, or related drawings of greater complexity. The devices, which began to appear in the mid-19th century and peaked in popularity in the 1890s, cannot be conclusively attributed to a single person, although Hugh Blackburn, a professor of mathematics at the University of Glasgow, is commonly believed to be the official inventor.

A simple, so-called "lateral" harmonograph uses two pendulums to control the movement of a pen relative to a drawing surface. One pendulum moves the pen back and forth along one axis and the other pendulum moves the drawing surface back and forth along a perpendicular axis. By varying the frequency and phase of the pendulums relative to one another, different patterns are created. Even a simple harmonograph as described can create ellipses, spirals, figure eights and other Lissajous figures.

More complex harmonographs incorporate three or more pendulums or linked pendulums together (for example hanging one pendulum off another), or involve rotary motion in which one or more pendulums is mounted on gimbals to allow movement in any direction.

A particular type of harmonograph, a pintograph, is based on the relative motion of two rotating disks, as illustrated in the links below.

What do you think?

Leave a Reply

%d bloggers like this: