Python how to plot graph sine wave

前端 未结 9 1226
死守一世寂寞
死守一世寂寞 2021-02-01 07:11

I have this signal :

from math import*
Fs=8000
f=500
sample=16
a=[0]*sample
for n in range(sample):
    a[n]=sin(2*pi*f*n/Fs)

How can I plot a

相关标签:
9条回答
  • 2021-02-01 07:41

    A simple way to plot sine wave in python using matplotlib.

    import numpy as np
    import matplotlib.pyplot as plt
    
    
    x=np.arange(0,3*np.pi,0.1)
    y=np.sin(x)
    plt.plot(x,y)
    plt.title("SINE WAVE")
    plt.show()
    
    0 讨论(0)
  • 2021-02-01 07:41

    Yet another way to plot the sine wave.

    import numpy as np
    import matplotlib
    matplotlib.use('TKAgg') #use matplotlib backend TKAgg (optional)
    import matplotlib.pyplot as plt
    
    t = np.linspace(0.0, 5.0, 50000)       # time axis
    sig = np.sin(t)
    plt.plot(t,sig)
    
    0 讨论(0)
    • Setting the x-axis with np.arange(0, 1, 0.001) gives an array from 0 to 1 in 0.001 increments.
      • x = np.arange(0, 1, 0.001) returns an array of 1000 points from 0 to 1, and y = np.sin(2*np.pi*x) you will get the sin wave from 0 to 1 sampled 1000 times

    I hope this will help:

    import matplotlib.pyplot as plt
    import numpy as np
    
    
    Fs = 8000
    f = 5
    sample = 8000
    x = np.arange(sample)
    y = np.sin(2 * np.pi * f * x / Fs)
    plt.plot(x, y)
    plt.xlabel('sample(n)')
    plt.ylabel('voltage(V)')
    plt.show()
    

    P.S.: For comfortable work you can use The Jupyter Notebook.

    0 讨论(0)
  • 2021-02-01 07:45
    import matplotlib.pyplot as plt
    import numpy as np
    #%matplotlib inline
    x=list(range(10))
    def fun(k):
         return np.sin(k)
    y=list(map(fun,x))
    plt.plot(x,y,'-.')
    #print(x)
    #print(y)
    plt.show()
    
    0 讨论(0)
  • 2021-02-01 07:47
    import math
    import turtle
    
    ws = turtle.Screen()
    ws.bgcolor("lightblue")
    fred = turtle.Turtle()
    for angle in range(360):
        y = math.sin(math.radians(angle))
        fred.goto(angle, y * 80)
    
    ws.exitonclick()
    
    0 讨论(0)
  • 2021-02-01 07:49
    import numpy as np
    import matplotlib.pyplot as plt
    
    F = 5.e2          # No. of cycles per second, F = 500 Hz
    T = 2.e-3         # Time period, T = 2 ms
    Fs = 50.e3        # No. of samples per second, Fs = 50 kHz
    Ts = 1./Fs        # Sampling interval, Ts = 20 us
    N = int(T/Ts)     # No. of samples for 2 ms, N = 100
    
    t = np.linspace(0, T, N)
    signal = np.sin(2*np.pi*F*t)
    
    plt.plot(t, signal)
    plt.xlabel('Time (s)')
    plt.ylabel('Voltage (V)')
    plt.show()
    
    0 讨论(0)
提交回复
热议问题