Exercises
ex-sp-ch15-01
EasyCreate a simple sinusoid plot using the OO API. Plot for with a blue line, labeled axes, and a title. Save as PDF.
Use fig, ax = plt.subplots() and ax.plot().
Implementation
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 1000)
y = np.sin(2 * np.pi * 5 * t)
fig, ax = plt.subplots(figsize=(7, 3))
ax.plot(t, y, color='#2563EB')
ax.set(xlabel='Time (s)', ylabel='Amplitude',
title='5 Hz Sinusoid')
fig.savefig('sinusoid.pdf', bbox_inches='tight')
ex-sp-ch15-02
EasyPlot a scatter diagram of 500 complex Gaussian samples in the I-Q plane with equal aspect ratio.
Generate with rng.standard_normal(500) + 1j * rng.standard_normal(500) divided by .
Implementation
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(42)
z = (rng.standard_normal(500) + 1j * rng.standard_normal(500)) / np.sqrt(2)
fig, ax = plt.subplots(figsize=(5, 5))
ax.scatter(z.real, z.imag, s=5, alpha=0.5)
ax.set(xlabel='I', ylabel='Q', aspect='equal',
title=r' Samples')
ax.grid(True, alpha=0.3)
ex-sp-ch15-03
EasyDisplay a random matrix as a heatmap using imshow
with the viridis colormap and a colorbar.
Implementation
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(42)
Z = rng.standard_normal((10, 10))
fig, ax = plt.subplots()
im = ax.imshow(Z, cmap='viridis')
fig.colorbar(im, ax=ax)
ax.set_title('Random Matrix Heatmap')
ex-sp-ch15-04
EasyCreate a grid of subplots with sharex=True and plot
a different frequency sinusoid in each panel.
Implementation
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 1, 500)
freqs = [1, 3, 5, 10]
fig, axes = plt.subplots(2, 2, sharex=True, figsize=(8, 5))
for ax, f in zip(axes.flat, freqs):
ax.plot(t, np.sin(2*np.pi*f*t))
ax.set_title(f'{f} Hz')
fig.tight_layout()
ex-sp-ch15-05
EasyPlot a magnitude/phase Bode plot for the transfer function
using scipy.signal.freqz.
Implementation
from scipy.signal import freqz
import numpy as np
import matplotlib.pyplot as plt
w, H = freqz([1, -0.9], [1, -0.5], worN=512)
fig, (ax1, ax2) = plt.subplots(2, 1, sharex=True, figsize=(7, 5))
ax1.plot(w/np.pi, 20*np.log10(np.abs(H)))
ax1.set_ylabel('Magnitude (dB)')
ax2.plot(w/np.pi, np.degrees(np.unwrap(np.angle(H))))
ax2.set(xlabel=r'Normalized Freq ( rad/sample)',
ylabel='Phase (deg)')
fig.tight_layout()
ex-sp-ch15-06
MediumCreate a BER curve comparison for BPSK, QPSK, 16-QAM, and 64-QAM with theoretical curves, simulated points with error bars, and a legend. Use IEEE single-column formatting (3.5 in wide, 8pt font).
ex-sp-ch15-07
MediumBuild a GridSpec figure with three panels: (a) a wide spectrogram on top, (b) time-domain signal on the bottom-left, and (c) power spectral density on the bottom-right. Add (a), (b), (c) labels.
ex-sp-ch15-08
MediumWrite a reusable function plot_constellation(symbols, ax=None, **kwargs)
that plots a constellation diagram with decision boundaries for
QPSK (lines at I=0 and Q=0).
ex-sp-ch15-09
MediumImplement HSV phase coloring for the complex function on the domain . Zeros should appear as spots where all hues meet; poles as dark regions.
ex-sp-ch15-10
MediumPlot a channel impulse response using ax.stem() with proper
formatting: sample indices on x-axis, magnitude on y-axis, with
an inset zoom on the main tap cluster.
ex-sp-ch15-11
HardCreate a 4-panel figure showing QPSK transmission through a multipath channel: (a) transmitted constellation, (b) channel impulse response, (c) received constellation (before equalization), (d) received constellation (after ZF equalization).
ex-sp-ch15-12
HardBuild a custom Matplotlib style file (.mplstyle) for your research
group with specific colors, fonts, line widths, and grid settings.
Demonstrate loading it with plt.style.use().
ex-sp-ch15-13
HardCreate a figure with a main BER plot and two inset panels: one showing the constellation at low SNR and another at high SNR. Connect each inset to the corresponding SNR point on the main curve.
ex-sp-ch15-14
ChallengeBuild a complete "figure factory" class that generates IEEE-formatted figures from simulation result dictionaries. It should support BER curves, constellation diagrams, spectrograms, and multi-panel layouts with a consistent style.
ex-sp-ch15-15
ChallengeReproduce Figure 1 from a published IEEE paper of your choice using only Matplotlib. Match fonts, colors, line styles, and annotations as closely as possible. Document the rcParams you used.