-
Notifications
You must be signed in to change notification settings - Fork 0
/
augmatrix.m
57 lines (49 loc) · 1.85 KB
/
augmatrix.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
% AUGMATRIX.M
% function MatrixOut = augmatrix(MatrixIn,nextra)
% Extend a square matrix of column vectors in 'MatrixIn' by 'nextra'
% rows at the start and end of each vector. The original vectors
% are assumed to be predictable in structure and have a local
% maximum or minimum near the diagonal element (e.g. for
% cochlear implant EFI waveforms). The added data points will
% be determined by either mirroring the data around the diagonal
% element of the original matrix, or by linear interpolation.
% 'MatrixIn' must be square and have at least 7 rows, and 'nextra'
% can't be more than half the number of rows.
% This function was designed for augmenting EFI matrices.
%
function MatrixOut = augmatrix(MatrixIn,nextra)
[nRow,nCol] = size(MatrixIn);
if ndims(MatrixIn) > 2
error('Input matrix has too many dimensions.');
elseif nRow ~= nCol
error('Input matrix must be square.');
elseif nRow < 7
error('The number of rows of the input matrix is too small.');
elseif mod(nextra,1) ~= 0
error('The number of augmented rows, ''nextra'', must be an integer.');
elseif nextra > nRow/2
error('The number of augmented rows, ''nextra'', is too large.');
elseif nextra < 1
error('The number of augmented rows, ''nextra'', must be at least one.');
end;
MatrixOut = nan(nRow + nextra*2, nCol);
for i = 1:nCol
origvec = MatrixIn(:,i);
if i < 4
a = i + i; b = a + (nextra - 1);
avec = origvec(b:-1:a);
else
% pp = polyfit(1:i-1,origvec(1:i-1)',1);
pp = polyfit(1:3,origvec(1:3)',1);
avec = polyval(pp,-(nextra-1):0)';
end;
if i > nRow - 3
b = i - (nRow - i) - 1; a = b - (nextra - 1);
bvec = origvec(b:-1:a);
else
% pp = polyfit(i+1:nRow,origvec(i+1:nRow)',1);
pp = polyfit(nRow-2:nRow,origvec(nRow-2:nRow)',1);
bvec = polyval(pp,nRow+1:nRow+nextra)';
end;
MatrixOut(:,i) = cat(1,avec,origvec,bvec);
end;