/// <summary> /// Parses a Mirage.Vector from a string. /// </summary> /// <param name="input"> /// A <see cref="System.String"/> representation of a vector /// </param> /// <returns> /// A <see cref="Mirage.Vector"/> /// </returns> public static Mirage.Vector ParseMirageVector (string input) { input = input.Substring (1, input.Length - 2); string[] rows = input.Split(';'); Mirage.Vector v = new Mirage.Vector (rows.Length); try { for (int i = 0; i < rows.Length; i++) { v.d [i, 0] = float.Parse(rows [i], System.Globalization.CultureInfo.InvariantCulture); } } catch (Exception e) { Log.Exception("NoNoise/DB - Mirage.Vector parse exception", e); return null; } return v; }
/// <summary> /// Computes the median for each row of the MFCC matrix. /// </summary> /// <param name="mfcc"> /// The MFCC <see cref="Mirage.Matrix"/> /// </param> /// <returns> /// A <see cref="Mirage.Vector"/> containing the median /// vector of the MFCC matrix /// </returns> private Mirage.Vector ConvertMfccToMedian (Mirage.Matrix mfcc) { Mirage.Vector data = new Mirage.Vector (mfcc.rows); for (int i = 0; i < mfcc.rows; i++) { float [] r = new float [mfcc.columns]; for (int j = 0; j < mfcc.columns; j++) { r [j] = mfcc.d [i, j]; } Array.Sort<float> (r); data.d [i, 0] = r [r.Length / 2]; } return data; }
/// <summary> /// Computes the minimum for each row of the MFCC matrix. /// </summary> /// <param name="mfcc"> /// The MFCC <see cref="Mirage.Matrix"/> /// </param> /// <returns> /// A <see cref="Mirage.Vector"/> containing the minimum /// vector of the MFCC matrix /// </returns> private Mirage.Vector ConvertMfccToMin (Mirage.Matrix mfcc) { Mirage.Vector data = new Mirage.Vector (mfcc.rows); for (int i = 0; i < mfcc.rows; i++) { float min = float.PositiveInfinity; for (int j = 0; j < mfcc.columns; j++) { min = Math.Min (min, mfcc.d [i, j]); } data.d [i, 0] = min; } return data; }
/// <summary> /// Computes the maximum for each row of the MFCC matrix. /// </summary> /// <param name="mfcc"> /// The MFCC <see cref="Mirage.Matrix"/> /// </param> /// <returns> /// A <see cref="Mirage.Vector"/> containing the maximum /// vector of the MFCC matrix /// </returns> private Mirage.Vector ConvertMfccToMax (Mirage.Matrix mfcc) { Mirage.Vector data = new Mirage.Vector (mfcc.rows); for (int i = 0; i < mfcc.rows; i++) { float max = float.NegativeInfinity; for (int j = 0; j < mfcc.columns; j++) { max = Math.Max (max, mfcc.d [i, j]); } data.d [i, 0] = max; } return data; }
/// <summary> /// Computes the squared mean for each row of the MFCC matrix. /// </summary> /// <param name="mfcc"> /// The MFCC <see cref="Mirage.Matrix"/> /// </param> /// <returns> /// A <see cref="Mirage.Vector"/> containing the squared mean /// vector of the MFCC matrix /// </returns> private Mirage.Vector ConvertMfccToSqrMean (Mirage.Matrix mfcc) { Mirage.Vector data = new Mirage.Vector (mfcc.rows); for (int i = 0; i < mfcc.rows; i++) { data.d [i, 0] = 0; for (int j = 0; j < mfcc.columns; j++) { data.d [i, 0] += (float) Math.Pow (mfcc.d [i, j], 2); } data.d [i, 0] = (float) Math.Sqrt (data.d [i, 0]); } return data; }
/// <summary> /// Test method for insert/select of a Mirage.Vector into/from the database. /// </summary> /// <returns> /// True if the vector is still the same after inserting it into the /// database and reading it from there again. False otherwise. /// </returns> private bool DBMirageVectorTest() { Mirage.Vector v = new Mirage.Vector (20); Random r = new Random (); for (int i = 0; i < v.rows; i++) { v.d [i, 0] = (float) r.NextDouble (); } Mirage.Vector v2 = DataParser.ParseMirageVector (DataParser.MirageVectorToString (v)); if (v.rows != v2.rows) { Hyena.Log.Warning ("NoNoise/Testing - mirage vectors don't have the same length"); return false; } for (int i = 0; i < v.rows; i++) { if (!v.d [i, 0].ToString ().Equals (v2.d [i, 0].ToString ())) { // string precision Hyena.Log.WarningFormat ("NoNoise/Testing - values at pos {0} are not the same ({1} | {2})", i, v.d [i, 0], v2.d [i, 0]); return false; } } return true; }