From 58fbed2cc930f82ce3992aa846be9945f785d93f Mon Sep 17 00:00:00 2001 From: shrutik pradip balwan Date: Wed, 7 Oct 2026 23:42:10 +0530 Subject: [PATCH 1/3] Fixes #11: Add FFT/STFT and spectrogram visualization --- src/signal_processing_toolkit/spectral.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 src/signal_processing_toolkit/spectral.py diff --git a/src/signal_processing_toolkit/spectral.py b/src/signal_processing_toolkit/spectral.py new file mode 100644 index 0000000..0809f2a --- /dev/null +++ b/src/signal_processing_toolkit/spectral.py @@ -0,0 +1,23 @@ +import numpy as np +import scipy.signal as signal +import matplotlib.pyplot as plt + +def compute_fft(data, fs): + """Computes 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): + """Computes 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') + plt.ylabel('Frequency [Hz]') + plt.xlabel('Time [sec]') + plt.colorbar(label='Power/Frequency (dB/Hz)') + plt.show() From dc4d65c6ddaf579135c47db63c4f70604d04902c Mon Sep 17 00:00:00 2001 From: shrutik pradip balwan Date: Wed, 7 Oct 2026 23:51:56 +0530 Subject: [PATCH 2/3] Fix formatting and improve plot labels in spectral.py --- src/signal_processing_toolkit/spectral.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/signal_processing_toolkit/spectral.py b/src/signal_processing_toolkit/spectral.py index 0809f2a..a221313 100644 --- a/src/signal_processing_toolkit/spectral.py +++ b/src/signal_processing_toolkit/spectral.py @@ -1,23 +1,26 @@ +import matplotlib.pyplot as plt import numpy as np import scipy.signal as signal -import matplotlib.pyplot as plt + def compute_fft(data, fs): """Computes 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]) + return xf[: N // 2], 2.0 / N * np.abs(yf[: N // 2]) + def compute_stft(data, fs, nperseg=256): """Computes 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') - plt.ylabel('Frequency [Hz]') - plt.xlabel('Time [sec]') - plt.colorbar(label='Power/Frequency (dB/Hz)') + plt.pcolormesh(t, f, 10 * np.log10(Sxx), shading="gouraud") + plt.ylabel("Frequency [Hz]") + plt.xlabel("Time [sec]") + plt.colorbar(label="Power/Frequency (dB/Hz)") plt.show() From ab636dd448a7fdb188b6b1a4c350ec7e98feea11 Mon Sep 17 00:00:00 2001 From: shrutik pradip balwan Date: Thu, 8 Oct 2026 00:04:52 +0530 Subject: [PATCH 3/3] Fix ruff linting errors in spectral.py --- src/signal_processing_toolkit/spectral.py | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/signal_processing_toolkit/spectral.py b/src/signal_processing_toolkit/spectral.py index a221313..8dbac1c 100644 --- a/src/signal_processing_toolkit/spectral.py +++ b/src/signal_processing_toolkit/spectral.py @@ -4,23 +4,19 @@ def compute_fft(data, fs): - """Computes the Fast Fourier Transform of a 1D signal.""" - N = len(data) + """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]) + 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): - """Computes the Short-Time Fourier Transform.""" - f, t, Zxx = signal.stft(data, fs, nperseg=nperseg) - return f, t, np.abs(Zxx) + """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): +def plot_spectrogram(f, t, sxx): """Visualizes STFT output as a spectrogram.""" - plt.pcolormesh(t, f, 10 * np.log10(Sxx), shading="gouraud") - plt.ylabel("Frequency [Hz]") - plt.xlabel("Time [sec]") - plt.colorbar(label="Power/Frequency (dB/Hz)") - plt.show() + plt.pcolormesh(t, f, 10 * np.log10(sxx), shading="gouraud")