Пример #1
0
        public void ProcessSamples(byte[] packet)
        {
            float timeInterval = 1.0f / SampleRate;
            float[] samples = new float[packet.Length/2];

            for (int k = 0; k < packet.Length / 2; k++)
            {
                short sh = BitConverter.ToInt16(packet, k*2);
                float currentValue = sh/1.0f;

                if (AdcMcp)
                {
                    currentValue = currentValue/32768.0f/1/1.5f*1.2f*23;
                    currentValue /= 10; // shunt

                    currentValue /= Gain; // gain

                    var voltageCorrection = LastHighresVoltage / 15220.588235294117647058823529412f;
                    currentValue -=voltageCorrection;

                    currentValue *= 1000000;
                    if (Gain == 10)
                        currentValue -= 148.3f;
                    if (Gain == 1)
                        currentValue -= 1300;
                    if (Gain == 1000)
                    {
                        currentValue -= 3.6f;

                        var adc = k%4;
                        switch(adc)
                        {
                            case 0:
                                currentValue += 0.18f;
                                break;
                            case 1:
                                currentValue += 0.12f;
                                break;
                            case 2:
                                currentValue += 0.37f;
                                break;
                            case 3:
                                currentValue += 0.34f;
                                break;
                        }
                    }
                    if (Gain == 100)
                    {
                        currentValue -= 2.5f;
                    }

                    if (Gain == 1000)
                        currentValue /= 1.03f;
                    else
                        currentValue /= 1.02f;
                    currentValue /= 1000000;
                }
                else
                {
                    currentValue = (currentValue - 2048)/2048.0f;
                    currentValue *= 2.5f*23;

                    currentValue /= 10; // shunt

                    currentValue /= Gain; // gain

                    currentValue -= LastHighresVoltage/15220.588235294117647058823529412f;
                    currentValue /= 2;

                    if (Gain == 1000)
                        currentValue -= 0.000149f;
                    if (Gain == 10)
                        currentValue -= 0.0013f;
                }
                if (currentValue < -20.0f/1000 || (currentValue > 20.0f / 1000 && currentValue < 100.0f/1000))
                {
                    err++;
                }
                samples[k] = currentValue;
            }

            var dpk = new DataPacket(samples, DataType.DutCurrent, (float)timeInterval);

            if (Data != null)
                Data(dpk);
        }
        void ProcessWaveform(DataPacket data)
        {
            AcquisitionLength = 1+(int)(AcquisitionTime*Source.SampleRate);
            if (AcquisitionLength < 15) AcquisitionLength = 15;

            Pretrigger = (int) (PretriggerTime*Source.SampleRate) + AcquisitionLength / 2;

            lock (samplesOverflowSink)
            {
                samplesOverflowSink.AddRange(data.Samples);

                var pretriggerSampleDepth = (int) Source.SampleRate;

                if (samplesOverflowSink.Count > pretriggerSampleDepth)
                {
                    samplesOverflowSink.RemoveRange(0, samplesOverflowSink.Count - pretriggerSampleDepth);
                }

                var triggerInfo = new TriggerInfo(true, 0); // dummy

                while (triggerInfo.Triggered && samplesOverflowSink.Count >= AcquisitionLength + Pretrigger) // always keep 1 waveform extra in memory
                {
                    triggerInfo = Trigger.IsTriggered(samplesOverflowSink, 0);

                    // Skip the waveform (if we keep trigger) till we satisfy the pretrigger requirement.
                    while (triggerInfo.Triggered && triggerInfo.TriggerPoint < Pretrigger)
                    {
                        triggerInfo = Trigger.IsTriggered(samplesOverflowSink, triggerInfo.TriggerPoint );
                    }
                    // Only process when actually triggered.
                    if (!triggerInfo.Triggered) break;

                    // We take the waveform from the buffer at the triggerpoint, but also subtracting the pre-trigger sample depth.
                    var waveformStart = triggerInfo.TriggerPoint - Pretrigger;

                    // Get these samples, and put them into the waveform.
                    var waveformSamples = samplesOverflowSink.Skip(waveformStart).Take(AcquisitionLength).ToArray();
                    if (waveformSamples.Length != AcquisitionLength) break;
                    var waveform = new Waveform(1, AcquisitionLength);
                    var timestamp = -Pretrigger*data.TimeInterval;
                    foreach(var s in waveformSamples)
                    {
                        timestamp += data.TimeInterval;
                        waveform.Store(timestamp, new float[1] {s});
                    }

                    FireTriggeredWaveform(waveform);

                    samplesOverflowSink.RemoveRange(0, waveformStart+AcquisitionLength-1);

                }
            }
        }
        void Source_Data(DataPacket data)
        {
            // Store each sample.
            foreach (var s in data.Samples)
            {
                overviewTimestamp += data.TimeInterval;
                overviewWf.Store(overviewTimestamp, new float[1] { s });
            }

            if (DateTime.Now.Subtract(overviewWfLastCapture).TotalMilliseconds >= LongAcquisitionTime *1000)
            {
                if (OverviewWaveform != null)
                    OverviewWaveform(overviewWf);

                overviewWf = new Waveform(1, 6000000);
                overviewTimestamp = 0.0f;
                overviewWfLastCapture = DateTime.Now;

            }
        }
        private void GenerateDataElapse(object sender, EventArgs eventArgs)
        {
            var r = new Random();
            int generatingSamples = samplesPerSecond /100;

            float[] samples = new float[generatingSamples];
            var timeInterval = 1.0f/samplesPerSecond;

            for (int i = 0; i < generatingSamples; i++)
            {
                var k = i + sampleCounter;

                var t = timeInterval*k;

                accumulatedPhase += timeInterval*1.0f*freq;
                accumulatedPhase %= 1;

                var s = Math.Sin(2*Math.PI*accumulatedPhase);
                s *= 1/Math.Sqrt(2);
                s += r.Next(-1005, 1005)/100000.0f;

                samples[i] = (float) (s);
            }
            sampleCounter += generatingSamples;

            var dpk = new DataPacket(samples, DataType.DutCurrent, (float)timeInterval);

            if (Data != null)
                Data(dpk);
        }