function x = r8vec_swtb ( n, s, d )

%*****************************************************************************80
%
%% R8VEC_SWTB computes a "slow" backward wavelet transform of an R8VEC.
%
%  Discussion:
%
%    This function inverts the D4 Daubechies wavelet.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    26 June 2015
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the number of data values.
%
%    Input, real S((N+1)/2), D((N+1)/2), the transformed data.
%
%    Output, real X(N), the original data sequence.
%

%
%  Destroy all row vectors!
%
  s = s(:);
  d = d(:);

  if ( mod ( n, 2 ) == 1 )
    n2 = n + 1;
  else
    n2 = n;
  end

  nh = floor ( n / 2 );
  np1h = floor ( ( n + 1 ) / 2 );

  d = d / ( ( sqrt ( 3.0 ) + 1.0 ) / sqrt ( 2.0 ) );

  s = s / ( ( sqrt ( 3.0 ) - 1.0 ) / sqrt ( 2.0 ) );

  s = s + r8vec_permute_cyclic ( n2 / 2, +1, d );

  y = zeros ( n2, 1 );

  y(2:2:n2) = d ...
    + sqrt ( 3.0 ) / 4.0 * s ...
    + ( sqrt ( 3.0 ) - 2.0 ) / 4.0 * r8vec_permute_cyclic ( n2 / 2, -1, s );

  y(1:2:n2-1) = s(1:nh) - sqrt ( 3.0 ) * y(2:2:n2);
 
  x(1:n) = y(1:n);

  return
end
