function [y,w]=weim(N,wt,x)
% WEIM - running weighted average of input data given a window
%
% USAGE:
% function [y,w]=weim(N,wt,x)
%
% DESCRIPTION:
% it computes a weighted average using the window function
% this filter is designed for odd weight numbers only
%
% INPUT:
% N is numbers of weights
% wt is the shape of the desired window (see window.m)
% x is the input data
%
% OUTPUT:
%  y: Smoothed data vector
%  w: The window used for smoothing (from window.m)
%
% SEE ALSO:
%  window.m
%

% 
% VERSION: 
%	$Revision: 1.4 $
%	$Date: 2008/02/18 19:47:44 $
%
% CHANGELOG:
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
w=window(N,wt);

r=size(x);
if r(1) > 1, x=x'; end

ln=(N-1)/2;
lx=length(x);
lf=lx-ln+1;
y=zeros(size(x));

for i=1:lx,

  if i <= ln,
  
   	y(i)=sum(x(1:ln+i).*w(ln+2-i:N))/sum(w(ln+2-i:N));
  
  elseif ((i > ln) & (i < lf)),
  
   	y(i)=sum(x(i-ln:i+ln).*w)/sum(w);

  else  % i >=lf 

   	y(i)=sum(x(i-ln:lx).*w(1:length(i-ln:lx)))/sum(w(1:length(i-ln:lx)));

  end
end
return


