lin-win-share/da4008_case_gen/nb_da4008_make_case.ipynb

323 lines
12 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"cells": [
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-03-14T10:01:29.820846Z",
"start_time": "2026-03-14T10:01:28.106163Z"
}
},
"cell_type": "code",
"source": [
"print(\"你好,世界!\")\n",
"name = input(\"请输入你的名字:\")\n",
"print(f\"欢迎你,{name}\")"
],
"id": "eea196e830ee8847",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"你好,世界!\n",
"欢迎你,!\n"
]
}
],
"execution_count": 2
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2026-03-16T12:16:00.812425Z",
"start_time": "2026-03-16T12:16:00.204555Z"
}
},
"cell_type": "code",
"source": [
"\n",
"#!/usr/bin/env python3\n",
"\"\"\"\n",
"批量生成常用测试波形\n",
"直接运行此脚本,一次性生成多个测试文件\n",
"\"\"\"\n",
"\n",
"import subprocess\n",
"import os\n",
"import sys\n",
"import time\n",
"from pathlib import Path\n",
"\n",
"# ========== 配置区 ==========\n",
"SCRIPT_NAME = \"da4008_gen_transaction.py\" # 波形生成脚本文件名\n",
"OUTPUT_DIR = \"./wave\" # 输出目录\n",
"# ============================\n",
"\n",
"# 确保输出目录存在\n",
"os.makedirs(OUTPUT_DIR, exist_ok=True)\n",
"\n",
"# 检查波形生成脚本是否存在\n",
"if not Path(SCRIPT_NAME).is_file():\n",
" print(f\"错误:找不到脚本文件 {SCRIPT_NAME}\")\n",
" print(\"请确保该脚本与批量生成脚本在同一目录下,或修改 SCRIPT_NAME 变量。\")\n",
" sys.exit(1)\n",
"\n",
"# 测试用例定义(使用结构化参数,便于增删改)\n",
"test_cases = [\n",
" {\n",
" \"name\": \"1GHz标准正弦波\",\n",
" \"params\": {\n",
" \"--type\": \"sine\",\n",
" \"--start_addr\": 0x100,\n",
" \"--length\": 10000, ##决定单次的持续波形时长一个点就是0.024ns。 1024就是能够持续24.5ns\n",
" \"--freq\": \"1e9\", ##决定正弦波的周期,频率越大,一个周期组成的点数越少,波形越不准。\n",
" \"--cycles\": 1,\n",
" \"--lvds_addr\": \"0x1000\",\n",
" \"--output\": f\"{OUTPUT_DIR}/sine_1g.txt\"\n",
" }\n",
" },\n",
" {\n",
" \"name\": \"10G正弦波\",\n",
" \"params\": {\n",
" \"--type\": \"sine\",\n",
" \"--start_addr\": 0,\n",
" \"--length\": 1024,\n",
" \"--freq\": \"1e10\",\n",
" \"--cycles\": 3,\n",
" \"--lvds_addr\": \"0x1000\",\n",
" \"--output\": f\"{OUTPUT_DIR}/sine_10g.txt\"\n",
" }\n",
" },\n",
" {\n",
" \"name\": \"20G正弦波\",\n",
" \"params\": {\n",
" \"--type\": \"sine\",\n",
" \"--start_addr\": 0,\n",
" \"--length\": 1024,\n",
" \"--freq\": \"2e10\",\n",
" \"--cycles\": 3,\n",
" \"--lvds_addr\": \"0x1000\",\n",
" \"--output\": f\"{OUTPUT_DIR}/sine_20g.txt\"\n",
" }\n",
" },\n",
" {\n",
" \"name\": \"1MHz正弦波\",\n",
" \"params\": {\n",
" \"--type\": \"sine\",\n",
" \"--start_addr\": 0x0,\n",
" \"--length\": 41667,\n",
" \"--freq\": \"1e6\",\n",
" \"--cycles\": 3,\n",
" \"--lvds_addr\": \"0x0\",\n",
" \"--output\": f\"{OUTPUT_DIR}/sine_1MHz.txt\"\n",
" }\n",
" },\n",
" {\n",
" \"name\": \"100Hz正弦波\",\n",
" \"params\": {\n",
" \"--type\": \"sine\",\n",
" \"--start_addr\": 0,\n",
" \"--length\": 1024,\n",
" \"--freq\": \"1e2\",\n",
" \"--cycles\": 3,\n",
" \"--lvds_addr\": \"0x1000\",\n",
" \"--output\": f\"{OUTPUT_DIR}/sine_100Hz.txt\"\n",
" }\n",
" },\n",
"\n",
" {\n",
" \"name\": \"测试用例3方波\",\n",
" \"params\": {\n",
" \"--type\": \"pulse\",\n",
" \"--start_addr\": 0,\n",
" \"--length\": 1024,\n",
" \"--freq\": \"500e6\",\n",
" \"--duty\": 0.5,\n",
" \"--cycles\": 3,\n",
" \"--lvds_addr\": \"0x1000\",\n",
" \"--output\": f\"{OUTPUT_DIR}/test3_pulse_500m.txt\"\n",
" }\n",
" },\n",
" {\n",
" \"name\": \"测试用例4三角波\",\n",
" \"params\": {\n",
" \"--type\": \"triangle\",\n",
" \"--start_addr\": 0,\n",
" \"--length\": 1024,\n",
" \"--step\": 16,\n",
" \"--duration\": 10,\n",
" \"--cycles\": 3,\n",
" \"--lvds_addr\": \"0x1000\",\n",
" \"--output\": f\"{OUTPUT_DIR}/test4_triangle.txt\"\n",
" }\n",
" },\n",
" {\n",
" \"name\": \"测试用例5Flattop波形\",\n",
" \"params\": {\n",
" \"--type\": \"flattop\",\n",
" \"--start_addr\": 0,\n",
" \"--rise_samples\": 100,\n",
" \"--hold_cycles\": 50,\n",
" \"--fall_samples\": 100,\n",
" \"--hold_value\": 255,\n",
" \"--cycles\": 1,\n",
" \"--lvds_addr\": \"0x1000\",\n",
" \"--output\": f\"{OUTPUT_DIR}/test5_flattop.txt\"\n",
" }\n",
" }\n",
"]\n",
"\n",
"def build_command(params):\n",
" \"\"\"将参数字典转换为命令行列表避免shell注入\"\"\"\n",
" cmd = [sys.executable, SCRIPT_NAME]\n",
" for key, value in params.items():\n",
" cmd.append(key)\n",
" cmd.append(str(value))\n",
" return cmd\n",
"\n",
"def run_test_case(case):\n",
" \"\"\"执行单个测试用例,返回成功标志和输出信息\"\"\"\n",
" name = case[\"name\"]\n",
" cmd = build_command(case[\"params\"])\n",
" cmd_str = \" \".join(cmd)\n",
"\n",
" print(f\"\\n▶ {name}\")\n",
" print(f\"命令: {cmd_str}\")\n",
"\n",
" try:\n",
" # 使用列表形式调用安全且无shell=True\n",
" result = subprocess.run(cmd, capture_output=True, text=True, timeout=30)\n",
" if result.returncode == 0:\n",
" print(\"✅ 成功\")\n",
" return True, result.stdout\n",
" else:\n",
" print(\"❌ 失败\")\n",
" print(f\"返回码: {result.returncode}\")\n",
" if result.stderr:\n",
" print(\"错误输出:\")\n",
" print(result.stderr)\n",
" return False, result.stderr\n",
" except subprocess.TimeoutExpired:\n",
" print(\"❌ 超时超过30秒\")\n",
" return False, \"执行超时\"\n",
" except Exception as e:\n",
" print(f\"❌ 异常: {e}\")\n",
" return False, str(e)\n",
"\n",
"def main():\n",
" print(\"开始批量生成测试文件...\")\n",
" print(f\"输出目录: {OUTPUT_DIR}\")\n",
" print(\"=\" * 60)\n",
"\n",
" start_time = time.time()\n",
" success_count = 0\n",
" fail_count = 0\n",
"\n",
" for i, case in enumerate(test_cases, 1):\n",
" print(f\"\\n[{i}/{len(test_cases)}] 正在生成...\")\n",
" success, _ = run_test_case(case)\n",
" if success:\n",
" success_count += 1\n",
" else:\n",
" fail_count += 1\n",
"\n",
" elapsed = time.time() - start_time\n",
" print(\"\\n\" + \"=\" * 60)\n",
" print(f\"批量生成完成!\")\n",
" print(f\"成功: {success_count}, 失败: {fail_count}, 耗时: {elapsed:.2f} 秒\")\n",
" print(f\"文件保存在 {OUTPUT_DIR} 目录下\")\n",
"\n",
"if __name__ == \"__main__\":\n",
" main()"
],
"id": "33083ea8b8420b7e",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"开始批量生成测试文件...\n",
"输出目录: ./wave\n",
"============================================================\n",
"\n",
"[1/8] 正在生成...\n",
"\n",
"▶ 1GHz标准正弦波\n",
"命令: D:\\yangshenbo\\workspace\\Document\\python_project\\da4008_case_gen\\.venv\\Scripts\\python.exe da4008_gen_transaction.py --type sine --start_addr 256 --length 10000 --freq 1e9 --cycles 1 --lvds_addr 0x1000 --output ./wave/sine_1g.txt\n",
"✅ 成功\n",
"\n",
"[2/8] 正在生成...\n",
"\n",
"▶ 10G正弦波\n",
"命令: D:\\yangshenbo\\workspace\\Document\\python_project\\da4008_case_gen\\.venv\\Scripts\\python.exe da4008_gen_transaction.py --type sine --start_addr 0 --length 1024 --freq 1e10 --cycles 3 --lvds_addr 0x1000 --output ./wave/sine_10g.txt\n",
"✅ 成功\n",
"\n",
"[3/8] 正在生成...\n",
"\n",
"▶ 20G正弦波\n",
"命令: D:\\yangshenbo\\workspace\\Document\\python_project\\da4008_case_gen\\.venv\\Scripts\\python.exe da4008_gen_transaction.py --type sine --start_addr 0 --length 1024 --freq 2e10 --cycles 3 --lvds_addr 0x1000 --output ./wave/sine_20g.txt\n",
"✅ 成功\n",
"\n",
"[4/8] 正在生成...\n",
"\n",
"▶ 1MHz正弦波\n",
"命令: D:\\yangshenbo\\workspace\\Document\\python_project\\da4008_case_gen\\.venv\\Scripts\\python.exe da4008_gen_transaction.py --type sine --start_addr 0 --length 41667 --freq 1e6 --cycles 3 --lvds_addr 0x0 --output ./wave/sine_1MHz.txt\n",
"✅ 成功\n",
"\n",
"[5/8] 正在生成...\n",
"\n",
"▶ 100Hz正弦波\n",
"命令: D:\\yangshenbo\\workspace\\Document\\python_project\\da4008_case_gen\\.venv\\Scripts\\python.exe da4008_gen_transaction.py --type sine --start_addr 0 --length 1024 --freq 1e2 --cycles 3 --lvds_addr 0x1000 --output ./wave/sine_100Hz.txt\n",
"✅ 成功\n",
"\n",
"[6/8] 正在生成...\n",
"\n",
"▶ 测试用例3方波\n",
"命令: D:\\yangshenbo\\workspace\\Document\\python_project\\da4008_case_gen\\.venv\\Scripts\\python.exe da4008_gen_transaction.py --type pulse --start_addr 0 --length 1024 --freq 500e6 --duty 0.5 --cycles 3 --lvds_addr 0x1000 --output ./wave/test3_pulse_500m.txt\n",
"✅ 成功\n",
"\n",
"[7/8] 正在生成...\n",
"\n",
"▶ 测试用例4三角波\n",
"命令: D:\\yangshenbo\\workspace\\Document\\python_project\\da4008_case_gen\\.venv\\Scripts\\python.exe da4008_gen_transaction.py --type triangle --start_addr 0 --length 1024 --step 16 --duration 10 --cycles 3 --lvds_addr 0x1000 --output ./wave/test4_triangle.txt\n",
"✅ 成功\n",
"\n",
"[8/8] 正在生成...\n",
"\n",
"▶ 测试用例5Flattop波形\n",
"命令: D:\\yangshenbo\\workspace\\Document\\python_project\\da4008_case_gen\\.venv\\Scripts\\python.exe da4008_gen_transaction.py --type flattop --start_addr 0 --rise_samples 100 --hold_cycles 50 --fall_samples 100 --hold_value 255 --cycles 1 --lvds_addr 0x1000 --output ./wave/test5_flattop.txt\n",
"✅ 成功\n",
"\n",
"============================================================\n",
"批量生成完成!\n",
"成功: 8, 失败: 0, 耗时: 0.60 秒\n",
"文件保存在 ./wave 目录下\n"
]
}
],
"execution_count": 20
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}