You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

拼接方波与f(x)=x²信号未达预期结果的技术问询

波形拼接问题修复

你遇到的核心问题是拼接t数组时的顺序错误:当前代码把t2、t3、t4、t5按非时间递增的顺序拼接,导致plt.plot按照混乱的时间顺序绘制数据点,最终波形和预期不符。

目标波形的时间区间逻辑是:

  • [-1, -0.5]:二次曲线
  • [-0.5, 0]:方波
  • [0, 0.5]:二次曲线
  • [0.5, 1]:方波

而你当前的t数组拼接顺序是(-0.5~0) → (0.5~1) → (-1~-0.5) → (0~0.5),完全打乱了时间轴的连续性。

修正后的代码

import numpy as np
from scipy.signal import square
import matplotlib.pyplot as plt

T = 0.5
dutycycle = 0.5
samples = 10000

t4 = np.linspace(-1, -0.5, samples)
f4 = (t4)**2
t2 = np.linspace(-0.5, 0, samples)
f2 = square(2*np.pi/T*t2, duty=dutycycle)
t5 = np.linspace(0, 0.5, samples)
f5 = (t5)**2
t3 = np.linspace(0.5, 1, samples)
f3 = square(2*np.pi/T*t3, duty=dutycycle)

# 按时间递增顺序拼接数组
t = np.concatenate((t4, t2, t5, t3))
f = np.concatenate((f4, f2, f5, f3))

plt.plot(t, f, label="$x(t)$")
plt.legend()
plt.show()

关键调整说明

  1. 按时间从左到右的逻辑重新排列了各区间数组的定义顺序
  2. 拼接t和f时严格遵循[-1,-0.5] → [-0.5,0] → [0,0.5] → [0.5,1]的时间递增顺序,保证波形连续
  3. 补充了原代码缺失的导入语句,避免执行报错

内容的提问来源于stack exchange,提问作者Lauren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 12:31:03