5.4.12 · D5Scientific Computing (Python)
Question bank — scipy.signal — filtering, convolution, FFT-based analysis
True or false — justify
Convolution and correlation of two sequences always give the same numbers.
False. Correlation is convolution without flipping one kernel; the two agree only when the kernel is symmetric (), because then flipping changes nothing.
fftconvolve(x, h) gives a different, approximate answer compared to convolve(x, h).
False. By the convolution theorem, time convolution equals frequency multiplication exactly;
fftconvolve returns the same result to floating-point rounding, just in instead of .Taking more samples of a signal lets you detect higher frequencies.
False. The highest honest frequency is Nyquist , fixed by the sampling rate. More samples only shrink the bin spacing — finer resolution, not higher reach. See Aliasing and Sampling Theorem.
filtfilt and lfilter with the same (b, a) produce the same output.
False.
lfilter is causal and delays the signal; filtfilt runs the filter forward then backward so the two delays cancel, giving zero phase and roughly the squared magnitude response (filter applied twice).A Butterworth filter (IIR) is always safe to use because scipy designed it for you.
False. IIR filters use feedback from past outputs and can be numerically unstable, especially at high order or extreme cutoffs; FIR filters (
firwin) are always stable because they only sum past inputs.The convolution theorem means you can always replace a convolution by an FFT multiply and back.
True in principle, but you must use enough FFT length (zero-padding to ) or the wrap-around gives circular convolution, not the linear one you wanted.
Doubling the recording length halves the frequency bin spacing .
True. ; keeping fixed and doubling (a longer recording) halves , so two close peaks become resolvable.
The output of a full convolution of length- and length- arrays has length .
False. It has length : the first sample is one-term overlap and the last is one-term overlap, so you gain distinct slide positions, not .
Spot the error
b, a = butter(4, 80) for a signal sampled at 1000 Hz.
Error: without
fs=, the cutoff is a fraction of Nyquist, so it must lie in . Passing 80 is illegal. Fix: butter(4, 80, fs=1000) or use .mag = np.abs(np.fft.rfft(x)) and calling mag "the amplitude spectrum".
Error: the raw magnitude is unscaled. For true one-sided amplitudes divide by and multiply the interior bins by 2 (DC and Nyquist are not doubled, since they have no mirror partner).
Using lfilter on recorded data and then reporting that an event "shifted later in time".
Error: the shift is the causal filter's own phase delay, not a real change in the data. For offline analysis use
filtfilt (zero phase) so features stay put.correlate(signal, template) and reading the maximum value as the match quality without normalising.
Error: raw correlation grows with signal energy, so a loud noisy region can outscore the true match. Normalise (e.g. divide by local norms) for a fair template score — see Cross-correlation and Template Matching.
convolve(x, h, mode='same') returns an array shorter than x.
Error:
'same' returns output exactly the length of x, centered. If you saw something shorter you likely used 'valid', which returns only fully-overlapping positions.Passing a filter cutoff above Nyquist, e.g. butter(4, 600, fs=1000).
Error: . There is no representable frequency there, so scipy raises an error; a low-pass cutoff must satisfy .
np.fft.fft on a real signal and then plotting all bins as if each were a distinct frequency.
Error: a real signal's spectrum is conjugate-symmetric, so the upper half mirrors the lower. Use
rfft/rfftfreq to get the meaningful positive half only.Why questions
Why is convolution — and not some other combining rule — forced on a linear time-invariant system?
Because any input is a sum of scaled, shifted spikes; linearity turns "response to a sum" into "sum of responses", and time-invariance makes each shifted spike give the same just shifted. Adding those up is exactly the convolution sum. See Linear Time-Invariant Systems.
Why does the probe inside the DFT tell us "how much" of frequency is present?
The sum is an inner product between the signal and that pure wave; it is large exactly when the signal resembles (aligns with) the wave, so measures overlap with frequency . See Fourier Transform.
Why does filtfilt distort a signal less in phase but more in magnitude than lfilter?
Running the filter twice (forward + reverse) cancels the phase (delays subtract) but multiplies the magnitude response by itself — so the effective attenuation is roughly squared. Design cutoffs with this doubling in mind.
Why is fftconvolve faster than direct convolve only for large arrays?
FFT is vs direct , but FFTs carry overhead (padding, transforms). For small the constant factors dominate and direct convolution can win; the crossover is around a few dozen to a few hundred samples.
Why can two sine components at 50 Hz and 51 Hz look like one peak in a short FFT?
If the recording is too short, may exceed 1 Hz, so both fall in (or spill across) the same bin. Record longer to shrink and split them.
Why do we zero-pad before an FFT-based convolution?
The FFT assumes the signal is periodic, so without padding the tail wraps around and contaminates the start (circular convolution). Padding to gives room for the full linear overlap.
Why is an FIR filter always stable while an IIR filter may not be?
FIR output is a finite weighted sum of inputs only — bounded input gives bounded output automatically. IIR feeds past outputs back in, so poor coefficients can let the output grow without bound.
Edge cases
What does convolving a signal with the unit impulse (a single 1) do?
Nothing — it returns the signal unchanged. The impulse is the identity of convolution, matching the idea that is the system's reaction to a lone spike.
What is the DC bin of an FFT, physically?
, the (unnormalised) average/offset of the signal — the "zero-frequency" component. It has no mirror twin, so it is never doubled in one-sided scaling.
What happens at the Nyquist bin () for an even-length real signal?
It is a real, single (unpaired) value like DC — so, like DC, it is not multiplied by 2 in one-sided amplitude scaling. Forgetting this over-counts its amplitude.
What does an FFT show for a pure constant (flat) signal with no oscillation?
All the energy sits in the DC bin and every other bin is (ideally) zero — a flat line has no frequency content besides its offset.
If the kernel is longer than the signal, does convolution still work?
Yes. Convolution is symmetric, ; the roles of "signal" and "kernel" are interchangeable and the output length is still .
What happens if you filter a signal whose frequencies are all already below the cutoff?
The low-pass leaves them essentially untouched (passband gain ); you mostly see only edge/transient effects at the very start and end from the filter's startup transient.
What does correlating a signal with itself (autocorrelation) peak at, and why?
It peaks at zero lag, because a signal aligns best with an unshifted copy of itself; the peak height equals the signal's energy .
Sampling a 120 Hz tone at Hz — what frequency will the FFT report?
An aliased ghost. Since , it folds to Hz. Nyquist is violated, so the true frequency is lost — the core warning of Aliasing and Sampling Theorem.