ARC094F Normalization

本文解析了一道AtCoder竞赛中的字符串操作问题,探讨如何通过动态规划算法计算给定字符串经特定转换后可能得到的不同字符串数量。重点在于利用模3运算保持序列和不变性的观察,以及基于此设计的状态转移方程。

原题链接:https://beta.atcoder.jp/contests/arc094/tasks/arc094_d

Normalization

Problem Statement

You are given a string S S consisting of ‘a’,’b’ and ‘c’. Find the number of strings that can be possibly obtained by repeatedly performing the following operation zero or more times, modulo 998244353:

Choose an integer i i such that 1i|S|1 and the i i -th and (i+1)-th characters in S S are different. Replace each of the i-th and (i+1) ( i + 1 ) -th characters in S S with the character that differs from both of them (among a, b and c).

Constraints

2|S|2×105
S S consists of ‘a’, ‘b’ and ‘c’.

Input

Input is given from Standard Input in the following format:

S

Output

Print the number of strings that can be possibly obtained by repeatedly performing the operation, modulo 998244353 998244353 .

Sample Input 1

abc

Sample Output 1

3
abc, aaa and ccc can be obtained.

Sample Input 2

abbac

Sample Output 2

65

Sample Input 3

babacabac

Sample Output 3

6310

Sample Input 4

ababacbcacbacacbcbbcbbacbaccacbacbacba

Sample Output 4

148010497

题目大意

给定一个由 a,b,c a , b , c 组成的字符串 S|S|<=2×105 S ( | S | <= 2 × 10 5 ) ,每次可以选择两个相邻的不同字符,把它们修改成与两者都不同的字符,求能得到的不同的字符串的个数。

题解

%%%watson,watson is so strong!!! % % % w a t s o n , w a t s o n   i s   s o   s t r o n g ! ! !

感觉这道题真神(虽然watson说很水),如果把 a,b,c a , b , c 表示为 0,1,2 0 , 1 , 2 ,可以发现上述替换操作在 mod 3 m o d   3 意义下是不改变整个序列的和的。

然后开始 dp d p dp[i][j][k][p] d p [ i ] [ j ] [ k ] [ p ] 表示在第 i i 个位置,前缀和为j(mod 3),当前字符为 k k ,有/无连续的相同字符时的字符个数,枚举一下i,i+1的字符分别是什么就可以转移了。

最后的答案为 dp[n][tot][0][1]+dp[n][tot][1][1]+dp[n][tot][2][1]+[] d p [ n ] [ t o t ] [ 0 ] [ 1 ] + d p [ n ] [ t o t ] [ 1 ] [ 1 ] + d p [ n ] [ t o t ] [ 2 ] [ 1 ] + [ 原 串 没 有 连 续 的 相 同 字 母 ]

