Home  >  Article  >  Backend Development  >  Examples of time domain waveforms and spectrograms of sinusoidal signals based on matplotlib Python

Examples of time domain waveforms and spectrograms of sinusoidal signals based on matplotlib Python

不言
不言Original
2018-05-04 14:30:035480browse

This article mainly introduces Python to implement the time domain waveform and spectrogram of sinusoidal signals, involving Python mathematical operations and graphics drawing related operating skills. Friends in need can refer to the following

The examples in this article describe the Python implementation Time domain waveforms and spectrograms of sinusoidal signals. Share it with everyone for your reference, the details are as follows:

# -*- coding: utf-8 -*-
# 正弦信号的时域波形与频谱图
import numpy as np
import matplotlib.pyplot as pl
import matplotlib
import math
import random
row = 4
col = 4
N = 500
fs = 5
n = [2*math.pi*fs*t/N for t in range(N)]  # 生成了500个介于0.0-31.35之间的点
# print n
axis_x = np.linspace(0,3,num=N)
#频率为5Hz的正弦信号
x = [math.sin(i) for i in n]
pl.subplot(221)
pl.plot(axis_x,x)
pl.title(u'5Hz的正弦信号',fontproperties='SimHei')
pl.axis('tight')
#频率为5Hz、幅值为3的正弦+噪声
x1 = [random.gauss(0,0.5) for i in range(N)]
xx = []
#有没有直接两个列表对应项相加的方式??
for i in range(len(x)):
  xx.append(x[i]*3 + x1[i])
pl.subplot(222)
pl.plot(axis_x,xx)
pl.title(u'频率为5Hz、幅值为3的正弦+噪声',fontproperties='SimHei')
pl.axis('tight')
#频谱绘制
xf = np.fft.fft(x)
xf_abs = np.fft.fftshift(abs(xf))
axis_xf = np.linspace(-N/2,N/2-1,num=N)
pl.subplot(223)
pl.title(u'频率为5Hz的正弦频谱图',fontproperties='SimHei')
pl.plot(axis_xf,xf_abs)
pl.axis('tight')
#频谱绘制
xf = np.fft.fft(xx)
xf_abs = np.fft.fftshift(abs(xf))
pl.subplot(224)
pl.title(u'频率为5Hz的正弦频谱图',fontproperties='SimHei')
pl.plot(axis_xf,xf_abs)
pl.axis('tight')
pl.show()

Operation effect:

Related recommendations:

Example of the least common multiple algorithm implemented in Python

The above is the detailed content of Examples of time domain waveforms and spectrograms of sinusoidal signals based on matplotlib Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn