Esempio n. 1
0
        public Main()
        {
            // this is generated by VisualStudio
            InitializeComponent();

            // create new windows speech recognizer
            initializeSpeechRecognier();

            // initialize codeBook with the speech recognizer
            codeBook = new CodeBook(speechRecognizer, authenticationThreshold);

            // for each folder next to the program, each folder contains MFCC files for a person
            foreach (string folderName in Directory.GetDirectories("."))
            {
                // obtail username from folder path, (folder path may start with .\ or sth like that)
                string username = Path.GetFileName(folderName);
                // add the saved user to code book
                codeBook.AddToModel(
                    username,
                    // pass all MFCC files inside the folder
                    Directory.GetFiles(folderName),
                    // pass the password priorly saved for that user
                    File.ReadAllText(username + ".password")
                    );
            }
        }
Esempio n. 2
0
        static void TestRandomData(string testDataSavePath = null)
        {
            var chars = "abcdefghijklmnopqrstuvwxyz";

            chars += chars.ToUpper() + "0123456789`~!@#$%^&*()_+=-{}[]|:><,.\"'\\?/";
            var validCharBytes   = Encoding.UTF8.GetBytes(chars);
            var codeBook         = CodeBook.InitFromValidChars(validCharBytes);
            var codeBookMetaData = codeBook.GetMetaData();
            var codeBook2        = CodeBook.InitFromMetaData(codeBookMetaData);
            var seeds            = new List <string> {
                "PaladinDu01",
                "PaladinDu02",
                "PaladinDu03",
            };
            var keys = new List <byte[]>();

            foreach (var seed in seeds)
            {
                keys.Add(codeBook.InitSeed(Encoding.UTF8.GetBytes(seed)));
            }
            var testDatas = new List <byte[]>
            {
                Encoding.UTF8.GetBytes("aaaaaaaaaaaaaaaaaaaaaaa"),
                Encoding.UTF8.GetBytes("aaaaaaaaaaaaaaaaaaaaaaab"),
                Encoding.UTF8.GetBytes("aaaaaaaaaaaaaaaaaaaaaaac"),
                Encoding.UTF8.GetBytes("aaaaaaaaaaaaaaaaaaaaaaad")
            };

            for (int i = 0; i < 1000; ++i)
            {
                testDatas.Add(GetRandomBytes(validCharBytes, 20));
            }
            foreach (var testData in testDatas)
            {
                TestData(codeBook, keys[0], testData);
                TestData(codeBook2, keys[0], testData);
            }
            foreach (var key in keys)
            {
                TestData(codeBook, key, testDatas[0]);
            }
            if (testDataSavePath != null)
            {
                var testCases = new List <List <string> >();
                foreach (var testData in testDatas)
                {
                    testCases.Add(new List <string> {
                        Encoding.UTF8.GetString(testData),
                        Encoding.UTF8.GetString(codeBook.Encryption(testData, keys[0]))
                    });
                }
                var json = JsonConvert.SerializeObject(new TestDataInfo
                {
                    meta  = codeBookMetaData,
                    seed  = seeds[0],
                    cases = testCases
                });
                System.IO.File.WriteAllText(testDataSavePath, json);
            }
        }
Esempio n. 3
0
        public async Task <CodeBook> AddCodeBook(CodeBook codeBook)
        {
            await _context.CodeBook.AddAsync(codeBook);

            await _context.SaveChangesAsync();

            return(codeBook);
        }
Esempio n. 4
0
 public EdditSetting(int id, CodeBook settingId, string name)
 {
     Id     = id;
     status = settingId;
     InitializeComponent();
     Name       = name;
     rnServices = new RnServices();
 }
Esempio n. 5
0
        public void WriteBook(CodeBook book, int a)
        {
            if ((a < 0) || (a >= book.Entries))
            {
                return;
            }

            Write(book.CodeList[a], book.StaticBook.LengthList[a]);
        }
Esempio n. 6
0
        private void EncodePart(EncodeBuffer buffer, int[] vec, int offset, int n, CodeBook book)
        {
            var step = n / book.Dimensions;

            for (var i = 0; i < step; i++)
            {
                var entry = LocalBookBestError(book, vec, offset + i * book.Dimensions);
                buffer.WriteBook(book, entry);
            }
        }
