Class MatrixTools
Assembly: TowseyLibrary.dll
public static class MatrixTools
Methods
|
Improve this Doc
View Source
Add rows of nan to pad out a matrix.
Declaration
public static double[, ] AddBlankRows(double[, ] m1, int number)
Parameters
Returns
|
Improve this Doc
View Source
ADD matrix m2 to matrix m1.
Declaration
public static double[, ] AddMatrices(double[, ] m1, double[, ] m2)
Parameters
Returns
|
Improve this Doc
View Source
Adds two matrices using weighted sum.
Typically expected that that w1 + w2 = 0 and both matrices are normalised.
Declaration
public static double[, ] AddMatricesWeightedSum(double[, ] m1, double w1, double[, ] m2, double w2)
Parameters
Returns
|
Improve this Doc
View Source
converts a vector to a matrix in the direction of column.
For example, the "Matrix2Array" method in MatrixTools.cs builds the vector by concatenating the columns.
Declaration
public static double[, ] ArrayToMatrixByColumn(double[] vector, int columnSize, int rowSize)
Parameters
Returns
|
Improve this Doc
View Source
converts a vector to a matrix in the direction of row.
Declaration
public static double[, ] ArrayToMatrixByRow(double[] vector, int columnSize, int rowSize)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static void AverageValuesInTriangleAboveAndBelowNegativeDiagonal(double[, ] m, out double upperAv, out double lowerAv)
Parameters
|
Improve this Doc
View Source
Declaration
public static void AverageValuesInTriangleAboveAndBelowPositiveDiagonal(double[, ] m, out double upperAv, out double lowerAv)
Parameters
|
Improve this Doc
View Source
bounds a matrix of numbers between a minimum and a maximum.
Numbers that fall outside the bound are truncated to the bound.
Declaration
public static double[, ] BoundMatrix(double[, ] matrix, double min, double max)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
the matrix to be bound.
|
Double |
min |
The minimum bound.
|
Double |
max |
The maximum bound.
|
Returns
|
Improve this Doc
View Source
Declaration
public static bool CentreIsLocalMaximum(double[, ] m, double threshold)
Parameters
Returns
|
Improve this Doc
View Source
TODO: This method concatenates time-sequence data but does not check that the files are in temporal sequence.
Nor does it check for temporal gaps.
This method assumes that the column count for each matrix in list is identical.
Declaration
public static double[, ] ConcatenateMatrixRows(List<double[, ]> list)
Parameters
Returns
|
Improve this Doc
View Source
Concatenates two matrices that have the same column count.
That is, each row of the output matrix is the join of the equivalent two rows of the input matrices.
Declaration
public static double[, ] ConcatenateMatrixRows(double[, ] m1, double[, ] m2)
Parameters
Returns
|
Improve this Doc
View Source
Concatenates two matrices that have the same row count.
That is, each row of the output matrix is the join of the equivalent two rows of the input matrices.
WARNING: If the two matrices do not have the same number of rows, an exception is thrown.
Declaration
public static double[, ] ConcatenateTwoMatrices(double[, ] matrix1, double[, ] matrix2)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] ConvertList2Matrix(List<double[]> list)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] ConvertMatrixOfByte2Double(byte[, ] matrix)
Parameters
Type |
Name |
Description |
Byte[,] |
matrix |
|
Returns
|
Improve this Doc
View Source
This method assumes that the passed matrix of double already takes values between 0.0 and 1.0.
Declaration
public static byte[, ] ConvertMatrixOfDouble2Byte(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
Returns
|
Improve this Doc
View Source
DIVIDE matrix m1 by factor.
Declaration
public static double[, ] DivideMatrix(double[, ] m1, double factor)
Parameters
Returns
|
Improve this Doc
View Source
Multiplies two matrices by summing m1[r,c]*m2[r,c].
Declaration
public static double DotProduct(double[, ] m1, double[, ] m2)
Parameters
Returns
|
Improve this Doc
View Source
returns EUCLIDIAN DISTANCE BETWEEN two matrices.
Declaration
public static double EuclidianDistance(double[, ] m1, double[, ] m2)
Parameters
Returns
|
Improve this Doc
View Source
The ColourFilter parameter determines how much the low index values are emphasized or de-emphasized.
The purpose is to make low intensity features stand out (emphasis) or become even less obvious (de-emphasis).
This parameter applies a function that lies between y=x^-2 and y=x^2, i.e. between the square-root and the square.
For an acoustic index value of X, newX = [(1/c - 1) * X^2 + X] * c, where c = the supplied filterCoeff.
When filterCoeff = 1.0, small values are maximally emphasized, i.e. y=sqrt(x).
When filterCoeff = 0.0, the matrix remains unchanged, that is, y=x.
When filterCoeff =-1.0, small values are maximally de-emphasized, i.e. y=x^2.
Generally usage suggests that a value of -0.25 is suitable. i.e. a slight de-emphasis.
..
Visual example https://www.wolframalpha.com/input/?i=plot+y+%3D+%5B(1%2Fc+-+1)+*+x%5E2+%2B+x%5D+*+c+,+x%3D0..1,+c%3D0.0..2.0.
Declaration
public static double[, ] FilterBackgroundValues(double[, ] m, double filterCoeff)
Parameters
Returns
|
Improve this Doc
View Source
Adds a frame around a matrix by adding row and columns of zeros.
Declaration
public static double[, ] FrameMatrixWithZeros(double[, ] matrix, int frameWidth)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
matrix.
|
Int32 |
frameWidth |
The number of rows/columns of zeros to be added.
|
Returns
|
Improve this Doc
View Source
Declaration
public static byte[] GetColumn(byte[, ] m, int columnIndex)
Parameters
Type |
Name |
Description |
Byte[,] |
m |
|
Int32 |
columnIndex |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetColumn(double[, ] m, int columnIndex)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetColumnAverages(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetColumnMedians(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetColumnSums(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetMaximumColumnValues(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetMaximumRowValues(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetRow(double[, ] m, int rowId)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetRowAverages(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Returns an array of row averages in the submatrix of passed matrix.
This method combines two methods, Submatrix() & GetRowAverages(), for efficiency
Assume that RowTop LT RowBottom, ColumnLeft LT ColumnRight.
Row, column indices start at 0.
Declaration
public static double[] GetRowAveragesOfSubmatrix(double[, ] m, int r1, int c1, int r2, int c2)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double GetRowSum(double[, ] m, int rowId)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[] GetRowSums(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] LogTransform(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
Returns
|
Improve this Doc
View Source
Converts a matrix to a vector by concatenating its columns.
Declaration
public static double[] Matrix2Array(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] Matrix2LogValues(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] Matrix2ZScores(double[, ] matrix, double av, double sd)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static bool MatrixDimensionsAreEqual(double[, ] m1, double[, ] m2)
Parameters
Returns
|
Improve this Doc
View Source
Rotates a matrix 90 degrees anticlockwise.
Used for Syntactic pattern recognition
Converts Image matrix to Spectrogram data orientation.
Declaration
public static double[, ] MatrixRotate90Anticlockwise(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
the matrix to rotate.
|
Returns
|
Improve this Doc
View Source
Declaration
public static byte[, ] MatrixRotate90Clockwise(byte[, ] m)
Parameters
Type |
Name |
Description |
Byte[,] |
m |
|
Returns
|
Improve this Doc
View Source
Rotates a matrix 90 degrees clockwise.
Declaration
public static double[, ] MatrixRotate90Clockwise(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
the matrix to rotate.
|
Returns
|
Improve this Doc
View Source
Rotates a matrix 90 degrees clockwise.
Declaration
public static int[, ] MatrixRotate90Clockwise(int[, ] m)
Parameters
Type |
Name |
Description |
Int32[,] |
m |
the matrix to rotate.
|
Returns
Type |
Description |
Int32[,] |
|
|
Improve this Doc
View Source
Create a matrix whose values are the max of two passed matrices, m1 and m2.
Declaration
public static double[, ] MaxOfTwoMatrices(double[, ] m1, double[, ] m2)
Parameters
Returns
|
Improve this Doc
View Source
returns the min and max values in a matrix of doubles.
Declaration
public static void MinMax(double[, ] data, out double min, out double max)
Parameters
|
Improve this Doc
View Source
returns the min and max values in a matrix.
Declaration
public static void MinMax(int[, ] data, out int min, out int max)
Parameters
|
Improve this Doc
View Source
Multiplies the values in a matrix by a factor.
Used to convert log-energy values to decibels.
Declaration
public static double[, ] MultiplyMatrixByFactor(double[, ] matrix, double factor)
Parameters
Returns
|
Improve this Doc
View Source
normalises the values in a matrix such that the minimum value
is the average of the edge values.
Truncate thos original values that are below the edge average.
This method is used to NormaliseMatrixValues image templates where there should be no power at the edge.
Declaration
public static double[, ] Normalise_zeroEdge(double[, ] m, double normMin, double normMax)
Parameters
Returns
|
Improve this Doc
View Source
Normalises a matrix so that ---
all values LT passed MIN are truncated to 0
and
all values GT passed MAX are truncated to 1.
Declaration
public static double[, ] NormaliseInZeroOne(double[, ] m, double truncateMin, double truncateMax)
Parameters
Returns
|
Improve this Doc
View Source
Normalises a matrix so that all values lie between 0 and 1.
Min value in matrix set to 0.0.
Max value in matrix is set to 1.0.
Rerturns the min and the max.
Declaration
public static double[, ] NormaliseInZeroOne(double[, ] m, out double min, out double max)
Parameters
Returns
|
Improve this Doc
View Source
Normalises a matrix so that the values in each column lie between 0 and 1.
This method is used in producing mfcc's where all the coefficients are weighted so has to have similar range.
Declaration
public static double[, ] NormaliseMatrixColumns(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Normalises matrix values so that the min and max values become [0,1], respectively.
Declaration
public static double[, ] NormaliseMatrixValues(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
returns the min and max percentile values of the values in passed matrix.
Declaration
public static void PercentileCutoffs(double[, ] matrix, int minPercentile, int maxPercentile, out double minCut, out double maxCut)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
the matrix.
|
Int32 |
minPercentile |
minPercentile.
|
Int32 |
maxPercentile |
maxPercentile.
|
Double |
minCut |
power value equivalent to minPercentile.
|
Double |
maxCut |
power value equivalent to maxPercentile.
|
|
Improve this Doc
View Source
Declaration
public static void PrintMatrix(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
|
Improve this Doc
View Source
Declaration
public static double[, ] RemoveLastNRows(double[, ] m1, int number)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static byte[, ] RemoveOrphanOnesInBinaryMatrix(byte[, ] binary)
Parameters
Type |
Name |
Description |
Byte[,] |
binary |
|
Returns
|
Improve this Doc
View Source
Rescales the values of a matrix so that its in and max values are those passed.
Declaration
public static double[, ] RescaleMatrixBetweenMinAndMax(double[, ] m, double normMin, double normMax)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static void SetColumn(double[, ] m, int colId, double[] array)
Parameters
|
Improve this Doc
View Source
Sets any element in matrix with value> 0.0 to zero if all surrounding elements also = zero.
Declaration
public static void SetDoubletsToZero(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
|
Improve this Doc
View Source
Declaration
public static void SetRow(double[, ] m, int rowId, double[] array)
Parameters
|
Improve this Doc
View Source
Sets any element in matrix with value> 0.0 to zero if all surrounding elements also = zero.
Declaration
public static void SetSingletonsToZero(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
|
Improve this Doc
View Source
This method smooths the columns of a matrix using a moving average filter.
It is useful for smoothing the freqeuncy bins of a spectrogram
where it is assumed that the matrix columns are the frequency bins.
Declaration
public static double[, ] SmoothColumns(double[, ] matrix, int window)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] SmoothRows(double[, ] matrix, int window)
Parameters
Returns
|
Improve this Doc
View Source
Convert the Decibels values in a matrix of spectrogram values to power values.
Assume that all matrix values are positive due to prior noise removal.
Declaration
public static double[, ] SpectrogramDecibels2Power(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
matrix of positive Decibel values.
|
Returns
|
Improve this Doc
View Source
Convert the power values in a matrix of spectrogram values to Decibels using: dB = 10*log10(power).
Assume that all matrix values are positive i.e. due to prior noise removal.
NOTE: This method also returns the min and max decibel values in the passed matrix.
NOTE: A decibel value should be a ratio.
Here the ratio is implied ie it is relative to the value of maximum power in the original normalised signal.
Declaration
public static double[, ] SpectrogramPower2DeciBels(double[, ] m, double powerEpsilon, out double min, out double max)
Parameters
Type |
Name |
Description |
Double[,] |
m |
matrix of positive power values.
|
Double |
powerEpsilon |
|
Double |
min |
min value to be return by out.
|
Double |
max |
max value to be return by out.
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] SquareRootOfValues(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
Returns
|
Improve this Doc
View Source
Squares the values in a matrix.
Primarily used when converting FFT coefficients in amplitude spectrogram to power values.
Declaration
public static double[, ] SquareValues(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
Returns
|
Improve this Doc
View Source
Returns the submatrix of passed matrix.
The returned submatrix includes the rows and column passed as bounds.
Assume that RowTop GT RowBottom, ColumnLeft LT ColumnRight.
Row, column indices start at 0.
Declaration
public static T[, ] Submatrix<T>(T[, ] m, int r1, int c1, int r2, int c2)
Parameters
Type |
Name |
Description |
T[,] |
m |
the parent matrix.
|
Int32 |
r1 |
start row.
|
Int32 |
c1 |
start column.
|
Int32 |
r2 |
end row inclusive.
|
Int32 |
c2 |
end column inclusive.
|
Returns
Type |
Description |
T[,] |
matrix to be returned.
|
Type Parameters
|
Improve this Doc
View Source
Declaration
public static double[, ] SubtractAndTruncate2Zero(double[, ] matrix, double threshold)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] SubtractConstant(double[, ] matrix, double constant)
Parameters
Returns
|
Improve this Doc
View Source
Subtract matrix m2 from matrix m1.
Declaration
public static double[, ] SubtractMatrices(double[, ] m1, double[, ] m2)
Parameters
Returns
|
Improve this Doc
View Source
Noise reduce matrix by subtracting the median value and truncating negative values to zero.
Declaration
public static double[, ] SubtractMedian(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double[, ] SubtractValuesFromOne(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static int SumColumn(byte[, ] m, int colId)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static int SumColumn(int[, ] m, int colId)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[] SumColumns(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double SumNegativeDiagonal(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static double SumPositiveDiagonal(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static int SumRow(int[, ] m, int rowId)
Parameters
Returns
|
Improve this Doc
View Source
Declaration
public static double[] SumRows(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
Returns
|
Improve this Doc
View Source
Declaration
public static void SumTriangleAboveNegativeDiagonal(double[, ] m, out double sum, out int count)
Parameters
|
Improve this Doc
View Source
Declaration
public static void SumTriangleAbovePositiveDiagonal(double[, ] m, out double sum, out int count)
Parameters
|
Improve this Doc
View Source
Declaration
public static void SumTriangleBelowNegativeDiagonal(double[, ] m, out double sum, out int count)
Parameters
|
Improve this Doc
View Source
Declaration
public static void SumTriangleBelowPositiveDiagonal(double[, ] m, out double sum, out int count)
Parameters
|
Improve this Doc
View Source
Declaration
public static byte[, ] ThresholdBinarySpectrum(byte[, ] binary, double[, ] m, double threshold)
Parameters
Returns
|
Improve this Doc
View Source
Converts a matrix of doubles to binary using passed threshold.
Declaration
public static int[, ] ThresholdMatrix2Binary(double[, ] matrix, double threshold)
Parameters
Returns
Type |
Description |
Int32[,] |
|
|
Improve this Doc
View Source
Converts a matrix of doubles to binary using passed threshold.
Declaration
public static double[, ] ThresholdMatrix2RealBinary(double[, ] matrix, double threshold)
Parameters
Returns
|
Improve this Doc
View Source
truncate values below threshold to zero.
Declaration
public static double[, ] Truncate2Zero(double[, ] matrix, double threshold)
Parameters
Returns
|
Improve this Doc
View Source
Writes the r, c location of the maximum valuesin the matrix.
Declaration
public static void WriteLocationOfMaximumValues(double[, ] m)
Parameters
Type |
Name |
Description |
Double[,] |
m |
|
|
Improve this Doc
View Source
Declaration
public static void WriteMatrix(double[, ] matrix)
Parameters
Type |
Name |
Description |
Double[,] |
matrix |
|
|
Improve this Doc
View Source
Declaration
public static void WriteMatrix(double[, ] matrix, string format)
Parameters
|
Improve this Doc
View Source
Declaration
public static void WriteMatrix(int[, ] matrix)
Parameters
Type |
Name |
Description |
Int32[,] |
matrix |
|
|
Improve this Doc
View Source
Declaration
public static void WriteMatrix2File(double[, ] matrix, string fPath)
Parameters