This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

dsp.MedianFilter System object

Median filter


The dsp.MedianFilter System object™ computes the moving median of the input signal along each channel, independently over time. The object uses the sliding window method to compute the moving median. In this method, a window of specified length is moved over each channel, sample by sample, and the object computes the median of the data in the window. For more details, see Algorithms.

The object accepts multichannel inputs, that is, m-by-n size inputs, where m ≥ 1, and n > 1. m is the number of samples in each frame (or channel), and n is the number of channels. The object also accepts variable-size inputs. Once the object is locked, you can change the size of each input channel. However, the number of channels cannot change. This object supports C and C++ code generation.

To compute the moving median of the input:

  1. Create a dsp.MedianFilter object and set the properties of the object.

  2. Call step to compute the moving median.


Alternatively, instead of using the step method to perform the operation defined by the System object, you can call the object with arguments, as if it were a function. For example, y = step(obj,x) and y = obj(x) perform equivalent operations.


medFilt = dsp.MedianFilter returns a median filter object, medFilt, using the default properties.

medFilt = dsp.MedianFilter(Len) sets the WindowLength property to Len.

medFilt = dsp.MedianFilter(Name,Value) specifies properties using Name,Value pairs. Unspecified properties have default values.


movMin = dsp.MedianFilter('WindowLength',5);


expand all

Length of the sliding window, specified as a positive scalar integer.


resetReset internal states of System object
stepMoving median of input signal
Common to All System Objects

Create System object with same property values


Expected number of inputs to a System object


Expected number of outputs of a System object


Check locked states of a System object (logical)


Allow System object property value changes


expand all

Filter high-frequency noise from a noisy sine wave signal using a median filter. Compare the performance of the median filter with an averaging filter.


Set up a dsp.MedianFilter object, medFilt, and a dsp.MovingAverage object, movavgWin. These objects use the sliding window method with a window length of 7. Create a time scope for viewing the output.

Fs = 1000;
medFilt = dsp.MedianFilter(7);
movavgWin = dsp.MovingAverage(7);
scope  = dsp.TimeScope('SampleRate',Fs,...
    'YLimits',[-3 3],...
    'LayoutDimensions',[3 1],...
scope.ActiveDisplay = 1;
scope.Title = 'Signal + Noise';
scope.ActiveDisplay = 2;
scope.Title = 'Moving Average Output (Window Length = 7)';
scope.ActiveDisplay = 3;
scope.Title = 'Median Filter Output (Window Length = 7)';

FrameLength = 256;
count = 1;
sine = dsp.SineWave('SampleRate',Fs,'Frequency',10,...

Filter the Noisy Sine Wave

Generate a noisy sine wave signal with a frequency of 10 Hz. Apply the median filter and the moving average object to the signal. View the output on the time scope.

for i = 1:500
    hfn = 3 * (rand(FrameLength,1) < 0.02);
    x = sine() + 1e-2 * randn(FrameLength,1) + hfn;
    y1 = movavgWin(x);
    y2 = medFilt(x);

The median filter removes the high-frequency noise more effectively than the moving average object does.

This example shows how to remove the high-frequency outliers from a streaming signal using the dsp.MedianFilter System object™.

Use the dsp.MatFileReader System object to read the gyroscope MAT file. The gyroscope MAT file contains 3 columns of data, with each column containing 7140 samples. The three columns represent the X-axis, Y-axis, and Z-axis data from the gyroscope motion sensor. Choose a frame size of 714 samples so that each column of the data contains 10 frames. The dsp.MedianFilter System object uses a window length of 10. Create a dsp.TimeScope object to view the filtered output.

reader = dsp.MatFileReader('SamplesPerFrame',714,'Filename','LSM9DS1gyroData73.mat',...
medFilt = dsp.MedianFilter(10);
scope = dsp.TimeScope('NumInputPorts',1,'SampleRate',119,'YLimits',[-300 300],...
    'ChannelNames',{'Input','Filtered Output'},'TimeSpan',60,'ShowLegend',true);

Filter the gyroscope data using the dsp.MedianFilter System object. View the filtered Z-axis data in the time scope.

for i = 1:10
    gyroData = reader();
    filteredData = medFilt(gyroData);

The original data contains several outliers. Zoom in on the data to confirm that the median filter removes all the outliers.


expand all


[1] Bodenham, Dean. “Adaptive Filtering and Change Detection for Streaming Data.” PH.D. Thesis. Imperial College, London, 2012.

Extended Capabilities

Introduced in R2016b

Was this topic helpful?