Bird
Raised Fist0
Signal Processingdata~5 mins

Band-pass and band-stop filters in Signal Processing

Choose your learning style10 modes available
Introduction

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.

To listen only to a certain range of radio frequencies and ignore others.
To remove unwanted noise from a sound recording that is outside the main signal range.
To isolate a specific frequency range in an ECG signal for heart analysis.
To block interference signals in communication systems.
To analyze only a certain band of frequencies in vibration data from machines.
Syntax
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.

Examples
This keeps frequencies between 100 Hz and 300 Hz in the signal.
Signal Processing
filtered_signal = band_pass_filter(signal, 100, 300)
This removes frequencies between 50 Hz and 60 Hz, often used to remove power line noise.
Signal Processing
clean_signal = band_stop_filter(signal, 50, 60)
Sample Program

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.

Signal Processing
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])
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What does a band-pass filter do to a signal?
easy
A. It removes frequencies within a certain range.
B. It blocks all frequencies and lets no signal pass.
C. It amplifies all frequencies equally.
D. It allows frequencies within a certain range to pass through.

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    It allows frequencies within a certain range to pass through. -> Option D
  4. Quick Check:

    Band-pass filter = allows certain frequency range [OK]
Hint: Band-pass lets frequencies inside a range pass [OK]
Common Mistakes:
  • Confusing band-pass with band-stop filters
  • Thinking band-pass blocks all frequencies
  • Assuming band-pass amplifies signals
2. Which of the following is the correct Python syntax to create a band-stop filter using SciPy's butter function for frequencies 10Hz to 20Hz with a sampling rate of 100Hz?
easy
A. b, a = butter(4, [10, 20], btype='bandpass', fs=100)
B. b, a = butter(4, [10, 20], btype='bandstop', fs=100)
C. b, a = butter(4, 10, btype='bandstop', fs=100)
D. b, a = butter(4, [10, 20], btype='stopband', fs=100)

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    b, a = butter(4, [10, 20], btype='bandstop', fs=100) -> Option B
  4. Quick Check:

    Band-stop filter syntax = butter(order, [low, high], btype='bandstop', fs) [OK]
Hint: Band-stop uses btype='bandstop' with two cutoff frequencies [OK]
Common Mistakes:
  • Using 'bandpass' instead of 'bandstop'
  • Providing a single cutoff frequency for band-stop
  • Using invalid btype like 'stopband'
3. Given the following Python code using SciPy, what will be the shape of the filtered signal array?
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)
medium
A. (2, 100)
B. (99,)
C. (100,)
D. (101,)

Solution

  1. 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,).
  2. 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,).
  3. Final Answer:

    (100,) -> Option C
  4. Quick Check:

    Filtered signal shape = input signal shape = (100,) [OK]
Hint: filtfilt output shape matches input signal shape [OK]
Common Mistakes:
  • Assuming filtering changes array length
  • Confusing filtfilt with convolution output size
  • Miscounting number of samples in signal
4. Identify the error in this code snippet intended to create a band-pass filter and fix it:
from scipy.signal import butter
b, a = butter(3, 15, btype='bandpass', fs=100)
medium
A. Cutoff frequency should be a list for band-pass, e.g., [10, 20]
B. Order should be a float, not integer
C. fs parameter is not valid in butter function
D. btype should be 'bandstop' for single cutoff

Solution

  1. 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.
  2. Step 2: Identify the fix

    Replace 15 with a list like [10, 20] to specify the frequency range for the band-pass filter.
  3. Final Answer:

    Cutoff frequency should be a list for band-pass, e.g., [10, 20] -> Option A
  4. Quick Check:

    Band-pass cutoff = list of two frequencies [OK]
Hint: Band-pass needs two cutoff frequencies as a list [OK]
Common Mistakes:
  • Using single cutoff frequency for band-pass
  • Misunderstanding fs parameter usage
  • Confusing band-pass with band-stop requirements
5. You have a noisy signal containing frequencies from 0 to 50Hz. You want to keep only the frequencies between 5Hz and 15Hz and remove the rest. Which filter type and cutoff frequencies should you use in SciPy's butter function to achieve this?
hard
A. Use a band-pass filter with cutoff frequencies [5, 15]
B. Use a band-stop filter with cutoff frequencies [5, 15]
C. Use a low-pass filter with cutoff frequency 15
D. Use a high-pass filter with cutoff frequency 5

Solution

  1. Step 1: Understand the goal

    The goal is to keep frequencies between 5Hz and 15Hz and remove others, which means passing only that band.
  2. 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.
  3. Final Answer:

    Use a band-pass filter with cutoff frequencies [5, 15] -> Option A
  4. Quick Check:

    Keep 5-15Hz = band-pass [OK]
Hint: Band-pass keeps frequencies inside cutoff range [OK]
Common Mistakes:
  • Choosing band-stop instead of band-pass
  • Using only low-pass or high-pass filters
  • Confusing cutoff frequency roles
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard