Esempio n. 1
0
        public void multiresolution(double[,] Xin, int N, int K, WaveletTransformType transform,
                                    WaveletBoundaryCondition boundary, out double[,] Xmra)
        {
/* Peform a multirsolution analysis using the DWT or MODWT matrix
*   obtained from `decompose.'  The inverse transform will be applied
*   to selected wavelet detail coefficients.  The wavelet smooth
*   coefficients from the original transform are added to the K+1
*   column in order to preserve the additive decomposition.
*   Reflection is used as the default boundary condition.
*
*   Input:
*   Xin    = dmatrix from `decompose'
*   N      = number of rows in Xin
*   K      = number of details in Xin
*   f      = wavelet filter structure
*   method = character string (either "dwt" or "modwt")
*
*   Output:
*   Xmra = dmatrix containg K wavelet details and 1 wavelet smooth */

            int i, k, t, length;

            double[] zero, Xout, Win;

            if (boundary == WaveletBoundaryCondition.Reflect)
            {
                length = 2 * N;
            }
            else
            {
                length = N;
            }
            zero = new double[length];
            Xout = new double[length];
            Win  = new double[length];
            Xmra = new double[N, K + 1];

            //printf("##### Length = %d #####\n", length);

            for (k = 0; k < K; k++)
            {
                for (t = 0; t < length; t++)
                {
                    Win[t]  = Xin[t, k];
                    zero[t] = 0.0;
                }
                if (transform == WaveletTransformType.DWT)
                {
                    idwt(Win, zero, (int)(N / Math.Pow(2, k)), out Xout);
                }
                else if (transform == WaveletTransformType.MODWT)
                {
                    imodwt(Win, zero, length, k, out Xout);
                }

                for (i = k; i >= 0; i--)
                {
                    for (t = 0; t < length; t++)
                    {
                        Win[t] = Xout[t];
                    }

                    if (transform == WaveletTransformType.DWT)
                    {
                        idwt(zero, Win, (int)(N / Math.Pow(2, i)), out Xout);
                    }
                    else
                    {
                        imodwt(zero, Win, length, i, out Xout);
                    }
                }
                for (t = 0; t < N; t++)
                {
                    Xmra[t, k] = Xout[t];
                }
            }

            /* One more iteration is required on the wavelet smooth coefficients
             * to complete the additive decomposition. */

            for (t = 0; t < length; t++)
            {
                Win[t]  = Xin[t, K - 1];
                zero[t] = 0.0;
            }
            if (transform == WaveletTransformType.DWT)
            {
                idwt(zero, Win, (int)(N / Math.Pow(2, K)), out Xout);
            }
            else
            {
                imodwt(zero, Win, length, K, out Xout);
            }
            for (i = K; i >= 0; i--)
            {
                for (t = 0; t < length; t++)
                {
                    Win[t] = Xout[t];
                }
                if (transform == WaveletTransformType.DWT)
                {
                    idwt(zero, Win, (int)(N / Math.Pow(2, i)), out Xout);
                }
                else
                {
                    imodwt(zero, Win, length, i, out Xout);
                }
                for (t = 0; t < length; t++)
                {
                    Win[t]  = Xout[t];
                    zero[t] = 0.0;
                }
            }
            for (t = 0; t < N; t++)
            {
                Xmra[t, K] = Xout[t];
            }
        }
Esempio n. 2
0
        public void decompose(double[] X, int N, int K, WaveletTransformType transform,
                              WaveletBoundaryCondition boundary, out double[,] Xout)
        {
/* Peform the discrete wavelet transform (DWT) or maximal overlap
 *  discrete wavelet transform (MODWT) to a time-series and obtain a
 *  specified number (K) of wavelet coefficients and subsequent
 *  wavelet smooth.  Reflection is used as the default boundary
 *  condition.
 *
 *  Input:
 *  X        = time-series (dvector of data)
 *  K        = number of details desired
 *  f        = wavelet filter structure
 *  method   = character string (either "dwt" or "modwt")
 *  boundary = boundary condition (either "period" or "reflect")
 *
 *  Output: Xout = dmatrix of wavelet coefficients and smooth
 *                 (length = N for "period" and 2N for "reflect") */

            int i, k;
            int scale, length;

            double[] Wout, Vout, Vin;



            /* Dyadic vector length required for DWT */
            if (transform == WaveletTransformType.DWT && N % 2 != 0)
            {
                throw new InvalidDataException("...data must have dyadic length for DWT...");
            }

            /* The choice of boundary methods affect things... */
            if (boundary == WaveletBoundaryCondition.Reflect)
            {
                length = 2 * N;
                scale  = length;
                Wout   = new double[length];
                Vout   = new double[length];
                Vin    = new double[length];
                Xout   = new double[length, K];
                reflect_vector(X, N, Vin);
                /* printf("Boundary Treatment is REFLECTION...\n"); */
            }
            else
            {
                length = N;
                scale  = N;
                Wout   = new double[length];
                Vout   = new double[length];
                Vin    = new double[length];
                Xout   = new double[length, K];
                for (i = 0; i < length; i++)
                {
                    Vin[i] = X[i];
                }
                /* printf("Boundary Treatment is PERIODIC...\n"); */
            }
            /* printf("##### Length = %d #####\n", length); */

            for (k = 0; k < K; k++)
            {
                for (i = 0; i < length; i++)
                {
                    Wout[i] = 0.0;
                    Vout[i] = 0.0;
                }
                if (transform == WaveletTransformType.DWT)
                {
                    dwt(Vin, scale, out Wout, out Vout);
                }
                else if (transform == WaveletTransformType.MODWT)
                {
                    modwt(Vin, length, k, out Wout, out Vout);
                }

                for (i = 0; i < N; i++)
                {
                    Xout[i, k] = Wout[i];
                }
                for (i = 0; i < length; i++)
                {
                    Vin[i] = Vout[i];
                }
                scale -= scale / 2;
            }
            for (i = 0; i < length; i++)
            {
                Xout[i, K - 1] = Wout[i];                     /* GMA */
            }
        }