function vo_t = dig_filter(vi_t)
% vo_t = dig_filter(vi_t)    Lowpass filter of input signal vi_t
%
% Input variable
% vi_t = sampled waveform  (col vector)
%
% Output variable
% vo_t = sampled waveform (lowpassed version of input signal, col vector)

% This function implements a simple lowpass filter.
% The digital filter takes values from a sampled waveform and moves them
%  along a conveyor belt.  As samples move through the digital filter,
%  three adjacent sample values are multiplied by weights: 1, 0.5, and
%  0.25.  The weighted values are summed to produce one filter output
%  value.  The conveyor belt then moves forward one position, and another output 
%  value is calculated.  (Two of the samples in this calculation are the same as
%  for the previous output value.)
%
% The diagram below illustrates the digital filter.  The input signla is
% vi_t(1), vi_t(2), vi_t(3), ...  and the output signal is
% vo_t(1), vo_t(2), vo_t(3), ...
% 
%    vi_t(5)  vi_t(4)  vi_t(3)  vi_t(3)  vi_t(2)  vi_t(1)
%    >-.--------.--------.--------|--------|--------|------------>
%    > "conveyor belt" >          |        |        |
%                                 x 0.25   x 0.5    x 1
%                                 |        |        |
%                                 ---------+--------+--->  vo_t(1)
%
% vo_t(1) = vi_t(1)*1 + vi_t(2)*0.5 + vi_t(3)*0.25
%
% For the next sample, we have the following picture:
%    vi_t(6)  vi_t(5)  vi_t(4)  vi_t(3)  vi_t(3)  vi_t(2)  vi_t(1)
%    >-.--------.--------.--------|--------|--------|------------>
%    > "conveyor belt" >          |        |        |
%                                 x 0.25   x 0.5    x 1
%                                 |        |        |
%                                 ---------+--------+--->  vo_t(2)
%
% vo_t(2) = vi_t(2)*1 + vi_t(3)*0.5 + vi_t(4)*0.25
%
% The process continues until the last sample in the input signal has moved
%  through the filter.

% Define the filter weights.
w = [1, 0.5, 0.25];   % Defined as a horizontal vector.

% For reference, we store the length of the original input signal waveform.
N = length(vi_t);



% We first resolve the difficulty that we require two extra 0's on the end of
%  our input signal in order to have three samples in the digital filter at
%  the end of the process.  (We need to have three samples in the filter at
%  every step in order to avoid an "index  exceeds matrix dimensions" error.
vi_t = [vi_t; 0; 0];

% Use "for" loop to process conveyer belt of input signals.
for n = 1:N
  % Extract the three sample values from vi_t that are in the digital filter
  %  at the nth step.
  vi_in_filter = vi_t(n:n+2);   % Note that it's n:n+2 rather than n:n+3.

  % w is horizontal, and vi_in_filter is vertical.  The following multiples and
  %  sums.
  vo_t(n) = w * vi_in_filter;
end

end   % This end terminates the function.
