Exemplo n.º 1
0
        /**
         * Performs the forward transform for the given array from time domain to
         * Hilbert domain and returns a new array of the same size keeping
         * coefficients of Hilbert domain and should be of length 2 to the power of p
         * -- length = 2^p where p is a positive integer.
         *
         * Returns Tuple<LeftWaveletPacket, RightWaveletPacket>
         */
        public Tuple <WaveletPacket, WaveletPacket> Forward(WaveletPacket parent)
        {
            if (parent.Coefficients.Length % 2 != 0)
            {
                throw new ArgumentException("Tree must contain an even number of features");
            }

            double[] arrHilbLeft  = new double[parent.Coefficients.Length / 2];
            double[] arrHilbRight = new double[parent.Coefficients.Length / 2];

            int k = 0;
            int h = parent.Coefficients.Length >> 1;

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < GetWaveLength(); j++)
                {
                    k = (i << 1) + j;
                    while (k >= parent.Coefficients.Length)
                    {
                        k -= parent.Coefficients.Length;
                    }

                    arrHilbLeft[i]  += parent.Coefficients[k] * GetScales()[j]; // low pass filter - energy (approximation)
                    arrHilbRight[i] += parent.Coefficients[k] * GetCoeffs()[j]; // high pass filter - details
                } // wavelet
            } // h
            return(new Tuple <WaveletPacket, WaveletPacket>(new WaveletPacket(arrHilbLeft, parent.Identifier, parent, parent.SubspaceIndex * 2), new WaveletPacket(arrHilbRight, parent.Identifier, parent, parent.SubspaceIndex * 2 + 1)));
        }
Exemplo n.º 2
0
        protected override List <Tuple <double, double[]> > GetTransformedRows(ISample sample, int[] columns)
        {
            if (sample.GetDataRows().Count != trainingRowCount)
            {
                throw new ArgumentException("Sample must match the length of the training samples");
            }

            // transform each applicable column with optimal wavelet packet decomposition
            double[][] transformedColumns = new double[trainingColumnCount][];
            for (int j = 0; j < trainingColumnCount; j++)
            {
                // only need to calculate optimal decomposition for applicable columns
                if (columns.Contains(j))
                {
                    WaveletPacket top = GetWaveletPacketDecomposition(sample, j, wavelet, toLevel);
                    transformedColumns[j] = optimalSubspace[j].Select(subspace => top.FindSubspace(subspace)).SelectMany(packet => packet.Coefficients).ToArray();
                }
                else
                {
                    transformedColumns[j] = sample.GetDataRows(j);
                }
            }
            List <Tuple <double, double[]> > waveletData = new List <Tuple <double, double[]> >();

            for (int row = 0; row < trainingRowCount; row++)
            {
                waveletData.Add(new Tuple <double, double[]>(sample.GetDataRows()[row].Item1, transformedColumns.Select(coefficientList => coefficientList[row]).ToArray()));
            }
            return(waveletData);
        }
Exemplo n.º 3
0
        public WaveletPacket GetWaveletPacketDecomposition(ISample sample, int column, Wavelet wavelet, int toLevel)
        {
            WaveletPacket top = new WaveletPacket(sample.GetDataRows(column), sample.GetIdentifier());

            BuildWaveletTree(top, wavelet, toLevel);
            return(top);
        }
Exemplo n.º 4
0
 private double GetNormalizedEuclidean(WaveletPacket packet, String identifier, Dictionary <string, List <double> > means, List <double> standardDeviations, List <int> exclusionCriterion)
 {
     return(packet.Coefficients.Select((row, i) => row - means[identifier][i])         // 1 subtract vector V (all feature means in this feature set for this class) from Xk (all features in this trial [packet])
            .Select((mean, i) => mean / standardDeviations[i])                         // 2 divide the result vector by the respective standard deviation vector
            .Where((mean, i) => !exclusionCriterion.Contains(i))                       // 3 remove excluded features (ignore excluded features)
            .Aggregate(0.0, (accumulated, next) => accumulated += Math.Pow(next, 2))); // 4 find the square of the euclidean magnitude for the resulting vector srt(x^2+y^2)^2 = x^2+y^2
 }
Exemplo n.º 5
0
 public void SetChildren(WaveletPacket leftChild, WaveletPacket rightChild)
 {
     this.LeftChild          = leftChild;
     this.RightChild         = rightChild;
     this.LeftChild.Sibling  = rightChild;
     this.RightChild.Sibling = leftChild;
 }
Exemplo n.º 6
0
 private void BuildWaveletTree(WaveletPacket parent, Wavelet wavelet, int toLevel)
 {
     if (parent.Coefficients.Length >= wavelet.GetWaveLength() && parent.DecompositionLevel < toLevel)
     {
         Tuple <WaveletPacket, WaveletPacket> forwardTransform = wavelet.Forward(parent);
         parent.SetChildren(forwardTransform.Item1, forwardTransform.Item2); // set children and recurse down to desired level
         BuildWaveletTree(parent.LeftChild, wavelet, toLevel);
         BuildWaveletTree(parent.RightChild, wavelet, toLevel);
     }
 }
Exemplo n.º 7
0
        public WaveletPacket FindSubspace(WaveletSubspace subspace)
        {
            // if this isn't the top of the tree move to the top of the tree
            WaveletPacket top = this;

            while (top.Parent != null)
            {
                top = top.Parent;
            }

            if (subspace.DecompositionLevel == top.DecompositionLevel)
            {
                return(top);
            }
            // do a recursive tree search for the desired subspace and return it
            return(SearchSubspace(top, subspace));
        }
Exemplo n.º 8
0
        public WaveletPacket(double[] coefficients, string identifier, WaveletPacket parent = null, int subspaceIndex = 0)
        {
            if (parent != null)
            {
                this.DecompositionLevel = parent.DecompositionLevel + 1;
            }
            else
            {
                this.DecompositionLevel = 0;
            }

            this.SubspaceIndex = subspaceIndex;
            this.Parent        = parent;
            this.Sibling       = null;
            this.LeftChild     = null;
            this.RightChild    = null;
            this.Coefficients  = coefficients;
            this.Identifier    = identifier;
        }
Exemplo n.º 9
0
        private WaveletPacket SearchSubspace(WaveletPacket top, WaveletSubspace subspace)
        {
            if (subspace.DecompositionLevel == top.LeftChild.DecompositionLevel)
            {
                if (subspace.SubspaceIndex == top.LeftChild.SubspaceIndex)
                {
                    return(top.LeftChild);
                }
                return(top.RightChild);
            }
            else
            {
                int levelDiff    = (subspace.DecompositionLevel - top.LeftChild.DecompositionLevel);
                int maxLeftIndex = top.LeftChild.SubspaceIndex * (levelDiff * 2) + ((int)Math.Pow(2, levelDiff) - 1);

                if (subspace.SubspaceIndex <= maxLeftIndex)
                {
                    return(SearchSubspace(top.LeftChild, subspace));
                }
                return(SearchSubspace(top.RightChild, subspace));
            }
        }