function bvec = bvec_next_grlex ( n, bvec )

%*****************************************************************************80
%
%% BVEC_NEXT_GRLEX generates the next binary vector in GRLEX order.
%
%  Discussion:
%
%    N = 3
%
%    Input      Output
%    -----      ------
%    0 0 0  =>  0 0 1
%    0 0 1  =>  0 1 0
%    0 1 0  =>  1 0 0
%    1 0 0  =>  0 1 1
%    0 1 1  =>  1 0 1
%    1 0 1  =>  1 1 0
%    1 1 0  =>  1 1 1
%    1 1 1  =>  0 0 0
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    13 March 2015
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the dimension.
%
%    Input, integer BVEC(N), the binary vector whose successor is desired.
%
%    Output, integer BVEC(N), the successor to the input vector.
%

%
%  Initialize locations of 0 and 1.
%
  if ( bvec(1) == 0 )
    z = 1;
    o = 0;
  else
    z = 0;
    o = 1;
  end
%
%  Moving from right to left, search for a "1", preceded by a "0".
%
  for i = n : -1 : 2
    if ( bvec(i) == 1 )
      o = i;
      if ( bvec(i-1) == 0 )
        z = i - 1;
        break
      end
    end
  end
%
%  BVEC = 0
%
  if ( o == 0 )
    bvec(n) = 1;
%
%  01 never occurs.  So for sure, B(1) = 1.
%
  elseif ( z == 0 )
    s = sum ( bvec(1:n) );
    if ( s == n )
      bvec(1:n) = 0;
    else
      bvec(1:n-s-1) = 0;
      bvec(n-s:n) = 1;
    end
%
%  Found the rightmost "01" string.
%  Replace it by "10", and shift following 1's to end.
%
  else
    bvec(z) = 1;
    bvec(o) = 0;
    s = sum ( bvec(o+1:n) );
    bvec(o+1:n-s) = 0;
    bvec(n+1-s:n) = 1;
  end

  return
end


function [K, R, t] = decomposeP(P)
%VGG_KR_FROM_P Extract K, R from camera matrix.
%
%    [K,R,t] = VGG_KR_FROM_P(P [,noscale]) finds K, R, t such that P = K*R*[eye(3) -t].
%    It is det(R)==1.
%    K is scaled so that K(3,3)==1 and K(1,1)>0. Optional parameter noscale prevents this.
%
%    Works also generally for any P of size N-by-(N+1).
%    Works also for P of size N-by-N, then t is not computed.

% Author: Andrew Fitzgibbon <awf@robots.ox.ac.uk>
% Modified by werner.
% Date: 15 May 98

N = size(P,1);
H = P(:,1:N);

[K,R] = rq(H);

K = K*sign(K(1));
if prod(sign(diag(K))) < 0
  D = diag(-1*sign(diag(K)));
else
  D = diag(sign(diag(K)));
end
K = K*D;
R = D*R;

K = K/K(end);

% $$$ if nargin < 2
% $$$   K = K / K(N,N);
% $$$   if K(1,1) < 0
% $$$     D = diag([-1 -1 ones(1,N-2)]);
% $$$     K = K * D;
% $$$     R = D * R;
% $$$
% $$$   %  test = K*R;
% $$$   %  vgg_assert0(test/test(1,1) - H/H(1,1), 1e-07)
% $$$   end
% $$$ end

if nargout > 2
  t = -P(:,1:N)\P(:,end);
end

return


function Anew = h_trid(A)
%  H_TRID(A) uses Householder method to form a tridiagonal matrix from A.
%  Must have a SQUARE SYMMETRIC matrix as the input.
%
%
%  Example:
%
%             B=[0 1 1;1 2 1;1 1 1];
%             h_trid(B)
%
%
% Author:  Matt Fig
% Contact: popkenai@yahoo.com

