close

How do I generate a sine wave using Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How do I generate a sine wave using Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How do I generate a sine wave using Python?

  1. How do I generate a sine wave using Python?

    You are mixing sample value and length! Try:
    samples = np.linspace(0, t, t * fs)
    Another detail: you don't need the str around raw_input.

  2. generate a sine wave using Python

    You are mixing sample value and length! Try:
    samples = np.linspace(0, t, t * fs)
    Another detail: you don't need the str around raw_input.

Method 1

Change

samples = np.arange(t * fs)

to

samples = np.linspace(0, t, int(fs*t), endpoint=False)

(This assumes that fs*t results in an integer value.)

Or, as Paul Panzer suggests in a comment,

samples = np.arange(t * fs) / fs

Your samples was just the sequence of integers 0, 1, 2, … fs*t. Instead, you need the actual time values (in seconds) of the samples.

Method 2

You are mixing sample value and length! Try:

samples = np.linspace(0, t, t * fs)

Another detail: you don’t need the str around raw_input.

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read