Skip to content

Commit

Permalink
Added repmatmatch.m and various other improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobD10 committed Aug 28, 2017
1 parent c52e5f3 commit 425afb3
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 76 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ _MiKTeX\_FNDB\_Refresh.m_ | Function to refresh the File Name DataBase in MiKTeX
_octaveBandMean.m_ | Given a magnitude spectrum this function will calculate the average (single, third, nth) octave band magnitude
_pesq2.m_ | Objective speech quality measure
_pesq3.m_ | A wrapper for the objective Perceptual Evaluation of Speech Quality measure
_pesq\_mex\_fast\_vec.m_ | Accepts vectors for a mex compiled version of the objective Perceptual Evaluation of Speech Quality measure
_pesq\_mex\_vec.m_ | Accepts vectors for a mex compiled version of the objective Perceptual Evaluation of Speech Quality measure
_printHyperlink.m_ | Prints a hyperlink to the command window
_repmatmatch.m_ | Replicate and tile an array to match the size of a given N-D array
_shapeSpectrum.m_ | This function will shape an input signal to the given spectrum (simple, unregulated spectral shaping)
_showTimeToCompletion.m_ | Prints the time to completion and expected finish of a looped simulation based on linear extrapolation.
_simpleWarning.m_ | Prints a coloured warning without the location information
Expand Down
5 changes: 3 additions & 2 deletions buildReleaseZIP.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
% University of Wollongong
% Email: [email protected]
% Copyright: Jacob Donley 2017
% Date: 07 May 2017
% Version: 0.1 (07 May 2017)
% Date: 09 August 2017
% Version: 1.1 (09 August 2017)
% Version: 1.0 (07 May 2017)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand Down
77 changes: 32 additions & 45 deletions interpVal.m
Original file line number Diff line number Diff line change
@@ -1,65 +1,52 @@
function [ interpolated_values, interpolated_indices] = interpVal( values, index_values, desired_index_values )
% This function will interpolate from desired arbitrarily spaced index values
%
%
% Syntax: [ interpolated_values, interpolated_indices] = interpVal( values, index_values, desired_index_values )
%
% Inputs:
%
% Inputs:
% values - A 1D array of values to interpolate between
% index_values - The axis values of the array
% desired_index_values - The desired axis values to interpolate to
% (Can be spaced abitrarily)
%
% Outputs:
%
% Outputs:
% interpolated_values - The new interpolated values
% interpolated_indices - The new interpolated indices
%
% Example:
%
% See also: List related files here
%
% Example:
%
% See also: interp1

% Author: Jacob Donley
% University of Wollongong
% Email: [email protected]
% Copyright: Jacob Donley 2017
% Date: 03 October 2015
% Revision: 0.1
%
% Copyright: Jacob Donley 2015-2017
% Date: 15 August 2017
% Version: 0.2 (15 August 2017)
% Version: 0.1 (03 October 2015)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

