Trying to create some musical notes by combining harmonic series. Very simple code, but the audio turns up blank. Any thoughts?
from IPython.display import Audio
import numpy as np
import matplotlib.pyplot as plt
def Harmonic(i,linComb):
x=np.linspace(0,3,24000)
y = [0 for _ in x]
weights = linComb
for n in range(0,i):
y += np.sin((2*n+1)*(2*np.pi*weights[n])*(x))/(2*n+1)
plt.plot(x,y)
plt.show()
return y
out = Harmonic(3,[0,2,3])
Audio(data=out, rate=8000)
Stuff I’ve tried:
- Changing the rate
- Manipulating the y-values
- Ensuring the harmonic function does indeed work
- Looking at this answer (same function, but still doesn’t work)
Would appreciate any help. Thanks.
>Solution :
The sound generated by the code is audible but weak.
I have no experience in audio programming, but some chaos of noise can be generated by the following:
from IPython.display import Audio
import numpy as np
import matplotlib.pyplot as plt
def Harmonic(i):
x=np.linspace(0,i,24000)
y = [0 for _ in x]
for n in range(0,i):
y += np.sin((2*n+1)*(2*np.pi)*(x))/(2*n+1)
plt.plot(x,y)
plt.show()
return y
out = Harmonic(1000)
Audio(data=out, rate=8000)
