% Signal Design Problem
%
% The output of a certain communication channel is modeled by
%
%        y(n) = sum_{k=0}^n h(n-k) x(k),  n = 0, ... , m,
%
% where h is a known, causal, (m+1)-tap FIR filter, and  x  is a causal signal
% to be designed in such a way that the output y(n) is close to the
% desired signal y0(n) subject to an energy constraint on x.
%-----------------------------------------------------------------------
% Initialization and parameter setup
%
showhvec = 0;		% Flag to print h(n)
plothvec = 1;		% Flag to plot h(n)
showHmat = 0;		% Flag to print H matrix
ntaps = 35;		% Number of filter taps = m + 1
m = ntaps - 1;
h = zeros(1,ntaps);	% Initialize h vector
g = h;			% Eventually, we set g = h in reverse order
v = [0:m];		% Vector of time indices; needed for plot(v,h), etc.
%-----------------------------------------------------------------------
% Build impulse response vector h
%
fprintf('\nh has %g taps.\n\n',ntaps)
for n=0:m
   yy = n^3/2^(n/2)/60+.01;
   n1 = n + 1;
   ntapsn = ntaps - n;
   if showhvec
      fprintf( 'h(%2g) = %8.4f    g(%2g) = %8.4f\n', n1, yy, ntapsn, yy);
   end
   h(n1) = yy;
   g(ntapsn) = yy;		% Reverse order of h
end
if plothvec
   stem(v,h,'filled')
   xlabel('n')
   ylabel('h(n)')
end
%-----------------------------------------------------------------------
% Build corresponding linear operator matrix H
%
H = zeros(ntaps,ntaps);
for i = 1:ntaps			% Build H matrix
   H(i,1:i) = g(ntaps-i+1:ntaps);
end
if showhvec
  fprintf('\n')
end
fprintf('Matrix of linear operator stored in H\n\n')
if showHmat
   H
end
