Files
spectrometer/README.md
T
starcells 45249b59f9 光谱工具集:采集、回放、寻峰、波长标定
- spectro_core.py: 共享核心(加载/标定/基线/寻峰/高斯拟合/双峰联合拟合/CSV-JSON)

- peak_finder.py: 寻峰与峰信息导出,--pick 交互选峰(含 RAW 预览)

- wavelength_calibration.py: 改用 core 寻峰,汞灯 577/579 双峰可分

- wavelength_player.py / hq_average.py: 复用 core 的标定与解码加载
2026-11-14 13:48:24 +08:00

87 lines
3.4 KiB
Markdown
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.
# spectrometer
Raspberry Pi HQ Camera(IMX477)RAW 光谱工具集:录制、回放、寻峰、波长标定。
采集的是相机 **RAW** 数据(`BGGR_PISP_COMP1`,2028×1520,16 bit 容器),不做 ISP、
不去马赛克、不做平滑;所有分析都基于按 CFA 分离后的 R/G/B 三条一维谱。
## 文件
| 文件 | 作用 |
| --- | --- |
| `hq_spectrum.py` | 核心库 + 实时播放器:PC1B 解码、CFA 分离、ROI、降维、预览、交互窗口 |
| `hq_record.py` | 把相机 RAW 流录成 `recordings/<时间戳>/{frames.bin,frames.jsonl,meta.json}` |
| `hq_average.py` | 多帧平均成一条新录制(降噪) |
| `spectro_core.py` | **共享核心**:模块加载、标定解析、基线/噪声/寻峰/拟合/双峰分解、CSV/JSON 输出 |
| `peak_finder.py` | 寻峰与峰信息导出:峰形图、峰形数据、像素坐标、RAW 像素块;`--pick` 交互选峰 |
| `wavelength_calibration.py` | 用汞灯谱线标定 像素→波长,产出 `wavelength_calibration/` |
| `wavelength_player.py` | 带已存标定的回放(波长轴、瀑布图) |
| `PEAK_FINDER.md` / `WAVELENGTH_PLAYER.md` | 两个工具的详细说明 |
| `test_peak_finder.py` / `test_wavelength_player.py` | 单元测试(含真实录制的端到端用例) |
| `wavelength_calibration/` | 一次汞灯标定的结果:`calibration.json` 及残差、候选、逐像素波长表 |
`anal.py` 是 2026-10-23 的 `hq_spectrum.py` 旧副本,函数一一对应、无独有内容,属于
遗留文件,可删。
## 依赖
```
pip install -r requirements.txt
```
- Python ≥ 3.10,**NumPy + Matplotlib**,不需要 SciPy。
- 树莓派上采集还需要 `picamera2`(系统包)和 `python3-tk`(交互窗口)。
- 在 PC 上分析已有录制只需要 NumPy + Matplotlib。
## 快速开始
录制(树莓派):
```bash
python3 hq_record.py --output recordings --exposure 66351 --gain 16 --duration 10
```
回放 / 看谱(PC 或树莓派):
```bash
python3 hq_spectrum.py --input recordings/hq_20261101_114417 --fps 15
python3 wavelength_player.py --input recordings/hq_20261101_114417 \
--calibration wavelength_calibration/calibration.json
```
寻峰并导出(`--calibration` 只是**应用**已有的标定,不给就只输出像素坐标):
```bash
python peak_finder.py --input recordings/hq_20261101_114417 \
--calibration wavelength_calibration/calibration.json --select all
python peak_finder.py --input recordings/hq_20261101_114417 --pick
```
重新标定:
```bash
python wavelength_calibration.py --input recordings/hq_20261101_114417 --headless
# 看清 candidates.png 后指定:候选编号:波长
python wavelength_calibration.py --input recordings/hq_20261101_114417 --headless \
--assign "B1:404.6563,B2:435.8328,G1:546.0735,G2:576.9598,G3:579.0663"
```
## 测试
```bash
python -m unittest test_peak_finder test_wavelength_player -v
```
端到端用例需要录制与标定;录制不在脚本附近时用环境变量指过去,否则自动跳过:
```powershell
$env:PEAK_FINDER_TEST_RECORDING = "C:\Users\peng0\Dev\Light\recordings\hq_20261101_114417"
$env:PEAK_FINDER_TEST_CALIBRATION = "wavelength_calibration\calibration.json"
```
## 仓库里不放什么
`recordings/`(原始录制,单个约 470 MB)、`peak_exports/`(导出结果)、`__pycache__/`
都在 `.gitignore` 里。标定结果 `wavelength_calibration/` 是**放**进来的,因为它决定了
所有波长读数。