Skip to content

Commit

Permalink
Further work on LewinsRail recogniser
Browse files Browse the repository at this point in the history
  • Loading branch information
towsey committed Dec 14, 2017
1 parent 24bd19c commit 81c013d
Showing 1 changed file with 35 additions and 28 deletions.
63 changes: 35 additions & 28 deletions AudioAnalysis/AnalysisPrograms/Sandpit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -973,21 +973,20 @@ public static void ExtractSpectralFeatures()
@"H:\Documents\SensorNetworks\MyPapers\2017_DavidWatson\CaseStudy1 Liz\MachineLearningExercise";
string fileName = "LizZnidersic_TasmanIsTractor_20151111__Towsey.Acoustic";
string[] indexNames = { "ACI", "ENT", "POW", "SPT", "RHZ" };
var framecount = 1440; // could read this from first matrix but easier to declare it. Need it for reading in tagged data.
int startOffsetMinute = 47; // 24 hours of recording starts at 12:47am. Need this as an offset.
int bottomBin = 3;
int topBin = 22;
int binCount = topBin - bottomBin + 1;

var dict = new Dictionary<string, double[,]>();
var framecount = 0;

// read spectral index matrices, extract required freq band, and store sub-band in dictionary of matrices.
var dict = new Dictionary<string, double[,]>();
foreach (string id in indexNames)
{
var fileinfo = new FileInfo(Path.Combine(dir, $"{fileName}.{id}.csv"));
var matrix = Csv.ReadMatrixFromCsv<double>(fileinfo, TwoDimensionalArray.ColumnMajor);
framecount = matrix.GetLength(1);

//framecount = matrix.GetLength(1);
//Console.WriteLine("\n" + id);
//Console.WriteLine(matrix[3, 0]);
//Console.WriteLine(matrix[3, 1]);
Expand All @@ -1009,9 +1008,15 @@ public static void ExtractSpectralFeatures()
dict.Add(id, normedM);
}

// concatenate feature vectors, one frame at a time. Then add to matrix data set.
// Read in labelling info from Liz Znidersic
string path = Path.Combine(dir, "lerafcis20151111.csv");
var tags = ReadFileOfTagsFromLizZnidersic(path, framecount, startOffsetMinute);

// Concatenate feature vectors, one frame at a time.
// Then add tag at the end.
// Then add to matrix data set.
int featureVectorLength = indexNames.Length * binCount;
var dataSet = new double[framecount, featureVectorLength];
var dataSet = new double[framecount, featureVectorLength + 1];
for (int frame = 0; frame < framecount; frame++)
{
var list = new List<double>();
Expand All @@ -1023,6 +1028,9 @@ public static void ExtractSpectralFeatures()
list.AddRange(featureVector);
}

// add in the tag and then set row in data matrix
list.Add(tags[frame] > 0 ? 1 : 0);

var array = list.ToArray();
MatrixTools.SetRow(dataSet, frame, array);
}
Expand All @@ -1031,46 +1039,45 @@ public static void ExtractSpectralFeatures()
var opFileinfo = new FileInfo(Path.Combine(dir, $"{fileName}.FeatureVectors.csv"));
Csv.WriteMatrixToCsv(opFileinfo, dataSet);

// Read in labelling info from Liz Znidersic
string path = Path.Combine(dir, "lerafcis20151111.csv");
var tags = ReadFileOfTagsFromLizZnidersic(path, framecount, startOffsetMinute);

// save dataset as image
dataSet = MatrixTools.SubtractValuesFromOne(dataSet);
var image = ImageTools.DrawMatrixWithoutNormalisation(MatrixTools.MatrixRotate90Anticlockwise(dataSet));

// add tags to image of feature vectors
var g = Graphics.FromImage(image);
var pen = new Pen(Color.Red);
for (int i = 1; i < tags.Length; i++)
for (int i = 0; i < framecount; i++)
{
if (tags[i] == 0) continue;
if (tags[i] == 1) pen = new Pen(Color.Red);
if (tags[i] == 2) pen = new Pen(Color.Green);
if (tags[i] == 3) pen = new Pen(Color.Blue);

g.DrawLine(pen, i, 0, i, 5);
g.DrawLine(pen, i, 2, i, 5);
g.DrawLine(pen, i, 20, i, 25);
g.DrawLine(pen, i, 40, i, 45);
g.DrawLine(pen, i, 60, i, 65);
}

// add tags to false-colour spectrogram
string path1 = Path.Combine(dir, "LizZnidersic_TasmanIsTractor_20151111__Tagged.png");
var image1 = ImageTools.ReadImage2Bitmap(path1);
var g1 = Graphics.FromImage(image1);
for (int i = 1; i < tags.Length; i++)
{
if (tags[i] == 0) continue;
if (tags[i] == 1) pen = new Pen(Color.Red);
if (tags[i] == 2) pen = new Pen(Color.Green);
if (tags[i] == 3) pen = new Pen(Color.Blue);

g1.DrawLine(pen, i, image1.Height - 18, i, image1.Height - 4);
}
var path3 = Path.Combine(dir, $"{fileName}.FeatureVectors.png");
image.Save(path3);

var path2 = Path.Combine(dir, $"{fileName}.Tagged.png");
image1.Save(path2);
// add tags to false-colour spectrogram
//string path1 = Path.Combine(dir, "LizZnidersic_TasmanIsTractor_20151111__Tagged.png");
//var image1 = ImageTools.ReadImage2Bitmap(path1);
//var g1 = Graphics.FromImage(image1);
//for (int i = 0; i < framecount; i++)
//{
// if (tags[i] == 0) continue;
// if (tags[i] == 1) pen = new Pen(Color.Red);
// if (tags[i] == 2) pen = new Pen(Color.Green);
// if (tags[i] == 3) pen = new Pen(Color.Blue);

// g1.DrawLine(pen, i, image1.Height - 18, i, image1.Height - 4);
//}

//var path2 = Path.Combine(dir, $"{fileName}.Tagged.png");
//image1.Save(path2);

Console.WriteLine("Finished");
Console.ReadLine();
Expand Down Expand Up @@ -1101,7 +1108,7 @@ public static int[] ReadFileOfTagsFromLizZnidersic(string path, int arraySize, i
// get the difficulty of the hit
int hitDifficulty = 1; //easy one
if (words[5].StartsWith("difficult")) hitDifficulty = 2;
if (words[5].StartsWith("very")) hitDifficulty = 3;
if (words[5].StartsWith("very d")) hitDifficulty = 3;

// add to the array
array[minuteCount] = hitDifficulty;
Expand Down

0 comments on commit 81c013d

Please sign in to comment.