Created
February 19, 2023 20:05
-
-
Save Lukelectro/ac88198e2a886280f4334b7b25494e39 to your computer and use it in GitHub Desktop.
Sine table for MCU use (Not below zero / offset, though ofc this can be changed)
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
import math | |
import matplotlib.pyplot as plt | |
sintable = " " | |
sinplottable =[] | |
sinplotfull = [] | |
# for conversion from volts to adc ticks | |
vfull=3.3 | |
fullscale = 4096 | |
R1 = 220000 | |
R2 = 820 | |
vref=1.0 | |
numpoints = 64 # number of point is table | |
fraction_of_full_sine = 1 # part of sine it should put in table. 1 for full sine, 0,25 for only 90 degrees of sine | |
# max_amplitude = (100/(R1+R2))*R2 * (fullscale/vfull) * vref # max amplitude of sine: in ADC ticks (output voltage as divided by resistor divider) | |
# Offset = (48/(R1+R2))*R2 * (fullscale/vfull) *vref # offset (DC input voltage of step-up, divided by output voltage divider) | |
max_amplitude = 255 #pwm max ducy | |
Offset = 0 #PWM min ducy | |
for x in range (0,numpoints): | |
newnumber=0.5*(max_amplitude-Offset)*math.sin((x/numpoints)*2*math.pi*fraction_of_full_sine)+0.5*(max_amplitude-Offset)+Offset | |
sintable+=str(int(newnumber)) | |
sintable+=',' | |
sinplottable.append(newnumber) | |
""" | |
for i in range (0,512): #construct full sine from table and plot it, for verification | |
if(i<=127): sinplotfull.append(sinplottable[i]) | |
elif(i<=255): sinplotfull.append(sinplottable[127-(i-128)]) | |
#elif(i<=(127+255)): sinplotfull.append(sinplottable[(i-255)]*-1) #skips element 0 because otherwise 2 0 in a row / not a smooth sine, but that would cause index issues later on | |
elif(i<=(127+256)): sinplotfull.append(sinplottable[(i-256)]*-1) # include the double element 0. Find a better fix later. (maybe use another part of the sine, say 0.5Pi to 1.5Pi instead of 0 to 0.5 Pi) | |
else: sinplotfull.append(-1*sinplottable[127-(i-(128+256))]) | |
for x in range (0,128): # this should make a 180 degree sine table in 128 steps | |
newnumber=math.sin((x/128)*math.pi+0.5*math.pi) | |
sintable+=str(newnumber) | |
sintable+=',' | |
sinplottable.append(newnumber) | |
for i in range (0,512): #construct full sine from table and plot it, for verification | |
if(i<=127): sinplotfull.append(sinplottable[i]) | |
elif(i<=255): sinplotfull.append(sinplottable[127-(i-128)]) | |
for x in range (0,128): # this should make a full sine table in 128 steps. (Eigenlijk is dat ook al ruim voldoende resolutie en dan hoef ik niet stukjes sinus met afrondingsfouten aan elkaar te plakken) | |
newnumber=math.sin((x/128)*math.pi*2) | |
sintable+=str(newnumber) | |
sintable+=',' | |
sinplottable.append(newnumber) | |
""" | |
fig = plt.figure() | |
ax = fig.add_subplot(111) | |
ax.plot(sinplottable,label="table") | |
#ax.plot(sinplotfull,label="full") | |
ax.legend() | |
plt.show() | |
print ("sinetable["+str(numpoints)+"]={"+sintable[:-1]+"};") #fit in C array and remove last ',' (Needs linefeeds added to taste) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment