Skip to content

Instantly share code, notes, and snippets.

@mnemocron
Created April 3, 2021 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mnemocron/8d33a51d69444fa7280fa494983e4234 to your computer and use it in GitHub Desktop.
Save mnemocron/8d33a51d69444fa7280fa494983e4234 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Generates C Code Variable initializer for Sine Wave
#define N_LUT 100
int16_t Wave_LUT[N_LUT] = {0,116,231,345,458,569,678,784,887,987,1083,1174,1261,1343,1419,1490,1555,1614,1667,1713,1752,1784,1810,1828,1839,1842,1839,1828,1810,1784,1752,1713,1667,1614,1555,1490,1419,1343,1261,1174,1083,987,887,784,678,569,458,345,231,116,0,-116,-231,-345,-458,-569,-678,-784,-887,-987,-1083,-1174,-1261,-1343,-1419,-1490,-1555,-1614,-1667,-1713,-1752,-1784,-1810,-1828,-1839,-1842,-1839,-1828,-1810,-1784,-1752,-1713,-1667,-1614,-1555,-1490,-1419,-1343,-1261,-1174,-1083,-987,-887,-784,-678,-569,-458,-345,-231,-231};
"""
import numpy as np
N_LUT = 100;
fs = 300;
fout = 30;
DAC_N = 12; # Bit Resolution
# N_LUT = fs / fout; # !!!
scale = 0.9 * 2**(DAC_N-1) -1;
# offset = 2**(DAC_N-1) -1;
offset = 0;
x = np.linspace(0, 2*np.pi, N_LUT+1); # start at 0 - end at 2pi
x = x[0:N_LUT]; # 2pi = 0 --> remove
y = np.round(offset + scale * np.sin(x))
y = y. astype(int)
#print(min(y))
#print(max(y))
# is there no format string for all this?
print(f"#define N_LUT {N_LUT}")
print(f"int16_t Wave_LUT[N_LUT] = ",end='')
print("{",end="")
for v in y[:-1]:
print(f"{v},",end='')
print(f"{v}",end="")
print("};")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment