#!/usr/bin/env python3
"""Pareto 前沿 + 深饱和区 DPoD 反常。英文标签（本机无中文字体）。"""
import json
import matplotlib; matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
r = json.load(open("results.json")); res = r["results"]
EN = {"no_processing": "No processing", "dpd": "DPD (transmitter)",
      "dpod": "DPoD (receiver)", "mismatched_dpd_null": "Mismatched-DPD (null)"}
CL = {"no_processing": "#7f8c8d", "dpd": "#c0392b", "dpod": "#2c6fbb",
      "mismatched_dpd_null": "#95a5a6"}
fig, ax = plt.subplots(1, 2, figsize=(12.5, 5))
for a in EN:
    e = np.array(res[a]["eta"]); s = np.array(res[a]["se"]); o = np.argsort(e)
    ax[0].plot(e[o], s[o], "o-", ms=3.5, color=CL[a],
               ls="--" if a == "mismatched_dpd_null" else "-", label=EN[a])
ax[0].set_xlabel("Power efficiency  (class-B PA)")
ax[0].set_ylabel("Spectral efficiency  (bit/s/Hz)")
ax[0].set_title("Spectral vs power efficiency trade-off\n(each point = one input back-off)")
ax[0].legend(fontsize=8.5); ax[0].grid(alpha=0.3)

ibo = np.array(res["no_processing"]["ibo"])
d = np.array(res["dpod"]["se"]) - np.array(res["no_processing"]["se"])
ax[1].axhline(0, c="k", lw=1)
ax[1].plot(ibo, d, "o-", color="#2c6fbb", ms=4)
ax[1].fill_between(ibo, d, 0, where=d < 0, color="#c0392b", alpha=0.35)
bad = ibo[d < 0]
if len(bad):
    ax[1].annotate(f"DPoD worse than doing nothing\n(IBO {bad.min():.1f}-{bad.max():.1f} dB)",
                   (bad.mean(), d[d < 0].min()), fontsize=9, color="#c0392b",
                   xytext=(2.5, -0.9), textcoords="offset points")
ax[1].set_xlabel("Input back-off (dB)"); ax[1].set_ylabel("DPoD - No processing  (bit/s/Hz)")
ax[1].set_title("Receiver-side post-distortion has a regime where it hurts")
ax[1].grid(alpha=0.3)
plt.tight_layout(); plt.savefig("figs/pareto.png", dpi=150)
print("figs/pareto.png 已生成")
