{
 "app": "MathPad",
 "version": 3,
 "nextId": 722,
 "name": "信号变换示例.mpad",
 "regions": [
  {
   "id": "1",
   "x": 62,
   "y": 40,
   "kind": "text",
   "latex": "",
   "text": "信号变换示例：傅里叶 · 卷积 · 小波。示例图是经典的 cameraman（512×512，scikit-image 自带、公有领域），"
  },
  {
   "id": "2",
   "x": 62,
   "y": 74,
   "kind": "text",
   "latex": "",
   "text": "随程序一起放在 samples/ 下，打开本示例时自动载入，直接 img := imread(\"sample-cameraman.png\") 就能用。"
  },
  {
   "id": "3",
   "x": 62,
   "y": 174,
   "kind": "text",
   "latex": "",
   "text": "一、一维傅里叶变换：把复合信号拆回成分"
  },
  {
   "id": "4",
   "x": 62,
   "y": 208,
   "kind": "text",
   "latex": "",
   "text": "复合信号 = 三个频率的正弦叠加。采样率 fs = 128 Hz，取 N = 128 个点（正好 1 秒）："
  },
  {
   "id": 5,
   "x": 78,
   "y": 242,
   "kind": "math",
   "latex": "fs:=128",
   "text": ""
  },
  {
   "id": 6,
   "x": 78,
   "y": 298,
   "kind": "math",
   "latex": "N:=128",
   "text": ""
  },
  {
   "id": 7,
   "x": 78,
   "y": 354,
   "kind": "math",
   "latex": "t:=0,\\frac{1}{fs}..\\frac{N-1}{fs}",
   "text": ""
  },
  {
   "id": "8",
   "x": 62,
   "y": 415,
   "kind": "text",
   "latex": "",
   "text": "三个成分：5 Hz（幅度 1）、20 Hz（幅度 0.5）、50 Hz（幅度 0.3）："
  },
  {
   "id": 9,
   "x": 78,
   "y": 448,
   "kind": "math",
   "latex": "s1:=\\sin\\left(2\\pi\\cdot 5\\cdot t\\right)",
   "text": ""
  },
  {
   "id": 10,
   "x": 78,
   "y": 500,
   "kind": "math",
   "latex": "s2:=0.5\\sin\\left(2\\pi\\cdot 20\\cdot t\\right)+0.3\\sin\\left(2\\pi\\cdot 50\\cdot t\\right)",
   "text": ""
  },
  {
   "id": 11,
   "x": 78,
   "y": 556,
   "kind": "math",
   "latex": "sig:=s1+s2",
   "text": ""
  },
  {
   "id": "12",
   "x": 62,
   "y": 612,
   "kind": "text",
   "latex": "",
   "text": "时域里三条曲线搅在一起，看不出谁是谁："
  },
  {
   "id": 13,
   "x": 78,
   "y": 646,
   "kind": "math",
   "latex": "plot(sig)",
   "text": ""
  },
  {
   "id": "14",
   "x": 62,
   "y": 1116,
   "kind": "text",
   "latex": "",
   "text": "fft 一眼看清：5 / 20 / 50 Hz 三个尖峰，峰高 = N/2 × 幅度（64 / 32 / 19.2）。"
  },
  {
   "id": "15",
   "x": 62,
   "y": 1150,
   "kind": "text",
   "latex": "",
   "text": "plot 只给 y 时，横轴是**点序号 0,1,2…**。但频谱图的横轴应该是**频率**（Hz），所以先定义频率轴 fhz:=0, 1 .. N/2，再 plot(mag, fhz) 把数据画在频率上——这样读峰才是真的 Hz 数。"
  },
  {
   "id": 16,
   "x": 78,
   "y": 1202,
   "kind": "math",
   "latex": "mag:=fft(sig)",
   "text": ""
  },
  {
   "id": "72",
   "x": 78,
   "y": 1252,
   "kind": "math",
   "latex": "fhz:=0, 1 .. \\frac{N}{2}",
   "text": ""
  },
  {
   "id": 17,
   "x": 78,
   "y": 1390,
   "kind": "math",
   "latex": "plot(fhz, mag)",
   "text": ""
  },
  {
   "id": "18",
   "x": 62,
   "y": 1886,
   "kind": "text",
   "latex": "",
   "text": "二、二维傅里叶：图像的频谱与频率域滤波"
  },
  {
   "id": "19",
   "x": 62,
   "y": 1920,
   "kind": "text",
   "latex": "",
   "text": "图像也能做傅里叶：低频在中心（轮廓和大块明暗），高频在四周（边缘和细节）。"
  },
  {
   "id": "20",
   "x": 62,
   "y": 1954,
   "kind": "text",
   "latex": "",
   "text": "fftspec 出的频谱做了 fftshift + log(1+|F|) + 归一化——中心那道亮十字就是图像的方向性能量："
  },
  {
   "id": 21,
   "x": 78,
   "y": 2028,
   "kind": "math",
   "latex": "img:=imread(\\text{“sample-cameraman.png”})",
   "text": ""
  },
  {
   "id": 22,
   "x": 78,
   "y": 2084,
   "kind": "math",
   "latex": "imshow(img)",
   "text": ""
  },
  {
   "id": 23,
   "x": 78,
   "y": 2670,
   "kind": "math",
   "latex": "sp:=fftspec(img)",
   "text": ""
  },
  {
   "id": 24,
   "x": 78,
   "y": 2720,
   "kind": "math",
   "latex": "imshow(sp)",
   "text": ""
  },
  {
   "id": "25",
   "x": 62,
   "y": 3306,
   "kind": "text",
   "latex": "",
   "text": "频率域滤波 = 在频谱上挖一个圆再反变换回图像。\"low\" 保留圆内低频 → 模糊；"
  },
  {
   "id": "26",
   "x": 62,
   "y": 3339,
   "kind": "text",
   "latex": "",
   "text": "\"high\" 只留圆外高频 → 只剩边缘。第三个参数是半径占短边的比例："
  },
  {
   "id": 27,
   "x": 78,
   "y": 3372,
   "kind": "math",
   "latex": "lo:=specfilt\\left(img,\\text{“low”},0.06\\right)",
   "text": ""
  },
  {
   "id": 28,
   "x": 78,
   "y": 3422,
   "kind": "math",
   "latex": "imshow(lo)",
   "text": ""
  },
  {
   "id": 29,
   "x": 78,
   "y": 4008,
   "kind": "math",
   "latex": "hi:=specfilt\\left(img,\\text{“high”},0.04\\right)",
   "text": ""
  },
  {
   "id": 30,
   "x": 78,
   "y": 4058,
   "kind": "math",
   "latex": "imshow(hi)",
   "text": ""
  },
  {
   "id": "31",
   "x": 62,
   "y": 4644,
   "kind": "text",
   "latex": "",
   "text": "三、卷积：一个核决定一种效果"
  },
  {
   "id": "32",
   "x": 62,
   "y": 4677,
   "kind": "text",
   "latex": "",
   "text": "卷积 = 核在图上滑动做加权求和，核是什么样，输出就是什么效果——这是图像处理最基本的一层。"
  },
  {
   "id": "33",
   "x": 62,
   "y": 4710,
   "kind": "text",
   "latex": "",
   "text": "conv2 用 FFT 加速，512×512 的图也是瞬间出。均值核 → 模糊（每个点换成 3×3 邻域的平均）："
  },
  {
   "id": 34,
   "x": 78,
   "y": 4743,
   "kind": "math",
   "latex": "kblur:=\\frac{1}{9}\\begin{pmatrix}1&1&1\\\\1&1&1\\\\1&1&1\\end{pmatrix}",
   "text": ""
  },
  {
   "id": 35,
   "x": 78,
   "y": 4837,
   "kind": "math",
   "latex": "cb:=conv2\\left(img,kblur\\right)",
   "text": ""
  },
  {
   "id": 36,
   "x": 78,
   "y": 4887,
   "kind": "math",
   "latex": "imshow(cb)",
   "text": ""
  },
  {
   "id": "37",
   "x": 62,
   "y": 5473,
   "kind": "text",
   "latex": "",
   "text": "拉普拉斯核 → 只剩边缘（平坦区 4 邻域加自己 −4，互相抵消为 0）："
  },
  {
   "id": 38,
   "x": 78,
   "y": 5506,
   "kind": "math",
   "latex": "kedge:=\\begin{pmatrix}0&1&0\\\\1&-4&1\\\\0&1&0\\end{pmatrix}",
   "text": ""
  },
  {
   "id": 39,
   "x": 78,
   "y": 5600,
   "kind": "math",
   "latex": "ce:=conv2\\left(img,kedge\\right)",
   "text": ""
  },
  {
   "id": 40,
   "x": 78,
   "y": 5650,
   "kind": "math",
   "latex": "imshow(ce)",
   "text": ""
  },
  {
   "id": "41",
   "x": 62,
   "y": 6236,
   "kind": "text",
   "latex": "",
   "text": "锐化核 = 原图 + 边缘（中心 5 = 自己 1 份 + 边缘 4 份）："
  },
  {
   "id": 42,
   "x": 78,
   "y": 6269,
   "kind": "math",
   "latex": "ksharp:=\\begin{pmatrix}0&-1&0\\\\-1&5&-1\\\\0&-1&0\\end{pmatrix}",
   "text": ""
  },
  {
   "id": 43,
   "x": 78,
   "y": 6363,
   "kind": "math",
   "latex": "cs:=conv2\\left(img,ksharp\\right)",
   "text": ""
  },
  {
   "id": 44,
   "x": 78,
   "y": 6413,
   "kind": "math",
   "latex": "imshow(cs)",
   "text": ""
  },
  {
   "id": "45",
   "x": 62,
   "y": 6999,
   "kind": "text",
   "latex": "",
   "text": "一维同理：给信号做滑动平均，就是和 (1/3)(1,1,1) 做卷积——5 Hz 还在，20/50 Hz 被抹平了大半："
  },
  {
   "id": 46,
   "x": 78,
   "y": 7032,
   "kind": "math",
   "latex": "k3:=\\frac{1}{3}\\begin{pmatrix}1\\\\1\\\\1\\end{pmatrix}",
   "text": ""
  },
  {
   "id": 47,
   "x": 78,
   "y": 7126,
   "kind": "math",
   "latex": "smooth:=convolve\\left(sig,k3\\right)",
   "text": ""
  },
  {
   "id": 48,
   "x": 78,
   "y": 7176,
   "kind": "math",
   "latex": "plot(smooth)",
   "text": ""
  },
  {
   "id": 74,
   "x": 60,
   "y": 7530,
   "kind": "text",
   "latex": "",
   "text": "四、小波变换：能同时看\"什么时候\"和\"什么频率\"的数学显微镜",
   "w": 860
  },
  {
   "id": 75,
   "x": 60,
   "y": 7563,
   "kind": "text",
   "latex": "",
   "text": "傅里叶只回答\"信号里**有哪些频率**\"，答不了\"**什么时候出现**\"。先造一个**频率随时间升高**的扫频信号（chirp）：起始 2 Hz，1 秒内均匀升到 14 Hz：",
   "w": 860
  },
  {
   "id": 76,
   "x": 60,
   "y": 7596,
   "kind": "math",
   "latex": "c:=\\sin\\left(2\\pi\\cdot\\left(2\\cdot t+6\\cdot emul\\left(t,t\\right)\\right)\\right)",
   "text": "",
   "w": 860
  },
  {
   "id": 77,
   "x": 60,
   "y": 7646,
   "kind": "text",
   "latex": "",
   "text": "时域一眼就懂：波形越来越密 = 频率在升高（emul(t,t) 是逐元素相乘，算出 t²）：",
   "w": 860
  },
  {
   "id": 78,
   "x": 60,
   "y": 7679,
   "kind": "math",
   "latex": "plot(c)",
   "text": "",
   "w": 860
  },
  {
   "id": 79,
   "x": 60,
   "y": 8033,
   "kind": "text",
   "latex": "",
   "text": "但 fft 只能说\"2~14 Hz 的成分**都存在**\"——频谱是一片宽包，完全看不出**哪一段是高频**：",
   "w": 860
  },
  {
   "id": 80,
   "x": 60,
   "y": 8066,
   "kind": "math",
   "latex": "magc:=fft(c)",
   "text": "",
   "w": 860
  },
  {
   "id": 81,
   "x": 60,
   "y": 8116,
   "kind": "math",
   "latex": "plot(fhz, magc)",
   "text": "",
   "w": 860
  },
  {
   "id": 82,
   "x": 60,
   "y": 8470,
   "kind": "text",
   "latex": "",
   "text": "小波按**尺度**分解：haar(v) 一层给出两条曲线——近似（相邻两点求平均）与细节（相邻两点求差）：",
   "w": 860
  },
  {
   "id": 83,
   "x": 60,
   "y": 8503,
   "kind": "math",
   "latex": "wc:=haar(c)",
   "text": "",
   "w": 860
  },
  {
   "id": 84,
   "x": 60,
   "y": 8553,
   "kind": "math",
   "latex": "plot(wc)",
   "text": "",
   "w": 860
  },
  {
   "id": 85,
   "x": 60,
   "y": 8907,
   "kind": "text",
   "latex": "",
   "text": "细节曲线在**波形变密的地方**出现大值——每个细节点对应一小段时间，所以它同时携带\"何时\"与\"多快\"。fft 的峰只会说\"有这些频率\"，小波的细节会说\"**后半段**频率高\"。这就是\"数学显微镜\"的含义：",
   "w": 860
  },
  {
   "id": 86,
   "x": 60,
   "y": 8959,
   "kind": "text",
   "latex": "",
   "text": "ihaar 一丝不差还原原信号（分解 + 反演无损）：",
   "w": 860
  },
  {
   "id": 87,
   "x": 60,
   "y": 8992,
   "kind": "math",
   "latex": "cwb:=ihaar(wc)",
   "text": "",
   "w": 860
  },
  {
   "id": 88,
   "x": 60,
   "y": 9042,
   "kind": "math",
   "latex": "plot(cwb)",
   "text": "",
   "w": 860
  },
  {
   "id": 89,
   "x": 60,
   "y": 9482,
   "kind": "text",
   "latex": "",
   "text": "二维小波把图像拆成四块子带（与原图同尺寸）：左上 = 近似轮廓，右上 / 左下 / 右下 = 水平 / 垂直 / 对角细节：",
   "w": 860
  },
  {
   "id": 90,
   "x": 60,
   "y": 9516,
   "kind": "text",
   "latex": "",
   "text": "系数大多在 0 附近，**直接 imshow 是一片黑**——先 imnorm 做对比度拉伸（min-max 归一化到 0–1）才能看清：",
   "w": 860
  },
  {
   "id": 91,
   "x": 60,
   "y": 9550,
   "kind": "math",
   "latex": "w2:=haar2(img)",
   "text": "",
   "w": 860
  },
  {
   "id": 92,
   "x": 60,
   "y": 9700,
   "kind": "math",
   "latex": "wn:=imnorm(w2)",
   "text": "",
   "w": 860
  },
  {
   "id": 93,
   "x": 60,
   "y": 9850,
   "kind": "math",
   "latex": "imshow(wn)",
   "text": "",
   "w": 860
  },
  {
   "id": 94,
   "x": 60,
   "y": 10436,
   "kind": "text",
   "latex": "",
   "text": "ihaar2 精确重建（误差为 0）：",
   "w": 860
  },
  {
   "id": 95,
   "x": 60,
   "y": 10469,
   "kind": "math",
   "latex": "rec:=ihaar2(w2)",
   "text": "",
   "w": 860
  },
  {
   "id": 96,
   "x": 60,
   "y": 10519,
   "kind": "math",
   "latex": "imshow(rec)",
   "text": "",
   "w": 860
  },
  {
   "id": 97,
   "x": 60,
   "y": 11105,
   "kind": "text",
   "latex": "",
   "text": "五、余弦变换（DCT）：能量打包——压缩的根基",
   "w": 860
  },
  {
   "id": 98,
   "x": 60,
   "y": 11138,
   "kind": "text",
   "latex": "",
   "text": "dct 是傅里叶的\"实数版\"：把信号展开成一堆余弦波。它的拿手好戏是**能量集中**——信号的信息挤进前面少数几个系数。看扫频信号的 DCT 系数：",
   "w": 860
  },
  {
   "id": 99,
   "x": 60,
   "y": 11171,
   "kind": "math",
   "latex": "d:=dct(c)",
   "text": "",
   "w": 860
  },
  {
   "id": 100,
   "x": 60,
   "y": 11221,
   "kind": "math",
   "latex": "plot(d)",
   "text": "",
   "w": 860
  },
  {
   "id": 101,
   "x": 60,
   "y": 11662,
   "kind": "text",
   "latex": "",
   "text": "前几十个系数之外几乎全是 0。idct 精确还原（dct → idct 无损往返）：",
   "w": 860
  },
  {
   "id": 102,
   "x": 60,
   "y": 11696,
   "kind": "math",
   "latex": "cback:=idct(d)",
   "text": "",
   "w": 860
  },
  {
   "id": 103,
   "x": 60,
   "y": 11846,
   "kind": "math",
   "latex": "plot(cback)",
   "text": "",
   "w": 860
  },
  {
   "id": 104,
   "x": 60,
   "y": 12316,
   "kind": "text",
   "latex": "",
   "text": "压缩演示：做一个 0/1 掩码，只保留**前 30 个系数**（不到 1/4 的数据），再重建——",
   "w": 860
  },
  {
   "id": 105,
   "x": 60,
   "y": 12350,
   "kind": "text",
   "latex": "",
   "text": "波形几乎没变（差别只是细小的毛刺）。JPEG 压缩图像用的就是同一招：DCT → 丢掉小系数 → 反变换。emul 是逐元素相乘（不是矩阵乘法），专门用来加窗、加掩码：",
   "w": 860
  },
  {
   "id": 106,
   "x": 60,
   "y": 12418,
   "kind": "math",
   "latex": "idx:=0,1..127",
   "text": "",
   "w": 860
  },
  {
   "id": 107,
   "x": 60,
   "y": 12474,
   "kind": "math",
   "latex": "kk:=\\frac{sign\\left(29.5-idx\\right)+1}{2}",
   "text": "",
   "w": 860
  },
  {
   "id": 108,
   "x": 60,
   "y": 12624,
   "kind": "math",
   "latex": "dc:=emul(d,kk)",
   "text": "",
   "w": 860
  },
  {
   "id": 109,
   "x": 60,
   "y": 12774,
   "kind": "math",
   "latex": "crec:=idct(dc)",
   "text": "",
   "w": 860
  },
  {
   "id": 110,
   "x": 60,
   "y": 12924,
   "kind": "math",
   "latex": "plot(crec)",
   "text": "",
   "w": 860
  },
  {
   "id": 111,
   "x": 60,
   "y": 13394,
   "kind": "text",
   "latex": "",
   "text": "小结：fft / fft2 看频率成分，specfilt 在频率域做滤波，conv2 用核改变图像，haar / haar2 拆出\"轮廓 + 细节\"，dct / idct 把能量打包（压缩的根基），emul 逐元素相乘（加窗 / 掩码），imnorm 对比度拉伸。全部算子都能对着帮助（F1）里的「信号与随机」一组查。",
   "w": 860
  }
 ]
}
