1-D median filtering


  • y = medfilt1(x)
  • y = medfilt1(x,n)
  • y = medfilt1(___,nanflag,padding)
  • y = medfilt1(x,n,blksz,dim)
  • y = medfilt1(x,n,[],dim)


y = medfilt1(x) applies a third-order one-dimensional median filter to the input vector, x. The function considers the signal to be 0 beyond the endpoints. The output, y, has the same length as x.


y = medfilt1(x,n) applies an nth-order one-dimensional median filter to x.


y = medfilt1(___,nanflag,padding) specifies how NaN values are treated over each segment, using any input arguments from previous syntaxes. This syntax also specifies padding, the type of filtering performed at the signal edges.

nanflag and padding can appear anywhere after x in the function call.

y = medfilt1(x,n,blksz,dim) or y = medfilt1(x,n,[],dim) specifies the dimension, dim, along which the filter operates. blksz is required for backward compatibility and is ignored.


collapse all

Noise Suppression by Median Filtering

Generate a sinusoidal signal sampled for 1 second at 100 Hz. Add a higher-frequency sinusoid to simulate noise.

fs = 100;
t = 0:1/fs:1;
x = sin(2*pi*t*3)+0.25*sin(2*pi*t*40);

Use a 10th-order median filter to smooth the signal. Plot the result.

y = medfilt1(x,10);


Multichannel Signal with Spikes and Missing Samples

Generate a two-channel signal consisting of sinusoids of different frequencies. Place spikes in random places. Use NaNs to add missing samples at random. Reset the random number generator for reproducible results. Plot the signal.


n = 59;
x = sin(pi./[15 10]'*(1:n)+pi/3)';

spk = randi(2*n,9,1);
x(spk) = x(spk)*2;
x(randi(2*n,6,1)) = NaN;


Filter the signal using medfilt1 with the default settings. Plot the filtered signal. By default, the filter assigns NaN to the median of any segment with missing samples.

y = medfilt1(x);

Transpose the original signal. Filter it again, specifying that the function work along the rows. Exclude the missing samples when computing the medians. If you leave the second argument empty, then medfilt1 uses the default filter order of 3.

y = medfilt1(x',[],[],2,'omitnan');

The function cannot assign a value to the segment that contains only NaNs. Increase the segment length to fix this issue. The change also removes the outlier more thoroughly.

y = medfilt1(x,4,'omitnan');

The default zero padding results in the function's underestimating the signal values at the edges. Lessen this effect by using decreasing windows to compute the medians at the ends.

y = medfilt1(x,4,'omitnan','truncate');

Input Arguments

collapse all

x — Input signalvector | matrix | N-D array

Input signal, specified as a real-valued vector, matrix, or N-D array.

Data Types: single | double

n — Filter order3 (default) | positive integer scalar

Order of the one-dimensional median filter, specified as a positive integer scalar.

  • When n is odd, y(k) is the median of x(k-(n-1)/2:k+(n-1)/2).

  • When n is even, y(k) is the median of x(k-n/2:k+(n/2)-1). In this case, medfilt1 sorts the numbers and takes the average of the two middle elements of the sorted list.

Example: If n = 11, then y(k) is the median of x(k-5:k+5).

Example: If n = 12, then y(k) is the median of x(k-6:k+5).

Data Types: double

dim — Dimension to filter alongpositive integer scalar

Dimension to filter along, specified as a positive integer scalar. By default, medfilt1 operates along the first nonsingleton dimension of x. In particular, if x is a matrix, the function filters its columns so that y(:,i) = medfilt1(x(:,i),n).

Data Types: double

nanflagNaN condition'includenan' (default) | 'omitnan'

NaN condition, specified as 'includenan' or 'omitnan'.

  • 'includenan' — Returns the filtered signal so that the median of any segment containing NaNs is also NaN.

  • 'omitnan' — Returns the filtered signal so that the median of any segment containing NaNs is the median of the non-NaN values. If all elements of a segment are NaNs, the result is NaN.

Data Types: char

padding — Endpoint filtering'zeropad' (default) | 'truncate'

Endpoint filtering, specified as 'zeropad' or 'truncate'.

  • 'zeropad' — Considers the signal to be zero beyond the endpoints.

  • 'truncate' — Computes medians of smaller segments as it reaches the signal edges.

Data Types: char

Output Arguments

collapse all

y — Filtered signalvector | matrix | N-D array

Filtered signal, returned as a real-valued vector, matrix, or N-D array. y is the same size as x

Data Types: double

More About

expand all


If you have a license for Image Processing Toolbox™ software, you can use the medfilt2 function to perform two-dimensional median filtering.


[1] Pratt, William K. Digital Image Processing. 4th Ed. Hoboken, NJ: John Wiley & Sons, 2007.

See Also

| | |

Introduced before R2006a

Was this topic helpful?