📜  使用 Matplotlib 在Python中绘制幅度谱

📅  最后修改于: 2022-05-13 01:55:23.348000             🧑  作者: Mango

使用 Matplotlib 在Python中绘制幅度谱

信号是用于传输数据的电磁场或电流。信号有多种成分,例如频率、幅度、波长、相位、角频率和描述信号的周期。
可以使用以下正弦函数表示周期信号:

y = A sin(w*t + Q)

其中A代表幅度(米), w代表频率(赫兹), t代表时间周期(秒), Q代表周期信号的相位(弧度)。

周期信号的两个主要分量频率和幅度定义了该信号的幅度谱。横轴表示周期信号的频率成分,纵轴表示周期信号的振幅成分。

在Python中, Python matplotlib库的pyplot模块中的magnitude_spectrum()方法绘制了周期信号的幅度谱。下面是一些程序,演示了使用幅度谱magnitude_spectrum()方法来可视化不同周期信号的幅度谱。
示例 1:

# importing modules
import numpy
from matplotlib import pyplot 
   
# assigning time values of the signal
# initial time period, final time period and phase angle 
signalTime = numpy.arange(5, 10, 0.25);
  
# getting the amplitude of the signal
signalAmplitude = numpy.sin(signalTime)
  
# plotting the signal 
pyplot.plot(signalTime, signalAmplitude, color ='green')
  
pyplot.xlabel('Time')
pyplot.ylabel('Amplitude')
pyplot.title("Signal")
  
  
# plotting the magnitude spectrum of the signal 
pyplot.magnitude_spectrum(signalAmplitude, color ='green')
  
pyplot.title("Magnitude Spectrum of the Signal")
pyplot.show()

输出:


第一张图表示幅度与时间分量中的信号,第二张图表示幅度与频率图中信号的幅度谱,通过对具有 5 到 10 秒时间段、0.25 弧度相位角、频率的信号使用幅度谱magnitude_spectrum()从给定的时间段计算信号的幅度,并使用numpy模块中的sin()函数计算信号的幅度。

示例 2:

# importing modules
import numpy
from matplotlib import pyplot 
   
# assigning time values of the signal
# initial time period, final time period and phase angle 
signalTime = numpy.arange(0, 1, 0.1);
  
# getting the amplitude of the signal
signalAmplitude = numpy.sin(signalTime)
  
# plotting the signal 
pyplot.plot(signalTime, signalAmplitude, color ='green')
  
pyplot.xlabel('Time')
pyplot.ylabel('Amplitude')
pyplot.title("Signal")
  
  
# plotting the magnitude spectrum of the signal 
pyplot.magnitude_spectrum(signalAmplitude, color ='green')
  
pyplot.title("Magnitude Spectrum of the Signal")
pyplot.show()

输出:


在上面的程序中,由于信号的幅度随时间增加,所以在第一张图中没有形成正弦波。信号存在于 0 到 1 秒的时间段内,相位角为 0.1 弧度,信号的幅度谱使用幅度谱magnitude_spectrum()方法描述。

示例 3:

# importing modules
import numpy
from matplotlib import pyplot 
   
# assigning time values of the signal
# initial time period, final time period and phase angle  
signalTime = numpy.arange(1, 100, 0.5);
  
# getting the amplitude of the signal
signalAmplitude = numpy.sin(signalTime)
  
# plotting the signal 
pyplot.plot(signalTime, signalAmplitude, color ='green')
  
pyplot.xlabel('Time')
pyplot.ylabel('Amplitude')
pyplot.title("Signal")
  
  
# plotting the magnitude spectrum of the signal 
pyplot.magnitude_spectrum(signalAmplitude, color ='green')
  
pyplot.title("Magnitude Spectrum of the Signal")
pyplot.show()

输出:


在这里,信号以形成正弦波的幅度与时间图表示,并且使用幅度与频率图中的幅度谱magnitude_spectrum()方法表示信号的幅度谱。信号的时间周期从 1 秒到 100 秒,相位角为 0.5 弧度。