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.