function y=med(x,n);

% Function MED: Vector smoothing using n-point median filtering.
%
% Usage: y=med(x,n);
% Input parameters:
%   x     - input sequence
%   n     - order of median filtering (!! 'n' should be ODD number!!)
% Output parameters:
%   y     - averaged output sequence

%                         Made by Petr Pollak
%                             13 May 1994   
%                            CTU FEL Prague


len=length(x);


for i=1:floor(n/2),
  frame=x(1:i-floor(n/2)+n-1);
  frame=sort(frame);
  y(i)=frame(floor(length(frame)/2)+1);
end;

for i=1+floor(n/2):len-floor(n/2),
  frame=x(i-floor(n/2):i-floor(n/2)+n-1);
  frame=sort(frame);
  y(i)=frame(floor(length(frame)/2)+1);
end;

for i=1+len-floor(n/2):len,
  frame=x(i-floor(n/2):len);
  frame=sort(frame);
  y(i)=frame(floor(length(frame)/2)+1);
end;



