Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading