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

Python中生成可自定义短时长双相脉冲的问题求助

Python中生成可自定义短时长双相脉冲的问题求助

大家好,我最近在Python里尝试生成双相脉冲,想要实现1微秒时长的脉冲,脉冲之间还有1微秒的间隔,但是不管怎么试都没法生成这么短的脉冲。我在Matlab里已经成功实现了,但Python里就是搞不定,有没有大佬能帮我看看?

核心问题

我需要一段可以直接设置脉冲时长和间隔时长的代码(时长范围是1微秒到0.1毫秒),但现在我的代码只能通过样本数来设置,而且生成的脉冲太长了,没法直接设置比如pulse_width=0.001(对应0.001毫秒即1微秒)和break_width=0.001这样的时间参数。

我目前的代码

import numpy as np
import matplotlib.pyplot as plt

pointCount = 5000
voltages = np.empty(pointCount)
times = np.arange(pointCount) * 0.05

# Stimulus:
stim = np.zeros(pointCount)
amplitude = 1  # Pulse amplitude
num_pulses = 400  # Number of biphasic pulses
pulse_width = 2   # Width of each pulse (samples)
break_width = 2   # Break duration (samples)
start_index = 1200  # Start of first pulse

# Generate 50 biphasic pulses
for i in range(num_pulses):
    index = start_index + i * (2 * pulse_width + 2 * break_width)  # Calculate start index for each pulse pair
    stim[index:index + pulse_width] = amplitude    # Positive pulse
    stim[index + pulse_width + break_width:index + 2 * pulse_width + break_width] = -amplitude  # Negative pulse

plt.figure()
plt.plot(times, stim, 'r')
plt.xlabel('Time (ms)')
plt.ylabel('Amplitude (mV)')
plt.show()

期望的脉冲效果

期望的1us时长双相脉冲效果


嗨,我来帮你分析下问题所在,其实核心问题出在采样率和参数定义方式上:

  1. 你的时间步长太大了:你代码里的times = np.arange(pointCount) * 0.05,这里的0.05是毫秒,也就是每个样本对应50微秒的时长!这意味着你最小的脉冲宽度就是50微秒,根本没法生成1微秒的脉冲,因为单个样本就比你要的脉冲还长。

  2. 参数用样本数定义不直观:你现在用pulse_width=2表示2个样本的脉冲,没法直接对应实际的时间长度,这也是你没法直接设置1us的原因。

修改后的解决方案

我们可以直接用实际时间参数来定义脉冲和间隔,同时提高采样率到足够高(比如1MHz,对应1微秒/样本,刚好满足你的需求),这样就能精准控制短时长脉冲了:

import numpy as np
import matplotlib.pyplot as plt

# -------------------------- 实际时间参数(直接设置你想要的值) --------------------------
total_duration_ms = 250  # 总时长(毫秒),根据需要调整
sample_rate = 1e6        # 采样率:1MHz = 1微秒/样本,刚好适配1us的脉冲
amplitude = 1            # 脉冲幅度
num_pulses = 400         # 双相脉冲的数量
pulse_width_us = 1       # 单个脉冲时长(微秒)
break_width_us = 1       # 脉冲间间隔时长(微秒)
start_time_ms = 60       # 第一个脉冲的起始时间(毫秒)

# -------------------------- 转换为样本数和时间数组 --------------------------
sample_rate_per_ms = sample_rate / 1000  # 每毫秒的样本数
pointCount = int(total_duration_ms * sample_rate_per_ms)
times = np.arange(pointCount) / sample_rate_per_ms  # 时间数组(单位:毫秒)

# 把时间参数转换为样本数
pulse_samples = int(pulse_width_us / 1000 * sample_rate)  # 1us = 0.001ms,转换为样本数
break_samples = int(break_width_us / 1000 * sample_rate)
start_index = int(start_time_ms * sample_rate_per_ms)

# -------------------------- 生成双相脉冲 --------------------------
stim = np.zeros(pointCount)
# 每个双相脉冲的总样本数:正脉冲 + 间隔 + 负脉冲 + 下一个脉冲前的间隔
pulse_pair_total_samples = pulse_samples + break_samples + pulse_samples + break_samples

for i in range(num_pulses):
    index = start_index + i * pulse_pair_total_samples
    # 正脉冲
    stim[index:index + pulse_samples] = amplitude
    # 负脉冲(正脉冲后加间隔)
    neg_start = index + pulse_samples + break_samples
    stim[neg_start:neg_start + pulse_samples] = -amplitude

# -------------------------- 绘图展示 --------------------------
plt.figure(figsize=(10,4))
plt.plot(times, stim, 'r')
plt.xlabel('Time (ms)')
plt.ylabel('Amplitude (mV)')
plt.title('1us双相脉冲波形')
plt.xlim(start_time_ms - 0.1, start_time_ms + 0.01)  # 放大看第一个脉冲的细节
plt.show()

关键修改点

  • 直接用微秒/毫秒定义脉冲时长,不用再手动换算样本数,更直观
  • 采样率设为1MHz,刚好每个样本对应1微秒,完美适配你的短脉冲需求
  • 时间数组的生成更准确,和实际采样率完全对应
  • 可以通过调整pulse_width_us和break_width_us直接设置1us到0.1ms范围内的任意时长

这样修改后,你就能生成和Matlab里一样的1us短脉冲啦!


备注:内容来源于stack exchange,提问作者marina05

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.14 10:33:01