Beispiel #1
0
        /// <summary>
        /// Perform an inverse haar wavelet mel scaled transform. E.g. perform an ihaar2d and inverse Mel Filterbands and return stftdata
        /// </summary>
        /// <param name="wavelet">wavelet matrix</param>
        /// <returns>matrix inverse wavelet'ed and mel removed (e.g. stftdata)</returns>
        public Matrix InverseMelScaleWaveletPadding(ref Matrix wavelet)
        {
            using (new DebugTimer("InverseMelScaleWaveletPadding(wavelet)")) {
                // 6. Perform the Inverse Wavelet Transform
                Matrix mel = WaveletUtils.InverseHaarWaveletTransform2D(wavelet.MatrixData);

                // Resize (remove padding)
                mel = mel.Resize(melScaleFreqsIndex.Length - 2, wavelet.Columns);

                // 5. Take Inverse Logarithm
                // Divide with first triangle height in order to scale properly
                for (int i = 0; i < mel.Rows; i++)
                {
                    for (int j = 0; j < mel.Columns; j++)
                    {
                        mel.MatrixData[i][j] = Math.Pow(10, (mel.MatrixData[i][j] / 20)) / melScaleTriangleHeights[0];
                    }
                }

                // 4. Inverse Mel Scale using interpolation
                // i.e. from e.g.
                // mel=Rows: 40, Columns: 165 (average freq, time slice)
                // to
                // m=Rows: 1024, Columns: 165 (freq, time slice)
                //Matrix m = filterWeights.Transpose() * mel;
                var m = new Matrix(filterWeights.Columns, mel.Columns);
                InverseMelScaling(mel, m);

                return(m);
            }
        }
Beispiel #2
0
        public static void TestDenoise(string imageInPath)
        {
            var image = ReadImageGrayscale(imageInPath);

            // Normalize the pixel values to the range 0..1.0. It does this by dividing all pixel values by the max value.
            double max = image.Max((b) => b.Max((v) => Math.Abs(v)));

            double[][] imageNormalized = image.Select(i => i.Select(j => j / max).ToArray()).ToArray();

            var normalizedMatrix = new Matrix(imageNormalized);

            normalizedMatrix.DrawMatrixImage("lena-original.png", -1, -1, false);

            // Add Noise using normally distributed pseudorandom numbers
            // image_noisy = image_normalized + 0.1 * randn(size(image_normalized));
            RandomUtils.Seed(Guid.NewGuid().GetHashCode());
            double[][] imageNoisy  = imageNormalized.Select(i => i.Select(j => j + (0.2 * RandomUtils.NextDouble())).ToArray()).ToArray();
            var        matrixNoisy = new Matrix(imageNoisy);

            matrixNoisy.DrawMatrixImage("lena-noisy.png", -1, -1, false);

            // Haar Wavelet Transform
            Matrix haarMatrix = WaveletUtils.HaarWaveletTransform2D(imageNoisy);

            // Thresholding
            const double threshold  = 0.10;            // 0.15 seems to work well with the noise added above, 0.1
            var          yHard      = Thresholding.PerformHardThresholding(haarMatrix.MatrixData, threshold);
            var          ySoft      = Thresholding.PerformSoftThresholding(haarMatrix.MatrixData, threshold);
            var          ySemisoft  = Thresholding.PerformSemisoftThresholding(haarMatrix.MatrixData, threshold, threshold * 2);
            var          ySemisoft2 = Thresholding.PerformSemisoftThresholding(haarMatrix.MatrixData, threshold, threshold * 4);
            var          yStrict    = Thresholding.PerformStrictThresholding(haarMatrix.MatrixData, 100);

            // Inverse 2D Haar Wavelet Transform
            Matrix zHard      = WaveletUtils.InverseHaarWaveletTransform2D(yHard);
            Matrix zSoft      = WaveletUtils.InverseHaarWaveletTransform2D(ySoft);
            Matrix zSemisoft  = WaveletUtils.InverseHaarWaveletTransform2D(ySemisoft);
            Matrix zSemisoft2 = WaveletUtils.InverseHaarWaveletTransform2D(ySemisoft2);
            Matrix zStrict    = WaveletUtils.InverseHaarWaveletTransform2D(yStrict);

            // Output the images
            zHard.DrawMatrixImage("lena-thresholding-hard.png", -1, -1, false);
            zSoft.DrawMatrixImage("lena-thresholding-soft.png", -1, -1, false);
            zSemisoft.DrawMatrixImage("lena-thresholding-semisoft.png", -1, -1, false);
            zSemisoft2.DrawMatrixImage("lena-thresholding-semisoft2.png", -1, -1, false);
            zStrict.DrawMatrixImage("lena-thresholding-strict.png", -1, -1, false);
        }