Filter Function in MATLAB
Last Updated :
28 Apr, 2025
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:
Similar Reads
Find() function in MATLAB The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t
3 min read
User defined function in MATLAB Functions let you do a specific task. User defined functions are the functions created by the users according to their needs. This article explains how the user defined function in MATLAB is created. Syntax : function [a1,...,an] = func(x1,...,xm) func is the function name a1,...,an are outputs x1,.
2 min read
Creating Function in Files in MATLAB MATLAB is a high-performance language that is used for matrix manipulation, performing technical computations, graph plottings, etc. It stands for Matrix Laboratory. Functions:The function is a set of statements or commands, which take input/s as parameters and return output. We write functions to
2 min read
PHP | Imagick filter() Function The Imagick::filter() function is an inbuilt function in PHP which is used to apply custom convolution kernel to the image. A kernel, convolution matrix, or mask is a small matrix. It is used for blur, sharpen, embossing, edge detection, and many more. Syntax: bool Imagick::filter( $ImagickKernel, $
1 min read
Alternatives To Eval Function in MATLAB The eval function is one of MATLAB's most powerful and flexible commands. Eval is an acronym for evaluating, which is exactly what it does: it evaluates MATLAB expressions. Any command that can be executed from the MATLAB prompt may be executed from a Mfile using eval. Use of Eval Function:The Eval
3 min read
How to create a function in MATLAB ? A function is a block of statements that intend to perform a specific task. Functions allow the users to reuse the code frequently. MATLAB has several predefined functions which are ready to use such as sin(), fact(), cos() etc. MATLAB also allows the users to define their own functions. Syntax: fun
2 min read