Is there a way to generate multiple sine waves without using loop in Python?

I am trying to generate multiple sine waves, but I want to avoid using loops. Is there a way to do it as in MATLAB?

Here is what I tried:

fs =250    
n = np.linspace(0,2,int(fs*2),endpoint=False)
frequencies = [1,3,4,12,70]
sines = np.sin(2*np.pi*frequencies*n)

When I tried this code I got the following error:

ValueError: operands could not be broadcast together with shapes (5,) (500,)

Is there any way to do create sine waves with 5 different frequencies without using loop?

>Solution :

You need to reshape n. Try this:

fs =250    
n = np.linspace(0.0,2.0,int(fs*2),endpoint=False)
n = n.reshape(len(n), 1)

frequencies = np.array([1,3,4,12,70])

sines = np.sin(2.0 * np.pi * frequencies * n)

Leave a Reply