Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/signal_processing_toolkit/spectral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import matplotlib.pyplot as plt
import numpy as np
import scipy.signal as signal


def compute_fft(data, fs):
"""Compute the Fast Fourier Transform of a 1D signal."""
n = len(data)
yf = np.fft.fft(data)
xf = np.fft.fftfreq(n, 1 / fs)
return xf[: n // 2], 2.0 / n * np.abs(yf[: n // 2])


def compute_stft(data, fs, nperseg=256):
"""Compute the Short-Time Fourier Transform."""
f, t, zxx = signal.stft(data, fs, nperseg=nperseg)
return f, t, np.abs(zxx)


def plot_spectrogram(f, t, sxx):
"""Visualizes STFT output as a spectrogram."""
plt.pcolormesh(t, f, 10 * np.log10(sxx), shading="gouraud")
Loading