Skip to content

Instantly share code, notes, and snippets.

@ctralie
Created March 3, 2020 01:31
Show Gist options
  • Save ctralie/0bc31487aab74a191ba789387e066ac4 to your computer and use it in GitHub Desktop.
Save ctralie/0bc31487aab74a191ba789387e066ac4 to your computer and use it in GitHub Desktop.
Bad Sampling Triangle Wave = Amazing Pattern
import numpy as np
import scipy.io.wavfile as wavfile
import matplotlib.pyplot as plt
duration = 20
fs = 44100
y = np.zeros(fs*duration)
n_harmonics = 5
t = np.arange(fs*duration) # INCORRECT SAMPLING
#t = np.linspace(0, duration, fs*duration) # CORRECT SAMPLING
freq = 440
for i in range(1, n_harmonics+1):
coeff = (1.0/i)*(-1)**(i+1)
y = y + coeff*np.sin(2*np.pi*freq*i*t)
y = y/np.max(np.abs(y))
plt.plot(y)
plt.show()
wavfile.write("out.wav", fs, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment