In Matlab, filter()
and freqz()
are two different functions used for digital signal processing. The main differences between these two functions are as follows:
filter()
is a function used to filter a signal using a digital filter. It takes two inputs: the filter coefficients and the input signal, and returns the filtered output. The filter coefficients can be obtained using various filter design methods, such as FIR or IIR design. In contrast,freqz()
is a function used to visualize the frequency response of a filter. It takes the filter coefficients as input and returns the magnitude and phase response of the filter at various frequencies.filter()
is a time-domain function that operates on a signal sample-by-sample, whilefreqz()
is a frequency-domain function that analyzes the frequency response of a filter.filter()
is typically used for real-time processing of signals, whilefreqz()
is used for analyzing the characteristics of a filter, such as its cutoff frequency, passband ripple, stopband attenuation, and phase response.filter()
is used to obtain the filtered output of a signal, whilefreqz()
is used to obtain the frequency response of a filter.
Matlab Filter function example
This code below is an example of using filter() function that generates a noisy signal with a 100 Hz sine wave and random
noise, designs a 4th-order lowpass Butterworth filter with a cutoff
frequency of 150 Hz, applies the filter to the signal using the filter
function, and plots the original and filtered signals using the plot
function.
% Generate a noisy signal with a 100 Hz sine wave and random noise
fs = 1000; % Sampling rate
ts= 1/fs; % Sampling time
t = 0:ts:20*ts; % Time vector
f = 100; % Sine wave frequency
x = sin(2*pi*f*t);
s = x + 0.5*randn(size(t));
% Design a lowpass filter with cutoff frequency 150 Hz
fc = 150; % Cutoff frequency
[b,a] = butter(4,fc/(fs/2),'low');
% Apply the filter to the signal
y = filter(b,a,s);
% Plot the original and filtered signals
figure;
subplot(3,1,1);
plot(t,x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Signal');
subplot(3,1,2);
plot(t,xn);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original + Noise Signal');
subplot(3,1,3);
plot(t,y);
xlabel('Time (s)');
ylabel('Amplitude');
title('Filtered Signal');
The butter()
function uses normalized frequency. That is, the cutoff frequency of fc/(fs/2)=150/(1000/2)=0.3 is a normalized frequency that refers to the frequency relative to the sampling rate of a digital signal processing system.
This following is freqz() function example code that plots the frequency response of the filter used in the above filter() example code. That is the code designs a lowpass filter with a cutoff frequency of 150 Hz using the Butterworth filter design method.
% Generate a noisy signal with a 100 Hz sine wave and random noise
fs = 1000; % Sampling rate
ts= 1/fs; % Sampling time
t = 0:ts:20*ts; % Time vector
f = 100; % Sine wave frequency
x = sin(2*pi*f*t);
s = x + 0.5*randn(size(t));
% Design a lowpass filter with cutoff frequency 150 Hz
fc = 150; % Cutoff frequency
[b,a] = butter(4,fc/(fs/2),'low');
% Plot frequency response of filter
[h, w] = freqz(b,a,1024,fs);
f = w/(2*pi);
H = abs(h);
HdB = 20*log10(H);
phase = angle(h);
delay = -diff(unwrap(phase))./diff(w);
figure;
subplot(3,1,1);
plot(f, H);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Magnitude Response');
subplot(3,1,2);
plot(f, phase);
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
title('Phase Response');
subplot(3,1,3);
plot(f(1:end-1), delay);
xlabel('Frequency (Hz)');
ylabel('Delay (samples)');
title('Group Delay');
The freqz
function is then used to plot the frequency response of the filter. The output of freqz
is the complex frequency response of the filter, h
, and the frequencies at which the response is evaluated, w
. The frequency vector, f
, is then calculated from w
using the formula f = w/(2*pi)
.
The magnitude response, H
, of the filter is calculated as the absolute value of h
, and HdB
is calculated as the magnitude response in decibels. The phase response, phase
, is calculated as the angle of h
. The group delay, delay
, is calculated as the negative derivative of the unwrapped phase response with respect to frequency.
Finally, the frequency response of the filter is plotted in three subplots: the magnitude response, the phase response, and the group delay. The x-axis of each plot is the frequency in Hz.
The following shows the magnitude response, phase response and group delay of the filter.
Summary
In summary, filter()
is used for filtering signals using a digital filter, while freqz()
is used for analyzing the frequency response of a filter.