Skip to content

fix(scopes): 处理示波器数学计算中的 NaN 和 Inf 异常样本 (#83) - #89

Merged
Mike-Solar merged 1 commit into
OakVideoEditorCommunity:mainfrom
liangshengsheng12333:fix/scopes-nan-inf-83
Nov 17, 2026
Merged

Mike-Solar merged 1 commit into
OakVideoEditorCommunity:mainfrom
liangshengsheng12333:fix/scopes-nan-inf-83

Conversation

@liangshengsheng12333

Copy link
Copy Markdown

关联 Issue

Fixes #83

为什么会出现这个 Bug?

原代码是用 sample.clamp(0.0, 1.0) 来限制数值范围的。
但浮点数有一个特性:任何数字跟 NaN 比大小,结果都是 false。

这就导致 NaN < 0.0 是假,NaN > 1.0 也是假,.clamp() 根本拦不住 NaN,坏数据直接漏了过去:

  1. 直方图(histogram_bins):NaN 转成整数后变成了 0,全被当成了最暗的纯黑像素,暗部数据被污染;
  2. 波形图(waveform_envelope):如果这一段数据全都是坏的,min 会停在极大值、max 停在极小值,导致 min 比 max 还大,绘图坐标直接倒挂;
  3. 矢量示波器(vectorscope_points):算出来的损坏坐标会直接传给界面渲染;
  4. 音量表与衰减(decay_peak 等):如果音量不小心算出了无穷大(+Inf),峰值线会被卡死在无穷大,之后再也掉不下来。

修复方法

在 5 个计算函数里加上了 .is_finite() 检查,把非正常数值(NaN 和 ±Inf)安全拦住:

  • histogram_bins:遇到非法数字直接 continue 跳过,不计入直方图;
  • waveform_envelope:记录当前分片有没有遇到有效数字,只有正常数字才更新 min/max;如果整段全是坏数据,安全回退到默认的 (0.0, 0.0);
  • vectorscope_points:用 .filter 筛掉坐标损坏的点;
  • meter_lit_segments:遇到 NaN 时音量表亮 0 格(静音);
  • decay_peak:计算前把异常值做兜底清洗,防止峰值被无穷大卡死。

测试验证

保留原有 6 个测试,额外补充了 4 个单元测试,专门塞入 NaN、+Inf、-Inf 以及全坏数据切片进行验证:

  • histogram_filters_nan_and_inf
  • waveform_envelope_handles_nan_and_inf
  • vectorscope_filters_nan_and_inf
  • meter_and_decay_nan_and_inf

本地单独编译运行测试,10 个用例全部通过(0.00s):

running 10 tests
test tests::histogram_bins_correctly ... ok
test tests::histogram_clamps_out_of_range ... ok
test tests::histogram_filters_nan_and_inf ... ok
test tests::histogram_handles_empty_and_zero_bins ... ok
test tests::meter_and_decay_nan_and_inf ... ok
test tests::meter_lit_and_peak_decay ... ok
test tests::vectorscope_filters_nan_and_inf ... ok
test tests::vectorscope_projection_centers ... ok
test tests::waveform_envelope_handles_nan_and_inf ... ok
test tests::waveform_envelope_slices ... ok

test result: ok. 10 passed; 0 failed; finished in 0.00s

@Mike-Solar

Copy link
Copy Markdown

Since there are forigners in this community, please speak English here.

@Mike-Solar
Mike-Solar merged commit c612300 into OakVideoEditorCommunity:main Nov 17, 2026
8 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v0.6] Scopes: handle NaN and infinite samples safely

2 participants