text:
function data = get_kernel_data(X,kernel, kernel_params, Xtrain, datatrain);
% nargin >= 4, when computations of the testing data depends on the training data (i.e., with incomplete Cholesky)

switch kernel
	case { 'polynomial','polynomial-mkl','polynomial-bimkl'}
		[n , p ] = size(X);
		q = kernel_params(1);
		data.n = n ;
		data.p = p;
		data.q = q;
		data.qs = (q+1) * ones(1,p);		  % number of kernels per directions
		data.Xs = zeros(n,sum(data.qs));      % storing all dimensions in the same vector
		data.ind_Xs = cell(1,p);
		data.d_Xs = cell(1,p);
		iK = 1;
		for i=1:p
			data.ind_Xs{i} = cell(1,data.qs(i));
			data.d_Xs{i} = zeros(1,data.qs(i));
			jK = iK;
			for j=1:data.qs(i)
				data.ind_Xs{i}{j} = iK;
				data.d_Xs{i}(j) = 1;
				if j==1
					data.Xs(:,iK) = ones(n,1);		% constant term
				else
					data.Xs(:,iK) = X(:,i).^(j-1) * sqrt( 1 / factorial(j-1) * factorial(q) / factorial(q-j+1) );
				end
				iK = iK + 1 ;
			end
			jK = jK + data.qs(i);
		end

	case {'hermite', 'hermite-mkl', 'hermite-bimkl'}
		[n , p ] = size(X);
		q = kernel_params(2);
		alpha = kernel_params(1);
		data.n = n ;
		data.p = p;
		data.q = q;
		data.qs = (q+1) * ones(1,p);		  % number of kernels per directions
		data.Xs = zeros(n,sum(data.qs));      % storing all dimensions in the same vector
		data.ind_Xs = cell(1,p);
		data.d_Xs = cell(1,p);
		iK = 1;
		for i=1:p
			data.ind_Xs{i} = cell(1,data.qs(i));
			data.d_Xs{i} = zeros(1,data.qs(i));
			jK = iK;
			for j=1:data.qs(i)
				data.ind_Xs{i}{j} = iK;
				data.d_Xs{i}(j) = 1;

				if j==1
					data.Xs(:,iK) = ones(n,1);
				else
					data.Xs(:,iK) = hermite_polynomials(j,X(:,i)) * sqrt( (alpha/2)^(j-1) / factorial(j-1) );
				end
				iK = iK + 1 ;
			end
			jK = jK + data.qs(i);
		end

	case {'gauss-hermite', 'gauss-hermite-mkl', 'gauss-hermite-bimkl'}
		[n , p ] = size(X);
		q = kernel_params(3);
		sigma = kernel_params(1);
		b = kernel_params(2);
		a = 1/4/sigma;
		c = sqrt(a*a+2*a*b);
		A = a + b + c;
		data.n = n ;
		data.p = p;
		data.q = q;
		data.qs = (q+1) * ones(1,p);		  % number of kernels per directions
		data.Xs = zeros(n,sum(data.qs));      % storing all dimensions in the same vector
		data.ind_Xs = cell(1,p);
		data.d_Xs = cell(1,p);
		iK = 1;
		for i=1:p
			data.ind_Xs{i} = cell(1,data.qs(i));
			data.d_Xs{i} = zeros(1,data.qs(i));
			jK = iK;
			for j=1:data.qs(i)
				data.ind_Xs{i}{j} = iK;
				data.d_Xs{i}(j) = 1;

				data.Xs(:,iK) =  ( 1 - (b/A)^2).^.25 * hermite_polynomials(j,X(:,i) * sqrt( 2 * c ) ) .* exp( - ( b/A*(a+c) * X(:,i).^ 2 ) ) ...
					* sqrt( (b/A)^(j-1) / ( 2^(j-1) * factorial(j-1) )) ;
				iK = iK + 1 ;
			end
			jK = jK + data.qs(i);
		end

	case {'gauss-hermite-full', 'gauss-hermite-full-mkl', 'gauss-hermite-full-bimkl'}
		% simply the same as 'gauss-hermite', but with the full kernels at
		% the end
		[n , p ] = size(X);
		q = kernel_params(3);
		sigma = kernel_params(1);
		b = kernel_params(2);
		a = 1/4/sigma;
		c = sqrt(a*a+2*a*b);
		A = a + b + c;
		data.n = n ;
		data.p = p;
		data.q = q+1;
		data.qs = (q+2) * ones(1,p);     % number of kernels per directions
		data.Xs = zeros(n,sum(data.qs));      % storing all dimensions in the same vector
		data.ind_Xs = cell(1,p);
		data.d_Xs = cell(1,p);
		iK = 1;
		for i=1:p
			data.ind_Xs{i} = cell(1,data.qs(i));
			data.d_Xs{i} = zeros(1,data.qs(i));
			jK = iK;
			fulldataloc = [];
			for j=1:data.qs(i)-1
				% first kernels as before
				data.ind_Xs{i}{j} = iK;
				data.d_Xs{i}(j) = 1;

				data.Xs(:,iK) =  ( 1 - (b/A)^2).^.25 * hermite_polynomials(j,X(:,i) * sqrt( 2 * c ) ) .* exp( - ( b/A*(a+c) * X(:,i).^ 2 ) ) ...
					* sqrt( (b/A)^(j-1) / ( 2^(j-1) * factorial(j-1) )) ;
				fulldataloc = [ fulldataloc; iK ];

				iK = iK + 1 ;
			end

			if nargin < 4
				% last kernel: first build the kernel matrix, then takes its
				% incomplete Cholesky decomposition

				K = exp( - b * sqdist(X(:,i)',X(:,i)' ) ) - data.Xs(:,fulldataloc)  * data.Xs(:,fulldataloc)';
				[Gf,Pf,mf,residualf] = icd_general(K,1e-3,kernel_params(6));
				If = Pf(1:mf);
				data.Is{i} = If;					% these two for test data

				[temp,Pif] = sort(Pf);
				Gf = Gf(Pif,1:mf);
				data.d_Xs{i}(end) = mf;
				data.ind_Xs{i}{end} = iK:iK+mf-1;
				data.Xs(:,data.ind_Xs{i}{end}) = Gf;
				iK = iK+mf;
			else
				% last kernel for test data! This compute testing data
				K = exp( - b * sqdist(X(:,i)',Xtrain(datatrain.Is{i},i)' ) ) - data.Xs(:,fulldataloc)  * datatrain.Xs(datatrain.Is{i},fulldataloc)';
				data.d_Xs{i}(end) = datatrain.d_Xs{i}(end);
				data.ind_Xs{i}{end} = datatrain.ind_Xs{i}{end};
				data.Xs(:,data.ind_Xs{i}{end}) = K * inv( datatrain.Xs(datatrain.Is{i},data.ind_Xs{i}{end})' ) ;
				iK = iK+data.d_Xs{i}(end);

			end
		end

	case {'anova', 'anova-mkl', 'anova-bimkl'}
		% simply the same as 'gauss-hermite', but with the full kernels at
		% the end
		[n , p ] = size(X);
		b = kernel_params(1);

		data.n = n ;
		data.p = p;
		data.q = 1;
		data.qs = 2 * ones(1,p);     % number of kernels per directions
		data.Xs = zeros(n,sum(data.qs));      % storing all dimensions in the same vector
		data.ind_Xs = cell(1,p);
		data.d_Xs = cell(1,p);
		iK = 1;
		for i=1:p
			% constant term
			data.ind_Xs{i} = cell(1,2);
			data.d_Xs{i} = zeros(1,2);
			data.ind_Xs{i}{1} = iK;
			data.d_Xs{i}(1) = 1;
			data.Xs(:,iK) = ones(size(X,1),1);
			iK = iK + 1 ;

			if nargin < 4
				% first build the kernel matrix, then takes it
				% incomplete Cholesky decomposition

				K = exp( - b * sqdist(X(:,i)',X(:,i)' ) );
				[Gf,Pf,mf,residualf] = icd_general(K,1e-3,kernel_params(4));
				If = Pf(1:mf);

				data.Is{i} = If;					% these two for test data
				[temp,Pif] = sort(Pf);
				Gf = Gf(Pif,1:mf);
				data.d_Xs{i}(2) = mf;
				data.ind_Xs{i}{2} = iK:iK+mf-1;
				data.Xs(:,data.ind_Xs{i}{2}) = Gf;
				iK = iK+mf;
			else
				% last kernel for test!
				K = exp( - b * sqdist(X(:,i)',Xtrain(datatrain.Is{i},i)' ) );
				data.d_Xs{i}(2) = datatrain.d_Xs{i}(2);
				data.ind_Xs{i}{2} = datatrain.ind_Xs{i}{2};
				data.Xs(:,data.ind_Xs{i}{2}) = K * inv( datatrain.Xs(datatrain.Is{i},data.ind_Xs{i}{2})' ) ;
				iK = iK+data.d_Xs{i}(2);

			end
		end

	case {'base kernels', 'base kernels-mkl', 'base kernels-bimkl'}
		p = kernel_params(1);
		if nargin<4
			% train
			n = size(X,1);
			n = floor( sqrt(2 * n) );
		else
			% test
			ntrain = size(Xtrain,1);
			ntrain = floor( sqrt(2 * ntrain) );
			n = size(X,1) / ntrain;
		end
		q = kernel_params(2);
		data.n = n ;
		data.p = p;
		data.q = kernel_params(2);
		data.qs = (q+1) * ones(1,p);			% number of kernels per directions
		data.Xs = zeros(n,sum(data.qs));		% storing all dimensions in the same vector
		data.ind_Xs = cell(1,p);
		data.d_Xs = cell(1,p);
		iK = 1;
		for i=1:p
			% constant term
			data.ind_Xs{i} = cell(1,2);
			data.d_Xs{i} = zeros(1,2);
			data.ind_Xs{i}{1} = iK;
			data.d_Xs{i}(1) = 1;
			data.Xs(:,iK) = ones(n,1);
			iK = iK + 1 ;

			if nargin < 4
				% first build the kernel matrix, then takes it
				% incomplete Cholesky decomposition
				for j=1:q
					K = double( devectorize_single(X(:,i,j)) );

					[Gf,Pf,mf,residualf] = icd_general(K,1e-3,kernel_params(5));
					If = Pf(1:mf);

					data.Is{i}{j+1} = If;					% these two for test data
					[temp,Pif] = sort(Pf);
					Gf = Gf(Pif,1:mf);
					data.d_Xs{i}(j+1) = mf;
					data.ind_Xs{i}{j+1} = iK:iK+mf-1;

					data.Xs(:,data.ind_Xs{i}{j+1}) = Gf;
					iK = iK+mf;
				end
			else
				for j=1:q

					K = double( reshape(X(:,i,j) ,n,ntrain ) );
					K = K(:,datatrain.Is{i}{j+1});
					data.d_Xs{i}(j+1) = datatrain.d_Xs{i}(j+1);
					data.ind_Xs{i}{j+1} = datatrain.ind_Xs{i}{j+1};
					data.Xs(:,data.ind_Xs{i}{j+1}) = K * inv( datatrain.Xs(datatrain.Is{i}{j+1},data.ind_Xs{i}{j+1})' ) ;
					iK = iK+data.d_Xs{i}(j+1);

				end
			end
		end

	case {'spline', 'spline-mkl', 'spline-bimkl'}
		% simply the same as 'gauss-hermite', but with the full kernels at
		% the end
		[n , p ] = size(X);
		data.n = n ;
		data.p = p;
		data.q = 2;
		data.qs = 3 * ones(1,p);     % number of kernels per directions
		data.Xs = zeros(n,sum(data.qs)*30);      % storing all dimensions in the same vector
		data.ind_Xs = cell(1,p);
		data.d_Xs = cell(1,p);
		iK = 1;
		for i=1:p
			% constant term
			data.ind_Xs{i} = cell(1,2);
			data.d_Xs{i} = zeros(1,2);

			data.ind_Xs{i}{1} = iK;
			data.d_Xs{i}(1) = 1;
			data.Xs(:,iK) = ones(size(X,1),1);
			iK = iK + 1 ;

			data.ind_Xs{i}{2} = iK;
			data.d_Xs{i}(2) = 1;
			data.Xs(:,iK) = X(:,i);
			iK = iK + 1 ;

			if nargin < 4
				% first build the kernel matrix, then takes it
				% incomplete Cholesky decomposition
				K = compute_cubic_spline_kernel(X(:,i),X(:,i));
				[Gf,Pf,mf,residualf] = icd_general(K,1e-3,kernel_params(3));
				If = Pf(1:mf);

				data.Is{i} = If;					% these two for test data
				[temp,Pif] = sort(Pf);
				Gf = Gf(Pif,1:mf);
				data.d_Xs{i}(3) = mf;
				data.ind_Xs{i}{3} = iK:iK+mf-1;
				data.Xs(:,data.ind_Xs{i}{3}) = Gf;
				iK = iK+mf;
			else
				% last kernel for test!
				K = compute_cubic_spline_kernel(X(:,i),Xtrain(datatrain.Is{i},i));
				data.d_Xs{i}(3) = datatrain.d_Xs{i}(3);
				data.ind_Xs{i}{3} = datatrain.ind_Xs{i}{3};
				data.Xs(:,data.ind_Xs{i}{3}) = K * inv( datatrain.Xs(datatrain.Is{i},data.ind_Xs{i}{3})' ) ;
				iK = iK+data.d_Xs{i}(3);

			end
		end

		data.Xs(:,iK:end) = [];

end

repo: REST


text:
% --- Executes on button press in multinetdect.
function multinetdect_Callback(hObject, eventdata, handles, SingleDetect)

if isempty(handles.audiofiles)
    errordlg('No Audio Selected')
    return
end
if isempty(handles.networkfiles)
    errordlg('No Network Selected')
    return
end
if exist(handles.data.settings.detectionfolder,'dir')==0
    errordlg('Please Select Output Folder')
    uiwait
    load_detectionFolder_Callback(hObject, eventdata, handles)
    handles = guidata(hObject);  % Get newest version of handles
end

%% Do this if button Multi-Detect is clicked
if ~SingleDetect
    audioselections = listdlg('PromptString','Select Audio Files:','ListSize',[500 300],'ListString',handles.audiofilesnames);
    if isempty(audioselections)
        return
    end
    networkselections = listdlg('PromptString','Select Networks:','ListSize',[500 300],'ListString',handles.networkfilesnames);
    if isempty(audioselections)
        return
    end

    %% Do this if button Single-Detect is clicked
elseif SingleDetect
    audioselections = get(handles.AudioFilespopup,'Value');
    networkselections = get(handles.neuralnetworkspopup,'Value');
end

Settings = [];
for k=1:length(networkselections)
    prompt = {'Total Analysis Length (Seconds; 0 = Full Duration)','Frequency Cut Off High (kHZ)','Frequency Cut Off Low (kHZ)','Score Threshold (0-1)','Append Date to FileName (1 = yes)'};
    dlg_title = ['Settings for ' handles.networkfiles(networkselections(k)).name];
    num_lines=[1 100]; options.Resize='off'; options.WindowStyle='modal'; options.Interpreter='tex';
    def = handles.data.settings.detectionSettings;
    current_settings = str2double(inputdlg(prompt,dlg_title,num_lines,def,options));

    if isempty(current_settings) % Stop if user presses cancel
        return
    end

    Settings = [Settings, current_settings];
    handles.data.settings.detectionSettings = sprintfc('%g',Settings(:,1))';
end

if isempty(Settings)
    return
end

% Save the new settings
handles.data.saveSettings();

update_folders(hObject, eventdata, handles);
handles = guidata(hObject);  % Get newest version of handles

%% For Each File
for j = 1:length(audioselections)
    CurrentAudioFile = audioselections(j);
    % For Each Network
    Calls = [];
    for k=1:length(networkselections)
        h = waitbar(0,'Loading neural network...');

        AudioFile = fullfile(handles.audiofiles(CurrentAudioFile).folder,handles.audiofiles(CurrentAudioFile).name);

        networkname = handles.networkfiles(networkselections(k)).name;
        networkpath = fullfile(handles.networkfiles(networkselections(k)).folder,networkname);
        NeuralNetwork=load(networkpath);%get currently selected option from menu
        close(h);

        Calls = [Calls; SqueakDetect(AudioFile,NeuralNetwork,handles.audiofiles(CurrentAudioFile).name,Settings(:,k),j,length(audioselections),networkname)];

    end

    [~,audioname] = fileparts(AudioFile);
    detectiontime=datestr(datetime('now'),'yyyy-mm-dd HH_MM PM');

    if isempty(Calls)
        fprintf(1,'No Calls found in: %s \n',audioname)
        continue
    end

    h = waitbar(1,'Saving...');
    Calls = Automerge_Callback(Calls, [], AudioFile);

    %% Save the file
    % Save the Call table, detection metadata, and results of audioinfo

    % Append date to filename
    if Settings(5)
        fname = fullfile(handles.data.settings.detectionfolder,[audioname ' ' detectiontime '.mat']);
    else
        fname = fullfile(handles.data.settings.detectionfolder,[audioname '.mat']);
    end

    % Display the number of calls
    fprintf(1,'%d Calls found in: %s \n',height(Calls),audioname)

    if ~isempty(Calls)
        detection_metadata = struct(...
            'Settings', Settings,...
            'detectiontime', detectiontime,...
            'networkselections', {handles.networkfiles(networkselections).name});
        audiodata = audioinfo(AudioFile);
        save(fname,'Calls', 'detection_metadata', 'audiodata' ,'-v7.3', '-mat');
    end

    delete(h)
end
update_folders(hObject, eventdata, handles);
guidata(hObject, handles);

author: DrCoffey

repo: DeepSqueak

save_path: github-repos/MATLAB/DrCoffey-DeepSqueak


text:
function determ = ring_adj_determinant ( n )

%*****************************************************************************80
%
%% RING_ADJ_DETERMINANT returns the determinant of the RING_ADJ matrix.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    20 October 2007
%
%  Author:
%
%    John Burkardt
%
%  Parameters:
%
%    Input, integer N, the order of A.
%
%    Output, real DETERM, the determinant.
%
  if ( n == 1 )
    determ = 1.0;
  elseif ( n == 2 )
    determ = -1.0;
  elseif ( mod ( n, 4 ) == 0 )
    determ = 0.0;
  elseif ( mod ( n, 4 ) == 1 )
    determ = 2.0;
  elseif ( mod ( n, 4 ) == 2 )
    determ = -4.0;
  elseif ( mod ( n, 4 ) == 3 )
    determ = 2.0;
  end

  return
end

author: johannesgerer

repo: jburkardt-m


text:
function X = abs(X)
%ABS (overloaded)

X = reshape(abs(reshape(X,prod(X.dim),1)),X.dim);

author: yalmip

repo: YALMIP

save_path: github-repos/MATLAB/yalmip-YALMIP


text:
function chrpak_test022 ( )

%*****************************************************************************80
%
%% TEST022 tests I4_TO_HEX_DIGIT and HEX_DIGIT_TO_I4.
%
%  Licensing:
%
%    This code is distributed under the GNU LGPL license.
%
%  Modified:
%
%    31 August 2009.
%
%  Author:
%
%    John Burkardt
%
  fprintf ( 1, '\n' );
  fprintf ( 1, 'TEST022\n' );
  fprintf ( 1, '  I4_TO_HEX_DIGIT: I4 -> hexadecimal digit\n' );
  fprintf ( 1, '  HEX_DIGIT_TO_I4: hexadecimal digit -> I4.\n' );
  fprintf ( 1, '\n' );

  for i = -2 : 11
    ch = i4_to_hex_digit ( i );
    i2 = hex_digit_to_i4 ( ch );
    fprintf ( 1, '  %8d  "%c"  %8d\n', i, ch, i2 );
  end

  return
end


text:
% In-paints the depth image using a cross-bilateral filter. The operation
% is implemented via several filterings at various scales. The number of
% scales is determined by the number of spacial and range sigmas provided.
% 3 spacial/range sigmas translated into filtering at 3 scales.
%
% Args:
%   imgRgb - the RGB image, a uint8 HxWx3 matrix
%   imgDepthAbs - the absolute depth map, a HxW double matrix whose values
%                 indicate depth in meters.
%   spaceSigmas - (optional) sigmas for the spacial gaussian term.
%   rangeSigmas - (optional) sigmas for the intensity gaussian term.
%
% Returns:
%    imgDepthAbs - the inpainted depth image.
function imgDepthAbs = fill_depth_cross_bfx(imgRgb, imgDepthAbs, mask, ...
    spaceSigmas, rangeSigmas)

  error(nargchk(2,4,nargin));
  assert(isa(imgRgb, 'uint8'), 'imgRgb must be uint8');
  assert(isa(imgDepthAbs, 'double'), 'imgDepthAbs must be a double');

  if nargin < 4
    spaceSigmas = [12 5 8];
  end
  if nargin < 5
    rangeSigmas = [0.2 0.08 0.02];
  end

  assert(numel(spaceSigmas) == numel(rangeSigmas));
  assert(isa(rangeSigmas, 'double'));
  assert(isa(spaceSigmas, 'double'));

  % Create the 'noise' image and get the maximum observed depth.
  maxv = max(imgDepthAbs(~mask));
  minv = min(imgDepthAbs(~mask));

  % Convert the depth image to uint8.
  imgDepth = (imgDepthAbs - minv) ./ (maxv - minv);
  imgDepth = uint8(imgDepth * 255);

  % Run the cross-bilateral filter.
  imgDepthAbs = mex_cbf(imgDepth, rgb2gray(imgRgb), mask, spaceSigmas(:), rangeSigmas(:));

  % Convert back to absolute depth (meters).
  imgDepthAbs = im2double(imgDepthAbs) .* (maxv - minv) + minv;
end

author: jianxiongxiao

repo: ProfXkit