Created
March 3, 2020 01:31
-
-
Save ctralie/0bc31487aab74a191ba789387e066ac4 to your computer and use it in GitHub Desktop.
Bad Sampling Triangle Wave = Amazing Pattern
This file contains hidden or 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 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