5.4.12 · HinglishScientific Computing (Python)

scipy.signal — filtering, convolution, FFT-based analysis

1,581 words7 min readRead in English

5.4.12 · Coding › Scientific Computing (Python)


1. Convolution — WHAT, WHY, HOW

WHY this formula? Ek linear time-invariant (LTI) system poori tarah se describe hota hai iss baat se ki woh ek single spike par kaise respond karta hai — us response ko kaho (the impulse response). Koi bhi input scaled, shifted spikes ka sum hoti hai: . Linearity ki wajah se output bhi scaled, shifted responses ka wohi sum hoga → exactly convolution sum. Toh convolution arbitrary nahi hai: ye forced hai linearity + time-invariance ki wajah se.

HOW (the flip-and-slide): ko flip karo, use ke across slide karo, overlapping samples ko multiply karo, sum karo. Output length .

from scipy.signal import convolve, correlate, fftconvolve
convolve([1,2,3],[1,1])            # -> [1,3,5,3], 'full' by default
convolve(x, h, mode='same')        # output same length as x (centered)
fftconvolve(x, h)                   # SAME result, O(N log N) via FFT — fast for big arrays

2. FFT — frequency content

WHY: ek probe wave hai; sum ek inner product hai, toh tab bada hota hai jab us wave se miltaa-julta ho. FFT bas saare ko ki jagah mein compute karta hai.

import numpy as np
fs = 1000                      # Hz sampling rate
t  = np.arange(0, 1, 1/fs)     # 1 s
x  = np.sin(2*np.pi*50*t) + 0.5*np.sin(2*np.pi*120*t)
X  = np.fft.rfft(x)
f  = np.fft.rfftfreq(len(x), d=1/fs)   # frequency in Hz
mag = np.abs(X)/len(x)*2               # *2 for one-sided amplitude
# peaks appear at 50 Hz and 120 Hz
Figure — scipy.signal — filtering, convolution, FFT-based analysis

3. Filtering — unwanted frequencies hatana

FIR vs IIR

from scipy.signal import butter, firwin, filtfilt, lfilter, freqz
 
# 4th-order Butterworth low-pass, cutoff 80 Hz, fs=1000
b, a = butter(4, 80, btype='low', fs=fs)
y_zerophase = filtfilt(b, a, x)   # forward+backward -> ZERO phase distortion
y_causal    = lfilter(b, a, x)    # real-time, but introduces phase delay

4. The unifying theorem


Common mistakes


Discrete convolution formula?
(flip, slide, multiply, sum)
Ek LTI system apni impulse response se poori tarah describe kyun hota hai?
Koi bhi input = scaled/shifted spikes ka sum; linearity se output = scaled/shifted impulse responses ka same sum = convolution.
Len-M aur len-N arrays ki full convolution ki output length?
Convolution aur correlation mein kya fark hai?
Correlation kernel ko flip NAHI karta; template matching / alignment ke liye use hota hai.
Nyquist frequency?
— aliasing ke bina represent karne layak highest frequency.
FFT frequency bin spacing?
Convolution theorem?
— time convolution = frequency multiplication.
FIR vs IIR?
FIR: output sirf past inputs se, hamesha stable, linear phase. IIR: past outputs se feedback, sasta lekin unstable ho sakta hai.
filtfilt vs lfilter?
filtfilt = forward+backward → zero phase; lfilter = causal lekin phase delay add karta hai.
80 Hz, fs=1000 ke liye normalised cutoff Wn?
Large arrays ke liye fftconvolve, convolve se behtar kyun hai?
Convolution theorem use karta hai: O(N log N) FFTs + pointwise multiply vs O(N²) direct sum.
Do close frequency peaks resolve karne ke liye kya change karte ho?
Zyada der record karo (bada N) → chhota Δf. Faster sampling se resolution help nahi hoti.
Recall Feynman: explain to a 12-year-old

Socho ek chhoti stamp (kernel) ko stickers ki ek lambi strip (signal) par slide kar rahe ho, aur har jagah dekh rahe ho kitna overlap hota hai — wahi hai convolution, aur isi se hum cheezein blur ya smooth karte hain. Ek filter matlab ek aisi stamp chunna jo jiggly fast bits mita de lekin slow shape rakhle. FFT ek jaaduyi prism jaisi hai: apni wiggly signal usmein se gujaro aur woh use pure musical notes (frequencies) mein tod deta hai, toh tum dekh sako "aha, yahan ek loud 50 Hz hum hai," aur phir us note ko zero kar do.

Connections

  • Fourier Transform — FFT ki math foundation.
  • Aliasing and Sampling Theorem — Nyquist kyun matter karta hai.
  • Linear Time-Invariant Systems — convolution sum ka source.
  • numpy.fft — lower-level FFT jis par scipy build karta hai.
  • Digital Filter Design — Butterworth/Chebyshev tradeoffs.
  • Cross-correlation and Template Matching

Concept Map

described by

linearity + time-invariance forces

without flipping h

peak marks

multiply spectra equals

computes

O N logN speeds up

same result via

bin spacing f_s/N

real signal symmetric use

time-domain method for

LTI system

Impulse response h n

Convolution x*h

Correlation

FFT / DFT

fftconvolve

Frequency spectrum X k

Nyquist f_s/2

rfft for real signals

Filtering / cleaning