Band-pass and band-stop filters help us keep or remove specific ranges of signals. This is useful to focus on important parts or remove noise.
Band-pass and band-stop filters in Signal Processing
band_pass_filter(signal, low_cutoff, high_cutoff) band_stop_filter(signal, low_cutoff, high_cutoff)
band_pass_filter keeps frequencies between low_cutoff and high_cutoff.
band_stop_filter removes frequencies between low_cutoff and high_cutoff, keeping others.
filtered_signal = band_pass_filter(signal, 100, 300)
clean_signal = band_stop_filter(signal, 50, 60)
This program creates a signal with two frequencies: 50 Hz and 200 Hz. It then applies a band-pass filter to keep only the 40-60 Hz range and a band-stop filter to remove that same range. The first 5 samples of each result are printed to show the effect.
import numpy as np import matplotlib.pyplot as plt from scipy.signal import butter, filtfilt # Create a sample signal with mixed frequencies fs = 1000 # Sampling frequency in Hz T = 1.0 # seconds n = int(T * fs) t = np.linspace(0, T, n, endpoint=False) # Signal with 50 Hz and 200 Hz components signal = np.sin(2 * np.pi * 50 * t) + 0.5 * np.sin(2 * np.pi * 200 * t) # Define band-pass filter function def band_pass_filter(data, lowcut, highcut, fs, order=4): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq b, a = butter(order, [low, high], btype='band') y = filtfilt(b, a, data) return y # Define band-stop filter function def band_stop_filter(data, lowcut, highcut, fs, order=4): nyq = 0.5 * fs low = lowcut / nyq high = highcut / nyq b, a = butter(order, [low, high], btype='bandstop') y = filtfilt(b, a, data) return y # Apply band-pass filter to keep 40-60 Hz bp_filtered = band_pass_filter(signal, 40, 60, fs) # Apply band-stop filter to remove 40-60 Hz bs_filtered = band_stop_filter(signal, 40, 60, fs) # Print first 5 values of each signal print('Original signal samples:', signal[:5]) print('Band-pass filtered samples:', bp_filtered[:5]) print('Band-stop filtered samples:', bs_filtered[:5])
Band-pass filters keep only the frequencies inside the chosen range.
Band-stop filters remove frequencies inside the chosen range and keep others.
Choosing the right cutoff frequencies depends on your signal and what you want to keep or remove.
Band-pass filters keep signals only in a specific frequency range.
Band-stop filters remove signals in a specific frequency range.
These filters help clean or focus on parts of signals in real-world data.
Practice
Solution
Step 1: Understand the purpose of a band-pass filter
A band-pass filter is designed to allow only a specific range of frequencies to pass through while blocking frequencies outside this range.Step 2: Compare options with the definition
It allows frequencies within a certain range to pass through. matches this definition exactly. Options A, B, and D describe other behaviors not matching a band-pass filter.Final Answer:
It allows frequencies within a certain range to pass through. -> Option DQuick Check:
Band-pass filter = allows certain frequency range [OK]
- Confusing band-pass with band-stop filters
- Thinking band-pass blocks all frequencies
- Assuming band-pass amplifies signals
Solution
Step 1: Recall the butter function parameters
The butter function uses order, cutoff frequency or frequencies, filter type, and sampling frequency. For band-stop, btype='bandstop' and cutoff is a list of two frequencies.Step 2: Check each option for correct syntax
b, a = butter(4, [10, 20], btype='bandstop', fs=100) correctly uses btype='bandstop', cutoff as [10, 20], and fs=100. b, a = butter(4, [10, 20], btype='bandpass', fs=100) uses 'bandpass' which is wrong. b, a = butter(4, 10, btype='bandstop', fs=100) uses a single cutoff frequency which is invalid for band-stop. b, a = butter(4, [10, 20], btype='stopband', fs=100) uses 'stopband' which is not a valid btype.Final Answer:
b, a = butter(4, [10, 20], btype='bandstop', fs=100) -> Option BQuick Check:
Band-stop filter syntax = butter(order, [low, high], btype='bandstop', fs) [OK]
- Using 'bandpass' instead of 'bandstop'
- Providing a single cutoff frequency for band-stop
- Using invalid btype like 'stopband'
import numpy as np from scipy.signal import butter, filtfilt fs = 100 t = np.linspace(0, 1, fs, endpoint=False) signal = np.sin(2*np.pi*5*t) + np.sin(2*np.pi*30*t) b, a = butter(3, [10, 20], btype='bandpass', fs=fs) filtered = filtfilt(b, a, signal) print(filtered.shape)
Solution
Step 1: Understand input signal shape
The signal is created with np.linspace(0, 1, fs) where fs=100, so signal has 100 points, shape (100,).Step 2: Check filtfilt output shape
filtfilt applies zero-phase filtering and returns an array of the same shape as input signal, so filtered.shape is (100,).Final Answer:
(100,) -> Option CQuick Check:
Filtered signal shape = input signal shape = (100,) [OK]
- Assuming filtering changes array length
- Confusing filtfilt with convolution output size
- Miscounting number of samples in signal
from scipy.signal import butter b, a = butter(3, 15, btype='bandpass', fs=100)
Solution
Step 1: Check butter function cutoff parameter for band-pass
Band-pass filters require two cutoff frequencies as a list or tuple, not a single number.Step 2: Identify the fix
Replace 15 with a list like [10, 20] to specify the frequency range for the band-pass filter.Final Answer:
Cutoff frequency should be a list for band-pass, e.g., [10, 20] -> Option AQuick Check:
Band-pass cutoff = list of two frequencies [OK]
- Using single cutoff frequency for band-pass
- Misunderstanding fs parameter usage
- Confusing band-pass with band-stop requirements
Solution
Step 1: Understand the goal
The goal is to keep frequencies between 5Hz and 15Hz and remove others, which means passing only that band.Step 2: Choose correct filter type and cutoff
A band-pass filter passes frequencies within a range, so use btype='bandpass' with cutoff [5, 15]. Band-stop removes frequencies in the range, which is opposite of the goal. Low-pass or high-pass alone cannot isolate the band.Final Answer:
Use a band-pass filter with cutoff frequencies [5, 15] -> Option AQuick Check:
Keep 5-15Hz = band-pass [OK]
- Choosing band-stop instead of band-pass
- Using only low-pass or high-pass filters
- Confusing cutoff frequency roles
Ready to prove it?
Pass the test and this pattern moves unknown
