Ejemplo n.º 1
0
        internal void SetData(ChannelDataSourceScope type, Channel ch, Array arr, bool partial = false)
        {
            if (arr == null)
            {
                return;
            }

            lock (dataLock)
            {
                if (arr.GetType().GetElementType() != ChannelDataTypes[ch.GetType()])
                {
                    throw new Exception("Invalid data type " + arr.GetType().GetElementType().ToString() + " for channel of type " + ch.GetType().ToString());
                }

                data[type][ch]      = new ChannelData(type, ch, arr, partial, samplePeriod[type], offset[type]);
                this.LastDataUpdate = DateTime.Now;

                //if update for LA channel data is received: delete all Digital channel data, as they are now outdated and need to be recalculated
                if (ch is LogicAnalyserChannel)
                {
                    for (int i = data[type].Count - 1; i >= 0; i--)
                    {
                        if (data[type].ElementAt(i).Key is DigitalChannel)
                        {
                            data[type].Remove(data[type].ElementAt(i).Key);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        internal void SetData(DataSourceType type, Channel ch, Array arr, bool partial = false)
        {
            lock (dataLock)
            {
                if (arr.GetType().GetElementType() != ChannelDataTypes[ch.GetType()])
                {
                    throw new Exception("Invalid data type " + arr.GetType().GetElementType().ToString() + " for channel of type " + ch.GetType().ToString());
                }

                data[type][ch] = new ChannelData(type, ch, arr, partial, samplePeriod[type], offset[type]);
            }
        }
Ejemplo n.º 3
0
 private ChannelData ExtractBitsFromLogicAnalyser(DigitalChannel ch, ChannelDataSourceScope t)
 {
     lock (dataLock)
     {
         //FIXME: expand for more than 1 LA
         if (!data[t].ContainsKey(LogicAnalyserChannel.LA))
         {
             return(null);
         }
         Func <byte, bool> bitFilter = new Func <byte, bool>(x => Utils.IsBitSet(x, ch.Value));
         var laChannel = data[t][LogicAnalyserChannel.LA];
         data[t][ch] = new ChannelData(t, ch, Utils.TransformArray(laChannel.array, bitFilter), laChannel.partial, samplePeriod[t], offset[t]);
         return(data[t][ch]);
     }
 }
Ejemplo n.º 4
0
        internal void SetData(ChannelDataSourceScope type, Channel ch, Array arr, bool partial = false)
        {
            if (arr == null)
            {
                return;
            }

            lock (dataLock)
            {
                if (arr.GetType().GetElementType() != ChannelDataTypes[ch.GetType()])
                {
                    throw new Exception("Invalid data type " + arr.GetType().GetElementType().ToString() + " for channel of type " + ch.GetType().ToString());
                }

                data[type][ch]      = new ChannelData(type, ch, arr, partial, samplePeriod[type], offset[type]);
                this.LastDataUpdate = DateTime.Now;
            }
        }
Ejemplo n.º 5
0
        internal void AddData(ChannelDataSourceScope type, Channel ch, Array arrayToAdd)
        {
            ChannelData arrayWeHad = GetData(type, ch);

            int MaxElements;

            if (type == ChannelDataSourceScope.Acquisition)
            {
                MaxElements = (int)AcquisitionSamples;
            }
            else if (type == ChannelDataSourceScope.Overview)
            {
                MaxElements = OVERVIEW_SAMPLES;
            }
            else if (type == ChannelDataSourceScope.Viewport)
            {
                MaxElements = ViewportSamples;
            }
            else
            {
                throw new Exception("Unhandled type");
            }

            int arrayToAddElements = Math.Min(arrayToAdd.Length, MaxElements);
            int arrayWeHadElements = arrayWeHad == null ? 0 : Math.Min(arrayWeHad.array.Length, MaxElements - arrayToAddElements);

            this.LatestChunkSize = arrayToAddElements;

            //When adding all elements from new array, don't bother copying things togehter
            if (arrayWeHadElements <= 0 && arrayToAddElements == arrayToAdd.Length)
            {
                SetData(type, ch, arrayToAdd, arrayToAdd.Length < MaxElements);
                return;
            }

            Array arrayResult = Array.CreateInstance(arrayToAdd.GetType().GetElementType(), arrayToAddElements + arrayWeHadElements);

            if (arrayWeHadElements > 0)
            {
                Array.Copy(arrayWeHad.array, arrayWeHad.array.Length - arrayWeHadElements, arrayResult, 0, arrayWeHadElements);
            }
            Array.Copy(arrayToAdd, arrayToAdd.Length - arrayToAddElements, arrayResult, arrayWeHadElements, arrayToAddElements);
            SetData(type, ch, arrayResult, arrayResult.Length < MaxElements);
        }
Ejemplo n.º 6
0
        public static void ComputeFrequencyDutyCycle(ChannelData data, out double frequency, out double frequencyError, out double dutyCycle, out double dutyCycleError)
        {
            frequency = double.NaN;
            frequencyError = double.NaN;
            dutyCycle = double.NaN;
            dutyCycleError = double.NaN;

            bool[] digitized = data.array.GetType().GetElementType() == typeof(bool) ? (bool[])data.array : LabNation.Common.Utils.Schmitt((float[])data.array);

            List<double> edgePeriod = new List<double>();
            List<double> highPeriod = new List<double>();
            List<double> lowPeriod = new List<double>();

            int lastRisingIndex = 0;
            int lastFallingIndex = 0;
            double samplePeriod = data.samplePeriod;
            for (int i = 1; i < digitized.Length; i++)
            {
                //Edge detection by XOR-ing sample with previous sample
                bool edge = digitized[i] ^ digitized[i - 1];
                if (edge)
                {
                    //If we're high now, it's a rising edge
                    if (digitized[i])
                    {
                        if (lastRisingIndex > 0)
                            edgePeriod.Add((i - lastRisingIndex) * samplePeriod);
                        if (lastFallingIndex > 0)
                            lowPeriod.Add((i - lastFallingIndex) * samplePeriod);

                        lastRisingIndex = i;
                    }
                    else
                    {
                        if (lastFallingIndex > 0)
                            edgePeriod.Add((i - lastFallingIndex) * samplePeriod);
                        if (lastRisingIndex > 0)
                            highPeriod.Add((i - lastRisingIndex) * samplePeriod);
                        lastFallingIndex = i;
                    }
                }
            }

            if (edgePeriod.Count < 1)
                return;

            double average = edgePeriod.Average();

            if (highPeriod.Count > 0 && lowPeriod.Count > 0)
            {
                dutyCycle = highPeriod.Average() / average;
                dutyCycle += 1.0 - lowPeriod.Average() / average;
                dutyCycle *= 50;
            }

            double f = 1 / average;
            double fError = edgePeriod.Select(x => Math.Abs(1 / x - f)).Max();
            if (fError > f * 0.6)
                return;
            frequency = f;
            frequencyError = fError;
        }