1-D median filtering


  • y = medfilt1(x,n) example
  • y = medfilt1(x,n,blksz)
  • y = medfilt1(x,n,blksz,dim)



y = medfilt1(x,n) applies an order-n one-dimensional median filter to the input vector, x. The default value of n is 3. The function considers the signal to be 0 beyond the endpoints. The output, y, has the same length as x.

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

For even n, y(k) is the median of x(k-n/2), x(k-(n/2)+1), ..., x(k+(n/2)-1). In this case, medfilt1 sorts the numbers, then takes the average of the n/2 and (n/2)+1 elements.

y = medfilt1(x,n,blksz) computes blksz (block size) output samples at a time. By default, blksz = length(x).

y = medfilt1(x,n,blksz,dim) specifies the dimension, dim, along which the filter operates.

By default, medfilt1 operates along the first nonsingleton dimension of x. In particular, if x is a matrix, the function median filters its columns: y(:,i) = medfilt1(x(:,i),n,blksz).

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: double

n — Filter order3 (default) | positive integer scalar

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

Data Types: double

blksz — Block sizepositive integer scalar

Number of output samples computed at a time, specified as a positive integer scalar. The default is length(x) for vectors and size(x,dim) for arrays of higher dimension.

Data Types: double

dim — Dimension to filter alongpositive integer scalar

Dimension to filter along, specified as a positive integer scalar. If no value is specified, the default is the first nonsingleton dimension.

Data Types: double

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


collapse all

Noise Suppression via Median Filtering

Generate a sinusoidal signal sampled for 1 second at 100 Hz. Add a higher-frequency sinusoid to simulate noise. Use a 10th-order median filter to smooth the signal. Plot the result.

fs = 100;
t = 0:1/fs:1;
x = sin(2*pi*t*3)+0.25*sin(2*pi*t*40);
y = medfilt1(x,10);
plot(t,x,t,y),grid on
xlabel 'Time (s)',ylabel Signal
legend boxoff

More About

collapse all


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


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

Was this topic helpful?