plt 仿真

加载库和魔法设置

import matplotlib
from IPython import display
%matplotlib notebook
# 下面这句清除plt原来画图
display.clear_output(wait=False)  ##True)

下面是仿真代码

import numpy as np 
from matplotlib import pyplot as plt 
from matplotlib import animation 
  
# First set up the figure, the axis, and the plot element we want to animate 
fig = plt.figure() 
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2)) 
line, = ax.plot([], [], lw=2) 
  
# initialization function: plot the background of each frame 
def init(): 
  line.set_data([], []) 
  return line, 
  
# animation function. This is called sequentially 
# note: i is framenumber 
def animate(i): 
  x = np.linspace(0, 2, 1000) 
  y = np.sin(2 * np.pi * (x - 0.01 * i)) 
  line.set_data(x, y) 
  return line, 
  
# call the animator. blit=True means only re-draw the parts that have changed. 
anim = animation.FuncAnimation(fig, animate, init_func=init, 
                frames=200, interval=20, blit=True) 
  
#anim.save('basic_animation.mp4', fps=30, extra_args=['-vcodec', 'libx264']) 
  
plt.show()



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Python是一种非常流行的通信仿真工具,可以使用Python实现各种通信系统的仿真。下面是一个使用Python实现OFDM通信系统仿真的例子: ```python import numpy as np import matplotlib.pyplot as plt from commpy.channels import SISOFlatChannel from commpy.modulation import QAMModem from commpy.utilities import bitarray2dec, dec2bitarray # OFDM参数 N = 64 # 子载波数 CP = 16 # 循环前缀长度 P = 8 # 导频数 K = N - P # 数据子载波数 QAM = 16 # 调制阶数 # 生成随机数据 data = np.random.randint(0, 2, K) # 生成导频 pilot_value = np.array([1 + 1j, 1 - 1j, -1 + 1j, -1 - 1j]) pilot = np.zeros(N, dtype=complex) pilot_idx = np.arange(0, N, N // P)[:P] pilot[pilot_idx] = pilot_value # 星座映射 modem = QAMModem(QAM) data_mod = modem.modulate(data) # IFFT data_ifft = np.fft.ifft(data_mod, N) # 添加循环前缀 data_with_cp = np.concatenate([data_ifft[-CP:], data_ifft]) # 添加导频 data_with_pilot = np.concatenate([pilot, data_with_cp]) # 信道 channel = SISOFlatChannel(None, 1) channel_response = np.array([1, 0.8 + 0.6j, 0.4 - 0.3j]) channel.set_channel_response(channel_response) rx_signal = channel.propagate(data_with_pilot) # 去除循环前缀 rx_signal_no_cp = rx_signal[CP:] # FFT rx_signal_fft = np.fft.fft(rx_signal_no_cp, N) # 提取导频 pilot_rx = rx_signal_fft[pilot_idx] # 星座解映射 data_demod = modem.demodulate(rx_signal_fft) # 计算误比特率 ber = np.sum(data != data_demod) / K # 可视化 plt.figure(figsize=(8, 6)) plt.subplot(2, 2, 1) plt.plot(data.real, data.imag, 'o') plt.title('Data') plt.subplot(2, 2, 2) plt.plot(pilot.real, pilot.imag, 'o') plt.title('Pilot') plt.subplot(2, 2, 3) plt.plot(rx_signal.real, rx_signal.imag, 'o') plt.title('Received signal') plt.subplot(2, 2, 4) plt.plot(data_demod.real, data_demod.imag, 'o') plt.title('Demodulated data') plt.show() print('误比特率:', ber) ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值