interpolated_indices = zeros(length(desired_index_values),1);
% center desireable value
temp = repmat(index_values, [length(desired_index_values) 1]) ...
- repmat(desired_index_values', [1 length(index_values)]);
temp(temp > 0) = NaN;

% interpolated_values = zeros(length(desired_index_values),1);
% for div = 1:length(desired_index_values)
% temp = index_values - desired_index_values( div ); % center desireable value
% temp(temp >= 0) = NaN;
% [~, i_low] = min(abs(temp));
% if i_low ~= length(temp)
% i_high = i_low+1;
% interpolated_indices( div ) = (desired_index_values(1, div) - index_values(i_low)) / ...
% (index_values(i_high) - index_values(i_low)) + i_low;
% else
% interpolated_indices( div ) = i_low;
% end
% interpolated_values( div ) = interp1(values, interpolated_indices( div ));
% end
% a = interpolated_indices;
% b = interpolated_values;
N = numel(index_values);
[~, i_low] = min(abs(temp), [], 2);
i_low = (i_low - (i_low>N).*(i_low-N))';
i_high = i_low + (N~=1);
i_high(i_high>N) = N;

temp = repmat(index_values, [length(desired_index_values) 1]) - repmat(desired_index_values', [1 length(index_values)]); % center desireable value
temp(temp >= 0) = NaN;
[~, i_low] = min(abs(temp), [], 2);
i_low = i_low';
i_high = zeros(size(i_low));
i_high(i_low ~= length(temp)) = i_low(i_low ~= length(temp)) + 1;

interpolated_indices(i_low ~= length(temp)) = (desired_index_values - index_values(i_low(i_low ~= length(temp)))) ./ ...
(index_values(i_high(i_low ~= length(temp))) - index_values(i_low(i_low ~= length(temp)))) + i_low(i_low ~= length(temp));

interpolated_indices(i_low == length(temp)) = i_low(i_low == length(temp));

num = (desired_index_values - index_values(i_low));
den = (index_values(i_high) - index_values(i_low));

div = num ./ den;
div(num==0&den==0)=0; % redefine zero devided by zero as 0/0=0

interpolated_indices = div + i_low;

interpolated_values = interp1(values, interpolated_indices);

interpolated_values = interp1(values, interpolated_indices);


end

56 changes: 38 additions & 18 deletions interpVal_2D.m
Original file line number Diff line number Diff line change
@@ -1,56 +1,76 @@
function [ interpolated_values ] = interpVal_2D( values, index_values1, index_values2, desired_index_values1, desired_index_values2, interpolation_type )
% This function will interpolate from desired abitrarily spaced index values in a 2D array
%
% Syntax: [ interpolated_values ] = interpVal_2D( values, index_values1, index_values2, desired_index_values1, desired_index_values2, interpolation_type )
% Syntax: [ interpolated_values ] = interpVal_2D( ...
% values, ...
% index_values1, index_values2, ...
% desired_index_values1, desired_index_values2, ...
% interpolation_type )
%
% Inputs:
% values - A 2D matrix of Z-axis values to interpolate between
% index_values1 - The x-axis values of the matrix
% index_values2 - The y-axis values of the matrix
% values - A 2D matrix of Z-axis values to interpolate
% between
% index_values1 - The x-axis values of the matrix
% index_values2 - The y-axis values of the matrix
% desired_index_values1 - The desired x-axis values to interpolate to
% (Can be spaced abitrarily)
% (Can be spaced abitrarily)
% desired_index_values2 - The desired y-axis values to interpolate to
% (Can be spaced abitrarily)
% interpolation_type - An interpolation type supported by interp2 (e.g.
% 'linear', 'nearest', 'cubic' or 'spline')
% (Can be spaced abitrarily)
% interpolation_type - An interpolation type supported by interp2
% (e.g. 'linear', 'nearest', 'cubic' or 'spline')
%
% Outputs:
% interpolated_values - The new interpolated values
% interpolated_values - The new interpolated values
%
% Example:
%
% See also: List related files here
% See also: interp2

% Author: Jacob Donley
% University of Wollongong
% Email: [email protected]
% Copyright: Jacob Donley 2017
% Date: 03 October 2015
% Revision: 0.1
% Copyright: Jacob Donley 2015-2017
% Date: 03 October 2015
% Revision: 0.1 (03 October 2015)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin < 6
interpolation_type = 'linear';
end

% Just incase this function tries to call interVal within a class folder we
% should create a function handle for interpVal regardless
%% Just incase this function tries to call interpVal within a class folder
% we should create a function handle for interpVal regardless
inf = dbstack('-completenames');
funcName = 'interpVal';
funcPath = inf.file;
classDirs = getClassDirs(funcPath);
interpVal_ = str2func([classDirs funcName]);


% Start interpolation procedure
%% Start interpolation procedure
interpolated_indices1 = zeros(length(desired_index_values1) + 1,1);
interpolated_indices2 = zeros(length(desired_index_values2) + 1,1);

[~, interpolated_indices1] = interpVal_(interpolated_indices1, index_values1, desired_index_values1);
[~, interpolated_indices2] = interpVal_(interpolated_indices2, index_values2, desired_index_values2);

%interpolated_values = interp2(values, interpolated_indices1, interpolated_indices2);
interpolated_values = interp2(values, interpolated_indices1, interpolated_indices2, interpolation_type);
if any(diff(interpolated_indices1)) && any(diff(interpolated_indices2))
% if interpolation is possible over both dimensions
interpolated_values = interp2(values, ...
interpolated_indices1, interpolated_indices2, interpolation_type);

elseif ~any(diff(interpolated_indices1))
% if interpolation is not possible along 1st dimension
interpolated_values = interp1(values(:,interpolated_indices1(1)), ...
interpolated_indices2, interpolation_type);

elseif ~any(diff(interpolated_indices2))
% if interpolation is not possible along 2nd dimension
interpolated_values = interp1(values(interpolated_indices2(1),:), ...
interpolated_indices1, interpolation_type);

end


end

Expand Down
39 changes: 39 additions & 0 deletions repmatmatch.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function A = repmatmatch(a,B)
%Replicate and tile an array to match the size of a given N-D array
%
% Syntax: A = repmatmatch(a,B)
%
% Inputs:
% a - Input array to tile
% B - N-D array to match the size of
%
% Outputs:
% A - The replicated and tiled copy of input 'a'
%
% Example:
% a = [1 2 3];
% B = rand(2,3);
% A = repmatmatch(a,B);
%
% See also: repmat

% Author: Jacob Donley
% University of Wollongong
% Email: [email protected]
% Copyright: Jacob Donley 2017
% Date: 28 August 2017
% Version: 0.1 (28 August 2017)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

aSz = size(a);
BSz = size(B);

if any(rem(BSz ./ aSz,1))
error(['The size of each dimension of ''a'' should match ''B'' ' ...
'or be evenly divisible by the corresponding dimension of ''B''.'])
end

A = repmat(a, BSz ./ aSz);

end
69 changes: 58 additions & 11 deletions showTimeToCompletion.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function [ num_curr_char, history_ ] = showTimeToCompletion( percent_complete, num_prev_char, history_ )
function [ num_curr_char, history_ ] = showTimeToCompletion( percent_complete, num_prev_char, history_, startTime )
% Prints the time to completion and expected finish of a looped simulation based on linear extrapolation.
%
% Syntax: [ num_curr_char ] = showTimeToCompletion( percent_complete, num_prev_char )
Expand All @@ -18,29 +18,68 @@
% function calls.
%
% Example:
% % Example 1
% fprintf('\t Completion: ');
% n=0; tic;
% len=1e2;
% for i = 1:len
% pause(1);
% n = showTimeToCompletion( i/len, n);
% end
%
% % Example 2
% fprintf('\t Completion: ');
% showTimeToCompletion; tic;
% len=1e2;
% for i = 1:len
% pause(1);
% showTimeToCompletion( i/len );
% end
%
% % Example 3
% fprintf('\t Completion: ');
% showTimeToCompletion; startTime=tic;
% len=1e2;
% p = parfor_progress( len );
% parfor i = 1:len
% pause(1);
% p = parfor_progress;
% showTimeToCompletion( p/100, [], [], startTime );
% end
%
% See also: tic, toc
% See also: tic, toc, parfor_progress

% Author: Jacob Donley
% University of Wollongong
% Email: [email protected]
% Copyright: Jacob Donley 2017
% Copyright: Jacob Donley 2015-2017
% Date: 25 August 2015
% Revision: 0.1
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

tElapsed = toc;
buffHeight = 4; % number of lines
buffWidth = 40; % number of lines
charSpace = buffHeight*buffWidth;
if nargin == 0
fprintf([repmat( ...
[repmat(' ',1,buffWidth-1) newline], ...
1,buffHeight)]);
return;
end
if nargin == 1 || nargin == 4
num_prev_char = charSpace;
end

if nargin ~=4
tElapsed = toc;
else
tElapsed = toc(startTime);
end
ratio = percent_complete;

if nargin >=3
% TODO: Use stable autoregressive models to provide much better prediction
if nargin == 3
history_ = [history_; tElapsed, ratio];
if size(history_,1) > 10 && ratio < 0.5
t=0:0.001:1;
Expand Down Expand Up @@ -75,7 +114,7 @@
tTot = tElapsed + tRem;

% Begin plot prediction
if nargin >=3
if nargin == 3
t_vec = (history_(:,1)-history_(end,1))/86400 + datenum(datetime);
plot(history_(:,2), t_vec,'ok');hold on;
if size(history_,1) > 10 && ratio < 0.5
Expand All @@ -101,16 +140,24 @@
% End plot prediction

fprintf(repmat('\b',1,num_prev_char));
num_curr_char=fprintf( ...
txt = sprintf( ...
['%.2f%%\n' ...
' Remaining: %s\n' ...
' Total: %s\n' ...
'Expected Finish: %s\n'], ...
'Expected Finish: %s'], ...
ratio * 100, ...
datestr(seconds(tRem),'hh:MM:SS'), ... floor(tRem/60), ... rem(tRem,60), ...
datestr(seconds(tTot),'hh:MM:SS'), ... floor(tTot/60), ... rem(tTot,60), ...
[datestr(seconds(tRem-86400),'hh:MM:SS')], ..., dd'), 'days'], ... floor(tRem/60), ... rem(tRem,60), ...
[datestr(seconds(tTot-86400),'hh:MM:SS')], ..., dd'), 'days'], ... floor(tTot/60), ... rem(tTot,60), ...
[strrep(datestr(datetime + seconds(tRem),'hh:MM:SS AM'),' ',''),' ', ...
datestr(datetime + seconds(tRem),'dd-mmm-yyyy')]);


if nargin == 1 || nargin == 4
txt = [txt repmat(' ',1,charSpace - numel(txt) - 1)];
end
txt(end+1) = newline;

num_curr_char = fprintf('%s',txt);


end

0 comments on commit 425afb3

Please sign in to comment.