Signal Processing
Signal Basics
import numpy as np
from scipy import signal
# Time-domain signal
fs = 1000 # Sample rate (Hz)
t = np.linspace(0, 1, fs)
x = np.sin(2 * np.pi * 10 * t) + 0.5 * np.random.randn(fs)
# Frequency domain
X = np.fft.fft(x)
freqs = np.fft.fftfreq(len(x), 1/fs)
power = np.abs(X) ** 2
Filtering
from scipy.signal import butter, sosfilt, firwin, lfilter
# IIR Butterworth filter
sos = butter(4, 50, btype='low', fs=fs, output='sos')
filtered = sosfilt(sos, x)
# Filter types: 'low', 'high', 'bandpass', 'bandstop'
sos = butter(4, [20, 80], btype='bandpass', fs=fs, output='sos')
# FIR filter
b = firwin(101, 50 / (fs/2))
filtered = lfilter(b, 1, x)
Convolution
from scipy.signal import convolve, fftconvolve, convolve2d
# 1D convolution
h = np.array([0.2, 0.2, 0.2, 0.2, 0.2]) # Moving average
y = convolve(x, h, mode='same')
# FFT-based (faster for large kernels)
y = fftconvolve(x, h, mode='same')
# 2D convolution (images)
kernel = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) # Sobel
edges = convolve2d(image, kernel, mode='same')
Spectral Analysis and Other Operations
from scipy.signal import welch, spectrogram, stft, find_peaks, resample, hilbert
# Power spectral density
freqs, psd = welch(x, fs, nperseg=256)
# Spectrogram
f, t, Sxx = spectrogram(x, fs)
# STFT
f, t, Zxx = stft(x, fs, nperseg=256)
# Peak detection
peaks, props = find_peaks(x, height=0.5, distance=10, prominence=0.3)
# Resample
x_resampled = resample(x, 500)
# Hilbert transform
envelope = np.abs(hilbert(x))
GPU Acceleration
import cupy as cp
# GPU FFT
x_gpu = cp.asarray(x)
X_gpu = cp.fft.fft(x_gpu)
# GPU convolution via FFT
def gpu_convolve(signal, kernel):
n = len(signal) + len(kernel) - 1
return cp.fft.ifft(cp.fft.fft(signal, n) * cp.fft.fft(kernel, n)).real
# Batch processing
batch = cp.random.rand(100, 1024)
batch_fft = cp.fft.fft(batch, axis=1)