Python how to plot graph sine wave

前端 未结 9 1236
死守一世寂寞
死守一世寂寞 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:49

    The window of usefulness has likely come and gone, but I was working at a similar problem. Here is my attempt at plotting sine using the turtle module.

    from turtle import *
    from math import *
    
    #init turtle
    T=Turtle()
    
    #sample size
    T.screen.setworldcoordinates(-1,-1,1,1) 
    
    #speed up the turtle
    T.speed(-1)
    
    #range of hundredths from -1 to 1
    xcoords=map(lambda x: x/100.0,xrange(-100,101))
    
    #setup the origin
    T.pu();T.goto(-1,0);T.pd()
    
    #move turtle
    for x in xcoords:
        T.goto(x,sin(xcoords.index(x)))
    
    0 讨论(0)
  • 2021-02-01 07:59

    This is another option

    #!/usr/bin/env python
    
    import numpy as np
    import matplotlib
    matplotlib.use('TKAgg') #use matplotlib backend TkAgg (optional)
    import matplotlib.pyplot as plt
    
    sample_rate = 200 # sampling frequency in Hz (atleast 2 times f)
    t = np.linspace(0,5,sample_rate)    #time axis
    f = 100 #Signal frequency in Hz
    sig = np.sin(2*np.pi*f*(t/sample_rate))
    plt.plot(t,sig)
    plt.xlabel("Time")
    plt.ylabel("Amplitude")
    plt.tight_layout() 
    plt.show()
    
    0 讨论(0)
  • 2021-02-01 08:00
    import matplotlib.pyplot as plt # For ploting
    import numpy as np # to work with numerical data efficiently
    
    fs = 100 # sample rate 
    f = 2 # the frequency of the signal
    
    x = np.arange(fs) # the points on the x axis for plotting
    # compute the value (amplitude) of the sin wave at the for each sample
    y = np.sin(2*np.pi*f * (x/fs)) 
    
    #this instruction can only be used with IPython Notbook. 
    % matplotlib inline
    # showing the exact location of the smaples
    plt.stem(x,y, 'r', )
    plt.plot(x,y)
    

    0 讨论(0)
提交回复
热议问题