Esempio n. 7
0
 public static CodeBook[,] CreateCodeBook(Image <Hsv, byte> image)
 {
     CodeBook[,] codebooks = new CodeBook[image.Height, image.Width];
     for (int i = 0; i < image.Height; i++)
     {
         for (int j = 0; j < image.Width; j++)
         {
             codebooks[i, j] = new CodeBook();
         }
     }
     return(codebooks);
 }
Esempio n. 8
0
    static public CodeBook InitFromValidChars(byte[] validChars)
    {
        var ret = new CodeBook();

        ret.CharToIndexMap = new int[256];
        ret.CharsCount     = validChars.Length;
        var charCount = 0;
        var charMap   = new int[256];

        for (int i = 0; i < 256; ++i)
        {
            charMap[i] = InvalidIndex;
        }
        foreach (var validChar in validChars)
        {
            if (charMap[validChar] != 1)
            {
                charMap[validChar] = 1;

                charCount += 1;
            }
        }
        ret.IndexToCharMap = new byte[charCount];
        for (int i = 0, j = 0; i < 256; ++i)
        {
            if (charMap[i] == 1)
            {
                ret.IndexToCharMap[j] = (byte)i;
                ret.CharToIndexMap[i] = j;
                j++;
            }
        }
        ret.EncryptionMap = new byte[charCount];
        ret.DecryptionMap = new byte[charCount];
        for (int i = 0; i < ret.CharsCount; ++i)
        {
            ret.EncryptionMap[i] = (byte)i;
        }
        var rand = new Random();

        for (int i = 0; i < ret.CharsCount; ++i)
        {
            var randInt = rand.Next(0, ret.CharsCount - i);
            var tmpCode = ret.EncryptionMap[i];
            ret.EncryptionMap[i] = ret.EncryptionMap[i + randInt];
            ret.DecryptionMap[ret.EncryptionMap[i]] = (byte)i;
            ret.EncryptionMap[i + randInt]          = tmpCode;
        }
        return(ret);
    }
