Open In App

Filter Function in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

The filter function or 1-D digital filter is a function in MATLAB that is used to filter a given noisy data by removing the noise in the data and sharpening or smoothing the input function. As MATLAB provides a dedicated Signal Processing Toolset, the filter function comes handy to remove noise from initial data. Following are the syntaxes of the filter() function in MATLAB.

Syntax:

output_signal = filter(b, a, input_signal)

Here, the arguments of the filter function have the following meanings:

  • a and b are the denominator and numerator of the rational transfer function respectively, which does the actual filtering on the system end. (The former will not be discussed in this article as it is not in the scope of this article.)
  • input_signal is the data fed to the filter function. It could be an N-dimensional array of numeric data.

Now, we shall see the working and different forms of filter functions using various examples.

We will create a sinusoidal signal that takes has some random error in it and will then filter it using the filter function. 

Example 1:

Matlab




% MATLAB code for generating initial
% dummy signal with random errors
time = -pi:.1:pi;
rng default
input_signal = sin(time) + .23*rand(size(time));
 
% Defining the parameters b and a for
% the rational transfer function
b=(1/3)*ones(1,3);
a=1;
 
% Filtering the input signal
output = filter(b,a,input_signal);   
 
% Plotting graphs of filtered and raw data to
% analyze the working of filtered data
hold on
plot(time,input_signal)
plot(time,output)
hold off
legend("Initial data","Filtered data")


In the above code, we created a dummy sinusoidal wave and added some random errors to it using the rand() function. Then we defined the parameters for the rational transfer function. After that, we stored the filtered signal and plotted it against the raw data to analyze it.

Output:

 

In case of multidimensional data, we can specify the dimension along which we require the filter function to filter our data. Syntax would change to:

output_signal = filter(b, a, input_signal,[] , dimension)

Example 2:

Matlab




% MATLAB code for creating a random matrix
rng default
input_signal = rand(2,23);
 
% Defining parameters for rational transfer function
b=1;
a=[1 -.1];
 
% Filtering the data
output = filter(b,a,input_signal,[],2);   
 
% Plotting the data along the filtered dimension
hold on
plot(input_signal(1,:))
plot(output(1,:))
hold off
legend("Initial data","Filtered data")


Output:

In this code, we create a 2-by-23 matrix of random numbers in the range of (0,1). Then we filter it with the filter function along the second dimension and plot the same dimension. The output is:

 



Last Updated : 22 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads