#!/usr/bin/env python # -*- coding: utf-8 -*- # copyright 2012, P. Lutus # released under the GPL import math import matplotlib.pyplot as plt # build the sin table using floating-point def build_sin_table(): table = [] for i in range(65): v = math.sin(2 * math.pi * i / 256) * 127.5 table.append(int(v)) return table # perform lookups in the sin table # using only integer values # and 256 = 360 degrees def int_sin(x,sin_table): x &= 255 neg = (x >= 128) x &= 127 if(x >= 64): x = 128-x v = sin_table[x] return (v,-v)[neg] sine_lookup = build_sin_table() xvalues = [] yvalues = [] for x in range(512): xvalues.append(x) yvalues.append(int_sin(x,sine_lookup)) plt.plot(xvalues,yvalues) # limit the x axis to 512 plt.xlim(0,512) # show a grid plt.grid(True) plt.title("y = sin(x)") # save the result as a file plt.savefig('integer_trig.png') # show the result plt.show()