Skip to content

Instantly share code, notes, and snippets.

@ttmarek
Created May 2, 2014 21:27
Show Gist options
  • Save ttmarek/c482087d0ed9cc53c18b to your computer and use it in GitHub Desktop.
Save ttmarek/c482087d0ed9cc53c18b to your computer and use it in GitHub Desktop.
Generates a fixed amplitude wavetable for use with FunctionGenerator.ino
# Fixed Amplitude (Maximum)
import numpy as np
table = []
two_pi = 2*np.pi
res = 20
top = 500
def map_value(x, in_min, in_max, out_min, out_max):
return (((x - in_min) * (out_max - out_min))/(in_max - in_min)) + out_min
for i in range(0,res):
sine_value = np.sin(two_pi*i/(res-1))
table.append(int(np.round(map_value(sine_value,-1,1,0,top),0)))
with open('wavetable.h', 'w') as wavetable:
wavetable.write('#include "Arduino.h"\n')
wavetable.write('const long length = '+str(len(table))+';\n');
table = [str(x) for x in table]
table_string = ', '.join(table)
wavetable.write('const byte waveTable[] PROGMEM = {'+table_string+'};')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment