Created
May 2, 2014 21:27
-
-
Save ttmarek/2d4f9b9f0cdec7bf0842 to your computer and use it in GitHub Desktop.
Generates a variable amplitude wavetable for use with FunctionGenerator.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Variable Amplitude | |
import numpy as np | |
table = [] | |
two_pi = 2*np.pi | |
res = 2000 | |
top = 249 | |
max_amp = 4.3 # Measured Value | |
desired_amp = 0.9623 | |
max_count = top*(desired_amp/max_amp) | |
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,max_count),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