/ proof-of-concept / cymsim_old
Sign in
proof-of-concept / cymsim_old
cymsim_old / examples / full_model.py
38 lines · 1.5 KBCodeBlameHistory
3688ceacymsim: Python port of cymbalsim with a modal linear model and a PyTorch von Karman modelJeremy Magland 1"""The full (nonlinear) model on the 16 inch crash with a bell, compared with the
2linear model for the same strike. On a CPU this takes a few minutes per second
3of sound; pass a shorter duration or a coarser spacing to try it out."""
5import sys
7import matplotlib.pyplot as plt
9import cymsim
10from cymsim import plot
12duration = float(sys.argv[1]) if len(sys.argv) > 1 else 1.0
13spacing = float(sys.argv[2]) if len(sys.argv) > 2 else 0.008
15plate = cymsim.presets.crash_bell_16().evaluate(spacing=spacing)
16print(plate.summary())
17strike = cymsim.Strike(x=0.9, y=0.5, velocity=3.0)
18listener = cymsim.Listener(distance=0.5, angle=30)
20sim = cymsim.Simulation(plate, strike, listener, duration=duration, movie=True)
21print(f"device {sim.device}, dt {sim.dt * 1e6:.2f} us, {sim.steps_per_sample} steps per sample, loss sigma0 {sim.loss[0]:.2f} 1/s, sigma1 {sim.loss[1]:.2e} m^2/s")
22sim.run(progress=True)
23full = sim.sound()
24print(full, f"in {full.info['seconds']:.0f} s wall")
25full.to_wav("crash_bell_full.wav")
27modes = cymsim.compute_modes(plate, fmax=16000)
28linear = cymsim.synthesize(modes, strike, listener, duration=duration)
29linear.to_wav("crash_bell_linear.wav")
31fig, axes = plt.subplots(2, 1, figsize=(10, 8), sharex=True)
32plot.spectrogram(full, ax=axes[0], modes=modes, fmax=16000)
33axes[0].set_title("full model")
34plot.spectrogram(linear, ax=axes[1], modes=modes, fmax=16000)
35axes[1].set_title("linear model")
36fig.tight_layout()
37fig.savefig("crash_bell.png", dpi=120)
38print("wrote crash_bell_full.wav, crash_bell_linear.wav, crash_bell.png")
↑↓ move↵openescclose