/// <summary> /// Multilevel 1-D Discreete Wavelet Transform /// </summary> /// <param name="signal">The signal. Example: new Signal(5, 6, 7, 8, 1, 2, 3, 4)</param> /// <param name="motherWavelet">The mother wavelet to be used. Example: CommonMotherWavelets.GetWaveletFromName("DB4")</param> /// <param name="level">The depth-level to perform the DWT</param> /// <param name="extensionMode">Signal extension mode</param> /// <param name="convolutionMode">Defines what convolution function should be used</param> /// <returns></returns> public static List <DecompositionLevel> ExecuteDWT(Signal signal, MotherWavelet motherWavelet, int level, SignalExtension.ExtensionMode extensionMode = SignalExtension.ExtensionMode.SymmetricHalfPoint, ConvolutionModeEnum convolutionMode = ConvolutionModeEnum.ManagedFFT) { var levels = new List <DecompositionLevel>(); var approximation = (double[])signal.Samples.Clone(); var details = (double[])signal.Samples.Clone(); var realLength = signal.Samples.Length; for (var i = 1; i <= level; i++) { var extensionSize = motherWavelet.Filters.DecompositionLowPassFilter.Length - 1; approximation = SignalExtension.Extend(approximation, extensionMode, extensionSize); details = SignalExtension.Extend(details, extensionMode, extensionSize); approximation = WaveMath.Convolve(convolutionMode, approximation, motherWavelet.Filters.DecompositionLowPassFilter); approximation = WaveMath.DownSample(approximation); details = WaveMath.Convolve(convolutionMode, details, motherWavelet.Filters.DecompositionHighPassFilter); details = WaveMath.DownSample(details); realLength = realLength / 2; levels.Add(new DecompositionLevel { Signal = signal, Index = i - 1, Approximation = approximation, Details = details }); details = (double[])approximation.Clone(); } return(levels); }
/// <summary> /// Multilevel inverse discrete 1-D wavelet transform /// </summary> /// <param name="decompositionLevels">The decomposition levels of the DWT</param> /// <param name="motherWavelet">The mother wavelet to be used. Example: CommonMotherWavelets.GetWaveletFromName("DB4") </param> /// <param name="level">The depth-level to perform the DWT</param> /// <param name="convolutionMode">Defines what convolution function should be used</param> /// <returns></returns> public static double[] ExecuteIDWT(List <DecompositionLevel> decompositionLevels, MotherWavelet motherWavelet, int level = 0, ConvolutionModeEnum convolutionMode = ConvolutionModeEnum.ManagedFFT) { if (level == 0 || level > decompositionLevels.Count) { level = decompositionLevels.Count; } if (level <= 0) { return(null); } var approximation = (double[])decompositionLevels[level - 1].Approximation.Clone(); var details = (double[])decompositionLevels[level - 1].Details.Clone(); for (var i = level - 1; i >= 0; i--) { approximation = WaveMath.UpSample(approximation); approximation = WaveMath.Convolve(convolutionMode, approximation, motherWavelet.Filters.ReconstructionLowPassFilter, true, -1); details = WaveMath.UpSample(details); details = WaveMath.Convolve(convolutionMode, details, motherWavelet.Filters.ReconstructionHighPassFilter, true, -1); //sum approximation with details approximation = WaveMath.Add(approximation, details); if (i <= 0) { continue; } if (approximation.Length > decompositionLevels[i - 1].Details.Length) { approximation = SignalExtension.Deextend(approximation, decompositionLevels[i - 1].Details.Length); } details = (double[])decompositionLevels[i - 1].Details.Clone(); } return(approximation); }
/// <summary> /// Convolves vectors input and filter. /// </summary> /// <param name="convolutionMode">Defines what convolution function should be used</param> /// <param name="input">The input signal</param> /// <param name="filter">The filter</param> /// <param name="returnOnlyValid">True to return only the middle of the array</param> /// <param name="margin">Margin to be used if returnOnlyValid is set to true</param> /// <param name="mode">FFT mode</param> /// <returns></returns> public static double[] Convolve(ConvolutionModeEnum convolutionMode, double[] input, double[] filter, bool returnOnlyValid = true, int margin = 0, ManagedFFTModeEnum mode = ManagedFFTModeEnum.UseLookupTable) { return(convolutionMode == ConvolutionModeEnum.Normal ? ConvolveNormal(input, filter, returnOnlyValid, margin) : ConvolveManagedFFT(input, filter, returnOnlyValid, margin, mode)); }