Explaination of Appcoef
function a = appcoef(c,l,varargin)
%APPCOEF Extract 1-D approximation coefficients.
% APPCOEF computes the approximation coefficients of a
% one-dimensional signal.
%
% A = APPCOEF(C,L,'wname',N) computes the approximation
% coefficients at level N using the wavelet decomposition
% structure [C,L] (see WAVEDEC).
% 'wname' is a string containing the wavelet name.
% Level N must be an integer such that 0 <= N <= length(L)-2.
%
% A = APPCOEF(C,L,'wname') extracts the approximation
% coefficients at the last level length(L)-2.
%
% Instead of giving the wavelet name, you can give the filters.
% For A = APPCOEF(C,L,Lo_R,Hi_R) or
% A = APPCOEF(C,L,Lo_R,Hi_R,N),
% Lo_R is the reconstruction low-pass filter and
% Hi_R is the reconstruction high-pass filter.
%
% See also DETCOEF, WAVEDEC.
% M. Misiti, Y. Misiti, G. Oppenheim, J.M. Poggi 12-Mar-96.
% Last Revision: 06-Feb-2011.
% Copyright 1995-2015 The MathWorks, Inc.
% explainer Damon Yan 2017/11/15(there are also some problems that the writer can not understand clearly,such as the iteration to compute the approximation of signals,so welcome for the advices and discussion)
% Check arguments.
% check the number of variable,if it isn’t between 2 to 5,then the function returns false 0.
narginchk(2,5)
% rmax is the length of l
rmax = length(l);
% nmax is the maximum level of decomposition
nmax = rmax-2;
if ischar(varargin{1}) %the first input variable is a char?
% True! { I.e:’db’,then [LO_D,HI_D,LO_R,HI_R] = WFILTERS('wname') }
% false! {Lo_R = varargin{1}; Hi_R = varargin{2};}
[Lo_R,Hi_R] = wfilters(varargin{1},'r'); next = 2;
else
Lo_R = varargin{1}; Hi_R = varargin{2}; next = 3;
end
% if the decomposition level N is defined, then
% execute this number; if not, then the signal is
% decomposed to the maximum level.
if nargin>=(2+next) , n = varargin{next}; else n = nmax; end
% the decomposition level should be N integer between o and nnmax.
if (n < 0) || (n > nmax) || (n ~= fix(n))
error(message('Wavelet:FunctionArgVal:Invalid_LevVal'));
end
% Initialization(aN).
a = c(1:l(1));
% Iterated reconstruction.
imax = rmax+1;
for p = nmax:-1:n+1
d = detcoef(c,l,p); % extract detail
a = idwt(a,d,Lo_R,Hi_R,l(imax-p));
end