# 信号生成和可视化

### 周期性波形

`sawtooth` 函数生成锯齿波，波峰在 $±1$，周期为 $2\pi$。可选宽度参数以 $2\pi$ 的小数倍来指定信号最大值出现的位置。

`square` 函数生成周期为 $2\pi$ 的方波。可选参数指定占空比，即信号为正的周期的百分比。

```fs = 10000; t = 0:1/fs:1.5; x1 = sawtooth(2*pi*50*t); x2 = square(2*pi*50*t); nexttile plot(t,x1) axis([0 0.2 -1.2 1.2]) xlabel("Time (sec)") ylabel("Amplitude") title("Sawtooth Periodic Wave") nexttile plot(t,x2) axis([0 0.2 -1.2 1.2]) xlabel("Time (sec)") ylabel("Amplitude") title("Square Periodic Wave")```

### 非周期性波形

`tripuls` 函数生成以 t = 0 为中心、默认宽度为 1 的采样非周期性单位高度三角形脉冲。

`rectpuls` 函数生成以 t = 0 为中心、默认宽度为 1 的采样非周期性单位高度矩形脉冲。非零振幅的区间定义为在右侧开放：`rectpuls(-0.5) = 1`，而 `rectpuls(0.5) = 0`

```fs = 10000; t = -1:1/fs:1; x1 = tripuls(t,20e-3); x2 = rectpuls(t,20e-3); figure nexttile plot(t,x1) axis([-0.1 0.1 -0.2 1.2]) xlabel("Time (sec)") ylabel("Amplitude") title("Triangular Aperiodic Pulse") nexttile plot(t,x2) axis([-0.1 0.1 -0.2 1.2]) xlabel("Time (sec)") ylabel("Amplitude") title("Rectangular Aperiodic Pulse")```

`gauspuls` 函数使用指定时间、中心频率和小数带宽生成高斯调制正弦脉冲。

`sinc` 函数计算输入向量或矩阵的数学正弦函数。正弦函数是宽度为 $2\pi$，高度为单位高度的矩形脉冲的连续傅里叶逆变换。

```tc = gauspuls("cutoff",50e3,0.6,[],-40); t1 = -tc : 1e-6 : tc; y1 = gauspuls(t1,50e3,0.6);```

```t2 = linspace(-5,5); y2 = sinc(t2); figure nexttile plot(t1*1e3,y1) xlabel("Time (ms)") ylabel("Amplitude") title("Gaussian Pulse") nexttile plot(t2,y2) xlabel("Time (sec)") ylabel("Amplitude") title("Sinc Function")```

### 扫频波形

```tlin = 0:0.001:2; ylin = chirp(tlin,100,1,250);```

```tq = -2:0.001:2; yq = chirp(tq,100,1,200,"quadratic");```

```figure nexttile pspectrum(ylin,tlin,"spectrogram", ... Leakage=0.85,TimeResolution=0.1,OverlapPercent=99) title("Linear Chirp") nexttile pspectrum(yq,tq,"spectrogram", ... Leakage=0.85,TimeResolution=0.1,OverlapPercent=99) title("Quadratic Chirp")```

```tcx = -1:0.001:1; fo = 100; f1 = 400; ycx = chirp(tcx,fo,1,f1,"quadratic",[],"convex");```

```tcv = -1:0.001:1; fo = 400; f1 = 100; ycv = chirp(tcv,fo,1,f1,"quadratic",[],"concave");```

```figure nexttile pspectrum(ycx,tcx,"spectrogram", ... Leakage=0.85,TimeResolution=0.1,OverlapPercent=99) title("Convex Chirp") nexttile pspectrum(ycv,tcv,"spectrogram", ... Leakage=0.85,TimeResolution=0.1,OverlapPercent=99) title("Concave Chirp")```

```fs = 10000; t = 0:1/fs:2; x1 = vco(sawtooth(2*pi*t,0.75),[0.1 0.4]*fs,fs); x2 = vco(square(2*pi*t),[0.1 0.4]*fs,fs);```

```figure nexttile pspectrum(x1,t,"spectrogram", ... Leakage=0.9,FrequencyResolution=55) title("VCO Triangle") nexttile pspectrum(x2,t,"spectrogram", ... Leakage=0.9,FrequencyResolution=55) title("VCO Rectangle")```

### 脉冲序列

```fs = 100e9; D = [2.5 10 17.5]' * 1e-9; t = 0 : 1/fs : 2500/fs; w = 1e-9; yp = pulstran(t,D,@rectpuls,w);```

```T = 0 : 1/50e3 : 10e-3; D = [0 : 1/1e3 : 10e-3 ; 0.8.^(0:10)]'; Y = pulstran(T,D,@gauspuls,10e3,.5); figure nexttile plot(t*1e9,yp) axis([0 25 -0.2 1.2]) xlabel("Time (ns)") ylabel("Amplitude") title("Rectangular Train") nexttile plot(T*1e3,Y) xlabel("Time (ms)") ylabel("Amplitude") title("Gaussian Pulse Train")```