Esempio n. 9
0
        static void TestData(CodeBook strCoding, byte[] key, byte[] data)
        {
            var encodeData = strCoding.Encryption(data, key);
            var decodeData = strCoding.Decryption(encodeData, key);

            if (Encoding.UTF8.GetString(data).Equals(Encoding.UTF8.GetString(decodeData)))
            {
                Console.WriteLine($"encryption success: {Encoding.UTF8.GetString(data)},{Encoding.UTF8.GetString(encodeData)}");
            }
            else
            {
                Console.WriteLine($"encryption error: {Encoding.UTF8.GetString(data)},{Encoding.UTF8.GetString(decodeData)}");
            }
        }
        /// <summary>
        /// Retrieve fragment index inside codebook that is associated to a time index
        /// </summary>
        /// <returns>Returns -1 if the time index isn't covered by the codebook</returns>
        public int GetCodeBookFragmentIndex(ref CodeBook codeBook, TimeIndex timeIndex)
        {
            int numIntervals = codeBook.intervals.Length;

            int fragmentIndex = 0;

            for (int i = 0; i < numIntervals; ++i)
            {
                ref Interval interval = ref GetInterval(codeBook.intervals[i]);
                if (interval.Contains(timeIndex))
                {
                    fragmentIndex += timeIndex.frameIndex - interval.firstFrame;
                    return(fragmentIndex);
                }

                fragmentIndex += interval.numFrames;
            }
Esempio n. 11
0
        public async Task <IActionResult> AddCodeBook(CodeBook codeBook)
        {
            var codeBookCreate = new CodeBook
            {
                CodeBookName = codeBook.CodeBookName
            };

            var createdCodeBook = await _codeBookRepo.AddCodeBook(codeBookCreate);

            var codeBookKeyCreate = new CodeBookKey
            {
                CodeBookKeyId = codeBook.Id,
                CodeBookId    = createdCodeBook.Id
            };

            var createdCodeBookKey = await _codeBookKeyRepo.AddCodeBookKeys(codeBookKeyCreate);

            return(Ok(createdCodeBookKey));
        }
        /// <summary>
        /// This function returns TRUE if code book item selected by code book name and
        /// code book code exists. Otherwise this function returns false.
        /// </summary>
        /// <param name="fieldItem"></param>
        /// <param name="valueToValidate"></param>
        /// <param name="currentCodeBook"></param>
        /// <returns></returns>
        private bool CodeBookFieldValidate(FieldItem fieldItem, string valueToValidate, out string currentCodeBook)
        {
            currentCodeBook = string.Empty;
            CodeBook codeBook = fieldItem.FieldType as CodeBook;

            if (codeBook == null || string.IsNullOrEmpty(valueToValidate))
            {
                return(true);
            }

            currentCodeBook = codeBook.CodeBookName;

            if (!this._package.CodeBook.IsValidValue(codeBook.CodeBookName, valueToValidate, out string message))
            {
                return(false);
            }

            return(true);
        }
Esempio n. 13
0
        public ResidueLookup(Residue residue, List <CodeBook> fullBooks)
        {
            if (residue.ResidueType != ResidueType.Two)
            {
                throw new NotImplementedException("ResidueTypes other than 'Two' are not yet implemented");
            }

            _residue = residue;

            _phraseBook = fullBooks[residue.GroupBook];

            var acc      = 0;
            var maxstage = 0;

            _partitionBooks = new CodeBook[residue.Partitions][];

            for (var j = 0; j < _partitionBooks.Length; j++)
            {
                var stages = Encoding.Log(residue.SecondStages[j]);
                if (stages == 0)
                {
                    continue;
                }

                if (stages > maxstage)
                {
                    maxstage = stages;
                }

                _partitionBooks[j] = new CodeBook[stages];

                for (var k = 0; k < stages; k++)
                {
                    if ((residue.SecondStages[j] & (1 << k)) != 0)
                    {
                        _partitionBooks[j][k] = fullBooks[residue.BookList[acc++]];
                    }
                }
            }

            _stages = maxstage;
        }
Esempio n. 14
0
        static void TestByTestData(string testDataPath)
        {
            string text         = System.IO.File.ReadAllText(testDataPath);
            var    testDataInfo = JsonConvert.DeserializeObject <TestDataInfo>(text);
            var    codeBook     = CodeBook.InitFromMetaData(testDataInfo.meta);
            var    key          = codeBook.InitSeed(Encoding.UTF8.GetBytes(testDataInfo.seed));

            foreach (var caseData in testDataInfo.cases)
            {
                var decryptionData = codeBook.Decryption(Encoding.UTF8.GetBytes(caseData[1]), key);
                if (caseData[0].Equals(Encoding.UTF8.GetString(decryptionData)))
                {
                    Console.WriteLine($"decryption success:{caseData[0]},{caseData[1]}");
                }
                else
                {
                    Console.WriteLine($"decryption error:{caseData[0]},{caseData[1]},{Encoding.UTF8.GetString(decryptionData)}");
                }
            }
        }
Esempio n. 15
0
    static public CodeBook InitFromMetaData(string metaData)
    {
        var ret = new CodeBook();

        byte[] metaDataBytes = Encoding.UTF8.GetBytes(metaData);
        ret.CharsCount     = metaData.Length / 4;
        ret.CharToIndexMap = new int[256];
        ret.IndexToCharMap = new byte[ret.CharsCount];
        ret.EncryptionMap  = new byte[ret.CharsCount];
        ret.DecryptionMap  = new byte[ret.CharsCount];
        for (int i = 0; i < ret.CharsCount; ++i)
        {
            var tmpChar = (byte)((HexToNumMap[metaDataBytes[i * 2]] << 4) | HexToNumMap[metaDataBytes[i * 2 + 1]]);
            ret.CharToIndexMap[tmpChar] = i;
            ret.IndexToCharMap[i]       = tmpChar;
            tmpChar = (byte)((HexToNumMap[metaDataBytes[i * 2 + ret.CharsCount * 2]] << 4) | HexToNumMap[metaDataBytes[i * 2 + 1 + ret.CharsCount * 2]]);
            ret.EncryptionMap[i]       = tmpChar;
            ret.DecryptionMap[tmpChar] = (byte)i;
        }
        return(ret);
    }
Esempio n. 16
0
    public ResidueLookup(ResidueEntry residue, CodeBook[] fullBooks)
    {
        _residue = residue;

        _phraseBook = fullBooks[residue.GroupBook];

        var acc      = 0;
        var maxstage = 0;

        _partitionBooks = new CodeBook[residue.Partitions][];

        for (var j = 0; j < _partitionBooks.Length; j++)
        {
            var stages = Encoding.Log(residue.SecondStages[j]);
            if (stages == 0)
            {
                continue;
            }

            if (stages > maxstage)
            {
                maxstage = stages;
            }

            _partitionBooks[j] = new CodeBook[stages];

            for (var k = 0; k < stages; k++)
            {
                if ((residue.SecondStages[j] & (1 << k)) != 0)
                {
                    _partitionBooks[j][k] = fullBooks[residue.BookList[acc++]];
                }
            }
        }

        _stages = maxstage;
    }
Esempio n. 17
0
        /// <summary>
        /// Separate inputs columns from output column (from ExtractedDataset
        /// table) and convert strings into numeric values.
        /// </summary>
        /// <param name="attributeToPredict">Name of the output column.</param>
        /// <param name="codeBook">Codebook to be used (by default null).</param>
        /// <returns>Bool value indicating whether the dataset was
        /// processed correctly or not.</returns>
        public bool ProcessDataset(string attributeToPredict, Codification codeBook = null)
        {
            // ProcessedDataset will have the same structure of ExtractedDataset.
            ProcessedDataset = ExtractedDataset.Clone();

            // Inputs and outputs must preserve ExtractedDataset's dimensions.
            InputData  = new double[ExtractedDataset.Rows.Count][];
            OutputData = new int[ExtractedDataset.Rows.Count];

            // Except for the output column, columns' types are changed to
            // double type (classifiers work with numbers, not with strings).
            foreach (DataColumn column in ExtractedDataset.Columns)
            {
                if (column.ColumnName != attributeToPredict)
                {
                    InputColumnNames.Add(column.ColumnName);
                    ProcessedDataset.Columns[column.Ordinal].DataType = typeof(double);
                }
                else
                {
                    OutputColumnName = column.ColumnName;
                }
            }

            try
            {
                // Temporary variables.
                double        tempValue    = 0;
                DataRow       processedRow = null;
                List <double> tempInput    = null;

                for (int row = 0; row < ExtractedDataset.Rows.Count; ++row)
                {
                    // Process one row at time.
                    processedRow = ProcessedDataset.NewRow();
                    tempInput    = new List <double>();
                    foreach (DataColumn column in ExtractedDataset.Columns)
                    {
                        if (column.ColumnName != attributeToPredict)
                        {
                            Double.TryParse(
                                ExtractedDataset.Rows[row][column.Ordinal] as string,
                                System.Globalization.NumberStyles.Any,
                                System.Globalization.CultureInfo.InvariantCulture,
                                out tempValue);
                            // Create a row of numeric values to be
                            // added to ProcessedDataset and InputData.
                            processedRow[column.Ordinal] = tempValue;
                            tempInput.Add(tempValue);
                        }
                        else
                        {
                            // Don't convert the output column to a number yet, just copy the string
                            // value (conversion to number will be done later by CodeBook).
                            processedRow[column.Ordinal] = ExtractedDataset.Rows[row][column.Ordinal];
                        }
                    }
                    // Add/fill a row in ProcessedDataset and InputData
                    // before going to next row.
                    ProcessedDataset.Rows.Add(processedRow);
                    InputData[row] = tempInput.ToArray();
                }

                if (codeBook != null)
                {
                    // Use given codebook (codebook should be given only when dealing
                    // with testing datasets, in order to have the same codebook both
                    // for training and for testing data).
                    this.CodeBook = codeBook;
                }
                else
                {
                    // If no codebook is given, create one for the output column.
                    CodeBook = new Codification(ExtractedDataset, attributeToPredict);
                }

                // Apply codebook to the ProcessedDataset.
                ProcessedDataset = CodeBook.Apply(ProcessedDataset);

                // InputData is already set, OutputData is set to be the output
                // column codified with the codebook.
                OutputData = ProcessedDataset.ToArray <int>(attributeToPredict);

                // Number of input columns.
                InputAttributeNumber = ExtractedDataset.Columns.Count - 1;

                // Number of possible values the output column may assume.
                OutputPossibleValues = CodeBook[attributeToPredict].Symbols;
            }
            catch
            {
                return(false);
            }
            return(true);
        }
Esempio n. 18
0
 public static void ClearStaleEntries(CodeBook[,] cbs)
 {
     //閾値は5秒
     long staleThresh = 5 * 10 ^ 7;
     foreach (CodeBook cb in cbs)
     {
         for (int i = 0; i < cb.Entries.Count; i++)
         {
             CodeElement e = (CodeElement)cb.Entries[i];
             if (e.Stale > staleThresh)
             {
                 cb.Entries.RemoveAt(i);
             }
         }
     }
 }
Esempio n. 19
0
 public static CodeBook[,] CreateCodeBook(Image<Hsv, byte> image)
 {
     CodeBook[,] codebooks = new CodeBook[image.Height, image.Width];
     for (int i = 0; i < image.Height; i++)
     {
         for (int j = 0; j < image.Width; j++)
         {
             codebooks[i, j] = new CodeBook();
         }
     }
     return codebooks;
 }
Esempio n. 20
0
            public static int UpdateCodeBook(byte[] p, CodeBook cb)
            {
                cb.Ticks = Time;

                byte[] high, low;
                high = new byte[3];
                low  = new byte[3];

                for (int i = 0; i < 3; i++)
                {
                    high[i] = (byte)((p[i] + 10 > 255) ? 255 : (p[i] + 10));
                    low[i]  = (byte)((p[i] - 10 < 0) ? 0 : (p[i] - 10));
                }

                int matchChannel;
                int matchedIndex = 0;

                for (int i = 0; i < cb.Entries.Count; i++)
                {
                    matchChannel = 0;
                    CodeElement e = (CodeElement)cb.Entries[i];
                    for (int k = 0; k < 3; k++)
                    {
                        if (e.learnLow[k] <= p[k] && e.learnHigh[k] >= p[k])
                        {
                            matchChannel++;
                        }
                    }

                    if (matchChannel == 3)
                    {
                        e.LastUpdate = cb.Ticks;
                        for (int k = 0; k < 3; k++)
                        {
                            if (e.max[k] < p[k])
                            {
                                e.max[k] = p[k];
                            }
                            else if (e.min[k] > p[k])
                            {
                                e.min[k] = p[k];
                            }

                            if (e.learnHigh[k] < high[k])
                            {
                                e.learnHigh[k] += 1;
                            }
                            if (e.learnLow[k] < low[k])
                            {
                                e.learnLow[k] -= 1;
                            }
                        }
                        matchedIndex = i;
                        break;
                    }

                    if (i == cb.Entries.Count - 1)
                    {
                        matchedIndex = cb.Entries.Count;
                        break;
                    }
                }

                for (int s = 0; s < cb.Entries.Count; s++)
                {
                    CodeElement e      = (CodeElement)cb.Entries[s];
                    long        negRun = cb.Ticks - e.LastUpdate;
                    if (e.Stale < negRun)
                    {
                        e.Stale = negRun;
                    }
                }

                if (matchedIndex == cb.Entries.Count)
                {
                    CodeElement enew = new CodeElement();
                    for (int k = 0; k < 3; k++)
                    {
                        enew.learnHigh[k] = high[k];
                        enew.learnLow[k]  = low[k];
                        enew.max[k]       = p[k];
                        enew.min[k]       = p[k];
                    }
                    enew.LastUpdate = cb.Ticks;
                    enew.Stale      = 0;

                    cb.Entries.Add(enew);
                }

                return(matchedIndex);
            }
Esempio n. 21
0
 public static void UpdateCodeBooks(Image<Hsv, byte> image, CodeBook[,] codebooks)
 {
     if (image.Height == codebooks.GetLength(0) && image.Width == codebooks.GetLength(1))
     {
         throw new ArgumentException("InvalidArgument");
     }
     for (int i = 0; i < image.Height; i++)
     {
         for (int j = 0; j < image.Width; j++)
         {
             Hsv p = image[i, j];
             UpdateCodeBook(new byte[3] { (byte)p.Hue, (byte)p.Satuation, (byte)p.Value }, codebooks[i, j]);
         }
     }
 }
Esempio n. 22
0
        public static LookupCollection Create(VorbisInfo info)
        {
            var codecSetup = info.CodecSetup;

            var psyGlobal = new PsyGlobalLookup(codecSetup.PsyGlobalParam);
            var envelope  = new EnvelopeLookup(codecSetup.PsyGlobalParam, info);

            // MDCT is tranform 0
            var transform = new MdctLookup[2];

            transform[0] = new MdctLookup(codecSetup.BlockSizes[0]);
            transform[1] = new MdctLookup(codecSetup.BlockSizes[1]);

            // analysis always needs an fft
            var fftLookup = new DrftLookup[2];

            fftLookup[0] = new DrftLookup(codecSetup.BlockSizes[0]);
            fftLookup[1] = new DrftLookup(codecSetup.BlockSizes[1]);

            // finish the codebooks
            if (codecSetup.FullBooks == null)
            {
                codecSetup.FullBooks = new CodeBook[codecSetup.BookParams.Count];
                for (var i = 0; i < codecSetup.BookParams.Count; i++)
                {
                    codecSetup.FullBooks[i] = CodeBook.InitEncode(codecSetup.BookParams[i]);
                }
            }

            var psyLookup = new PsyLookup[codecSetup.PsyParams.Count];

            for (var i = 0; i < psyLookup.Length; i++)
            {
                psyLookup[i] = new PsyLookup(
                    codecSetup.PsyParams[i],
                    codecSetup.PsyGlobalParam,
                    codecSetup.BlockSizes[codecSetup.PsyParams[i].BlockFlag] / 2,
                    info.SampleRate);
            }

            // initialize all the backend lookups
            var floor = new FloorLookup[codecSetup.FloorParams.Count];

            for (var i = 0; i < floor.Length; i++)
            {
                floor[i] = new FloorLookup(codecSetup.FloorParams[i]);
            }

            var residue = new ResidueLookup[codecSetup.ResidueParams.Count];

            for (var i = 0; i < residue.Length; i++)
            {
                residue[i] = new ResidueLookup(codecSetup.ResidueParams[i], new List <CodeBook>(codecSetup.FullBooks));
            }

            return(new LookupCollection(
                       envelope,
                       new List <MdctLookup>(transform),
                       psyGlobal,
                       new List <PsyLookup>(psyLookup),
                       new List <DrftLookup>(fftLookup),
                       new List <FloorLookup>(floor),
                       new List <ResidueLookup>(residue)));
        }
Esempio n. 23
0
        private static int LocalBookBestError(CodeBook book, int[] vec, int offset)
        {
            int i;
            int o;
            var ze    = book.QuantValues >> 1;
            var index = 0;

            // assumes integer/centered encoder codebook maptype 1 no more than dim 8
            var p = new int[8];

            if (book.Delta != 1)
            {
                for (i = 0, o = book.Dimensions; i < book.Dimensions; i++)
                {
                    var v = (vec[offset + --o] - book.MinVal + (book.Delta >> 1)) / book.Delta;
                    var m = v < ze ? ((ze - v) << 1) - 1 : (v - ze) << 1;
                    index = index * book.QuantValues + (m < 0 ? 0 : (m >= book.QuantValues ? book.QuantValues - 1 : m));
                    p[o]  = v * book.Delta + book.MinVal;
                }
            }
            else
            {
                for (i = 0, o = book.Dimensions; i < book.Dimensions; i++)
                {
                    var v = vec[offset + --o] - book.MinVal;
                    var m = v < ze ? ((ze - v) << 1) - 1 : (v - ze) << 1;
                    index = index * book.QuantValues + (m < 0 ? 0 : (m >= book.QuantValues ? book.QuantValues - 1 : m));
                    p[o]  = v * book.Delta + book.MinVal;
                }
            }

            if (book.StaticBook.LengthList[index] <= 0)
            {
                // assumes integer/centered encoder codebook maptype 1 no more than dim 8
                var best   = -1;
                var e      = new int[8];
                var maxval = book.MinVal + book.Delta * (book.QuantValues - 1);
                for (i = 0; i < book.Entries; i++)
                {
                    if (book.StaticBook.LengthList[i] > 0)
                    {
                        var current = 0;
                        for (var j = 0; j < book.Dimensions; j++)
                        {
                            var val = e[j] - vec[offset + j];
                            current += val * val;
                        }

                        if ((best == -1) || (current < best))
                        {
                            for (var x = 0; x < e.Length; x++)
                            {
                                p[x] = e[x];
                            }

                            best  = current;
                            index = i;
                        }
                    }

                    // assumes the value patterning created by the tools in vq
                    var l = 0;
                    while (e[l] >= maxval)
                    {
                        e[l++] = 0;
                    }

                    if (e[l] >= 0)
                    {
                        e[l] += book.Delta;
                    }

                    e[l] = -e[l];
                }
            }

            if (index > -1)
            {
                for (i = 0; i < book.Dimensions; i++)
                {
                    vec[offset++] -= p[i];
                }
            }

            return(index);
        }
Esempio n. 24
0
            public static int UpdateCodeBook(byte[] p, CodeBook cb)
            {
                cb.Ticks = Time;

                byte[] high, low;
                high = new byte[3];
                low = new byte[3];

                for (int i = 0; i < 3; i++)
                {
                    high[i] = (byte)((p[i] + 10 > 255) ? 255 : (p[i] + 10));
                    low[i] = (byte)((p[i] - 10 < 0) ? 0 : (p[i] - 10));
                }

                int matchChannel;
                int matchedIndex = 0;
                for (int i = 0; i < cb.Entries.Count; i++)
                {
                    matchChannel = 0;
                    CodeElement e = (CodeElement)cb.Entries[i];
                    for (int k = 0; k < 3; k++)
                    {
                        if (e.learnLow[k] <= p[k] && e.learnHigh[k] >= p[k])
                        {
                            matchChannel++;
                        }
                    }

                    if (matchChannel == 3)
                    {
                        e.LastUpdate = cb.Ticks;
                        for (int k = 0; k < 3; k++)
                        {
                            if (e.max[k] < p[k])
                            {
                                e.max[k] = p[k];
                            }
                            else if (e.min[k] > p[k])
                            {
                                e.min[k] = p[k];
                            }

                            if (e.learnHigh[k] < high[k]) e.learnHigh[k] += 1;
                            if (e.learnLow[k] < low[k]) e.learnLow[k] -= 1;
                        }
                        matchedIndex = i;
                        break;
                    }

                    if (i == cb.Entries.Count - 1)
                    {
                        matchedIndex = cb.Entries.Count;
                        break;
                    }
                }

                for (int s = 0; s < cb.Entries.Count; s++)
                {
                    CodeElement e = (CodeElement)cb.Entries[s];
                    long negRun = cb.Ticks - e.LastUpdate;
                    if (e.Stale < negRun)
                    {
                        e.Stale = negRun;
                    }
                }

                if (matchedIndex == cb.Entries.Count)
                {
                    CodeElement enew = new CodeElement();
                    for (int k = 0; k < 3; k++)
                    {
                        enew.learnHigh[k] = high[k];
                        enew.learnLow[k] = low[k];
                        enew.max[k] = p[k];
                        enew.min[k] = p[k];
                    }
                    enew.LastUpdate = cb.Ticks;
                    enew.Stale = 0;

                    cb.Entries.Add(enew);
                }

                return matchedIndex;
            }