[M N] = size(A);
if M~=N || ~isequal(A,A')  %This just screens matricies that can't work.
   error('Matrix must be square symmetric only, see help.');
end

lngth = length(A);  % Preallocations.
v = zeros(lngth,1);
I = eye(lngth);
Aold = A;

for jj=1:lngth-2  % Build each vector j and run the whole procedure.
    v(1:jj) = 0;
    S = ss(Aold,jj);
    v(jj+1) = sqrt(.5*(1+abs(Aold(jj+1,jj))/(S+2*eps)));
    v(jj+2:lngth) = Aold(jj+2:lngth,jj)*sign(Aold(jj+1,jj))...
                   /(2*v(jj+1)*S+2*eps);
    P = I-2*v*v';
    Anew = P*Aold*P;
    Aold = Anew;
end

Anew(abs(Anew(:))<5e-14)=0; % Tolerence.

function anss = ss(A,jj)
% Subfunction for h_trid.
anss = sqrt(sum(A(jj+1:end,jj).^2));


function J=SimFisherMatrix(obj,Prot,x,variables,sigma)
% Alexander, D.C., 2008. A general framework for experiment design in diffusion MRI and its application in measuring direct tissue-microstructure features. Magn. Reson. Med. 60, 439?448.
obj.Prot.(obj.MRIinputs{1}).Mat = Prot;
if ~exist('variables','var'), variables=1:5; end
if ~exist('sigma','var'), sigma=0.1; end

J=zeros(max(variables));
if ismethod(obj,'equation_x')
    S_nominal = obj.equation_x(x);
else
    S_nominal = obj.equation(x);
end
% Gaussian Noise
for i=variables
    for j=variables
        Xi = x;
        Xistep = max(1e-10,Xi(i) / 100);
        Xi(i) = Xi(i) + Xistep;
        Xj = x;
        Xjstep = max(1e-10,Xj(j) / 100);
        Xj(j) = Xj(j) + Xjstep;
        if ismethod(obj,'equation_x')
            J(i,j) = sum( 1./sigma.^2 .* (S_nominal - obj.equation_x(Xi))/Xistep .* (S_nominal - obj.equation_x(Xj))/Xjstep);
        else
            J(i,j) = sum( 1./sigma.^2 .* (S_nominal - obj.equation(Xi))/Xistep .* (S_nominal - obj.equation(Xj))/Xjstep);
        end
    end
end
J=J(variables,variables);

% % Rician Noise
% for i=1:variables
%     for j=variables
%         Xi=x;
%         Xi(i)=Xi(i)+Xi(i)/100;
%         Xj=x;
%         Xj(j)=Xj(j)+Xj(j)/100;
%
%         A = scd_model_GPD_composite(x,Ax);
%
%         Z=zeros(size(scheme,1),1);
%         for k=1:size(scheme,1)
%             Z(k) = integral(@(a) A(k)^2*besseli(1,A(k)*a/sigma^2)^2*besseli(0,A(k)*a/sigma^2)^-2  *a/sigma^2.*besseli(0,A(k)*a/sigma^2).*exp(-(A(k)^2+a^2)/2*sigma^2)  ,0,inf);
%         end
%
%         dAi = (scd_model_GPD_composite(x,Ax)-scd_model_GPD_composite(Xi,Ax))/(x(i)/100);
%         dAj = (scd_model_GPD_composite(x,Ax)-scd_model_GPD_composite(Xj,Ax))/(x(j)/100);
%
%         J(i,j)= sum(1/sigma^4.*(dAi.*dAj).*(Z-A.^2));
%     end
% end
% J=J(variables,variables);
% if J<0, error('bug'); end

end


% Demo for DAVB Van Der Pol oscillator.
% This demo inverts a model of nonlinear Van Dr Pol oscillator, which is
% observed through a nonlinear sigmoid function.

clear variables
close all

% Choose basic settings for simulations
n_t = 2e2;
deltat = 1e-1;
f_fname = @f_vanDerPol;
g_fname = @g_sigmoid;
alpha   = 1e2;
sigma   = 1e1;
theta   = [1e0];
phi     = [];
u       = [];

% Build options structure for temporal integration of SDE
inG.scale = 50;
inG.slope = 5;
inF.deltat = deltat;
options.inF     = inF;
options.inG     = inG;
options.backwardLag = 5;

% Build priors for model inversion
priors.muX0 = 1e-1*ones(2,1);
priors.SigmaX0 = 1e0*eye(2);
priors.muTheta = 0.*ones(1,1);
priors.SigmaTheta = 1e0*eye(1);
priors.a_alpha = 1e0;
priors.b_alpha = 1e0;
priors.a_sigma = 1e0;
priors.b_sigma = 1e0;

% Build options and dim structures for model inversion
options.priors      = priors;
dim.n_theta         = 1;
dim.n_phi           = 0;
dim.n               = 2;

% Build time series of hidden states and observations
[y,x,x0,eta,e] = VBA_simulate (n_t,f_fname,g_fname,theta,phi,u,alpha,sigma,options);

% display time series of hidden states and observations
displaySimulations(y,x,eta,e);
% disp('--paused--')
% pause

% [posterior,out] = VBA_onlineWrapper(y,u,f_fname,g_fname,dim,options);

% Call inversion routine
[posterior,out] = VBA_NLStateSpaceModel(y,u,f_fname,g_fname,dim,options);

% Display results
displayResults(posterior,out,y,x,x0,theta,phi,alpha,sigma);

% Make predictions
try
    options = out.options;
    [xs,ys,xhat,vx,yhat,vy] = VBA_comparePredictions(...
        n_t,theta,phi,u,alpha,sigma,options,posterior,dim);
catch
    disp('------!!Unable to form predictions!!------')
end


function [m] = nm2m(nm)
% Convert length from nanometers to meters.
% Chad A. Greene 2012
m = nm*1e-9;


clear all, close all, clc
n = 1000;
X = zeros(n,n);
X(n/4:3*n/4,n/4:3*n/4) = 1;
[U,S,V] = svd(X);
imagesc(X), hold on;
semilogy(diag(S),'-o','color',cm(1,:)), hold on, grid on

nAngles = 12;  % sweep through 12 angles, from 0:4:44
Xrot = X;
for j=2:nAngles
    Y = imrotate(X,(j-1)*4,'bicubic'); % rotate (j-1)*4
    startind = floor((size(Y,1)-n)/2);
    Xrot1 = Y(startind:startind+n-1, startind:startind+n-1);
    Xrot2 = Xrot1 - Xrot1(1,1);
    Xrot2 = Xrot2/max(Xrot2(:));
    Xrot(Xrot2>.5) = j;

    [U,S,V] = svd(Xrot1);
    subplot(1,2,1), imagesc(Xrot), colormap([0 0 0; cm])
    subplot(1,2,2), semilogy(diag(S),'-o','color',cm(j,:))
end


function [mi2] = m22mi2(m2)
% Convert area from square meters to square miles.
% Chad A. Greene 2012
mi2 = m2*3.861021585425E-7;


function [dataX, dataY] = pix2data( ax, pixelX, pixelY )
%PIX2DATA Translate a Figure Pixel position into an Axes data point
%  If no Axes given, it defaults to GCA.
%  [X, Y] = PIX2DATA( AXES, x, y )
%  [X, Y] = PIX2DATA( AXES, [x y] )
%  [X, Y] = PIX2DATA( x, y )
%  [X, y] = PIX2DATA( [x y] )
%
%  For scalar or vector X and Y, output will have X and Y in columns
%  POINT  = PIX2DATA( AXES, x, y )
%  POINT  = PIX2DATA( AXES, [x y] )
%  POINT  = PIX2DATA( x, y )
%  POINT  = PIX2DATA( [x y] )
%
%  See Also: DATA2PIX

% Chuck Packard, The Mathworks, Inc.
% 25 March 95

if nargin == 0
   error( 'Need AXES and X,Y position.' )
end

%See if there is a GCA, don't create one
if isempty( get(0,'children') )
   error( 'No Figure windows.' );
end
if isempty( get( gcf, 'children' ) )
   error( 'No Axes in current Figure.' );
end

if nargin == 1
   if size( ax, 2 ) ~= 2
      error( 'When defaulting to GCA, first argument should be [X Y].' )
   end
   pixelY = ax(:,2);
   pixelX = ax(:,1);
   ax = gca;

elseif nargin == 2
   if length(ax) == 1 & size(pixelX,2) == 2
      pixelY = pixelX(:,2);
      pixelX(:,2) = [];
   else
      pixelY = pixelX;
      pixelX = ax;
      ax = gca;
   end
end

%Check data, make X and Y column vectors
xSize = size(pixelX);
ySize = size(pixelY);
if any( ySize ~= xSize )
   error( 'Size of X and Y data not consistent.' );
end

%Get position of Axes in pixels
origAxesUnits = get(ax,'units');
set( ax, 'units', 'pixels' )
axPos = fix(get(ax,'position'));
set( ax, 'units', origAxesUnits )

%Find extent of Axes in data
xlimits = get(ax,'xlim');
ylimits = get(ax,'ylim');
dataWidth = xlimits(2) - xlimits(1);
dataHeight = ylimits(2) - ylimits(1);

%Find normalized position of points and determine pixel position
% as offset from lower left of Axes.
X = xlimits(1) + (((pixelX - axPos(1)) ./ axPos(3)) .* dataWidth);
Y = ylimits(1) + (((pixelY - axPos(2)) ./ axPos(4)) .* dataHeight);

if nargout < 2
   if min(ySize) ~= 1
      %What would it mean to concatenate two matrices for this function?
      dataX = X;
   elseif ySize(1) == 1 & ySize(2) > 1
      %Need to transpose data so it looks nice in column format
      dataX = [X' Y'];
   else
      dataX = [X Y];
   end
else
   dataX = X;
   dataY = Y;
end


%% Copyright (C) 2016 Lagu
%% Copyright (C) 2017-2019 Colin B. Macdonald
%%
%% This file is part of OctSymPy.
%%
%% OctSymPy is free software; you can redistribute it and/or modify
%% it under the terms of the GNU General Public License as published
%% by the Free Software Foundation; either version 3 of the License,
%% or (at your option) any later version.
%%
%% This software is distributed in the hope that it will be useful,
%% but WITHOUT ANY WARRANTY; without even the implied warranty
%% of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
%% the GNU General Public License for more details.
%%
%% You should have received a copy of the GNU General Public
%% License along with this software; see the file COPYING.
%% If not, see <http://www.gnu.org/licenses/>.

%% -*- texinfo -*-
%% @documentencoding UTF-8
%% @deftypemethod @@sym {@var{L} =} chol (@var{A})
%% Cholesky factorization of symbolic symmetric matrix.
%%
%% Returns a lower-triangular matrix @var{L}, such that @code{L*L'}
%% is matrix @var{A}.  The matrix @var{A} must be symmetric
%% positive-definite.  Example:
%% @example
%% @group
%% A = sym([1 2 4; 2 13 23; 4 23 43])
%%   @result{} A = (sym 3×3 matrix)
%%
%%       ⎡1  2   4 ⎤
%%       ⎢         ⎥
%%       ⎢2  13  23⎥
%%       ⎢         ⎥
%%       ⎣4  23  43⎦
%%
%% L = chol(A)
%%   @result{} L = (sym 3×3 matrix)
%%
%%       ⎡1  0  0 ⎤
%%       ⎢        ⎥
%%       ⎢2  3  0 ⎥
%%       ⎢        ⎥
%%       ⎣4  5  √2⎦
%%
%% L*L'
%%   @result{} (sym 3×3 matrix)
%%
%%       ⎡1  2   4 ⎤
%%       ⎢         ⎥
%%       ⎢2  13  23⎥
%%       ⎢         ⎥
%%       ⎣4  23  43⎦
%% @end group
%% @end example
%%
%% @seealso{chol, @@sym/qr, @@sym/lu}
%% @end deftypemethod

function y = chol(x)
  if (nargin == 2)
    error('Operation not supported yet.');
  elseif (nargin > 2)
    print_usage ();
  end
  y = pycall_sympy__ ('return _ins[0].cholesky(),', x);
end

%!error <must be> chol (sym ([1 2; 3 4]));
%!error <must be square> chol (sym ([1 2; 3 4; 5 6]));

%!test
%! A = chol(hilb(sym(2)));
%! B = [[1 0]; sym(1)/2 sqrt(sym(3))/6];
%! assert( isequal( A, B ))


function [X,names]=elipsod
% [X,names]=elipsod defines ellipsoidal
% coordinates with 0 < lm < b,
% b < t < c, and c < e <infinity
syms e t lm real
names=[lm,t,e];
b=1; c=2*b; b2=b^2; c2=c^2; cb=c2-b2;
x=e*t*lm/(b*c);
y=sqrt((e^2-b2)*(t^2-b2)*...
    (b2-lm^2)/(b2*cb));
z=sqrt((e^2-c2)*(c2-t^2)*...
     (c2-lm^2)/(c2*cb));
X=[x;y;z];


function arctan_int_values_test ( )

%*****************************************************************************80
%
%% ARCTAN_INT_VALUES_TEST demonstrates the use of ARCTAN_INT_VALUES.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    02 February 2009
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'ARCTAN_INT_VALUES_TEST:\n' );
  fprintf ( 1, '  ARCTAN_INT_VALUES stores values of \n' );
  fprintf ( 1, '  the Arctangent integral function.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '      X           FX\n' );
  fprintf ( 1, '\n' );

  n_data = 0;

  while ( 1 )

    [ n_data, x, fx ] = arctan_int_values ( n_data );

    if ( n_data == 0 )
      break
    end

    fprintf ( 1, '  %12f  %24.16f\n', x, fx );

  end

  return
end


function op = prox_l1linf( q )

%PROX_L1LINF    L1-LInf block norm: sum of L-inf norms of rows.
%    OP = PROX_L1LINF( q ) implements the nonsmooth function
%        OP(X) = q * sum_{i=1:m} norm(X(i,:),Inf)
%    where X is a m x n matrix.  If n = 1, this is equivalent
%    to PROX_L1
%    Q is optional; if omitted, Q=1 is assumed. But if Q is supplied,
%    then it must be positive and real.
%    If Q is a vector, it must be m x 1, and in this case,
%    the weighted norm OP(X) = sum_{i} Q(i)*norm(X(i,:),Inf)
%    is calculated.

if nargin == 0,
	q = 1;
elseif ~isnumeric( q ) || ~isreal( q ) || any(q <= 0),
	error( 'Argument must be positive.' );
end
op = tfocs_prox( @(x)f(x,q), @(x,t)prox_f(x,t,q), 'vector' );
end % end of main function

function v = f(x,q)
    if numel(q) ~= 1 && size(q,1) ~= size(x,1)
        error('Weight must be a scalar or a column vector');
    end
    v = sum( q.* max(abs(x),[],2) );
end

function x = prox_f(x,t,q)
  if nargin < 2,
      error( 'Not enough arguments.' );
  end

  [n,d] = size(x);
  dim   = 2;

  % Option 1: explicitly call prox_linf on the rows:
  % slow, but the chief benefit is that it is low memory
  % This would probably be faster if the matrix was transposed before and after
%   for k= 1:n
%       if isscalar(q), qk = q;
%       else, qk = q(k);
%       end
%       x(k,:) = prox_linf_q( qk, x(k,:).', t ).';
%   end
%   return;

% Option 2: vectorize the call.  By far, more efficient than option 1

  %s     = sort( abs(x), dim, 'descend' );
  %cs    = cumsum(s,dim);
  % Since Matlab stores matrices in column-major order, this method is more cache friendly:
  s     = sort( abs(x)', q, 'descend' );
  cs    = cumsum(s,1)';
  s     = s';

  s     = [s(:,2:end), zeros(n,1)];

  ndx1 = zeros(n,1);
  ndx2 = zeros(n,1);

  if isscalar(q),
      tq = repmat( t*q, n, d );
  else
      tq = repmat( t*q, 1, d );
  end
  %Z = cs - s*diag(1:d);
  % The above may require a lot of memory, so use spdiag or this:
  Z = cs - bsxfun(@times,s,1:d);

  Z = ( Z >= tq );
  Z = Z.';
  % now Z is d x n (typically d is large, n is small)

  % Not sure how to vectorize the find.
  % One option is to use the [i,j] = find(...) form,
  % but that's also extra work, since we can't just find the "first".
  if n > 5
      % avoid the for-loop
      ndx1  = (d+1)-sum(Z)'; % this is the first row with Z > 0
      ndx2  = ndx1;
      ndx2( ndx2 == d+1 ) = Inf;
      ndx1( ndx1 == d+1)  = d; % arbitrary, but do this so we don't have a special case later
  else
      % this might be slightly less memory, so keep the code
      for k = 1:n
          % This is why we transposed Z: due to column-major order,
          %     find( columnVector ) is faster than find( rowVector )
          ndxk = find( Z(:,k), 1 );
          if ~isempty(ndxk)
              ndx1(k) = ndxk;
              ndx2(k) = ndxk;
          else
              ndx1(k) = 1; % value doesn't matter
              ndx2(k) = Inf;
          end
      end
  end
  indx_cs = sub2ind( [n,d], (1:n)', ndx1 );
  tau = (cs(indx_cs) - tq(:,1))./ndx2;
  tau = repmat( tau, 1, d );
  tau_noZeros = tau;
  tau_noZeros( ~x ) = 1;
  x   = x .* (  tau ./ max( abs(x), tau_noZeros ) );
  % Another, but not really better, way is to not do the rempat stuff and do:
  % x     = sign(x).*bsxfun( @min, tau, abs(x) );

end

% TFOCS v1.3 by Stephen Becker, Emmanuel Candes, and Michael Grant.
% Copyright 2013 California Institute of Technology and CVX Research.
% See the file LICENSE for full license information.


function hermite_polynomial_test09 ( p, e )

%*****************************************************************************80
%
%% HERMITE_POLYNOMIAL_TEST09 tests HN_POWER_PRODUCT.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    25 February 2012
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer P, the maximum degree of the polynomial factors.
%
%    Input, integer E, the exponent of X in the integrand.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'HERMITE_POLYNOMIAL_TEST09\n' );
  fprintf ( 1, '  Compute a normalized physicist''s Hermite polynomial power product table.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Tij = integral ( -oo < X < +oo ) X^E Hn(I,X) Hn(J,X) exp(-X*X) dx\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  where Hn(I,X) = normalized physicist''s Hermite polynomial of degree I.\n' );

  fprintf ( 1, '\n' );
  fprintf ( 1, '  Maximum degree P = %d\n', p );
  fprintf ( 1, '  Exponent of X = %d\n', e );

  table = hn_power_product ( p, e );

  r8mat_print ( p + 1, p + 1, table, '  Power product table:' );

  return
end


function pass = test_inv(pref)
% This test constructs two CHEBFUN objects and uses INV() to invert them. It
% checks the inverse calculated is accurate.

% Taken from Chebfun v4 test, invtest.m, by Nick Hale  07/06/2009.

if ( nargin == 0 )
    pref = chebfunpref();
end

algoList = {'roots', 'newton', 'bisection', 'regulafalsi', 'illinois', 'brent'};

for k = 1:6
    x = chebfun('x');
    f = sin(x);
    g = chebfun(@(x) asin(x), [sin(-1), sin(1)]);
    f_inv = inv(f, pref, 'algorithm', algoList{k});
    tol = 100*eps*vscale(f_inv);
    pass(k,1) = norm(g - f_inv, inf) < tol;

    g = inv(f_inv, 'algorithm', algoList{k});
    [f, g] = tweakDomain(f, g, 2e-15);
    pass(k,2) = norm(f - g, inf) < tol;

    x = chebfun('x');
    f = chebfun(@(x) sausagemap(x));
    f_inv = inv(f, pref, 'algorithm', algoList{k});
    tol = 100*eps*vscale(f_inv);
    pass(k,3) = norm(f(f_inv) - x, inf) + norm(f_inv(f) - x, inf) < tol;

    % Check the 'monocheck' and 'rangecheck' options.
    f = chebfun(@exp);
    f_inv = inv(f, pref, 'algorithm', algoList{k}, 'monocheck', 'on', ...
        'rangecheck', 'on');
    tol = 100*eps*vscale(f_inv);
    xx = linspace(f_inv.domain(1), f_inv.domain(end), 20);
    pass(k,4) = norm(f(f_inv(xx)) - xx, inf) < tol;
    pass(k,5) = all(abs(f_inv.domain([1, end]) - exp([-1 1])) < 10*eps);
end

% Check that 'monocheck' fails for a non-monotonic function.
try
    f = chebfun(@(x) x.^2);
    f_inv = inv(f, pref, 'splitting', 'on', 'monocheck', 'on');
    pass(:,6) = false;
catch ME
    if ( strcmpi(ME.identifier, ...
            'CHEBFUN:CHEBFUN:inv:doMonoCheck:notMonotonic') )
        pass(:,6) = true;
    else
        pass(:,6) = false;
    end
end

% Test the example from the help text:
x = chebfun('x');
f = x + .5*abs(x) + .6*sign(x-.5);
g = inv(f);
xx = linspace(f.domain(1), f.domain(end), 10);
pass(:,7) = norm(g(f(xx)) - xx, inf) < 10*vscale(g)*eps;

% Test the inverse of a decreasing function (see #1098).
x = chebfun('x');
f = -sin(x);
g = inv(f);
xx = linspace(f.domain(1), f.domain(end), 10);
pass(:,8) = norm(g(f(xx)) - xx, inf) < 10*vscale(g)*eps;

end

function g = sausagemap(s,d)
if ( nargin < 2 )
    d = 9; % This can be adjusted
end
c = zeros(1,d+1);
c(d:-2:1) = [1 cumprod(1:2:d-2)./cumprod(2:2:d-1)]./(1:2:d);
c = c/sum(c); g = polyval(c,s);
cp = c(1:d).*(d:-1:1);
end


function idx = findClosestCentroids(X, centroids)
%FINDCLOSESTCENTROIDS computes the centroid memberships for every example
%   idx = FINDCLOSESTCENTROIDS (X, centroids) returns the closest centroids
%   in idx for a dataset X where each row is a single example. idx = m x 1
%   vector of centroid assignments (i.e. each entry in range [1..K])
%

% Set K
K = size(centroids, 1);

% You need to return the following variables correctly.
idx = zeros(size(X,1), 1);

% ====================== YOUR CODE HERE ======================
% Instructions: Go over every example, find its closest centroid, and store
%               the index inside idx at the appropriate location.
%               Concretely, idx(i) should contain the index of the centroid
%               closest to example i. Hence, it should be a value in the
%               range 1..K
%
% Note: You can use a for-loop over the examples to compute this.
%

m = size(X,1);

for i = 1:m
    for j = 1:K
        idx(i,j) = (X(i,:)-centroids(j,:))*(X(i,:)-centroids(j,:))';
    end
end
[dummy idx] = min(idx,[],2);

% =============================================================

end


function [dist1, dist2] = distance2(i, bgevent, ac, newcat)
    % calculates the distance in [km] between two eqs
    % precise version based on Raesenbergs Program
    % the calculation is done simultaniously for the biggest event in the
    % cluster and for the current event
    %
    % A. Allmann

    global err derr

    if isempty(err)
        err=1.5; % from InitVariables --epicenter error
    end

    if isempty(derr)
        derr=2; % from InitVariables --depth error
    end

    % columns
    LAT = 2;
    LON = 1;
    DEP = 3;

    % if any of these fail, then we are dealing with multiple events simultaneously, and must take that into account
    assert(numel(i)==1)
    assert(numel(bgevent) == 1)

    evs = newcat.XYZ([i,bgevent,reshape(ac,1,[])],:);

    evs(:,[LAT,LON]) = deg2rad(evs(:,[LAT,LON])); % convert lats & lons to radians

    i_ev = evs(1,:);
    big_ev = evs(2,:);
    ac_ev = evs(3,:);

    pi2 = pi/2;
    flat= 0.993231;

    tana = flat .* tan([i_ev(LAT); big_ev(LAT)]);
    acol = pi2 - atan(tana);

    tanb = flat * tan(ac_ev(LAT));
    bcol = pi2 - atan(tanb);

    diflon = (ac_ev(LON) - [i_ev(LON),  big_ev(LON)]);

    cosdel = [ ...
        (sin(acol(1))*sin(bcol)) .* cos(diflon(1)) + (cos(acol(1))*cos(bcol)) , ...
        (sin(acol(2))*sin(bcol)) .* cos(diflon(2)) + (cos(acol(2))*cos(bcol)) ];

    colat = pi2 - (ac_ev(LAT) + [i_ev(LAT), big_ev(LAT)] ./2);

    radius = 6371.227*(1+(3.37853e-3)*(1/3-((cos(colat)).^2)));
    r = acos(cosdel) .* radius;            % epicenter distance

    r = r - 1.5 * err;               %influence of epicenter error

    r(r<0) = 0;

    % depth distance
    z = abs(ac_ev(DEP) - [i_ev(DEP), big_ev(DEP)] );
    z = z - derr;
    z(z<0)=0;

    r = sqrt(z .^ 2 + r.^2);                   %hypocenter distance
    dist1 = r(:,1);           %distance between eqs
    dist2 = r(:,2);


function sigma = IRnormest(A, b)
%  We sometimes need an estimate of the 2-norm of the matrix A.  We want to
%  allow for user-defined objects and function handles that implement matrix
%  vector multiplication with A.  So we cannot use MATLAB's built-in
%  normest, nor can we use svds.  So we'll use a few iterations of our
%  Lanczos bidiagonalization as implemented in HyBR to get an estimate of
%  the largest singular value.
% HyBRoptions = HyBRset;
% HyBRoptions = HyBRset(HyBRoptions, 'InSolv', 'Tikhonov', 'RegPar', 0, 'Iter', 5, 'Reorth', 'on', 'verbosity', 'off');
% [~, HyBRout] = HyBR_for_IRtools(A, b, [], HyBRoptions, 'off', 'off');
% sigma = max(svd(HyBRout.B));

optnrm.RegParam = 0;
optnrm.MaxIter = 5;
optnrm.Reorth = 'on';
optnrm.DecompOut = 'on';
optnrm.IterBar = 'off';
optnrm.verbosity = 'off';
[~, infonrm] = IRhybrid_lsqr(A, b, optnrm);
sigma = max(svd(infonrm.B));


% Plot the per-pixel histogram intersection value when two distributions
% are compared.

% Zoya Bylinskii, April 2016
% linked to: "What do different evaluation metrics tell us about saliency models?"

% Note: if comparing multiple saliency maps, compute the resMap for each of
% the saliency maps using this function, but then normalize them jointly
% before plotting (to make range of values comparable across plots).

function [resMap,figtitle] = visualize_SIM(salMap, fixMap, toplot)
% salMap is the saliency model prediction (distribution)
% fixMap is the ground truth fixation map (distribution)
% if only plotting one map, set toplot=1; but if comparing multiple maps,
% see note above

if nargin < 3
    toplot = 1;
end

figtitle = 'Histogram intersection';

% format, resize, and normalize maps
map1 = im2double(fixMap);
map2 = im2double(imresize(salMap, size(fixMap)));
map1= (map1-min(map1(:)))/(max(map1(:))-min(map1(:)));
map2= (map2-min(map2(:)))/(max(map2(:))-min(map2(:)));

% compute per-pixel histogram interseciton
map1 = map1/sum(map1(:));
map2 = map2/sum(map2(:));
resMap = min(map1, map2);

% plot the visualization
if toplot
    figure;
    resMap_norm = resMap/max(resMap(:));
    imshow(resMap_norm);
    colormap('parula'); colorbar;
    title(figtitle,'fontsize',14);
end


%SAMMONM Sammon mapping
%
%   W = SAMMONM(A,K,MAX)
%   W = A*SAMMONM([],K,MAX)
%   W = A*SAMMONM(K,MAX)
%   D = B*W
%
% INPUT
%   A    Dataset, used for training the mapping
%   B    Dataset, same dimensionality as A, to be mapped
%   K    Target dimension of mapping (default 2)
%   MAX  Maximum number of iterations, default 100
%
% OUTPUT
%   W    Trained mapping
%   D    K-dimensional dataset
%
% DESCRIPTION
% This is a simplified interface to the more complex MDS routine for high
% dimensional data visualisation. The output is a non-linear projection of
% the original vector space to a K-dimensional target space.
%
% The main differences with MDS are that SAMMONM operates on feature based
% datasets, while MDS expects dissimilarity matrices; MDS maps new objects
% by a second optimization procedures minimizing the stress for the test
% objects, while SAMMONM uses a linear mapping between dissimilarities and
% the target space. See also PREX_MDS for examples. A different procedure
% for the same purpose is TSNEM.
%
% EXAMPLE
% prdatasets;            % make sure prdatasets is in the path
% a = satellite;         % 36D dataset, 6 classes, 6435 objects
% [x,y] = gendat(a,0.5); % split in train and test set
% w = x*sammonm;         % compute mapping
% figure; scattern(x*w); % show trainset mapped to 2D: somewhat overtrained
% figure; scattern((x+randn(size(x))*1e-5)*w): % some noise helps
% figure; scattern(y*w); % show test set mapped to 2D
%
% REFERENCES
% 1. JW Sammon Jr A nonlinear mapping for data structure analysis,
%   IEEE Transactions on Computers C-18, pp. 401-409,1969.
% 2. E. Pekalska, D. de Ridder, R.P.W. Duin, and M.A. Kraaijveld, A new
%    method of generalizing Sammon mapping with application to algorithm
%    speed-up, ASCI99, Proc. 5th Annual ASCI Conf., 1999, 221-228. [pdf]
%
% SEE ALSO (PRTools Guide)
% DATASETS, MAPPINGS, PCAM, MDS, TSNEM, PREX_MDS, SCATTERD, SCATTERN

% Copyright: E. Pekalska, R.P.W. Duin, r.p.w.duin@37steps.com

function out = sammonm(varargin)

  argin = shiftargin(varargin,'scalar');
  argin = setdefaults(argin,[],2,100,0.01);
  if mapping_task(argin,'definition')
    out = define_mapping(argin,'untrained');
  elseif mapping_task(argin,'training')
    [a,k,max_iter,pow] = deal(argin{:});
    d = sqrt(distm(+a));
    opt.maxiter = max_iter;
    w = mds(d,a*pcam(a,k),opt);
    y = getdata(w,1);
    % refer to distances with a low power to get some smoothness
    v = prpinv(d.^pow)*y;
    out = trained_mapping(a,{v,a,pow},2);
  elseif mapping_task(argin,'trained execution')
    [b,w] = deal(argin{1:2});
    [v,a,pow] = getdata(w);
    out = setdata(b,(sqrt(distm(b,a)).^pow)*v);
  else
    error('Illegal call');
  end

return


% T in K
% es in Pa
function es = calculate_saturation_vapor_pressure_liquid (T, method)
    if (nargin < 2) || isempty(method),  method = 'Murphy&Koop2005';  end
    switch method
    case {'Bolton1980', 'approx'}
        t = T - 273.15;
        es2 = 0.6112 .* exp(17.67 .* t ./ (t + 243.5));
        es = es2 * 1000;
        % The AMS Glossary gives [1] the simplified formula above,
        % accordingly to Bolton (1980, eq. 10).
        %
        % AMS Glossary. <http://amsglossary.allenpress.com/glossary/search?id=clausius-clapeyron-equation1>
        % Bolton, D., 1980: The computation of equivalent potential temperature. Mon. Wea. Rev., 108, 1046-1053. <http://dx.doi.org/10.1175/1520-0493(1980)108<1046:TCOEPT>2.0.CO;2>
    case 'Murphy&Koop2005'
        if ~all(123 < T & T < 332)
            warning('calculate_saturation_vapor_pressure_liquid:outRange', ...
                'Temperature out of range [123-332] K.');
        end
        temp = 54.842763 - 6763.22 ./ T - 4.210 .* log(T) + 0.000367 .* T ...
          + tanh( 0.0415 * (T - 218.8) ) ...
          .* (53.878 - 1331.22 ./ T - 9.44523 .* log(T) + 0.014025 .* T);
        es = exp(temp);
        % D. M. MURPHY and T. KOOP
        % Review of the vapour pressures of ice and supercooled water for
        % atmospheric applications
        % Q. J. R. Meteorol. Soc. (2005), 131, pp. 1539-1565
        % doi: 10.1256/qj.04.94
        % <http://dx.doi.org/10.1256/qj.04.94>
        %
        % "Widely used expressions for water vapour (Goff and Gratch 1946; Hyland and Wexler 1983) are being applied outside the ranges of data used by the original authors for their fits. This work may be the first time that data on the molar heat capacity of supercooled water [i.e., at temperatures below its freezing temperature] have been used to constrain its vapour pressure.
    otherwise
        error('calculate_saturation_vapor_pressure_liquid:methodUnknown', ...
            'Method "%s" unknown.', method);
    end
end

%!test
%! % Table C1. VALUES RECOMMENDED FOR CHECKING COMPUTER CODES
%! temp = [...
%! % Temperature (K), Liquid vapor pressure (Pa)
%! 150     1.562e-5    0.001e-5
%! 180     0.011239    0.000001
%! 210     1.2335      0.0001
%! 240     37.667      00.001
%! 273.15  611.213     000.001
%! 273.16  611.657     000.001
%! 300     3536.8      0000.1
%! ];
%! T = temp(:,1);
%! es = temp(:,2);
%! tol = temp(:,3)/10*5;  % rounding error.
%! es2 = calculate_saturation_vapor_pressure_liquid (T);
%! %[es, es2, es2-es]  % DEBUG
%! myassert(es2, es, -tol);

%!test
%! T = (123:10:332)';
%! es  = calculate_saturation_vapor_pressure_liquid (T, 'Murphy&Koop2005');
%! es2 = calculate_saturation_vapor_pressure_liquid (T, 'Bolton1980');
%! %[T-273.15, es, es2, es2-es, 100*(es2-es)./es]  % DEBUG
%! % worse agreement (in percentage) for supercooled temperatures, as expected.
%! temp = 100*(es2-es)./es;
%! myassert(abs(temp(1)) > abs(temp(end)))


function findBallFcn(greenBall1)

%% Find Green Object
% This script reads in an image file and then attempts to find a green
% object in the image. It is designed to find one green ball and highlight
% that ball on the original image

% Copyright 2013 The MathWorks, Inc.

%% Step 1: Read image into MATLAB
% First we read the specified image from the file and bring it into MATLAB
% as a variable. We also display the image to ensure it is correct.
% greenBall1 = imread('greenBall3.jpg');
% imtool(greenBall1);

%% Step 2: Identify Unique Characteristics of Object of Interest

%%
% Extract each color
% Next we using indexing to extract three 2D matrices from the 3D image
% data corresponding to the red, green, and blue components of the image.
r = greenBall1(:, :, 1);
g = greenBall1(:, :, 2);
b = greenBall1(:, :, 3);

%%
% View different color planes
% figure
% subplot(2,2,1),imagesc(r)
% subplot(2,2,2),imagesc(g)
% subplot(2,2,3),imagesc(b)

%%
% Calculate Green
% Then we perform an arithmetic operation on the matrices as a whole to try
% to create one matrix that represents an intensity of green.
justGreen = g - r/2 - b/2;
% colorPlanesPlot(r,g,b,justGreen);

%%
% close all

%% Step 3: Isolate Object of Interest

%%
% Threshold the image
% Now we can set a threshold to separate the parts of the image that we
% consider to be green from the rest.
bw = justGreen > 50;
% imagesc(bw);
% colormap(gray);

%%
% Remove small unwanted objects
% We can use special functions provided by the Image Processing toolbox to
% quickly perform common image processing tasks. Here we are using
% BWAREAOPEN to remove groups of pixels less than 30.
ball1 = bwareaopen(bw, 30);
% imagesc(ball1);

%% Step 4: Find center of green object
% Now we are using REGIONPROPS to extract the centroid of the group of
% pixels representing the ball.
s  = regionprops(ball1, {'centroid','area'});
if isempty(s)
  error('No ball found!');
else
  [~, id] = max([s.Area]);
  hold on, plot(s(id).Centroid(1),s(id).Centroid(2),'wp','MarkerSize',20,'MarkerFaceColor','r'), hold off
  disp(['Center location is (',num2str(s(id).Centroid(1),4),', ',num2str(s(id).Centroid(2),4),')'])
end

%% Step 5: Verify estimated location
% Finally we will plot the center on the original image to clearly evaluate
% how well we have found the center.
imagesc(greenBall1);
hold on, plot(s(id).Centroid(1),s(id).Centroid(2),'wp','MarkerSize',20,'MarkerFaceColor','r'), hold off


function img = discreteHalfPlane(varargin)
%DISCRETEHALFPLANE Discretize a half plane
%
%   A Halfplane is the set of point delimited by a straight line.
%   Only the points located 'on the left' of the line belong to the
%   halfplane.
%
%   IMG = discreteHalfPlane(DIM, LINE)
%   DIM is the size of image, with the format [x0 dx x1;y0 dy y1]
%   LINE is a 1x4 array of the form [x0 y0 dx dy], x0 and y0 being
%   coordinate of a point belonging to the boundary line, and dx and dy
%   being direction vectors of the boundary line of the halfplane.
%
%   IMG = discreteHalfPlane(DIM, POINT, DIRECTION)
%   POINT is a point belonging to the boundary line
%   DIRECTION is a 2x1 vector containing direction vector of the boundary
%   line.
%
%   IMG = discreteHalfPlane(LX, LY, ...);
%   Specifes the pixels coordinates with the two row vectors LX and LY.
%
%   Example
%   img = discreteHalfPlane([1 1 100;1 1 100], [50 50], 30, 10);
%
%   See Also
%     imShapes, discreteDisc, discreteSquare
%

% ------
% Author: David Legland
% e-mail: david.legland@inra.fr
% Created: 2006-10-12
% Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).

%   HISTORY
%   04/01/2007: concatenate transforms before applying them
%   04/03/2009: use meshgrid
%   29/04/2009: update transforms
%   29/05/2009: use more possibilities for specifying grid
%   22/01/2010: fix auto center with odd image size

% compute coordinate of image voxels
[lx, ly, varargin] = parseGridArgs(varargin{:});
[x, y]   = meshgrid(lx, ly);

% default parameters
center = [lx(ceil(end/2)) ly(ceil(end/2))];
theta   = 0;

% process input parameters
if length(varargin)==1
    % first argument contains all parameters
    var = varargin{1};
    center = var(1:2);
    theta = atan2(var(4), var(3));

elseif ~isempty(varargin)
    % parameters are given as separate arguments
    center = varargin{1};
    var = varargin{2};
    theta = atan2(var(2), var(1));
end

% transforms voxels according to square orientation
tra     = createTranslation(-center);
rot     = createRotation(-theta);
[x, y]  = transformPoint(x, y, rot*tra); %#ok<ASGLU>

% create image : simple threshold over 1 dimension
img = y > -1e-14;


function gegenbauer_poly_test ( )

%*****************************************************************************80
%
%% GEGENBAUER_POLY_TEST tests GEGENBAUER_POLY.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    19 October 2008
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'GEGENBAUER_POLY_TEST:\n' );
  fprintf ( 1, '  GEGENBAUER_POLY computes values of \n' );
  fprintf ( 1, '    the Gegenbauer polynomial.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '       N       A       X       GPV      GEGENBAUER\n' );
  fprintf ( 1, '\n' );

  n_data = 0;

  while ( 1 )

    [ n_data, n, a, x, fx ] = gegenbauer_poly_values ( n_data );

    if ( n_data == 0 )
      break
    end

    c = gegenbauer_poly ( n, a, x );
    fx2 = c(n+1);

    fprintf ( 1, '  %6d  %8f  %8f  %12f  %12f\n', n, a, x, fx, fx2 );

  end

  return
end


function [res,K] = dotk(x,y,n)
%DOTK Dot product in K-fold precision.
%   dotk(x,y) computes the scalar product x'*y of the vectors x and y as in
%   K-fold precision, where K is chosen adaptively so that the result is
%   accurate up to machine precision. If x and y are N-D arrays, dotk(x,y)
%   operates along their first non-singleton dimension.
%
%   dotk(x,y,n) returns the dot product of x and y in the dimension n.

%   Authors: Laurent Sorber (Laurent.Sorber@cs.kuleuven.be)
%            Marc Van Barel (Marc.VanBarel@cs.kuleuven.be)
%            Lieven De Lathauwer (Lieven.DeLathauwer@kuleuven-kulak.be)
%
%   References:
%   [1] T. Ogita, S. M. Rump, S. Oishi, "Accurate sum and dot product",
%       SIAM J. Sci. Comp., Vol. 26, No. 6, 2005, pp. 1955-1988.

% Permute x and y so that the n-th mode is the first one.
sx = size(x);
sy = size(y);
if any(sx ~= sy), error('dotk:xy','x and y should have equal size.'); end
if nargin < 3, n = find(sx > 1,1); end
if isempty(n) || size(x,n) == 1, res = conj(x).*y; K = 0; return; end

% Call recursively if either x or y is complex.
if ~isreal(x) && isreal(y)
    [resr,Kr] = dotk(real(x),y,n);
    [resi,Ki] = dotk(-imag(x),y,n);
    res = resr+resi*1i;
    K = Kr+Ki*1i;
    return;
elseif isreal(x) && ~isreal(y)
    [resr,Kr] = dotk(x,real(y),n);
    [resi,Ki] = dotk(x,imag(y),n);
    res = resr+resi*1i;
    K = Kr+Ki*1i;
    return;
elseif ~isreal(x) && ~isreal(y)
    x = cat(n,real(x),imag(x));
    [resr,Kr] = dotk(x,cat(n,real(y),imag(y)),n);
    [resi,Ki] = dotk(x,cat(n,imag(y),-real(y)),n);
    res = resr+resi*1i;
    K = Kr+Ki*1i;
    return;
end
if n > 1
    x = permute(x,[n 1:n-1 n+1:ndims(x)]);
    y = permute(y,[n 1:n-1 n+1:ndims(y)]);
end

% Initialize the algorithm.
res = zeros([2*sx(n) sx(1:n-1) sx(n+1:length(sx))]);
factor = 2^ceil(log2(2/eps(class(x)))/2)+1;

% Adaptive K-fold precision dot product.
[res(end,:),res(1,:)] = twoproduct(x(1,:),y(1,:),factor);
for i = 2:size(x,1)
    [tmp,res(i,:)] = twoproduct(x(i,:),y(i,:),factor);
    [res(end,:),res(i+size(x,1)-1,:)] = twosum(res(end,:),tmp);
end
[res,K] = sumk(res);
if n > 1, res = permute(res,[2:n 1 n+1:length(sx)]); end
if isreal(K), K = K+1; else K = K+1+1*1i; end

end

function [x,y] = twoproduct(a,b,factor)

tmp = factor*a;
a1 = tmp-(tmp-a);
a2 = a-a1;
tmp = factor*b;
b1 = tmp-(tmp-b);
b2 = b-b1;

x = a.*b;
y = a2.*b2-(((x-a1.*b1)-a2.*b1)-a1.*b2);

end

function [x,y] = twosum(a,b)

x = a+b;
tmp = x-a;
y = (a-(x-tmp))+(b-tmp);

end


function img = discreteSquare(varargin)
%DISCRETESQUARE Discretize a planar square
%
%   IMG = discreteSquare(DIM, CENTER, SIDE)
%   DIM is the size of image, with the format [x0 dx x1;y0 dy y1]
%   CENTER is the center of the square
%   SIDE is the length of the side of the square.
%
%   IMG = discreteSquare(DIM, CENTER, SIDE, THETA)
%   Also specify spherical angle of the normal of a face of the square.
%   THETA is the angle with the horizontal, in degrees, counted counter-
%   clockwise in direct basis (and clockwise in image basis).
%
%   IMG = discreteSquare(DIM, SQUARE)
%   send parameters in a row vector, where SQUARE contains at least the
%   center coordinate, and possibly the other parameters.
%
%   IMG = discreteSquare(LX, LY, ...);
%   Specifes the pixels coordinates with the two row vectors LX and LY.
%
%   Example
%   img = discreteSquare([1 1 100;1 1 100], [50 50], 30);
%   img = discreteSquare([1 1 100;1 1 100], [50 50 30]);
%   img = discreteSquare([1 1 100;1 1 100], [50 50 30 30]);
%
%   See Also
%   imShapes, discreteDisc, discreteRectangle
%

% ------
% Author: David Legland
% e-mail: david.legland@inra.fr
% Created: 2006-05-16
% Copyright 2006 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas).

%   HISTORY
%   12/10/2006: typo in the doc
%   04/01/2007: concatenate transforms before applying them
%   19/06/2007: update doc
%   04/03/2009: use meshgrid
%   29/04/2009: update transforms
%   29/05/2009: use more possibilities for specifying grid
%   22/01/2010: fix auto center with odd image size
%   2011-03-30 use degrees

% compute coordinate of image voxels
[lx, ly, varargin] = parseGridArgs(varargin{:});
[x, y]   = meshgrid(lx, ly);

% default parameters
center = [lx(ceil(end/2)) ly(ceil(end/2))];
side    = center;
theta   = 0;

% process input parameters
if length(varargin)==1
    % all paramater in the first argument
    var = varargin{1};
    center = var(:,1:2);
    if size(var, 2)>2
        side = var(:,3);
    end
    if size(var, 2)>3
        theta = var(:,4);
    end

elseif ~isempty(varargin)
    % first argument is center, optionnally followed by side length, then
    % by rotation angle
    center = varargin{1};
    if length(varargin)>1
        side = varargin{2};
    end
    if length(varargin)>2
        theta = varargin{3};
    end
end

% For squares, size of sides is the same in all directions
side    = [side(1) side(1)];

% transforms voxels according to square orientation
tra     = createTranslation(-center);
rot     = createRotation(-deg2rad(theta));
sca     = createScaling(1./side);
[x, y]  = transformPoint(x, y, sca*rot*tra);

% create image: simple threshold over 2 dimensions
img = abs(x) <= .5 & abs(y) <= .5;


syms x
I=int(1/(1+sqrt(1-x^2)))
pretty(I)


function test_values_test100 ( )

%*****************************************************************************80
%
%% TEST100 demonstrates the use of LAPLACE_CDF_VALUES.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    03 November 2005
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST100:\n' );
  fprintf ( 1, '  LAPLACE_CDF_VALUES stores values of\n' );
  fprintf ( 1, '  the Laplace Cumulative Density Function.\n' );
  fprintf ( 1, '\n' );
  fprintf ( 1, '      Mu        Beta          X                  CDF(X)\n' );
  fprintf ( 1, '\n' );

  n_data = 0;

  while ( 1 )

    [ n_data, mu, beta, x, fx ] = laplace_cdf_values ( n_data );

    if ( n_data == 0 )
      break
    end

    fprintf ( 1, '  %12f  %12f  %12f  %24.16f\n', mu, beta, x, fx );

  end

  return
end


function ChanLoc = channel_project_scalp(Vertices, ChanLoc)
% CHANNEL_PROJECT_SCALP: Project EEG electrodes on a scalp surface,
% radially from the center of mass of the scalp surface points.
%
% INPUT:
%     - Vertices : [Mx3] positions of the scalp vertices
%     - ChanLoc  : [Nx3] positions of the EEG electrodes

% @=============================================================================
% This function is part of the Brainstorm software:
% https://neuroimage.usc.edu/brainstorm
%
% Copyright (c) University of Southern California & McGill University
% This software is distributed under the terms of the GNU General Public License
% as published by the Free Software Foundation. Further details on the GPLv3
% license can be found at http://www.gnu.org/copyleft/gpl.html.
%
% FOR RESEARCH PURPOSES ONLY. THE SOFTWARE IS PROVIDED "AS IS," AND THE
% UNIVERSITY OF SOUTHERN CALIFORNIA AND ITS COLLABORATORS DO NOT MAKE ANY
% WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO WARRANTIES OF
% MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, NOR DO THEY ASSUME ANY
% LIABILITY OR RESPONSIBILITY FOR THE USE OF THIS SOFTWARE.
%
% For more information type "brainstorm license" at command prompt.
% =============================================================================@
%
% Authors: Francois Tadel, 2014

% Center the surface on its center of mass
center = mean(Vertices, 1);
Vertices = bst_bsxfun(@minus, Vertices, center);
% Parametrize the surface
p   = .2;
th  = -pi-p   : 0.01 : pi+p;
phi = -pi/2-p : 0.01 : pi/2+p;
rVertices = tess_parametrize(Vertices, th, phi);

% Process each sensor
for iChan = 1:size(ChanLoc,1)
    % Get the closest surface from the point
    c = ChanLoc(iChan,:);
    % Center electrode
    c = c - center;
    % Convert in spherical coordinates
    [c_th,c_phi,c_r] = cart2sph(c(1), c(2), c(3));
    % Interpolate
    c_r = interp2(th, phi, rVertices, c_th, c_phi);
    % Project back in cartesian coordinates
    [c(1),c(2),c(3)] = sph2cart(c_th, c_phi, c_r);
    % Restore initial origin
    c = c + center;
    ChanLoc(iChan,:) = c;
end


function pass = test_chebmatrix

% TODO: Tests 1 and 2 assume a chebcolloc2 discretization.

pref = cheboppref();
pref.discretization = @chebcolloc2;

%% Building blocks
dom = [-2 -0.5 1 2];
I = operatorBlock.eye(dom);
D = operatorBlock.diff(dom);
Z = operatorBlock.zeros(dom);
x = chebfun('x', dom);
u = sin(x.^2);
U = operatorBlock.mult(u);

D5 = [
  -5.499999999999999   6.828427124746189  -2.000000000000000   1.171572875253810  -0.500000000000000
  -1.707106781186547   0.707106781186547   1.414213562373095  -0.707106781186548   0.292893218813452
   0.500000000000000  -1.414213562373095                   0   1.414213562373095  -0.500000000000000
  -0.292893218813452   0.707106781186548  -1.414213562373095  -0.707106781186547   1.707106781186547
   0.500000000000000  -1.171572875253810   2.000000000000000  -6.828427124746189   5.499999999999999
];

%%
A = [ I,Z; D,U ];
M = matrix(A, [5 5 5], pref);
DD = blkdiag(2/1.5*D5,2/1.5*D5,2/1*D5);
[xx, ww] = chebpts([5 5 5], dom);
UU = diag(u(xx));

err(1) = norm( M - [ eye(15), zeros(15); DD, UU ]);

%% more complicated chebmatrix
A = [ I, x, -3*I;
    functionalBlock.sum(dom), 5, functionalBlock.feval(dom(end),dom);
    D, chebfun(1,dom), U ];
M = matrix(A, [5 5 5], pref);
MM = [ eye(15), xx, -3*eye(15);
    ww, 5, [zeros(1,14) 1];
    DD, ones(15,1), UU ];

err(2) = norm( M - MM );

%%
% application to an appropriate chebmatrix
v = [ exp(x); pi; cos(x) ];
Av = A*v;
err(3) = norm( (v{1} + pi*x -3*v{3}) - Av{1} );
err(4) = norm( sum(v{1})+5*v{2}+feval(v{3},dom(end)) - Av{2} );
err(5) = norm( diff(v{1})+pi+(u).*v{3} - Av{3} );

pass = err < 1e-14;

end


function y = GUSFT_simple(x,shift,boxlen,center,w);
% GUSFT_simple -- Gram matrix of the USFT
% Usage:
%   y = GUSFT_simple(x,shift,boxlen,center,w)
% Inputs:
%   x	    vector of length n
%   shift   vector of shifts
%   boxlen  half-length of the window associated with shift
%   center  boolean variable
%   w       window of size 2*boxlen
% Outputs:
%   y      vector le length 2*n
% Description:
%  Evaluates the A'A where A is the nonuniform FT at the points omega_k =
%  shift + k, -boxlen <= k <boxlen
%  See Also
%    USFT_simple, USFFT, MakeFourierDiagonal, GUSFT_Toeplitz
%
% By Emmanuel candes, 2003-2004

if nargin < 5,
  w = ones(1,2*boxlen);
end

if nargin < 4,
  center = 0;
end

       tx  =  USFT_simple(x,shift,boxlen,center,w);
       y   =  Adj_USFT_simple(tx,shift,boxlen,center,w);


function [F,xnames,CRLB, Fall]=SimCRLB(obj,Prot,xvalues,sigma,vars)
% Protocol design for qMR: Optimize the stability of fitting parameters
% toward gaussian noise.
% Use the Cramer-Rao Lower bound for objective function: Wikipedia
% [F,xnames,CRLB]=SimCRLB(obj,Prot,xvalues,sigma)
% https://en.wikipedia.org/wiki/Cramer-Rao_bound
% Based on: Alexander, D.C., 2008. A general framework for experiment design in diffusion MRI and its application in measuring direct tissue-microstructure features. Magn. Reson. Med. 60, 439?448.
%
% [F,xnames,CRLB]=SimCRLB(obj,Prot,xvalues,sigma)
% Outputs:
%   F           minimum COV per variable

if 0%~isprop(obj,'fx'), variables = 1:length(obj.xnames);
else
    variables=find(~obj.fx);
end
F=zeros(size(xvalues,1),length(variables));

for ix=1:size(xvalues,1)

    CRLB = (SimFisherMatrix(obj,Prot,xvalues(ix,:),variables,sigma)+eps)^(-1);
    F(ix,:) = diag(CRLB)'./xvalues(ix).^2;

end
Fall = F(:);
F = mean(F(:));
xnames=obj.xnames(variables);


function corrected = apply_cmatrix(im,cmatrix)
% CORRECTED = apply_cmatrix(IM,CMATRIX)
%
% Applies CMATRIX to RGB input IM. Finds the appropriate weighting of the
% old color planes to form the new color planes, equivalent to but much
% more efficient than applying a matrix transformation to each pixel.
if size(im,3)~=3
error('Apply cmatrix to RGB image only.')
end
r = cmatrix(1,1)*im(:,:,1)+cmatrix(1,2)*im(:,:,2)+cmatrix(1,3)*im(:,:,3);
g = cmatrix(2,1)*im(:,:,1)+cmatrix(2,2)*im(:,:,2)+cmatrix(2,3)*im(:,:,3);
b = cmatrix(3,1)*im(:,:,1)+cmatrix(3,2)*im(:,:,2)+cmatrix(3,3)*im(:,:,3);
corrected = cat(3,r,g,b);
end


% tests for denoising using tv and wavelets

% 1D or 2D test
test = 1;

%% load data
if test==1
    name = 'piece-polynomial';
    n = 1024;
    x0 = load_signal(name, n);
    sigma = .03;
else
    name = 'barb';
    n = 256;
    x0 = load_image(name);
    x0 = crop(x0,n);
    sigma = .05;
end

eta = .05;
x0 = rescale(x0,eta,1-eta);

randn('state',123456);
x = x0 + sigma*randn(size(x0));

%% wavelet domain denoising
%options for wavelets
options.ti = 1;
if test==1
    options.wavelet_vm = 4;
    options.wavelet_type = 'haar';
else
    options.wavelet_vm = 3;
    options.wavelet_type = 'biorthogonal';
    options.decomp_type = 'quad';
end
Jmin = 3;

niter = 10;
Tlist = sigma*linspace(.5,2.5,niter);
err = [];
if test==1
    xw = perform_wavelet_transform(x, Jmin, +1, options);
else
    xw = perform_atrou_transform(x, Jmin, options);
end
for i=1:niter
    xwT = perform_thresholding(xw,Tlist(i), 'soft');
    if test==1
        x1 = perform_wavelet_transform(xwT, Jmin, -1, options);
    else
        x1 = perform_atrou_transform(xwT, Jmin, options);
    end
    err(i) = norm(x0-x1,'fro');
    if i>1 && err(i)<min(err(1:i-1))
        xwav = x1;
    elseif i==1
        xwav = x1;
    end
end

%% TV denoising
options.niter = 5000;

options.etgt = norm(x-x0);
options.etgt = [];

if test==1
    options.lambda_max = .4;
    options.lambda_min = .1;
else
    options.lambda_max = .1;
    options.lambda_min = .0;
end
options.x0 = x0;
[xtv,err] = perform_tv_denoising(x,options);

pwav = psnr(x0,xwav,1);
ptv = psnr(x0,xtv,1);
pnoisy = psnr(x0,x1,1);
disp(['Wav=' num2str(pwav) 'dB, TV=' num2str(ptv) 'dB.' ]);

rep = 'results/tv-denoising/';
if not(exist(rep))
    mkdir(rep);
end

% save in a file the PSNR
filename = [rep 'results.txt'];
fid = fopen(filename, 'a');
fprintf( fid, '%s - noisy=%fdB - wav(%s)=%fdB - tv=%fdB\n', name, pnoisy, options.wavelet_type, pwav, ptv );
fclose(fid);

% display
if test==1
    fs = 20;
    clf;
    subplot(3,1,1);
    plot(x, 'k'); axis([1 n 0 1]);
    set(gca, 'FontSize', fs);
    subplot(3,1,2);
    plot(xwav, 'k'); axis([1 n 0 1]);
    set(gca, 'FontSize', fs);
    subplot(3,1,3);
    plot(xtv, 'k'); axis([1 n 0 1]);
    set(gca, 'FontSize', fs);
    saveas(gcf, [rep name '-tv-denoising.png'], 'png');
    saveas(gcf, [rep name '-tv-denoising.eps'], 'eps');
else
    imageplot({x0 x xwav xtv}, ...
            {'Original' ['Noisy=' num2str(pnoisy)] ['Wav=' num2str(pwav)]  ['TV=' num2str(ptv)] }, 2,2);
    saveas(gcf, [rep name '-tv-denoising.png'], 'png');
    warning off;
    imwrite(clamp(x0), [rep name '-original.png'], 'png' );
    imwrite(clamp(x), [rep name '-noisy.png'], 'png' );
    imwrite(clamp(xwav), [rep name '-wav.png'], 'png' );
    imwrite(clamp(xtv), [rep name '-tv.png'], 'png' );
    warning on;
end


function Sk = mriphantom(kx,ky)
%========================================================================
% Sk = function mriphantom(kx,ky)
%
% Creates simulated raw MRI data of a Shepp Logan head phantom
% for given k space points
% input is the kx and ky coordinates in the image frequency space
% default values for kx and ky are for a Carthesian sampling.
% Input
%   kx, ky  Matrices with kx and ky sampling coordinates
%   E       Optional analytical phantom data matrix as produced by
%           image toolbox function phantom.m (see comments below).
% Data are from an analytical expression for a continuous phantom.
% Core equations for this are taken form :
%  Rik van der Walle et al IEEE Trans Med Imag 19(12) p1160.
%
% Note that the standard matlab function just gives the phantom
% sampled in image space on a carthesian grid.
% This function delivers the simulated raw MRI data sampled through
% a user defined path in k-space (image frequency space)
%
% Author : Ronald Ouwekerk Johns Hopkins University Dept. Radiology
% 601 N. Caroline Street Room 4250 Baltimore, MD 21287-0845 USA
% rouwerke@mri.jhu.edu
% Written :May 2002
% Modified June 2002 :  Revised to give a non-NaN result for k=0
%                       Suppress output if nargout == 0
%                       Added comments to explain the difference with phantom.m
%
% Whenever this software is used for publications an acknowledgment
% would be much appreciated.
%                           RO
%========================================================================
FOV = 2;
N = 64;

if nargin < 2
    %=====================================================================
    % Set the field of view to 24 cm (see image toolbox phantom.m)
    % resolution 128x128 points
    % create k-space coordinates for Carthesion sampling
    % Should be equivalent to IFFT of the phantom
    %=====================================================================
    FOV = 0.24;
    res = FOV/N;
    Kmax = 1/(2*res);
    ky = linspace(-Kmax,Kmax, N);
    ky = ky(ones(N,1),:);
    kx = permute(ky, [2,1]);
end;
if ( (nargin < 3 ) | isempty(E) )  & (exist('phantom.m')==2)
    %========================================================================
    % Create 2D phantom to get defining matrix E
    %========================================================================
    [P,E] = phantom;
elseif ~(exist('phantom.m')==2)
    P = [];
    E = modified_shepp_logan;
end;

%========================================================================
% copy the elements of E and scale to field of view
% Matlab phantom.m default yields FOV =[-1,1]= 2
% Scale all dimensions to desired FOV (*FOV/2).
%   Column 1:  rho  the additive intensity value of the ellipse
%   Column 2:  a    the length of the horizontal semi-axis of the ellipse
%   Column 3:  b    the length of the vertical semi-axis of the ellipse
%   Column 4:  x0   the x-coordinate of the center of the ellipse
%   Column 5:  y0   the y-coordinate of the center of the ellipse
%   Column 6:  alpha  the angle (in degrees) between the horizontal semi-axis
%                   of the ellipse and the x-axis of the image
%========================================================================
[me,ne] = size(E);
rho = E(:,1)';
nellipses = length(rho);
a   = FOV/2 * E(:,2)';
b   = FOV/2 * E(:,3)';
x0  = FOV/2 * E(:,4)';
y0  = FOV/2 * E(:,5)';
alpha = pi*E(:,6)'/180;

%========================================================================
% Stretch kspace coordinates to column vector (retain original size info)
%========================================================================
[mk,nk] = size(kx);
kx = kx(:);
ky = ky(:);
k = kx + j*ky;
klen = length(kx);

%========================================================================
% Calculate angle of the vector to the ellipse center in real space.
%========================================================================
gamma = atan2(y0, x0);
te = sqrt(x0.^2 + y0.^2);

%========================================================================
% Replicate all the ellipse defining vectors to the number of k space values
%========================================================================
gamma = gamma(ones(klen,1),:);
te = te(ones(klen,1),:);
alpha = alpha(ones(klen,1),:);
rho = rho(ones(klen,1),:);
a = a(ones(klen,1),:);
b = b(ones(klen,1),:);

%========================================================================
% Calculate k space vectors angle and magnitude
%========================================================================
kphi = angle(k);
kr = abs(k);

%========================================================================
% Replicate k space and time vectors to the number of ellipses
%========================================================================
kr = kr(:,ones(1,nellipses));
kphi = kphi(:,ones(1,nellipses));

%========================================================================
% Precalculate some values
%========================================================================
pi2 = 2*pi;
cose = cos(gamma - kphi);

%========================================================================
% Projection angles:
% Difference of the k space vector angle and the angles of the ellipses
%========================================================================
sind = sin(kphi - alpha);
cosd = cos(kphi - alpha);

akphi = sqrt(a.^2 .* cosd.^2 + b.^2 .* sind.^2);
%========================================================================
% Calculate the signals for each ellips and each k space point kx,ky
% avoid division by zero    besselj(1,0) = 0 so set 0/0 = 1
%========================================================================
knz = (akphi.*kr ~=0);
bess = ones(size(akphi.*kr));
bess(knz) = besselj(1,pi2.*akphi(knz).*kr(knz)) ./(akphi(knz).*kr(knz));
Sk = exp(-j*pi2.*kr.*te.*cose) .*rho.*a.*b.* bess;
%========================================================================
% Sum the signals for all ellipses and reshape the data back to
% the matrix size of the kx and ky input data
%========================================================================
Sk = sum(Sk,2);
Sk = reshape(Sk, mk,nk);
%========================================================================
% Show the result, including the reconstruction if default Carthesian
% sampling was used
%========================================================================
if  nargin >= 2
    fh = figure;
    subplot(1,2,1);
    if ~isempty(P)
        imagesc(abs(P));
    end;
    title('Software head phantom')
    axis image

    subplot(1,2,2);
    imagesc(abs(Sk));
    title('Simulated raw data')
    return
else
    fh = figure;
    subplot(1,3,1);
    if ~isempty(P)
        imagesc(abs(P));
    end;
    axis image

    subplot(1,3,2);
    imagesc(abs(Sk));
    title('Simulated raw data')
    axis image

    subplot(1,3,3);
    I = fftshift(fft2(Sk));
    imagesc(abs(I));
    axis image
    title('2DFFT Reconstructed image')
end;
%========================================================================
% Suppress output if not asked for any
%========================================================================
if nargout < 1
    Sk = [];
end;

%========================================================================
% END
%========================================================================

%========================================================================
% local function Copied from function phantom.m
% to produce a result without the image toolbox
%========================================================================

%========================================================================
function toft=modified_shepp_logan
%
%   This head phantom is the same as the Shepp-Logan except
%   the intensities are changed to yield higher contrast in
%   the image.  Taken from Toft, 199-200.
%
%         A    a     b    x0    y0    phi
%        ---------------------------------
toft = [  1   .69   .92    0     0     0
        -.8  .6624 .8740   0  -.0184   0
        -.2  .1100 .3100  .22    0    -18
        -.2  .1600 .4100 -.22    0     18
         .1  .2100 .2500   0    .35    0
         .1  .0460 .0460   0    .1     0
         .1  .0460 .0460   0   -.1     0
         .1  .0460 .0230 -.08  -.605   0
         .1  .0230 .0230   0   -.606   0
         .1  .0230 .0460  .06  -.605   0   ];
%========================================================================
% end local function toft=modified_shepp_logan
%========================================================================


%%
clear;
close all;
%%
% load an image
x = uiuc_sample;
x = x(1:251, 1:256);

% compute filter bank
opt_filters = struct();
opt_filters.min_margin = [25, 800];
filters = morlet_filter_bank_2d(size(x), opt_filters);

%%
opt_wavelet = struct();
tic;
[x_phi, x_psi, meta_phi, meta_psi] = wavelet_2d(x, filters, opt_wavelet);
toc;
%%
% compute energy
energy_x = sum(x(:).^2);
energy_Wx = sum(x_phi(:).^2) + sum(cellfun(@(x)(sum(abs(x(:).^2))),x_psi));
assert(abs(energy_x-energy_Wx)/energy_x<1e-2);


function [ y, n ] = r8_upak ( x )

%*****************************************************************************80
%
%% R8_UPAK unpacks an R8 into a mantissa and exponent.
%
%  Discussion:
%
%    This function unpacks a floating point number x so that
%
%      x = y * 2.0^n
%
%    where
%
%      0.5 <= abs ( y ) < 1.0.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    26 September 2011
%
%  Author:
%
%    MATLAB version by John Burkardt.
%
%  Parameters:
%
%    Input, real X, the number to be unpacked.
%
%    Output, real Y, the mantissa.
%
%    Output, integer N, the exponent.
%
  absx = abs ( x );
  n = 0;
  y = 0.0;

  if ( x == 0.0 )
    return
  end

  while ( absx < 0.5 )
    n = n - 1;
    absx = absx * 2.0;
  end

  while ( 1.0 <= absx )
    n = n + 1;
    absx = absx * 0.5;
  end

  if ( x < 0.0 )
    y = - absx;
  else
    y = + absx;
  end

  return
end


function c=ref_dsti_1(f)
%REF_DSTI_1  Reference Discrete Sine Transform type I
%   Usage:  c=ref_dsti_1(f);
%
%

L=size(f,1);
W=size(f,2);

if L==1
  c=f;
  return;
end;

R=1/sqrt(2)*[zeros(1,L,assert_classname(f));...
	     eye(L);
	     zeros(1,L,assert_classname(f));...
	     -flipud(cast(eye(L),assert_classname(f)))];

c=i*R'*dft(R*f);


function test_one ( )

%*****************************************************************************80
%
%% TEST_ONE performs interpolation with SPINTERP.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    15 January 2013
%
%  Author:
%
%    John Burkardt
%
  timestamp ( );
  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST_ONE:\n' );
  fprintf ( 1, '  An example of the use of SPINTERP\n' );
  fprintf ( 1, '  for sparse interpolation in multiple dimensions.\n' );

  addpath ( '../spinterp' );

  options = spset ( ...
    'GridType', 'Chebyshev', ...
    'MinDepth', 11 );

  m = 2;

  box = [ 0.0, 1.0; ...
          0.0, 1.0 ];
%
%  Set up the sparse grid structure for the function.
%
  c = spvals ( @f_one, m, box, options );
%
%  C is a structure, so this is the easiest way to
%  print it out and examine it:
%
  c
%
%  Pick some sample points in the unit square
%  and compare the interpolant Z to the function F_ONE(X,Y).
%
  sample_num = 100;

  x = rand ( 1, sample_num );
  y = rand ( 1, sample_num );
  z = spinterp ( c, x, y );

  error = max ( abs ( z - f_one ( x, y ) ) );
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Maximum interpolation error was %g\n', error );
%
%  Plot the function and the interpolant.
%
  subplot ( 1, 2, 1 );
  ezmesh ( @f_one, [ 0.0, 1.0 ] );
  title ( 'f(x,y) = sin(x) + cos(y)' );
  subplot ( 1, 2, 2 );
  ezmesh ( @(x,y) spinterp ( c, x, y ), [ 0.0, 1.0 ] );
  title ( 'Sparse grid interpolant' );

  rmpath ( '../spinterp' )
%
%  Terminate.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST_ONE:\n' );
  fprintf ( 1, '  Normal end of execution.\n' );
  fprintf ( 1, '\n' );
  timestamp ( );

  return
end


#!/usr/bin/env octave
%% Machine Learning Online Class
%  Exercise 5 | Regularized Linear Regression and Bias-Variance
%
%  Instructions
%  ------------
%
%  This file contains code that helps you get started on the
%  exercise. You will need to complete the following functions:
%
%     linearRegCostFunction.m
%     learningCurve.m
%     validationCurve.m
%
%  For this exercise, you will not need to change any code in this file,
%  or any other files other than those mentioned above.
%

%% Initialization
clear ; close all; clc

%% =========== Part 1: Loading and Visualizing Data =============
%  We start the exercise by first loading and visualizing the dataset.
%  The following code will load the dataset into your environment and plot
%  the data.
%

% Load Training Data
fprintf('Loading and Visualizing Data ...\n')

% Load from ex5data1:
% You will have X, y, Xval, yval, Xtest, ytest in your environment
load ('ex5data1.mat');

% m = Number of examples
m = size(X, 1);

% Plot training data
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
xlabel('Change in water level (x)');
ylabel('Water flowing out of the dam (y)');

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 2: Regularized Linear Regression Cost =============
%  You should now implement the cost function for regularized linear
%  regression.
%

theta = [1 ; 1];
J = linearRegCostFunction([ones(m, 1) X], y, theta, 1);

fprintf(['Cost at theta = [1 ; 1]: %f '...
         '\n(this value should be about 303.993192)\n'], J);

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 3: Regularized Linear Regression Gradient =============
%  You should now implement the gradient for regularized linear
%  regression.
%

theta = [1 ; 1];
[J, grad] = linearRegCostFunction([ones(m, 1) X], y, theta, 1);

fprintf(['Gradient at theta = [1 ; 1]:  [%f; %f] '...
         '\n(this value should be about [-15.303016; 598.250744])\n'], ...
         grad(1), grad(2));

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 4: Train Linear Regression =============
%  Once you have implemented the cost and gradient correctly, the
%  trainLinearReg function will use your cost function to train
%  regularized linear regression.
%
%  Write Up Note: The data is non-linear, so this will not give a great
%                 fit.
%

%  Train linear regression with lambda = 0
lambda = 0;
[theta] = trainLinearReg([ones(m, 1) X], y, lambda);

%  Plot fit over the data
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
xlabel('Change in water level (x)');
ylabel('Water flowing out of the dam (y)');
hold on;
plot(X, [ones(m, 1) X]*theta, '--', 'LineWidth', 2)
hold off;

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 5: Learning Curve for Linear Regression =============
%  Next, you should implement the learningCurve function.
%
%  Write Up Note: Since the model is underfitting the data, we expect to
%                 see a graph with "high bias" -- slide 8 in ML-advice.pdf
%

lambda = 0;
[error_train, error_val] = ...
    learningCurve([ones(m, 1) X], y, ...
                  [ones(size(Xval, 1), 1) Xval], yval, ...
                  lambda);

plot(1:m, error_train, 1:m, error_val);
title('Learning curve for linear regression')
legend('Train', 'Cross Validation')
xlabel('Number of training examples')
ylabel('Error')
axis([0 13 0 150])

fprintf('# Training Examples\tTrain Error\tCross Validation Error\n');
for i = 1:m
    fprintf('  \t%d\t\t%f\t%f\n', i, error_train(i), error_val(i));
end

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 6: Feature Mapping for Polynomial Regression =============
%  One solution to this is to use polynomial regression. You should now
%  complete polyFeatures to map each example into its powers
%

p = 8;

% Map X onto Polynomial Features and Normalize
X_poly = polyFeatures(X, p);
[X_poly, mu, sigma] = featureNormalize(X_poly);  % Normalize
X_poly = [ones(m, 1), X_poly];                   % Add Ones

% Map X_poly_test and normalize (using mu and sigma)
X_poly_test = polyFeatures(Xtest, p);
X_poly_test = bsxfun(@minus, X_poly_test, mu);
X_poly_test = bsxfun(@rdivide, X_poly_test, sigma);
X_poly_test = [ones(size(X_poly_test, 1), 1), X_poly_test];         % Add Ones

% Map X_poly_val and normalize (using mu and sigma)
X_poly_val = polyFeatures(Xval, p);
X_poly_val = bsxfun(@minus, X_poly_val, mu);
X_poly_val = bsxfun(@rdivide, X_poly_val, sigma);
X_poly_val = [ones(size(X_poly_val, 1), 1), X_poly_val];           % Add Ones

fprintf('Normalized Training Example 1:\n');
fprintf('  %f  \n', X_poly(1, :));

fprintf('\nProgram paused. Press enter to continue.\n');
pause;

%% =========== Part 7: Learning Curve for Polynomial Regression =============
%  Now, you will get to experiment with polynomial regression with multiple
%  values of lambda. The code below runs polynomial regression with
%  lambda = 0. You should try running the code with different values of
%  lambda to see how the fit and learning curve change.
%

lambda = 0;
[theta] = trainLinearReg(X_poly, y, lambda);

% Plot training data and fit
figure(1);
plot(X, y, 'rx', 'MarkerSize', 10, 'LineWidth', 1.5);
plotFit(min(X), max(X), mu, sigma, theta, p);
xlabel('Change in water level (x)');
ylabel('Water flowing out of the dam (y)');
title (sprintf('Polynomial Regression Fit (lambda = %f)', lambda));

figure(2);
[error_train, error_val] = ...
    learningCurve(X_poly, y, X_poly_val, yval, lambda);
% Should we use logarithmix scale there?
plot(1:m, error_train, 1:m, error_val);

title(sprintf('Polynomial Regression Learning Curve (lambda = %f)', lambda));
xlabel('Number of training examples')
ylabel('Error')
axis([0 13 0 100])
legend('Train', 'Cross Validation')

fprintf('Polynomial Regression (lambda = %f)\n\n', lambda);
fprintf('# Training Examples\tTrain Error\tCross Validation Error\n');
for i = 1:m
    fprintf('  \t%d\t\t%f\t%f\n', i, error_train(i), error_val(i));
end

fprintf('Program paused. Press enter to continue.\n');
pause;

%% =========== Part 8: Validation for Selecting Lambda =============
%  You will now implement validationCurve to test various values of
%  lambda on a validation set. You will then use this to select the
%  "best" lambda value.
%

[lambda_vec, error_train, error_val] = ...
    validationCurve(X_poly, y, X_poly_val, yval);

close all;
plot(lambda_vec, error_train, lambda_vec, error_val);
legend('Train', 'Cross Validation');
xlabel('lambda');
ylabel('Error');

fprintf('lambda\t\tTrain Error\tValidation Error\n');
for i = 1:length(lambda_vec)
	fprintf(' %f\t%f\t%f\n', ...
            lambda_vec(i), error_train(i), error_val(i));
end

fprintf('Program paused. Press enter to continue.\n');
pause;


function E=TeagerEnergy(x,dim)
%%TEAGERENERGY Compute the Teager energy of a discrete signal as defined in
%              [1]. The Teager energy is a type of instantaneous "energy"
%              signal that produces a result related to the rate of change
%              of the amplitude and frequency of the original signal. It
%              has found use in a number of applications, primarily related
%              to acoustic processing, such as in the wavelet-based speech
%              enhancement of [2].
%
%INPUTS: x A vector signal or a matrix of signals whose Teager energy is
%          desired.
%      dim The dimension of x over which the Teager energy is to be found.
%          For example, for a NXnumSigs set of signals, one would use
%          dims=1 as each column is a separate signal.
%
%OUTPUTS: E The Teager energy. This has the same size as x.
%
%EXAMPLE 1:
%Here we compute the Teager energy signal of an up-chirp followeb by a down
%chirp.
% fStart=10;
% fEnd=100;
% T=1;
%
% %We want the chirp to be sampled at 100 times the Nyquist frequency.
% T0=1/(100*2*fEnd);%The sampling period.
% %We want to sample just the duration of the signal. Thus, time goes from
% %T=0 to T=1. This means that we need N=T/T0 samples.
% N=T/T0;
%
% %Make a chirp up and then a chirp down
% [x1,t]=LFMChirp(T,fStart,fEnd,T0);
% x2=LFMChirp(T,fEnd,fStart,T0);
% x=real([x1(:);x2(:)]);
% t=[t(:);t(end)+t(2)-t(1)+t(:)];
%
% E=TeagerEnergy(x);
%
% figure(1)
% clf
% subplot(2,1,1)
% hold on
% plot(t,x,'-r','linewidth',2)
% title('Original Signal')
% subplot(2,1,2)
% plot(t,E,'-r','linewidth',2)
% title('Transformed Signal')
%
%EXAMPLE 2:
%Here we compute the Teager energy of a sum of sinusoids. It essentially
%just follows the beat pattern.
% n=(0:100).';
% x=sin(pi/6*n)+sin(pi/4*n);
%
% E=TeagerEnergy(x);
%
% figure(2)
% clf
% subplot(2,1,1)
% hold on
% plot(n,x,'-r','linewidth',2)
% title('Original Signal')
% subplot(2,1,2)
% plot(n,E,'-r','linewidth',2)
% title('Transformed Signal')
%
%REFERENCES:
%[1] J. F. Kaiser, "On a simple algorithm to calculate the 'energy' of a
%    signal," in Proceedings of the International Conference on Acoustics,
%    Speech and Signal Processing, Albuquerque, NM, 3-6 Apr. 1990, pp.
%    381-384.
%[2] M. Bahoura and J. Rouat, "Wavelet speech enhancement based on the
%    Teager energy operator," IEEE Signal Processing Letters, vol. 8, no.
%    1, pp. 10-12, Jan. 2001.
%
%November 2016 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

if(nargin<2||isempty(dim))
    E=x.^2-circshift(x,-1).*circshift(x,1);
else
    E=x.^2-circshift(x,-1,dim).*circshift(x,1,dim);
end
end

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.


function hermite_exactness ( n, x, w, p_max )

%*****************************************************************************80
%
%% HERMITE_EXACTNESS investigates exactness of Hermite quadrature.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    16 May 2014
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the number of points in the rule.
%
%    Input, real X(N), the quadrature points.
%
%    Input, real W(N), the quadrature weights.
%
%    Input, integer P_MAX, the maximum exponent.
%    0 <= P_MAX.
%
  fprintf ( 1, '\n' );
  fprintf ( 1, '  Quadrature rule for Hermite integral\n' );
  fprintf ( 1, '  Rule of order N = %d\n', n );
  fprintf ( 1, '  Degree          Relative Error\n' );
  fprintf ( 1, '\n' );

  for p = 0 : p_max

    s = hermite_integral ( p );

    v(1:n,1) = x(1:n) .^ p;

    q = w' * v;

    if ( s == 0.0 )
      e = abs ( q - s );
    else
      e = abs ( ( q - s ) / s );
    end

    fprintf ( 1, '  %6d  %24.16f\n', p, e );

  end

  return
end


function K = covGaboriso(hyp, x, z, i)

% Gabor covariance function with length scale ell and period p. The
% covariance function is parameterized as:
%
% k(x,z) = h(x-z) with h(t) = exp(-t'*t/(2*ell^2))*cos(2*pi*sum(t)/p).
%
% The hyperparameters are:
%
% hyp = [ log(ell)
%         log(p)   ]
%
% Note that covSM implements a weighted sum of Gabor covariance functions, but
% using an alternative (spectral) parameterization.
%
% For more help on design of covariance functions, try "help covFunctions".
%
% Copyright (c) by Hannes Nickisch, 2014-09-26.
%
% See also COVFUNCTIONS.M, COVGABORARD.M, COVSM.M.

if nargin<2, K = '2'; return; end                          % report no of params
if nargin<3, z = []; end                                   % make sure, z exists
xeqz = isempty(z); dg = strcmp(z,'diag');                       % determine mode

[n,D] = size(x);                                                % dimensionality
ell = exp(hyp(1));                                                % length scale
p = exp(hyp(2));                                                        % period

if dg                                              % compute squared distance d2
  d2 = zeros(n,1);
else
  if xeqz                                                 % symmetric matrix Kxx
    d2 = sq_dist(x'/ell);
  else                                                   % cross covariances Kxz
    d2 = sq_dist(x'/ell,z'/ell);
  end
end

dp = zeros(size(d2));                                % init sum(t)/p computation
if ~dg
  if xeqz                                                 % symmetric matrix Kxx
    for d=1:D, dp = dp + (x(:,d)*ones(1,size(x,1))-ones(n,1)*x(:,d)')/p; end
  else                                                   % cross covariances Kxz
    for d=1:D, dp = dp + (x(:,d)*ones(1,size(z,1))-ones(n,1)*z(:,d)')/p; end
  end
end

K = exp(-d2/2);
if nargin<4                                                        % covariances
  K = cos(2*pi*dp).*K;
else                                                               % derivatives
  if i==1                                                         % length scale
    K = d2.*cos(2*pi*dp).*K;
  elseif i==2                                                           % period
    K = 2*pi*dp.*sin(2*pi*dp).*K;
  else
    error('Unknown hyperparameter')
  end
end


%% Basic Geometric Drawing
% In this demo, we show how to:
%
% * Draw a line by using the function |cv.line|
% * Draw an ellipse by using the function |cv.ellipse|
% * Draw a rectangle by using the function |cv.rectangle|
% * Draw a circle by using the function |cv.circle|
% * Draw a filled polygon by using the function |cv.fillPoly|
%
% Sources:
%
% * <https://docs.opencv.org/3.2.0/d3/d96/tutorial_basic_geometric_drawing.html>
% * <https://github.com/opencv/opencv/blob/3.2.0/samples/cpp/tutorial_code/core/Matrix/Drawing_1.cpp>
%

%%
% image size
W = 400;

%% Atom
img1 = zeros(W,W,3,'uint8');
for ang=-45:45:90
    img1 = cv.ellipse(img1, [W W]/2, [W/4 W/16], ...
        'Angle',ang, 'Color',[0 0 255], 'Thickness',2);
end
img1 = cv.circle(img1, [W W]/2, W/32, ...
    'Color',[255 0 0], 'Thickness','Filled');

figure(1), imshow(img1)
title('Atom')

%% Rook
img2 = zeros(W,W,3,'uint8');
pts = [
       W/4   7*W/8  ;
     3*W/4   7*W/8  ;
     3*W/4  13*W/16 ;
    11*W/16 13*W/16 ;
    19*W/32  3*W/8  ;
     3*W/4   3*W/8  ;
     3*W/4     W/8  ;
    26*W/40    W/8  ;
    26*W/40    W/4  ;
    22*W/40    W/4  ;
    22*W/40    W/8  ;
    18*W/40    W/8  ;
    18*W/40    W/4  ;
    14*W/40    W/4  ;
    14*W/40    W/8  ;
       W/4     W/8  ;
       W/4   3*W/8  ;
    13*W/32  3*W/8  ;
     5*W/16 13*W/16 ;
       W/4  13*W/16 ;
];
img2 = cv.fillPoly(img2, {pts}, 'Color',[255 255 255]);
img2 = cv.rectangle(img2, [0 7*W/8], [W W], ...
    'Color',[255 255 0], 'Thickness','Filled');
img2 = cv.line(img2, [0 15*W/16], [W 15*W/16]);
img2 = cv.line(img2, [W/4 7*W/8], [W/4 W]);
img2 = cv.line(img2, [W/2 7*W/8], [W/2 W]);
img2 = cv.line(img2, [3*W/4 7*W/8], [3*W/4 W]);

figure(2), imshow(img2)
title('Rook')


% DFTFILT3 - discrete complex wavelet filters
%
% Usage:
%   >> [wavelet,cycles,freqresol,timeresol] = dftfilt3( freqs, cycles, srate, varargin)
%
% Inputs:
%   freqs    - vector of frequencies of interest.
%   cycles   - cycles array. If cycles=0, then the Hanning tapered Short-term FFT is used.
%              If one value is given and cycles>0, all wavelets have
%              the same number of cycles. If two values are given, the
%              two values are used for the number of cycles at the lowest
%              frequency and at the highest frequency, with linear or
%              log-linear interpolation between these values for intermediate
%              frequencies
%   srate    - sampling rate (in Hz)
%
% Optional Inputs: Input these as 'key/value pairs.
%   'cycleinc' - ['linear'|'log'] increase mode if [min max] cycles is
%              provided in 'cycle' parameter. {default: 'linear'}
%   'winsize'  Use this option for Hanning tapered FFT or if you prefer to set the length of the
%              wavelets to be equal for all of them (e.g., to set the
%              length to 256 samples input: 'winsize',256). {default: [])
%              Note: the output 'wavelet' will be a matrix and it may be
%              incompatible with current versions of timefreq and newtimef.
%   'timesupport' The number of temporal standard deviation used for wavelet lengths {default: 7)
%
% Output:
%   wavelet - cell array or matrix of wavelet filters
%   timeresol - temporal resolution of Morlet wavelets.
%   freqresol - frequency resolution of Morlet wavelets.
%
% Note: The length of the window is always made odd.
%
% Authors: Arnaud Delorme, SCCN/INC/UCSD, La Jolla, 3/28/2003
%          Rey Ramirez, SCCN/INC/UCSD, La Jolla, 9/26/2006

% Copyright (C) 3/28/2003 Arnaud Delorme 8, SCCN/INC/UCSD, arno@salk.edu
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.

%
% Revision 1.12 2006/09/25  rey r
% Almost complete rewriting of dftfilt2.m, changing both Morlet and Hanning
% DFT to be more in line with conventional implementations.
%
% Revision 1.11  2006/09/07 19:05:34  scott
% further clarified the Morlet/Hanning distinction -sm
%
% Revision 1.10  2006/09/07 18:55:15  scott
% clarified window types in help msg -sm
%
% Revision 1.9  2006/05/05 16:17:36  arno
% implementing cycle array
%
% Revision 1.8  2004/03/04 19:31:03  arno
% email
%
% Revision 1.7  2004/02/25 01:45:55  arno
% sinus test
%
% Revision 1.6  2004/02/15 22:23:08  arno
% implementing morlet wavelet
%
% Revision 1.5  2003/05/09 20:55:10  arno
% adding hanning function
%
% Revision 1.4  2003/04/29 16:02:54  arno
% header typos
%
% Revision 1.3  2003/04/29 01:09:16  arno
% debug imaginary part
%
% Revision 1.2  2003/04/28 23:01:13  arno
% *** empty log message ***
%
% Revision 1.1  2003/04/28 22:46:49  arno
% Initial revision
%

function [wavelet,cycles,freqresol,timeresol] = dftfilt3( freqs, cycles, srate, varargin);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Rey fixed all input parameter sorting.
if nargin < 3
    error(' A minimum of 3 arguments is required');
end
numargin=length(varargin);
if rem(numargin,2)
    error('There is an uneven number key/value inputs. You are probably missing a keyword or its value.')
end
varargin(1:2:end)=lower(varargin(1:2:end));

% Setting default parameter values.
cycleinc='linear';
winsize=[];
timesupport=7;  % Setting default of 7 temporal standard deviations for wavelet's length.

for n=1:2:numargin
    keyword=varargin{n};
    if strcmpi('cycleinc',keyword)
        cycleinc=varargin{n+1};
    elseif strcmpi('winsize',keyword)
        winsize=varargin{n+1};
        if ~mod(winsize,2)
            winsize=winsize+1; % Always set to odd length wavelets and hanning windows;
        end
    elseif strcmpi('timesupport',keyword)
        timesupport=varargin{n+1};
    else
        error(['What is ' keyword '? The only legal keywords are: type, cycleinc, winsize, or timesupport.'])
    end
end
if isempty(winsize) && cycles(1)==0
    error('If you are using a Hanning tapered FFT, please supply the winsize input-pair.')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% compute number of cycles at each frequency
% ------------------------------------------
type='morlet';
if length(cycles) == 1 && cycles(1)~=0
    cycles = cycles*ones(size(freqs));
elseif length(cycles) == 2
    if strcmpi(cycleinc, 'log') % cycleinc
         cycles = linspace(log(cycles(1)), log(cycles(2)), length(freqs));
         cycles = exp(cycles);
         %cycles=logspace(log10(cycles(1)),log10(cycles(2)),length(freqs)); %rey
    else
        cycles = linspace(cycles(1), cycles(2), length(freqs));
    end
end
if cycles==0
    type='sinus';
end

sp=1/srate; % Rey added this line (i.e., sampling period).
% compute wavelet
for index = 1:length(freqs)
    fk=freqs(index);
    if strcmpi(type, 'morlet') % Morlet.
        fk=fk/srate; % Normalize frequency for textbook equations as in TB97
        sigf=fk/cycles(index); % Computing time and frequency standard deviations, resolutions, and normalization constant.
        sigt=1./(2*pi*sigf);
        A=1./sqrt(sigt*sqrt(pi));
        timeresol(index)=2*sigt/srate; % sec
        freqresol(index)=2*sigf*srate; % Hz
        if isempty(winsize) % bases will be a cell array.
%             tneg=[-sp:-sp:-sigt*timesupport/2];
%             tpos=[0:sp:sigt*timesupport/2];
%             t=[fliplr(tneg) tpos];
            t = (0:floor(sigt*timesupport/2)*2)-floor(sigt*timesupport/2); % Always odd; backward compatible
            psi=A.*(exp(-(t.^2)./(2*(sigt^2))).*exp(2*i*pi*fk*t));
            wavelet{index}=psi;  % These are the wavelets with variable number of samples based on temporal standard deviations (sigt).
        else % bases will be a matrix.
%             tneg=[-sp:-sp:-sp*winsize/2];
%             tpos=[0:sp:sp*winsize/2];
%             t=[fliplr(tneg) tpos];
            t = (0:floor(winsize/2)*2)-floor(winsize/2); % Always odd; backward compatible
            psi=A.*(exp(-(t.^2)./(2*(sigt^2))).*exp(2*i*pi*fk*t));
            wavelet(index,:)=psi; % These are the wavelets with the same length.
            % This is useful for doing time-frequency analysis as a matrix vector or matrix matrix multiplication.
        end
    elseif strcmpi(type, 'sinus') % Hanning
        tneg=[-sp:-sp:-sp*winsize/2];
        tpos=[0:sp:sp*winsize/2];
        t=[fliplr(tneg) tpos];
        win = exp(2*i*pi*fk*t);
        wavelet(index,:) = win .* hanning(winsize)';
        %wavelet{index} = win .* hanning(winsize)';
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    end
end

% symmetric hanning function
function w = hanning(n)
if ~rem(n,2)
    w = .5*(1 - cos(2*pi*(1:n/2)'/(n+1)));
    w = [w; w(end:-1:1)];
else
    w = .5*(1 - cos(2*pi*(1:(n+1)/2)'/(n+1)));
    w = [w; w(end-1:-1:1)];
end


function [pca_samples,params]=cosmo_pca(samples,retain)
% Principal Component Analysis
%
% [pca_samples,params]=cosmo_pca(samples[,retain])
%
% Input:
%   samples                 M x N  numeric matrix
%   retain                  (optional) number of components to retain;
%                           must be less than or equal to N. Default: N
%
% Output:
%   pca_samples             M x retain samples in Principal Component
%                           space, after samples have been centered
%   params                  struct with fields:
%     .coef                 M x retain Principal Component coefficients
%     .mu                   M x 1 column-wise average of samples
%                           It holds that:
%                             samples=bsxfun(@plus,params.mu,...
%                                                 pca_samples*params.coef')
%     .explained            1 x N Percentage of explained variance
%
%
% Examples:
%     samples=[      2.0317   -0.8918   -0.8258;...
%                    0.5838    1.8439    1.1656;...
%                   -1.4437   -0.2617   -1.9207;...
%                   -0.5177    2.3387    0.4412;...
%                    1.1908   -0.2040   -0.2088;...
%                   -1.3265    2.7235    0.1476];
%     %
%     % apply PCA, keeping two dimensions
%     [pca_samples,params]=cosmo_pca(samples,2);
%     %
%     % show samples in PC space
%     cosmo_disp(pca_samples);
%     %|| [  -2.64     0.654
%     %||    0.923      1.43
%     %||   -0.723     -2.48
%     %||     1.64     0.265
%     %||    -1.46     0.569
%     %||     2.27    -0.438 ]
%     %
%     % show parameters
%     cosmo_disp(params);
%     %|| .coef
%     %||   [ -0.512     0.744
%     %||      0.794     0.219
%     %||      0.328     0.632 ]
%     %|| .mu
%     %||   [ 0.0864     0.925      -0.2 ]
%     %|| .explained
%     %||   [    66
%     %||      33.3
%     %||     0.676 ]
%
% #   For CoSMoMVPA's copyright information and license terms,   #
% #   see the COPYING file distributed with CoSMoMVPA.           #

    if nargin<2
        retain=[];
    end

    verify_parameters(samples,retain)
    ndim=get_number_of_components(samples,retain);

    % subtract mean
    mu=mean(samples,1);
    samples_demu=bsxfun(@minus, samples, mu);

    % singular value decomposition
    [u,s,w]=svd(samples_demu,'econ');

    % extract eigen values
    [nrow,ncol]=size(samples);
    samples_is_vector=nrow==1 || ncol==1;
    if samples_is_vector
        % single eigen value
        eigvals=s(1);
    else
        % take diagonal
        eigvals=diag(s);
    end

    if ndim==0
        % seperate case for zero dimensions
        pca_samples=zeros(1,0);
        coef=zeros(ncol,0);
    else
        pca_samples_rand_sign=bsxfun(@times,u(:,1:ndim),eigvals(1:ndim)');
        [coef,sgn]=max_abs_positive_columnwise(w(:,1:ndim));
        pca_samples=bsxfun(@times,pca_samples_rand_sign,sgn);
    end

    % store coefficients
    params=struct();
    params.coef=coef;
    params.mu=mu;

    nexpl=min([nrow-1,ncol]);
    if nrow==1 || ncol==0
        % special case for empty vecto with explained variance
        params.explained=zeros(1,0);
    else
        explained_ratio=eigvals'.^2;
        params.explained=100*explained_ratio(1:nexpl)/sum(explained_ratio);
    end

function ncomp=get_number_of_components(samples,retain)
    max_retain=size(samples,2);

    if isempty(retain)
        retain=max_retain;
    end

    if retain>max_retain
        error('retain argument %d must be less than %d',...
                                retain,max_retain);
    end

    [nrow,ncol]=size(samples);
    ncomp=min([nrow-1,ncol,retain]);

function [coef_pos,sgn]=max_abs_positive_columnwise(coef)
    % swap sign for each column in which the maximum absolute value is
    % negative. sgn contains the sign used in each column (-1 or 1)
    [unused,i]=max(abs(coef),[],1);
    [nrows,ncols]=size(coef);
    mx_idx=(0:(ncols-1))*nrows+i;
    mx=coef(mx_idx);

    sgn=(mx>0)*2-1;
    coef_pos=bsxfun(@times,coef,sgn);

function verify_parameters(samples,retain)
    if ~isnumeric(samples)
        error('samples argument must be numeric');
    end

    if numel(size(samples))>2
        error('samples argument must be a matrix');
    end

    if ~(isempty(retain) || ...
                    (isscalar(retain) && ...
                     retain>0 && ...
                     isequal(round(retain),retain)))
        error('retain argument must be positive integer');
    end


function uI = edgeinterpolate2(exactu,node,edge,face,face2edge)
%% EDGEINTERPOLATE2 interpolate to the quadratic (1st type) edge finite element space.
%
% uI = edgeinterpolate(u,node,edge) interpolates a given function
% u into the lowesr order edge finite element spaces. The coefficient
% is given by the line integral int_e u*t ds. Simpson rule is used to
% evaluate this line integral.
%
% The input u could be a funtional handel or an array of length N (linear
% element) or N+NE (quadratic element).
%
% Example
%
%   node = [-1,-1,-1; 1,-1,-1; 1,1,-1; -1,1,-1; -1,-1,1; 1,-1,1; 1,1,1; -1,1,1];
%   elem = [1,2,3,7; 1,6,2,7; 1,5,6,7; 1,8,5,7; 1,4,8,7; 1,3,4,7];
%   maxIt = 3;
%   HcurlErr = zeros(maxIt,1);
%   L2Err = zeros(maxIt,1);
%   N = zeros(maxIt,1);
%   for k = 1:maxIt
%       [node,elem] = uniformbisect3(node,elem);
%       [elem2dof,T] = dof3NE2(elem);
%       pde = Maxwelldata2;
%       uI = edgeinterpolate2(pde.exactu,node,T.edge,T.face,T.face2edge);
%       HcurlErr(k) = getHcurlerror3NE2(node,elem,pde.curlu,uI);
%       L2Err(k) = getL2error3NE2(node,elem,pde.exactu,uI);
%       N(k) = length(uI);
%   end
%   figure(1)
%   r1 = showrate(N,HcurlErr,1,'r-+');
%   hold on
%   r2 = showrate(N,L2Err,1,'b-+');
%   legend('||u-u_I||_{curl}',['N^{' num2str(r1) '}'],...
%          '||u-u_I||',['N^{' num2str(r2) '}'],'LOCATION','Best');
%
% See also edgeinterpolate, edgeinterpolate1
%
% Maxwell doc Section: Dirichlet
% boundary condition
%
% Copyright (C) Long Chen. See COPYRIGHT.txt for details.

%% Evaluate function at vertices and middle points
if isnumeric(exactu)
    uQ = exactu;
else
    mid = (node(edge(:,1),:) + node(edge(:,2),:))/2;
    uQ = exactu([node; mid]);
end

%% Edge dof coefficients
N = size(node,1);	NE = size(edge,1);    NF = size(face,1);
edgeVec = node(edge(:,2),:)-node(edge(:,1),:);
uI = zeros(2*(NE+NF),1);
uI(1:NE,1) = dot(edgeVec,(uQ(edge(:,1),:)+uQ(edge(:,2),:)+4*uQ(N+1:N+NE,:))/6,2);
uI(NE+1:2*NE,1) = dot(edgeVec,0.5*(uQ(edge(:,1),:)-uQ(edge(:,2),:)),2);

%% Face dof coefficients
eik = node(face(:,3),:)-node(face(:,1),:);
eij = node(face(:,2),:)-node(face(:,1),:);
uquadpts = uQ(N+face2edge(:,1),:)+uQ(N+face2edge(:,2),:)+uQ(N+face2edge(:,3),:);
lf = [4*dot(eik,uquadpts,2) 4*dot(eij,uquadpts,2)];
face2edgeDofValue = [uI(face2edge(:,1:3)) uI(face2edge(:,1:3)+NE)];
localMatrix = [4 8 4 -4  0 4; 8 4 -4 0 -4 4]';
lf = lf - face2edgeDofValue*localMatrix;
uI((2*NE+1):end) = [2*lf(:,1) - lf(:,2); 2*lf(:,2) - lf(:,1)]/3;


function [distance_precision, PASCAL_precision, average_center_location_error] = ...
    compute_performance_measures(positions, ground_truth, distance_precision_threshold, PASCAL_threshold)

% [distance_precision, PASCAL_precision, average_center_location_error] = ...
%    compute_performance_measures(positions, ground_truth, distance_precision_threshold, PASCAL_threshold)
%
% For the given tracker output positions and ground truth it computes the:
% * Distance Precision at the specified threshold (20 pixels as default if
% omitted)
% * PASCAL Precision at the specified threshold (0.5 as default if omitted)
% * Average Center Location error (CLE).
%
% The tracker positions and ground truth must be Nx4-matrices where N is
% the number of time steps in the tracking. Each row has to be on the form
% [c1, c2, s1, s2] where (c1, c2) is the center coordinate and s1 and s2
% are the size in the first and second dimension respectively (the order of
% x and y does not matter here).

if nargin < 3 || isempty(distance_precision_threshold)
    distance_precision_threshold = 20;
end
if nargin < 4 || isempty(PASCAL_threshold)
    PASCAL_threshold = 0.5;
end

if size(positions,1) ~= size(ground_truth,1),
    disp('Could not calculate precisions, because the number of ground')
    disp('truth frames does not match the number of tracked frames.')
    return
end

%calculate distances to ground truth over all frames
distances = sqrt((positions(:,1) - ground_truth(:,1)).^2 + ...
    (positions(:,2) - ground_truth(:,2)).^2);
distances(isnan(distances)) = [];

%calculate distance precision
distance_precision = nnz(distances < distance_precision_threshold) / numel(distances);

%calculate average center location error (CLE)
average_center_location_error = mean(distances);

%calculate the overlap in each dimension
overlap_height = min(positions(:,1) + positions(:,3)/2, ground_truth(:,1) + ground_truth(:,3)/2) ...
    - max(positions(:,1) - positions(:,3)/2, ground_truth(:,1) - ground_truth(:,3)/2);
overlap_width = min(positions(:,2) + positions(:,4)/2, ground_truth(:,2) + ground_truth(:,4)/2) ...
    - max(positions(:,2) - positions(:,4)/2, ground_truth(:,2) - ground_truth(:,4)/2);

% if no overlap, set to zero
overlap_height(overlap_height < 0) = 0;
overlap_width(overlap_width < 0) = 0;

% remove NaN values (should not exist any)
valid_ind = ~isnan(overlap_height) & ~isnan(overlap_width);

% calculate area
overlap_area = overlap_height(valid_ind) .* overlap_width(valid_ind);
tracked_area = positions(valid_ind,3) .* positions(valid_ind,4);
ground_truth_area = ground_truth(valid_ind,3) .* ground_truth(valid_ind,4);

% calculate PASCAL overlaps
overlaps = overlap_area ./ (tracked_area + ground_truth_area - overlap_area);
%-lx calculate the precision with different threshold(1-50)
pre_o=zeros(1,100);
pre_d=zeros(1,50);
for thre=1:100
pre_o(thre)=nnz(overlaps >=(thre/100))/numel(overlaps);
end

for thre=1:50
pre_d(thre)=nnz(distances < thre) / numel(distances);
end
% figure,plot([1:100]/100,pre_o);
PASCAL_precision=sum(pre_o)/100;
%save  'result.mat' pre_o pre_d;
%-lx 2015-7-1
% calculate PASCAL precision
%PASCAL_precision = nnz(overlaps >= PASCAL_threshold) / numel(overlaps);

end


function [U,H] = poldec(f)
%POLDEC   Polar decomposition of a CHEBFUN2.
%   [U, H] = POLDEC(F) computes chebfun2 objects U and H such that F = U*H.
%   The domain of U is the same as that of F and it is a partial isometry,
%   which means all its singular values are 1 (a finite number) or 0.
%   The domain of H is the square [a,b]x[a,b] where [a,b] is the
%   x-domain of F, and H is Hermitian positive semidefinite (its
%   eigenvalues are all positive).
%
% See also SVD.

% Copyright 2017 by The University of Oxford and The Chebfun Developers.
% See http://www.chebfun.org/ for Chebfun information.

[u, S, v] = svd(f); % SVD of chebfun2
U = u*v';
H = v*S*v';

if ( nargout > 1 )
    varargout = { U, H };
else
    varargout = { U };
end

end