Reputation: 445
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:
Would appreciate any help. Thanks.
Upvotes: 3
Views: 256
Reputation: 507
The sound generated by the code is audible but weak.
I have no experience in audio programming, but some type of noise resembling a loud beep can be generated by the following:
from IPython.display import Audio
import numpy as np
import matplotlib.pyplot as plt
def Harmonic(i, weights):
x=np.linspace(0,3,24000)
y = [0 for _ in x]
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
i = 1000
weights = [1000] * 1000 # Length equal to i
out = Harmonic(i, weights)
Audio(data=out, rate=8000)
Upvotes: 1