0
0
MatlabHow-ToBeginner ยท 3 min read

How to Use Butter Filter in MATLAB for Signal Processing

In MATLAB, use the butter function to design a Butterworth filter by specifying the filter order and cutoff frequency. Then apply the filter to your data using filter or filtfilt for zero-phase filtering.
๐Ÿ“

Syntax

The basic syntax to create a Butterworth filter in MATLAB is:

  • [b,a] = butter(n, Wn) creates an n-th order lowpass filter with normalized cutoff frequency Wn.
  • n is the filter order (a positive integer).
  • Wn is the cutoff frequency normalized to the Nyquist frequency (half the sampling rate), so it ranges between 0 and 1.
  • b and a are the filter coefficients used to apply the filter.

You can specify filter type by adding a third argument: 'low', 'high', 'bandpass', or 'stop'.

matlab
[b,a] = butter(n, Wn, 'low');
๐Ÿ’ป

Example

This example creates a 4th order lowpass Butterworth filter with a cutoff frequency of 0.3 times the Nyquist frequency. It then filters a noisy sine wave signal and plots the original and filtered signals.

matlab
fs = 1000; % Sampling frequency in Hz
t = 0:1/fs:1-1/fs; % Time vector
x = sin(2*pi*50*t) + 0.5*randn(size(t)); % Signal with noise

n = 4; % Filter order
Wn = 0.3; % Normalized cutoff frequency
[b,a] = butter(n, Wn, 'low'); % Design Butterworth filter

y = filtfilt(b, a, x); % Apply zero-phase filter

plot(t, x, 'b', t, y, 'r', 'LineWidth', 1.5)
legend('Noisy Signal', 'Filtered Signal')
title('Butterworth Lowpass Filter Example')
xlabel('Time (seconds)')
ylabel('Amplitude')
โš ๏ธ

Common Pitfalls

  • Incorrect cutoff frequency: Remember that Wn must be normalized by the Nyquist frequency (half the sampling rate). Using raw frequency values will cause errors or unexpected results.
  • Filter order too high: Very high orders can cause instability or ringing effects. Start with low orders like 2 or 4.
  • Using filter instead of filtfilt: filter causes phase delay, which can distort signals. Use filtfilt for zero-phase filtering if phase preservation is important.
matlab
% Wrong: Using raw frequency instead of normalized
fs = 1000;
cutoff = 300; % Hz, should be normalized
[b,a] = butter(4, cutoff/(fs/2)); % Incorrect usage

% Right: Normalize cutoff frequency
Wn = cutoff/(fs/2);
[b,a] = butter(4, Wn); % Correct usage
๐Ÿ“Š

Quick Reference

Summary tips for using Butterworth filters in MATLAB:

  • Always normalize cutoff frequency by Nyquist frequency: Wn = cutoff/(fs/2).
  • Use butter to get filter coefficients [b,a].
  • Apply filter with filtfilt(b,a,x) for zero-phase filtering or filter(b,a,x) for causal filtering.
  • Choose filter order carefully; higher order means sharper cutoff but more complexity.
  • Specify filter type explicitly: 'low', 'high', 'bandpass', or 'stop'.
โœ…

Key Takeaways

Use butter(n, Wn, 'type') to design Butterworth filters with normalized cutoff frequency.
Normalize cutoff frequency by dividing by half the sampling rate (Nyquist frequency).
Apply filters with filtfilt for zero-phase distortion or filter for causal filtering.
Avoid very high filter orders to prevent instability and ringing.
Specify filter type explicitly to get the desired filter behavior.