Exemplo n.º 1
0
        /// <summary>
        /// concatenate submatrices column-wise into one matrix, i.e., the number of rows for the output matrix
        /// is equal to the number of rows of each of the frequency band matrices.
        /// </summary>
        public static double[,] ConcatFreqBandMatrices(List <double[, ]> submatrices)
        {
            // The assumption here is all frequency band matrices have the same number of columns.
            int columnSize = submatrices.Count * submatrices[0].GetLength(1);
            int rowSize    = submatrices[0].GetLength(0);

            double[,] matrix = new double[rowSize, columnSize];
            int count = 0;

            while (count < submatrices.Count)
            {
                DoubleSquareArrayExtensions.AddToArray(matrix, submatrices[count], DoubleSquareArrayExtensions.MergingDirection.Column, submatrices[count].GetLength(1) * count);
                count++;
            }

            // If we have frequency band matrices with different number of columns,
            // Then the below commented code need to be used.

            /*
             * double[][,] submatrix = submatrices.ToArray();
             * int colSize = 0;
             * for (int i = 0; i < submatrix.Length; i++)
             * {
             *  colSize = colSize + submatrix[i].GetLength(1);
             * }
             *
             * // storing the number of rows of each submatrice in an array
             * int[] noRows = new int[submatrix.Length];
             * for (int i = 0; i < submatrix.Length; i++)
             * {
             *  noRows[i] = submatrix[i].GetLength(0);
             * }
             *
             * // find the max number of rows from noRows array
             * int maxRows = noRows.Max();
             *
             * double[,] matrix = new double[maxRows, colSize];
             *
             * // might be better way to do this
             * AddToArray(matrix, submatrix[0], "column");
             * AddToArray(matrix, submatrix[1], "column", submatrix[0].GetLength(1));
             * AddToArray(matrix, submatrix[2], "column", submatrix[0].GetLength(1) + submatrix[1].GetLength(1));
             */

            return(matrix);
        }
Exemplo n.º 2
0
        /// <summary>
        /// convert a list of patch matrices to one matrix
        /// </summary>
        public static double[,] ListOf2DArrayToOne2DArray(List <double[, ]> listOfPatchMatrices)
        {
            int numberOfPatches = listOfPatchMatrices[0].GetLength(0);

            double[,] allPatchesMatrix = new double[listOfPatchMatrices.Count * numberOfPatches, listOfPatchMatrices[0].GetLength(1)];
            for (int i = 0; i < listOfPatchMatrices.Count; i++)
            {
                var m = listOfPatchMatrices[i];
                if (m.GetLength(0) != numberOfPatches)
                {
                    throw new ArgumentException("All arrays must be the same length");
                }

                DoubleSquareArrayExtensions.AddToArray(allPatchesMatrix, m, DoubleSquareArrayExtensions.MergingDirection.Row, i * m.GetLength(0));
            }

            return(allPatchesMatrix);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Calculates the modal noise value for each freq bin.
        /// Does so using a series of overlapped matrices.
        /// TODO!!!! COULD SIMPLY THIS METHOD. JUST CALCULATE MODE FOR EACH FREQ BIN WITHOUT OVERLAP ....
        /// .... AND THEN APPLY MORE SEVERE SMOOTHING TO THE MODAL NOISE PROFILE IN PREVIOUS METHOD.
        ///
        /// COMPARE THIS METHOD WITH SNR.SubtractModalNoise().
        /// </summary>
        /// <param name="matrix">Audio sample matrix.</param>
        /// <returns>Modal noise values.</returns>
        private static double[] CalculateModalNoise(double[,] matrix)
        {
            //set parameters for noise histograms based on overlapping bands.
            //*******************************************************************************************************************
            int    bandWidth         = 3;     // should be an odd number
            int    binCount          = 64;    // number of pixel intensity bins
            double upperLimitForMode = 0.666; // sets upper limit to modal noise bin. Higher values = more severe noise removal.
            int    binLimit          = (int)(binCount * upperLimitForMode);

            //*******************************************************************************************************************

            DoubleSquareArrayExtensions.MinMax(matrix, out var minIntensity, out var maxIntensity);
            double binWidth = (maxIntensity - minIntensity) / binCount;  // width of an intensity bin

            // LoggedConsole.WriteLine("minIntensity=" + minIntensity + "  maxIntensity=" + maxIntensity + "  binWidth=" + binWidth);

            int rowCount = matrix.GetLength(0);
            int colCount = matrix.GetLength(1);

            if (bandWidth > colCount)
            {
                bandWidth = colCount - 1;
            }

            int halfWidth = bandWidth / 2;

            // init matrix from which histogram derived
            double[,] submatrix = Submatrix(matrix, 0, 0, rowCount - 1, bandWidth);
            double[] modalNoise = new double[colCount];

            //  for all cols i.e. freq bins
            for (int col = 0; col < colCount; col++)
            {
                // construct new submatrix to calculate modal noise
                int start = col - halfWidth;   //extend range of submatrix below col for smoother changes
                if (start < 0)
                {
                    start = 0;
                }

                int stop = col + halfWidth;
                if (stop >= colCount)
                {
                    stop = colCount - 1;
                }

                submatrix = Submatrix(matrix, 0, start, rowCount - 1, stop);
                int[] histo = Histo(submatrix, binCount, minIntensity, maxIntensity, binWidth);

                //DataTools.writeBarGraph(histo);
                double[] smoothHisto = FilterMovingAverage(histo, 7);
                GetMaxIndex(smoothHisto, out var maxindex); //this is mode of histogram
                if (maxindex > binLimit)
                {
                    maxindex = binLimit;
                }

                modalNoise[col] = minIntensity + (maxindex * binWidth);

                //LoggedConsole.WriteLine("  modal index=" + maxindex + "  modalIntensity=" + modalIntensity.ToString("F3"));
            }

            return(modalNoise);
        }