import wave
import numpy as np
import struct
import matplotlib.pyplot as plt
def create_wave(A, f0, fs, t):
data = []
# for each sample
for n in np.arange(t * fs):
y = 0.0
# composite 30 sine wave samples to get the resultant triangle wave sample
for k in range(30):
y += (-1)**k * (A / (2*k+1)**2) * np.sin((2*k+1) * 2 * np.pi * f0 * n / fs)
# clipping
if y > 1.0: y = 1.0
if y < -1.0: y = -1.0
data.append(y)
return data
def main():
data = create_wave(0.5, 261.63, 10000, 1)
plt.plot(data)
plt.xlim([0, 100])
plt.show()
if __name__ == '__main__':
main()