代码
#include<bits/stdc++.h>
using namespace std;
const int M=2e5+5,mod=998244353;
char ch[M];
int n,sum,i,j,k,a,b,dp[M][3][3][2];
bool flag=1;
void spj()
{
    for(i=2;i<=n;++i)if(ch[i]!=ch[i-1]){flag=0;break;}
    if(flag)puts("1"),exit(0);
    if(n<=3)(n==2?puts("2"):((ch[1]!=ch[2]&&ch[1]!=ch[3]&&ch[2]!=ch[3])?puts("3"):((ch[1]==ch[3])?puts("7"):puts("6")))),exit(0);
}
bool check(){for(int i=1;i<=n;++i)if(ch[i]==ch[i-1])return 0;return 1;}
void in(){scanf("%s",ch+1);}
void ac()
{
    n=strlen(ch+1);spj();
    for(i=1;i<=n;++i)sum=(sum+ch[i]-'a')%3;
    for(i=0;i<=2;++i)dp[1][i][i][0]=1;
    for(i=1;i<=n-1;++i)for(j=0;j<=2;++j)for(k=0;k<=2;++k)for(a=0;a<=1;++a)for(b=0;b<=2;++b)
    (dp[i+1][(j+b)%3][b][a|(k==b)]+=dp[i][j][k][a])%=mod;
    printf("%d",(0ll+dp[n][sum][0][1]+dp[n][sum][1][1]+dp[n][sum][2][1]+check())%mod);
}
int main(){in();ac();}
最新的代码如下,请帮忙绘图加一个功能,画出眼图中心位置,并打印出眼图中心位置的X和Y轴坐标,并同时打印出换算回来的LOG中表达方式的16进制Vref,和X轴对应的十进制的延迟数字(图是包含0X38C偏移,所以这里的值也是偏移后的)。当前已经算出最大眼宽和眼高,用细线或者在图中把最大眼宽和眼高连线起来,然后交点位置,标出来打印换算前后坐标信息,就好了。这样就有了所有DQ每个DQ一个眼图中心点的Vref和Delay,然后把所有DQ的这个值,求出平均数和中位数,并以16进制Vref和10进制Delay(原自然数单位,不是UI单位)打印在整个图片最下方。新代码不要删除已有注释。新写的部分也要加注释。 ===================================================== 导入必要的库模块 - 详细解释每个模块的作用 ===================================================== import numpy as np “”" NumPy (Numerical Python) - 科学计算基础库 主要功能: 提供高效的N维数组对象(ndarray) 支持广播功能函数 提供线性代数、傅里叶变换、随机数生成等功能 在本代码中主要用于: 数值计算和数组操作 科学计算支持 “”" import matplotlib.pyplot as plt “”" Matplotlib - Python中最强大的绘图库 主要功能: 创建静态、动态和交互式图表 支持多种图表类型(线图、散点图、柱状图等) 高度可定制化(颜色、线型、标签等) 在本代码中主要用于: 绘制眼图 可视化DDR校准数据 创建图表和图形界面 “”" import re “”" re (Regular Expression) - 正则表达式模块 主要功能: 文本搜索和模式匹配 文本替换 复杂字符串处理 在本代码中主要用于: 解析日志文件中的关键数据 提取VREF、偏移量、数据点等信息 处理复杂的文本匹配任务 “”" import datetime “”" datetime - 日期和时间处理模块 主要功能: 日期和时间的表示 日期和时间的计算 日期和时间的格式化 在本代码中主要用于: 生成时间戳 创建带时间戳的文件名 记录报告生成时间 “”" from matplotlib.lines import Line2D “”" Line2D - 用于创建二维线条对象 主要功能: 表示二维坐标系中的线条 控制线条属性(颜色、线宽、样式等) 在本代码中主要用于: 创建图例元素 自定义图表中的线条样式 “”" import os “”" os (Operating System) - 操作系统接口模块 主要功能: 文件和目录操作 路径操作 进程管理 在本代码中主要用于: 文件路径处理 目录创建 文件存在性检查 “”" from collections import defaultdict “”" defaultdict - 带默认值的字典 主要功能: 当访问不存在的键时返回默认值 避免KeyError异常 在本代码中主要用于: 存储电压-窗口映射关系 简化字典初始化操作 “”" ===================================================== 健壮的文件读取函数 - 详细解释每个编程概念 ===================================================== def robust_read_file(file_path): “”" 健壮的文件读取函数,处理不同编码的文件 参数: file_path - 文件在电脑上的完整路径(字符串) 编程概念详解: 1. 函数定义:def关键字用于定义函数,函数是一段可重复使用的代码块 2. 参数传递:file_path是形式参数,调用时传入实际文件路径 3. 异常处理:try-except结构用于捕获和处理运行时错误 4. 上下文管理器:with语句用于资源管理,确保文件正确关闭 5. 编码处理:不同文件可能使用不同编码(UTF-8, Latin-1等) 6. 正则表达式:用于过滤控制字符 """ ########################################################## # try-except 异常处理结构 # try: 尝试执行可能出错的代码块 ########################################################## try: ########################################################## # with 上下文管理器 # 语法:with expression [as variable]: # 特点:自动管理资源(如文件),确保资源正确释放 # open() 函数:打开文件 # 'r':只读模式 # encoding='utf-8':指定UTF-8编码 ########################################################## with open(file_path, 'r', encoding='utf-8') as f: # 文件对象方法:f.read()读取整个文件内容 return f.read() ########################################################## # except 捕获特定异常 # UnicodeDecodeError:当文件编码不匹配时抛出 ########################################################## except UnicodeDecodeError: try: # 尝试使用Latin-1编码(支持所有256个字节值) with open(file_path, 'r', encoding='latin-1') as f: content = f.read() ########################################################## # 正则表达式详解:r'[\x00-\x1F]+' # 用途:匹配并删除控制字符(ASCII 0-31) # 分解: # r'':原始字符串,避免转义字符处理 # []:字符类,匹配括号内任意字符 # \x00-\x1F:十六进制范围,表示ASCII控制字符(0-31) # +:量词,匹配1次或多次 # re.sub():替换匹配项 # 参数1:模式 # 参数2:替换内容(空字符串) # 参数3:输入字符串 ########################################################## return re.sub(r'[\x00-\x1F]+', '', content) ########################################################## # Exception 捕获所有异常 # 通用异常处理,打印错误信息 ########################################################## except Exception as e: # 格式化字符串:f-string (Python 3.6+) print(f"文件解码错误: {e}") return None ===================================================== 日志解析函数 - 重点讲解正则表达式 ===================================================== def parse_log_file(log_content, normalization_point): “”" 解析DDR校准日志文件,提取关键数据 参数: log_content - 日志文件的内容(字符串) normalization_point - 归一化点(十六进制整数) 数据结构说明: data = { vref: { dq_index: { 'read': (min, max, window), 'write': (min, max, window) } } } raw_data = { vref: { dq_index: { 'read': {'min': min_val, 'max': max_val}, 'write': {'min': min_val, 'max': max_val} } } } """ # 初始化数据结构 data = {} # 主数据结构,存储解析后的数据 current_vref = None # 当前处理的vref值 pending_data = {} # 临时存储待处理的数据(字典) current_offset = None # 当前偏移量 raw_data = {} # 存储原始数据(偏移前) # 按行处理日志内容 # 字符串方法:split('\n') 按换行符分割字符串 for line in log_content.split('\n'): # 字符串方法:strip() 移除首尾空白字符 line = line.strip() # 空行检查 if not line: continue # 跳过空行 ########################################################## # 正则表达式1:匹配VREF行 # 模式:r'.*vref:\s*0x([0-9a-fA-F]+)' # 目标示例: "Setting vref: 0x1A3" # # 详细分解: # .* - 匹配任意字符(除换行符外)0次或多次(贪婪匹配) # vref: - 匹配字面字符串 "vref:" # \s* - 匹配0个或多个空白字符(空格、制表符等) # 0x - 匹配字面字符串 "0x" # ( - 开始捕获组 # [0-9a-fA-F] - 字符类,匹配十六进制字符(0-9, a-f, A-F) # + - 匹配前面的元素1次或多次 # ) - 结束捕获组 # # 匹配过程: # "Setting vref: 0x1A3" -> 匹配整个字符串 # 捕获组1: "1A3" ########################################################## vref_match = re.match(r'.*vref:\s*0x([0-9a-fA-F]+)', line) if vref_match: # 获取捕获组内容 hex_str = vref_match.group(1) # int()函数:字符串转整数 # 参数1:字符串 # 参数2:基数(16表示十六进制) current_vref = int(hex_str, 16) # 字典初始化 data[current_vref] = {} # 嵌套字典初始化 raw_data[current_vref] = {} pending_data = {} # 重置临时数据 current_offset = None continue # 跳过后续处理 ########################################################## # 正则表达式2:匹配偏移量行 # 模式:r'.*0x38c:\s*(?:0x)?([0-9a-fA-F]+)' # 目标示例: "Offset 0x38c: 0x25" 或 "0x38c: 25" # # 详细分解: # .* - 匹配任意字符0次或多次 # 0x38c: - 匹配字面字符串 "0x38c:" # \s* - 匹配0个或多个空白字符 # (?: - 开始非捕获组 # 0x - 匹配字面字符串 "0x" # )? - 非捕获组出现0次或1次 # ( - 开始捕获组 # [0-9a-fA-F]+ - 匹配1个或多个十六进制字符 # ) - 结束捕获组 # # 特殊说明: # (?:...) 是非捕获组,匹配但不捕获内容 # 用于处理可选前缀而不创建额外捕获组 ########################################################## offset_match = re.match(r'.*0x38c:\s*(?:0x)?([0-9a-fA-F]+)', line) if offset_match and current_vref is not None: try: hex_str = offset_match.group(1) offset_value = int(hex_str, 16) # 计算偏移量:归一化点 - 读取值 current_offset = normalization_point - offset_value except ValueError: # 异常处理:打印警告信息 print(f"警告: 无法解析偏移量: {offset_match.group(1)}") current_offset = None continue ########################################################## # 正则表达式3:匹配最大值点 # 模式:r'.*dq(\d+)\s+max_(\w+)_point\s*:\s*(-?\d+)' # 目标示例: "dq5 max_read_point: 120" # # 详细分解: # .* - 匹配任意字符0次或多次 # dq - 匹配字面字符串 "dq" # (\d+) - 捕获组1:匹配1个或多个数字(DQ索引) # \s+ - 匹配1个或多个空白字符 # max_ - 匹配字面字符串 "max_" # (\w+) - 捕获组2:匹配1个或多个单词字符(方向:read/write) # _point - 匹配字面字符串 "_point" # \s*:\s* - 匹配冒号前后任意空白 # (-?\d+) - 捕获组3:匹配可选负号后跟1个或多个数字 # # 捕获组说明: # 组1: DQ索引 (如 "5") # 组2: 方向 (如 "read") # 组3: 最大值 (如 "120") ########################################################## max_match = re.match(r'.*dq(\d+)\s+max_(\w+)_point\s*:\s*(-?\d+)', line) if max_match and current_vref is not None: # 提取捕获组内容 dq_index = int(max_match.group(1)) # 转换为整数 direction = max_match.group(2) # 字符串 max_val = int(max_match.group(3)) # 转换为整数 # 字典操作:检查键是否存在并初始化 if current_vref not in raw_data: # 字典设置默认值 raw_data[current_vref] = {} if dq_index not in raw_data[current_vref]: raw_data[current_vref][dq_index] = {} if direction not in raw_data[current_vref][dq_index]: # 嵌套字典初始化 raw_data[current_vref][dq_index][direction] = {'min': None, 'max': None} # 存储原始值(不应用偏移) raw_data[current_vref][dq_index][direction]['max'] = max_val # 只有读方向应用偏移 if direction == 'read' and current_offset is not None: # 应用偏移 max_val += current_offset # 存储到临时数据字典 key = (dq_index, direction) # 元组作为字典键 pending_data[key] = {'max': max_val} # 字典值也是字典 continue ########################################################## # 正则表达式4:匹配最小值点(结构类似最大值匹配) # 模式:r'.*dq(\d+)\s+min_(\w+)_point\s*:\s*(-?\d+)' # 目标示例: "dq5 min_read_point: 32" ########################################################## min_match = re.match(r'.*dq(\d+)\s+min_(\w+)_point\s*:\s*(-?\d+)', line) if min_match and current_vref is not None: dq_index = int(min_match.group(1)) direction = min_match.group(2) min_val = int(min_match.group(3)) key = (dq_index, direction) # 存储原始值(类似最大值处理) if current_vref not in raw_data: raw_data[current_vref] = {} if dq_index not in raw_data[current_vref]: raw_data[current_vref][dq_index] = {} if direction not in raw_data[current_vref][dq_index]: raw_data[current_vref][dq_index][direction] = {'min': None, 'max': None} raw_data[current_vref][dq_index][direction]['min'] = min_val # 只有读方向应用偏移 if direction == 'read' and current_offset is not None: min_val += current_offset # 更新临时数据 if key in pending_data: # 字典更新操作 pending_data[key]['min'] = min_val continue ########################################################## # 正则表达式5:匹配窗口行 # 模式:r'.*dq(\d+)\s+(\w+)_windows\s*:\s*(-?\d+)' # 目标示例: "dq5 read_windows: 88" # # 详细分解: # .* - 匹配任意字符0次或多次 # dq - 匹配字面字符串 "dq" # (\d+) - 捕获组1:匹配1个或多个数字(DQ索引) # \s+ - 匹配1个或多个空白字符 # (\w+) - 捕获组2:匹配1个或多个单词字符(方向) # _windows - 匹配字面字符串 "_windows" # \s*:\s* - 匹配冒号前后任意空白 # (-?\d+) - 捕获组3:匹配可选负号后跟1个或多个数字 ########################################################## win_match = re.match(r'.*dq(\d+)\s+(\w+)_windows\s*:\s*(-?\d+)', line) if win_match and current_vref is not None: dq_index = int(win_match.group(1)) direction = win_match.group(2) windows = int(win_match.group(3)) key = (dq_index, direction) # 检查是否已收集最小值和最大值 if key in pending_data and 'min' in pending_data[key] and 'max' in pending_data[key]: min_val = pending_data[key]['min'] max_val = pending_data[key]['max'] # 确定最大延迟值(读0x7F=127,写0xFF=255) max_delay = 0x7F if direction == 'read' else 0xFF # 确保值在有效范围内 min_val = max(0, min_val) # 最小值不小于0 max_val = min(max_delay, max_val) # 最大值不超过最大延迟 # 检查数据有效性 if min_val > max_val or windows < 0: result = None # 无效数据 else: # 计算窗口大小 window_size = max_val - min_val + 1 result = (min_val, max_val, window_size) # 存储到最终数据结构 if dq_index not in data[current_vref]: # 初始化嵌套字典 data[current_vref][dq_index] = {} data[current_vref][dq_index][direction] = result # 从临时数据中移除 del pending_data[key] # 删除字典键 # 返回解析结果 return data, raw_data ===================================================== 眼图指标计算函数 - 算法详解 ===================================================== def calculate_eye_metrics(data, avddq, dq_index, direction): “”" 计算眼图的最大宽度和最大高度 参数: data - 解析后的日志数据(字典结构) avddq - AVDDQ电压值(浮点数) dq_index - DQ索引(0-15,整数) direction - 方向(‘read’或’write’,字符串) 算法说明: 1. 遍历所有VREF值 2. 计算实际电压 = (vref / 0x1FF) * avddq 3. 获取当前DQ和方向的数据 4. 计算窗口大小(UI单位) 5. 确定最大眼宽(所有窗口中的最大值) 6. 计算最大眼高(连续电压范围的最大高度) """ # 初始化变量 max_eye_width = 0.0 max_eye_height = 0.0 # defaultdict:带默认值的字典 # float作为默认工厂函数,访问不存在的键时返回0.0 voltage_windows = defaultdict(float) # 遍历所有VREF值 for vref, dq_data in data.items(): # 计算实际电压 # 0x1FF = 511(9位最大值) voltage = (vref / 0x1FF) * avddq # 字典安全访问:get()方法 # 避免KeyError异常 dq_info = dq_data.get(dq_index, {}).get(direction) if dq_info is None: continue # 跳过无数据项 # 解包元组 min_point, max_point, window_size = dq_info # 重新计算窗口大小(确保正确性) window_size = max_point - min_point + 1 # 确定UI范围(读2UI,写4UI) max_delay = 0x7F if direction == 'read' else 0xFF ui_range = 2 if direction == 'read' else 4 # 计算窗口大小(UI单位) window_ui = (window_size / max_delay) * ui_range # 更新最大眼宽 if window_ui > max_eye_width: max_eye_width = window_ui # 存储电压-窗口映射 voltage_windows[voltage] = window_ui # 计算最大眼高(连续电压范围) # 步骤: # 1. 对电压排序 # 2. 遍历排序后的电压 # 3. 计算连续有效窗口的电压范围 sorted_voltages = sorted(voltage_windows.keys()) # 排序电压值 current_height = 0 # 当前连续高度 max_height = 0 # 最大高度 # 遍历排序后的电压(从第二个元素开始) for i in range(1, len(sorted_voltages)): # 计算电压差 voltage_diff = sorted_voltages[i] - sorted_voltages[i-1] # 检查相邻电压点是否都有有效窗口 # 字典键存在性检查 if sorted_voltages[i] in voltage_windows and sorted_voltages[i-1] in voltage_windows: current_height += voltage_diff if current_height > max_height: max_height = current_height else: current_height = 0 # 重置高度计数器 max_eye_height = max_height return max_eye_width, max_eye_height ===================================================== 眼图数据生成函数 - 详细解释算法 ===================================================== def generate_eye_diagram(data, avddq, ui_ps, dq_index, direction): “”" 生成眼图数据点 参数: data - 解析后的日志数据(字典) avddq - AVDDQ电压值(浮点数) ui_ps - 每个UI的时间(皮秒) dq_index - DQ索引(0-15,整数) direction - 方向(‘read’或’write’,字符串) 算法说明: 1. 遍历所有VREF值 2. 计算实际电压 = (vref / 0x1FF) * avddq 3. 遍历所有可能的延迟值 4. 将延迟值转换为UI单位 5. 根据数据有效性标记为通过点或失败点 """ pass_points = [] # 存储通过点(绿色) fail_points = [] # 存储失败点(红色) # 确定最大延迟值(读0x7F=127,写0xFF=255) max_delay = 0x7F if direction == 'read' else 0xFF # 确定UI范围(读2UI,写4UI) ui_range = 2 if direction == 'read' else 4 # 遍历所有VREF值 for vref, dq_data in data.items(): # 计算实际电压 voltage = (vref / 0x1FF) * avddq # 获取当前DQ和方向的数据 dq_info = dq_data.get(dq_index, {}).get(direction) # 遍历所有可能的延迟值 for delay in range(0, max_delay + 1): # 将延迟值转换为UI单位 ui_value = (delay / max_delay) * ui_range # 如果没有有效数据,标记为失败点 if dq_info is None: fail_points.append((ui_value, voltage)) else: # 解包元组 min_point, max_point, _ = dq_info # 检查当前延迟是否在有效范围内 if min_point <= delay <= max_point: pass_points.append((ui_value, voltage)) else: fail_points.append((ui_value, voltage)) return pass_points, fail_points ===================================================== 输出原始数据到新日志 - 文件操作详解 ===================================================== def export_raw_data(raw_data, normalization_point, log_path): “”" 输出原始数据到新日志文件(按DQ划分) 参数: raw_data - 原始数据(偏移前) normalization_point - 归一化点 log_path - 原始日志文件路径 文件操作详解: 1. 创建输出目录:os.makedirs() 2. 构建文件路径:os.path.join() 3. 写入文件:open()配合write() 4. 格式化输出:f-string """ # 获取当前时间戳 timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") # 获取日志文件名(不含扩展名) log_filename = os.path.basename(log_path) if '.' in log_filename: # rsplit() 从右边分割字符串,maxsplit=1表示只分割一次 log_name = log_filename.rsplit('.', 1)[0] else: log_name = log_filename # 创建输出目录 log_dir = os.path.dirname(log_path) or os.getcwd() # 获取目录或当前工作目录 output_dir = os.path.join(log_dir, "raw_data_export") # 创建输出目录路径 ########################################################## # os.makedirs() 创建目录(如果不存在) # exist_ok=True 表示目录已存在时不报错 ########################################################## os.makedirs(output_dir, exist_ok=True) # 创建输出文件路径 output_file = os.path.join(output_dir, f"{log_name}_raw_data_{timestamp}.txt") # 写入原始数据 with open(output_file, 'w', encoding='utf-8') as f: # 写入标题信息 f.write("=" * 80 + "\n") f.write(f"DDR校准原始数据报告 (归一化点: 0x{normalization_point:X})\n") f.write(f"生成时间: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n") f.write(f"原始日志: {log_path}\n") f.write("=" * 80 + "\n\n") # 按vref排序 sorted_vrefs = sorted(raw_data.keys()) for vref in sorted_vrefs: # 写入vref标题 f.write(f"VREF: 0x{vref:03X}\n") # :03X表示3位十六进制大写,不足补0 f.write("-" * 60 + "\n") # 按DQ索引排序 sorted_dq = sorted(raw_data[vref].keys()) for dq_index in sorted_dq: # 写入DQ标题 f.write(f" DQ{dq_index}:\n") # 处理读方向数据 if 'read' in raw_data[vref][dq_index]: rd = raw_data[vref][dq_index]['read'] f.write(f" 读方向:\n") f.write(f" 原始最小值: {rd['min']}\n") f.write(f" 原始最大值: {rd['max']}\n") # 计算并写入窗口大小 window_size = rd['max'] - rd['min'] + 1 f.write(f" 窗口大小: {window_size}\n") # 处理写方向数据 if 'write' in raw_data[vref][dq_index]: wr = raw_data[vref][dq_index]['write'] f.write(f" 写方向:\n") f.write(f" 原始最小值: {wr['min']}\n") f.write(f" 原始最大值: {wr['max']}\n") # 计算并写入窗口大小 window_size = wr['max'] - wr['min'] + 1 f.write(f" 窗口大小: {window_size}\n") f.write("\n") # DQ间空行 f.write("\n") # VREF间空行 print(f"原始数据已导出至: {output_file}") return output_file ===================================================== 眼图绘制函数 - 数据可视化详解 ===================================================== def plot_eye_diagrams(log_content, data_rate, avddq, log_path, normalization_point): “”" 绘制DDR眼图 参数: log_content - 日志内容 data_rate - 数据速率(Mbps) avddq - AVDDQ电压(V) log_path - 日志文件路径 normalization_point - 归一化点 数据可视化详解: 1. 创建图表对象:plt.subplots() 2. 设置图表属性:suptitle(), set_title(), set_xlabel()等 3. 绘制散点图:scatter() 4. 添加标注:annotate() 5. 保存图像:savefig() """ # 设置中文字体支持 plt.rcParams['font.sans-serif'] = ['SimHei', 'Arial Unicode MS', 'Microsoft YaHei', 'WenQuanYi Micro Hei'] # 指定支持的字体 plt.rcParams['axes.unicode_minus'] = False # 解决负号显示问题 # 计算UI时间(皮秒) # UI = Unit Interval, 单位时间间隔 ui_ps = (1 / (data_rate * 1e6)) * 1e12 # 解析日志文件(获取处理后的数据和原始数据) data, raw_data = parse_log_file(log_content, normalization_point) # 导出原始数据到新日志 raw_data_file = export_raw_data(raw_data, normalization_point, log_path) # 检查是否解析到有效数据 if not data: print("错误: 无法从日志中解析出有效数据") return None, None, None # 创建图表对象 # figsize=(20, 20) 设置图表大小(英寸) fig_write, axes_write = plt.subplots(4, 4, figsize=(20, 20)) fig_read, axes_read = plt.subplots(4, 4, figsize=(20, 20)) # 设置标题(包含原始数据文件路径) norm_title = f" (Normalized to 0x{normalization_point:X}, Raw Data: {os.path.basename(raw_data_file)})" fig_write.suptitle(f'DDR Write Eye Diagram (Data Rate: {data_rate} Mbps, UI: {ui_ps:.2f} ps){norm_title}', fontsize=18) fig_read.suptitle(f'DDR Read Eye Diagram (Data Rate: {data_rate} Mbps, UI: {ui_ps:.2f} ps){norm_title}', fontsize=18) # 展平坐标轴数组(将4x4网格转换为一维数组) axes_write = axes_write.flatten() axes_read = axes_read.flatten() # 创建图例元素 legend_elements = [ Line2D([0], [0], marker='o', color='w', label='Pass', markerfacecolor='green', markersize=10), Line2D([0], [0], marker='o', color='w', label='Fail', markerfacecolor='red', markersize=10) ] # 遍历16个DQ通道 for dq_index in range(16): # 计算写眼图指标 write_width, write_height = calculate_eye_metrics(data, avddq, dq_index, 'write') # 生成写眼图数据点 write_pass, write_fail = generate_eye_diagram(data, avddq, ui_ps, dq_index, 'write') # 计算读眼图指标 read_width, read_height = calculate_eye_metrics(data, avddq, dq_index, 'read') # 生成读眼图数据点 read_pass, read_fail = generate_eye_diagram(data, avddq, ui_ps, dq_index, 'read') # 绘制写眼图 if write_fail: # zip(*iterable) 解压坐标列表 x_fail, y_fail = zip(*write_fail) # 绘制失败点(红色,透明度0.1,底层) axes_write[dq_index].scatter(x_fail, y_fail, s=1, c='red', alpha=0.1, zorder=1) if write_pass: x_pass, y_pass = zip(*write_pass) # 绘制通过点(绿色,透明度0.5,上层) axes_write[dq_index].scatter(x_pass, y_pass, s=1, c='green', alpha=0.5, zorder=2) # 添加写眼图标注 write_text = f"Max Eye Width: {write_width:.3f} UI\nMax Eye Height: {write_height:.3f} V" axes_write[dq_index].annotate( write_text, xy=(0.98, 0.02), # 坐标(相对坐标轴) xycoords='axes fraction', # 使用相对坐标 fontsize=9, ha='right', # 水平对齐方式:右对齐 va='bottom', # 垂直对齐方式:底部对齐 bbox=dict(boxstyle='round', facecolor='white', alpha=0.8) # 文本框样式 ) # 绘制读眼图 if read_fail: x_fail, y_fail = zip(*read_fail) axes_read[dq_index].scatter(x_fail, y_fail, s=1, c='red', alpha=0.1, zorder=1) if read_pass: x_pass, y_pass = zip(*read_pass) axes_read[dq_index].scatter(x_pass, y_pass, s=1, c='green', alpha=0.5, zorder=2) # 添加读眼图标注 read_text = f"Max Eye Width: {write_width:.3f} UI\nMax Eye Height: {write_height:.3f} V" axes_read[dq_index].annotate( read_text, xy=(0.98, 0.02), xycoords='axes fraction', fontsize=9, ha='right', va='bottom', bbox=dict(boxstyle='round', facecolor='white', alpha=0.8) ) # 设置公共轴属性 for ax, direction in [(axes_write[dq_index], 'Write'), (axes_read[dq_index], 'Read')]: ax.set_title(f'DQ{dq_index} {direction} Eye', fontsize=12) ax.set_xlabel('Delay (UI)', fontsize=10) ax.set_ylabel('Voltage (V)', fontsize=10) # 设置X轴范围(写眼图0-4UI,读眼图0-2UI) ax.set_xlim(0, 4 if direction == 'Write' else 2) ax.set_ylim(0, avddq) # 设置Y轴范围(0到AVDDQ电压) ax.grid(True, linestyle='--', alpha=0.6) # 添加网格线 ax.legend(handles=legend_elements, loc='upper right', fontsize=9) # 添加图例 ax.tick_params(axis='both', which='major', labelsize=9) # 设置刻度标签大小 # 调整布局(rect参数指定子图区域) fig_write.tight_layout(rect=[0, 0, 1, 0.96]) fig_read.tight_layout(rect=[0, 0, 1, 0.96]) # 文件路径处理 log_dir = os.path.dirname(log_path) or os.getcwd() timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") log_filename = os.path.basename(log_path) log_name = log_filename.rsplit('.', 1)[0] if '.' in log_filename else log_filename # 构建输出文件路径 write_filename = os.path.join(log_dir, f"{log_name}_ddr_write_eye_{timestamp}.png") read_filename = os.path.join(log_dir, f"{log_name}_ddr_read_eye_{timestamp}.png") # 保存图像(高分辨率300dpi) fig_write.savefig(write_filename, dpi=300, bbox_inches='tight') fig_read.savefig(read_filename, dpi=300, bbox_inches='tight') # 关闭图像释放内存 plt.close(fig_write) plt.close(fig_read) # 打印结果 print(f"写眼图已保存至: {write_filename}") print(f"读眼图已保存至: {read_filename}") return write_filename, read_filename, raw_data_file ===================================================== 主函数 - 程序入口点详解 ===================================================== def main(): “”" 主函数,程序入口点 功能: 1. 获取用户输入 2. 读取日志文件 3. 解析数据 4. 生成眼图 5. 导出结果 用户交互详解: 1. 使用input()获取用户输入 2. 使用循环处理无效输入 3. 使用try-except捕获异常 """ # 打印欢迎信息 print("=" * 50) print("DDR眼图生成器(带原始数据导出)") print("=" * 50) # 用户输入DataRate(带异常处理) while True: try: data_rate = float(input("请输入DataRate (Mbps/Pin): ")) break except ValueError: print("错误: 请输入有效的数字") # 用户输入AVDDQ电压(带异常处理) while True: try: avddq = float(input("请输入AVDDQ电压值 (V): ")) break except ValueError: print("错误: 请输入有效的数字") # 归一化点输入处理(带错误检查) while True: norm_input = input("请输入归一化点(十六进制值,如0x40或40): ").strip() if not norm_input: print("错误: 输入不能为空,请重新输入") continue try: # 处理十六进制前缀 if norm_input.startswith(("0x", "0X")): hex_str = norm_input[2:] else: hex_str = norm_input # 字符串转整数(16进制) normalization_point = int(hex_str, 16) break except ValueError: print(f"错误: '{norm_input}' 不是有效的十六进制数,请重新输入") # 日志文件路径输入(带文件存在检查) while True: log_path = input("请输入日志文件路径: ").strip() # 检查文件是否存在 # os.path.exists() 判断路径是否存在 if not os.path.exists(log_path): print(f"错误: 文件 '{log_path}' 不存在,请重新输入") else: # 获取绝对路径 log_path = os.path.abspath(log_path) break # 读取文件内容 log_content = robust_read_file(log_path) if log_content is None: print("无法读取日志文件") return # 尝试生成眼图(带异常处理) try: # 调用眼图生成函数(返回三个值) write_file, read_file, raw_data_file = plot_eye_diagrams( log_content, data_rate, avddq, log_path, normalization_point ) print("\n眼图生成成功!") print(f"原始数据文件: {raw_data_file}") except Exception as e: # 捕获所有异常并打印错误信息 print(f"眼图生成失败: {e}") # 异常对象:e.args 获取异常参数 print(f"错误详情: {e.args}") ===================================================== Python特殊检查 - 模块执行控制 ===================================================== if name == “main”: “”" name 是Python的内置变量 当脚本直接运行时,name 等于 “main” 当脚本被导入时,name 等于模块名 这种结构允许: 1. 直接运行脚本时执行测试代码 2. 作为模块导入时不执行测试代码 """ main() # 调用主函数
最新发布
08-01
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ShadyPi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值