Created
April 14, 2018 01:54
-
-
Save chipbell4/43ecb5fac4cfad5e1587f890b6388420 to your computer and use it in GitHub Desktop.
Triangle Wave Generation
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 numpy as np | |
import scipy.io.wavfile | |
# generate a "canonical" waveform | |
elements = [] | |
for i in range(0, 255): | |
for k in range(20): | |
elements.append(i) | |
for i in range(255, 0, -1): | |
for k in range(20): | |
elements.append(i) | |
# Figure out how many elements we should skip per step to acheive our target frequency | |
target_frequency = 220.0 | |
base_frequency = 44100.0 / len(elements) | |
skip_amount = target_frequency / base_frequency | |
# stretch the array out a little bit, so our resolution is better | |
for k in range(4): | |
elements += elements | |
# loop over the array skipping elements to acheive our target frequency | |
squashed = [] | |
k = 0 | |
while int(k) <= len(elements): | |
squashed.append(elements[int(k)]) | |
k += skip_amount | |
# make sure we have enough samples to be a single second | |
while len(squashed) < 44100: | |
squashed += squashed | |
# dump it to a wave file | |
squashed = np.array(map(np.uint8, squashed)) | |
scipy.io.wavfile.write('triangle.wav', 44100, squashed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment