Filtering helps us remove unwanted parts from signals so we can focus on the important information.
Why filtering is essential in Signal Processing
filtered_signal = filter_function(original_signal, filter_parameters)
The filter_function depends on the type of filter you want (e.g., low-pass, high-pass).
filter_parameters define how the filter behaves, like cutoff frequency.
filtered_signal = low_pass_filter(signal, cutoff=1000)filtered_signal = high_pass_filter(signal, cutoff=500)filtered_signal = band_pass_filter(signal, low_cut=300, high_cut=3000)
This code creates a signal with low and high frequencies plus noise. Then it uses a low-pass filter to keep only the low frequencies and remove noise and high frequencies. The plot shows the difference.
import numpy as np import matplotlib.pyplot as plt from scipy.signal import butter, filtfilt # Create a noisy signal with two frequencies fs = 5000 # Sampling frequency T = 1.0 # seconds t = np.linspace(0, T, int(fs*T), endpoint=False) signal = np.sin(2*np.pi*50*t) + 0.5*np.sin(2*np.pi*1200*t) + 0.3*np.random.randn(len(t)) # Design a low-pass Butterworth filter to keep frequencies below 100 Hz def butter_lowpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='low', analog=False) return b, a b, a = butter_lowpass(100, fs) filtered_signal = filtfilt(b, a, signal) # Plot original and filtered signals plt.figure(figsize=(10, 6)) plt.plot(t, signal, label='Noisy signal') plt.plot(t, filtered_signal, label='Filtered signal', linewidth=2) plt.xlabel('Time [seconds]') plt.ylabel('Amplitude') plt.title('Low-pass Filtering Example') plt.legend() plt.show()
Filtering can change the shape of the signal, so choose filter settings carefully.
Some filters cause delay or distortion; zero-phase filters like filtfilt avoid this.
Always check the filtered result visually or with statistics to ensure it meets your needs.
Filtering removes unwanted parts of signals to focus on important data.
It is used to clean noise, isolate frequencies, and prepare data for analysis.
Choosing the right filter type and settings is key for good results.
Practice
Solution
Step 1: Understand the purpose of filtering
Filtering is used to remove noise or unwanted parts from a signal to make the important information clearer.Step 2: Evaluate the options
Only It removes unwanted noise to clarify the signal. correctly describes filtering as removing noise to clarify the signal. Other options describe incorrect or unrelated actions.Final Answer:
It removes unwanted noise to clarify the signal. -> Option CQuick Check:
Filtering cleans signals = A [OK]
- Thinking filtering adds data instead of removing noise
- Confusing filtering with signal format conversion
- Believing filtering increases frequency beyond limits
Solution
Step 1: Recall SciPy filtering functions
SciPy useslfilter(b, a, signal)to apply filters, where b and a are filter coefficients.Step 2: Check each option
filtered_signal = scipy.signal.lfilter(b, a, signal) uses correct functionlfilter. Options A, B, and D use incorrect or non-existent functions.Final Answer:
filtered_signal = scipy.signal.lfilter(b, a, signal) -> Option AQuick Check:
Use lfilter for filtering in SciPy = C [OK]
- Using non-existent functions like lowpass or highpass directly
- Confusing filter function names
- Omitting filter coefficients b and a
import numpy as np
from scipy.signal import butter, lfilter
def butter_lowpass(cutoff, fs, order=5):
nyq = 0.5 * fs
normal_cutoff = cutoff / nyq
b, a = butter(order, normal_cutoff, btype='low', analog=False)
return b, a
fs = 500
cutoff = 100
b, a = butter_lowpass(cutoff, fs)
signal = np.array([1, 2, 3, 4, 5])
filtered = lfilter(b, a, signal)
print(np.round(filtered, 2))Solution
Step 1: Understand the code filtering process
The code creates a low-pass Butterworth filter with cutoff 100 Hz and sampling rate 500 Hz, then applies it to the signal array.Step 2: Predict the filtered output
The low-pass filter smooths the signal, so output values gradually increase but are less than original at start. [1. 1.81 2.62 3.43 4.24] matches expected smoothed values rounded to two decimals.Final Answer:
[1. 1.81 2.62 3.43 4.24] -> Option BQuick Check:
Low-pass filter smooths signal = A [OK]
- Expecting output same as input without filtering
- Confusing low-pass with high-pass filter output
- Ignoring rounding in output
from scipy.signal import butter, lfilter
def highpass_filter(signal, cutoff, fs):
b, a = butter(cutoff, fs, btype='high')
return lfilter(b, a, signal)
signal = [1, 2, 3, 4, 5]
filtered = highpass_filter(signal, 100, 500)
print(filtered)Solution
Step 1: Check butter function parameters
The butter function expects order first, then normalized cutoff frequency. Here, cutoff and fs are passed incorrectly as first two arguments.Step 2: Identify correct parameter order
The correct call is butter(order, normalized_cutoff, btype='high'). The code passes cutoff and fs instead of order and normalized cutoff, and order parameter is missing.Final Answer:
The order parameter is missing in butter function. -> Option DQuick Check:
Butter function requires order parameter first = A [OK]
- Passing cutoff frequency before order
- Not normalizing cutoff frequency
- Assuming list input causes error
Solution
Step 1: Choose filter type and cutoff
To keep frequencies below 40 Hz, use a low-pass filter with cutoff at 40 Hz.Step 2: Normalize cutoff frequency and apply filter
Normalize cutoff by Nyquist frequency (half sampling rate = 500 Hz). Then apply filter to the signal.Final Answer:
Design a low-pass filter with cutoff 40 Hz, normalize cutoff by Nyquist frequency (500 Hz), apply filter to signal. -> Option AQuick Check:
Low-pass filter with normalized cutoff = D [OK]
- Using high-pass instead of low-pass filter
- Not normalizing cutoff frequency
- Ignoring filter type needed for frequency range
Ready to prove it?
Pass the test and this pattern moves unknown
