Skip to content

Instantly share code, notes, and snippets.

@stephenc
Created August 4, 2025 10:08
Show Gist options
  • Select an option

  • Save stephenc/dbe68f6bebe0f36cf00c67dbdac71adf to your computer and use it in GitHub Desktop.

Select an option

Save stephenc/dbe68f6bebe0f36cf00c67dbdac71adf to your computer and use it in GitHub Desktop.
Finger saver - Lesson 7
window_duration = 2.0 # seconds
window_size = int(window_duration * sr)
num_windows = 16
fig, axes = plt.subplots(4, 4, figsize=(16, 12))
axes = axes.flatten()
for i in range(num_windows):
start = i * window_size
end = start + window_size
segment = y[start:end]
if len(segment) < window_size:
# Pad if the last segment is too short
segment = np.pad(segment, (0, window_size - len(segment)))
Y = np.fft.rfft(segment)
freqs = np.fft.rfftfreq(len(segment), d=1/sr)
ax = axes[i]
ax.plot(freqs, np.abs(Y), lw=0.7)
ax.set_title(f"Window {i+1}: {i*2}-{(i+1)*2} s")
ax.set_xlim(0, 4096)
ax.set_ylim(0, None)
ax.set_xticks([0, 1000, 2000, 3000, 4000])
ax.tick_params(labelsize=8)
plt.tight_layout()
plt.suptitle("Rolling FFT: 2-second Windows", fontsize=18, y=1.02)
plt.show()
from scipy.signal import lombscargle
window_duration = 2.0 # seconds
window_size = int(window_duration * sr)
num_windows = 16
frequencies = np.linspace(1, 4096, 2048)
angular_freqs = 2 * np.pi * frequencies
fig, axes = plt.subplots(4, 4, figsize=(16, 12))
axes = axes.flatten()
for i in range(num_windows):
start = i * window_size
end = start + window_size
segment = y[start:end]
if len(segment) < window_size:
segment = np.pad(segment, (0, window_size - len(segment)))
t = np.linspace(0, window_duration, len(segment)) # Even sampling
power = lombscargle(t, segment, angular_freqs, normalize="power")
ax = axes[i]
ax.plot(frequencies, power, lw=0.7)
ax.set_title(f"Window {i+1}: {i*2}-{(i+1)*2} s")
ax.set_xlim(0, 4096)
ax.set_ylim(0, None)
ax.set_xticks([0, 1000, 2000, 3000, 4000])
ax.tick_params(labelsize=8)
plt.tight_layout()
plt.suptitle("Rolling Lomb-Scargle: 2-second Windows",
fontsize=18, y=1.02)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment