def strike_bar(profile, f1=261.6, dur=1.2, xe=0.4, xo=0.05, KH=3e10, alpha=2.5, MH=0.02, vH=1.0):
om, V, x = bar_modes(150, profile)
keep = om * f1 / om[0] < 2 * np.pi * 18000 # modes below 18 kHz
om, V = om[keep] * 2 * np.pi * f1 / om[0], V[:, keep]
sig = 0.6 + 2e-8 * om**2 # higher modes decay faster
phe, pho = V[int(xe * 150)], V[int(xo * 150)] # spreading / interpolation weights
k = 1 / fs; c1, c2 = 2 * np.exp(-sig * k) * np.cos(om * k), np.exp(-2 * sig * k)
q, qp = np.zeros(len(om)), np.zeros(len(om))
uH, uHp, out, F_hist = -1e-5, -1e-5 - k * vH, np.zeros(int(dur * fs)), [] # just short of contact, moving in
for n in range(len(out)):
F = KH * max(uH - phe @ q, 0.0)**alpha # contact force
q, qp = c1 * q - c2 * qp + k**2 * phe * F, q
uH, uHp = 2 * uH - uHp - k**2 * F / MH, uH
out[n] = pho @ q; F_hist.append(F)
return out, np.array(F_hist), om / (2 * np.pi)
uniform = lambda x: np.ones_like(x)
fig, axs = plt.subplots(5, 1, figsize=(4.9, 4.2), height_ratios=[1, 1, 1, 0.45, 0.8],
gridspec_kw=dict(hspace=0.25))
axs[3].axis("off"); axs = axs[[0, 1, 2, 4]] # a spacer row before the force plot
runs = [(uniform, 0.4, "0.45", "uniform bar, $x_e=0.4$"),
(arch(d_mar), 0.4, COL["bar"], "marimba bar, $x_e=0.4$"),
(arch(d_mar), 0.5, "darkorange", "marimba bar, $x_e=0.5$ (center)")]
for ax, (prof, xe, col, lab) in zip(axs, runs):
y, F, fm = strike_bar(prof, xe=xe) # read near the end (x_o = 0.05): every mode shows
Y = np.abs(np.fft.rfft(y * np.hanning(len(y)))); f = np.fft.rfftfreq(len(y), 1 / fs)
ax.plot(f / 261.6, 20 * np.log10(Y / Y.max() + 1e-9), color=col, lw=0.9)
for r in [1, 2.76, 4, 5.4, 9.35]:
ax.axvline(r, color="0.85", ls="--", lw=0.8, zorder=0)
ax.text(11.9, -10, lab, color=col, fontsize=8, ha="right", va="top")
ax.set_xlim(0, 12); ax.set_ylim(-120, 5); ax.set_yticks([])
ax.set_xticks([1, 2.76, 4, 5.4, 9.35]); ax.tick_params(labelbottom=False)
if prof is uniform:
axs[3].plot(1e3 * np.arange(len(F)) / fs, F / F.max(), color="k", lw=1.2)
axs[2].annotate("no mode 2", (4, -100), (5.3, -75), fontsize=7.5, color="darkorange",
arrowprops=dict(arrowstyle="->", color="darkorange", lw=0.8))
axs[2].tick_params(labelbottom=True, labelsize=8)
axs[2].set_xlabel("frequency / $f_1$ ($f_1$ = C4)", fontsize=8, labelpad=1)
axs[0].set_title("same mallet, same pitch: mode 2 at 2.76 vs 4; gone at the center", fontsize=9)
axs[3].set_xlim(0, 3); axs[3].set_xlabel("time (ms)", fontsize=8); axs[3].set_yticks([])
axs[3].set_title("contact force", fontsize=9)
fig.subplots_adjust(left=0.03, right=0.97, top=0.93, bottom=0.09)
plt.show()