Skip to content

Commit

Permalink
Experiment with combined BGN index
Browse files Browse the repository at this point in the history
  • Loading branch information
towsey committed Jun 13, 2019
1 parent b8b39aa commit 37cf217
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ namespace AudioAnalysisTools.LongDurationSpectrograms.Zooming
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using Acoustics.Shared;
using Acoustics.Shared.Contracts;
using Fasterflect;
using Indices;
using TowseyLibrary;

Expand All @@ -30,6 +28,10 @@ public static (Dictionary<string, double[,]>, Dictionary<string, IndexProperties
keys = keys.ToList().Append("SUM");
keys = keys.ToList().Append("DIF");

//add following matrix for possible subsequent BNG combination matrix.
string comboIndexID = "RHZ";
keys = keys.ToList().Append(comboIndexID);

var relevantIndexProperties = keys.ToDictionary(x => x, x => indexProperties[x]);

Dictionary<string, double[,]> spectra = IndexMatrices.ReadSpectralIndices(
Expand All @@ -38,6 +40,11 @@ public static (Dictionary<string, double[,]>, Dictionary<string, IndexProperties
analysisTag,
keys.ToArray());

// Make a BNG COMBINATION Spectral matrix.
//var comboMatrix = MatrixTools.MaxOfTwoMatrices(spectra["BNG"], spectra["RHZ"]);
var comboMatrix = MatrixTools.AddMatricesWeightedSum(spectra["BGN"], 1.0, spectra[comboIndexID], 20.0);
spectra["BGN"] = comboMatrix;

return (spectra, relevantIndexProperties);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ public static void DrawStackOfZoomedSpectrograms(
sw.Stop();
LoggedConsole.WriteLine("Finished spectrograms derived from spectral indices. Elapsed time = " + sw.Elapsed.TotalSeconds + " seconds");

// NOTE: The following code is deprecated. It was originally developed to provide some intermediate steps between the hi-resolution false-colour spectrograms
// and the standard grey scale spectrograms.
// ####################### DERIVE ZOOMED IN SPECTROGRAMS FROM STANDARD SPECTRAL FRAMES

/*
Expand Down
178 changes: 97 additions & 81 deletions src/TowseyLibrary/MatrixTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -976,8 +976,8 @@ public static void WriteMatrix(int[,] matrix)

public static void WriteMatrix2File(double[,] matrix, string fPath)
{
int rowCount = matrix.GetLength(0); //height
int colCount = matrix.GetLength(1); //width
int rowCount = matrix.GetLength(0); // height
int colCount = matrix.GetLength(1); // width
for (int i = 0; i < rowCount; i++)
{
for (int j = 0; j < colCount; j++)
Expand All @@ -994,91 +994,107 @@ public static void WriteMatrix2File(double[,] matrix, string fPath)
}

/// <summary>
/// ADD matrix m2 to matrix m1
/// ADD matrix m2 to matrix m1.
/// </summary>
public static double[,] AddMatrices(double[,] m1, double[,] m2)
{
if (m1 == null)
{
return m2;
}
{
if ((m1 == null) || (m2 == null))
{
return null;
}

if (m2 == null)
{
return m1;
}
int m1Rows = m1.GetLength(0);
int m1Cols = m1.GetLength(1);
int m2Rows = m2.GetLength(0);
int m2Cols = m2.GetLength(1);
if (m1Rows != m2Rows)
{
throw new Exception("ERROR! Matrix dims must be same for matrix addition.");
}

int m1Rows = m1.GetLength(0);
int m1Cols = m1.GetLength(1);
int m2Rows = m2.GetLength(0);
int m2Cols = m2.GetLength(1);
if (m1Rows != m2Rows)
{
throw new Exception("ERROR! Matrix dims must be same for matrix subtraction.");
}
if (m1Cols != m2Cols)
{
throw new Exception("ERROR! Matrix dims must be same for matrix addition.");
}

if (m1Cols != m2Cols)
double[,] newMatrix = (double[,])m1.Clone();
for (int i = 0; i < m1Rows; i++)
{
for (int j = 0; j < m1Cols; j++)
{
throw new Exception("ERROR! Matrix dims must be same for matrix subtraction.");
newMatrix[i, j] = m1[i, j] + m2[i, j];
}
}

double[,] newMatrix = (double[,])m1.Clone();
for (int i = 0; i < m1Rows; i++)
{
for (int j = 0; j < m1Cols; j++)
{
newMatrix[i, j] = m1[i, j] + m2[i, j];
}
}
return newMatrix;
}

return newMatrix;
}
/// <summary>
/// Create a matrix whose values are the max of two passed matrices, m1 and m2.
/// </summary>
public static double[,] MaxOfTwoMatrices(double[,] m1, double[,] m2)
{
if ((m1 == null) || (m2 == null))
{
return null;
}

/// <summary>
/// adds two matrices using weighted sum
/// Typically expected that that w1 + w2 = 0 and both matrices are normalised.
/// </summary>
public static double[,] AddMatricesWeightedSum(double[,] m1, double w1, double[,] m2, double w2)
{
if (m1 == null)
{
return m2;
}
int m1Rows = m1.GetLength(0);
int m1Cols = m1.GetLength(1);
int m2Rows = m2.GetLength(0);
int m2Cols = m2.GetLength(1);
if (m1Rows != m2Rows || m1Cols != m2Cols)
{
throw new Exception("ERROR! Matrix dimensions must be same.");
}

if (m2 == null)
{
return m1;
}
double[,] newMatrix = new double[m1Rows, m1Cols];
for (int i = 0; i < m1Rows; i++)
{
for (int j = 0; j < m1Cols; j++)
{
newMatrix[i, j] = Math.Max(m1[i, j], m2[i, j]);
}
}

int m1Rows = m1.GetLength(0);
int m1Cols = m1.GetLength(1);
int m2Rows = m2.GetLength(0);
int m2Cols = m2.GetLength(1);
if (m1Rows != m2Rows)
{
throw new Exception("ERROR! Matrix dims must be same for matrix subtraction.");
}
return newMatrix;
}

if (m1Cols != m2Cols)
{
throw new Exception("ERROR! Matrix dims must be same for matrix subtraction.");
}
/// <summary>
/// Adds two matrices using weighted sum.
/// Typically expected that that w1 + w2 = 0 and both matrices are normalised.
/// </summary>
public static double[,] AddMatricesWeightedSum(double[,] m1, double w1, double[,] m2, double w2)
{
if ((m1 == null) || (m2 == null))
{
return null;
}

double[,] newMatrix = new double[m1Rows, m1Cols];
for (int i = 0; i < m1Rows; i++)
{
for (int j = 0; j < m1Cols; j++)
{
newMatrix[i, j] = (w1 * m1[i, j]) + (w2 * m2[i, j]);
}
}
int m1Rows = m1.GetLength(0);
int m1Cols = m1.GetLength(1);
int m2Rows = m2.GetLength(0);
int m2Cols = m2.GetLength(1);
if (m1Rows != m2Rows || m1Cols != m2Cols)
{
throw new Exception("ERROR! Matrix dimensions must be same.");
}

return newMatrix;
}
double[,] newMatrix = new double[m1Rows, m1Cols];
for (int i = 0; i < m1Rows; i++)
{
for (int j = 0; j < m1Cols; j++)
{
newMatrix[i, j] = (w1 * m1[i, j]) + (w2 * m2[i, j]);
}
}

/// <summary>
/// DIVIDE matrix m1 by factor
/// </summary>
return newMatrix;
}

/// <summary>
/// DIVIDE matrix m1 by factor.
/// </summary>
public static double[,] DivideMatrix(double[,] m1, double factor)
{
if (m1 == null)
Expand All @@ -1101,24 +1117,24 @@ public static void WriteMatrix2File(double[,] matrix, string fPath)
return newMatrix;
}

/// <summary>
/// Subtract matrix m2 from matrix m1
/// </summary>
/// <summary>
/// Subtract matrix m2 from matrix m1.
/// </summary>
public static double[,] SubtractMatrices(double[,] m1, double[,] m2)
{
{
int m1Rows = m1.GetLength(0);
int m1Cols = m1.GetLength(1);
int m2Rows = m2.GetLength(0);
int m2Cols = m2.GetLength(1);
if (m1Rows != m2Rows)
{
throw new Exception("ERROR! Matrix dims must be same for matrix subtraction.");
}
{
throw new Exception("ERROR! Matrix dims must be same for matrix subtraction.");
}

if (m1Cols != m2Cols)
{
throw new Exception("ERROR! Matrix dims must be same for matrix subtraction.");
}
{
throw new Exception("ERROR! Matrix dims must be same for matrix subtraction.");
}

double[,] newMatrix = (double[,])m1.Clone();
for (int i = 0; i < m1Rows; i++)
Expand Down

0 comments on commit 37cf217

Please sign in to comment.