How To design a Butterworth low-pass filter in MATLAB, follow these steps for both 1D signals and 2D images.
The Butterworth filter is characterized by a maximally flat frequency response in the passband and is widely used for noise reduction while preserving edges.
Design for 1D Signals (Time Series)
Steps:
- Define filter specifications: Order (
n
), cutoff frequency (Wn
), and sampling frequency (Fs
). - Design the filter: Use MATLAB’s
butter
function to compute filter coefficients. - Apply the filter: Use
filtfilt
for zero-phase distortion. - Visualize results: Plot the original/filtered signals and frequency response.
MATLAB Code:
% Example: Filter a noisy sine wave Fs = 1000; % Sampling frequency (Hz) t = 0:1/Fs:1; % Time vector f_signal = 10; % Signal frequency (Hz) f_noise = 200; % High-frequency noise (Hz) % Generate signal + noise x = sin(2*pi*f_signal*t) + 0.5*sin(2*pi*f_noise*t); % Step 1: Define filter parameters n = 4; % Filter order cutoff = 50; % Cutoff frequency (Hz) Wn = cutoff/(Fs/2); % Normalized cutoff (0 < Wn < 1) % Step 2: Design Butterworth filter [b, a] = butter(n, Wn, 'low'); % Step 3: Apply filter (zero-phase) y = filtfilt(b, a, x); % Step 4: Plot results figure; subplot(2,1,1); plot(t, x, 'b', t, y, 'r', 'LineWidth', 1.5); legend('Noisy Signal', 'Filtered Signal'); title('Time Domain'); xlabel('Time (s)'); % Frequency response [h, freq] = freqz(b, a, 1024, Fs); subplot(2,1,2); plot(freq, 20*log10(abs(h)), 'LineWidth', 1.5); title('Frequency Response'); xlabel('Frequency (Hz)'); ylabel('Gain (dB)'); xlim([0 Fs/2]); grid on;
Design for 2D Images
Steps:
- Create a frequency-domain mask using the Butterworth formula.
- Apply the filter in the frequency domain via FFT.
- Invert the FFT to get the filtered image.
MATLAB Code:
% Example: Remove high-frequency noise from an image img = im2double(imread('cameraman.tif')); [M, N] = size(img); % Add noise noisy_img = imnoise(img, 'salt & pepper', 0.05); % Step 1: Design Butterworth filter n = 4; % Filter order D0 = 30; % Cutoff frequency (pixels) % Create grid of frequencies [u, v] = meshgrid(-N/2:N/2-1, -M/2:M/2-1); D = sqrt(u.^2 + v.^2); % Distance from center H = 1 ./ (1 + (D./D0).^(2*n)); % Butterworth LPF formula % Step 2: Apply filter in frequency domain F = fftshift(fft2(noisy_img)); F_filtered = F .* H; filtered_img = real(ifft2(ifftshift(F_filtered))); % Step 3: Display results figure; subplot(1,3,1), imshow(img), title('Original'); subplot(1,3,2), imshow(noisy_img), title('Noisy Image'); subplot(1,3,3), imshow(filtered_img), title('Filtered Image');
Key Parameters:
- Order (
n
): Higher order = sharper roll-off (steeper transition between passband and stopband). - Cutoff (
Wn
orD0
): Frequency beyond which attenuation begins. - Sampling Frequency (
Fs
): Critical for normalizingWn
in 1D.
Notes:
- Zero-Phase Filtering: Use
filtfilt
instead offilter
to eliminate phase distortion in 1D. - Frequency Domain for Images: The 2D filter is applied via FFT for computational efficiency.
- Adjust Parameters: Tune
n
andD0
/Wn
based on your data’s noise characteristics. - Toolbox Requirement: Requires the Signal Processing Toolbox for
butter
andfiltfilt
.