public ERROR_CODES SetupScopeRead(ScopeInputChannel inputChannel, int pointsToRead, ScopeSampleMode sampleMode, int measurementToAverage)
 {
     return ERROR_CODES.AA_OK;
 }
        public ERROR_CODES ReadScopeData(ScopeInputChannel inputChannel, int pointsToRead, ScopeSampleMode sampleMode, int measurementToAverage, out IEnumerable<double> signalData)
        {
            lock (this)
            {
                if (asyncReading)
                {
                    //disable locking event pooling on modulator data until end of scope reading data.
                    zilockAmp.DisconnectEventListener();
                }
                short[] data = new short[pointsToRead + 5 * BufferSize];
                zilockAmp.PollScopeData(ref data, pointsToRead, BufferSize, SampleRate.GetHashCode(),  15000);
                bool phaseFixDataBeforeAverage = false;
                double[] phaseFixSignal = new double[0];
                int dataLenToAverage = data.Length;

                if (phaseFixDataBeforeAverage)
                {
                    int bufferCount = data.Length / BufferSize;
                    double[] bufferData = new double[BufferSize];
                    double[] ZeroPhaseOutputReference = FunctionGenerator.PositivieNormalizeCosine(PointsInCycle);
                    phaseFixSignal = new double[BufferSize];

                    for (int i = 0; i < bufferCount; i++)
                    {
                        double signalAmplitude;
                        Array.Copy(data, i * BufferSize, bufferData, 0, BufferSize);
                        double[] noramlizeSignal = SignalDigitizer.NormalizeSignal(bufferData, new DigitzerParameters(DigitizingMethods.AverageMinMax, 0.05, 0.05, 1, false), out signalAmplitude).ToArray();
                        PhaseData OutputPhase = PhaseAnalyzer.FindBinPhaseByShift(noramlizeSignal, ZeroPhaseOutputReference, PointsInCycle, -50, 50, 0, false);
                        double[] NormalizedZeroPhaseOutput = new double[PointsInCycle];

                        for (int idx = 0; idx < BufferSize; idx++)
                        {
                            int phasedIndex = (idx + OutputPhase.PhaseInBins) % BufferSize;
                            phasedIndex = (phasedIndex > BufferSize) ? phasedIndex - BufferSize : phasedIndex;
                            phasedIndex = (phasedIndex < 0) ? phasedIndex + BufferSize : phasedIndex;
                            phaseFixSignal[idx] += bufferData[phasedIndex];
                            if (i == bufferCount - 1)
                            {
                                phaseFixSignal[idx] = phaseFixSignal[idx] / bufferCount;
                            }
                        }

                    }
                    dataLenToAverage = phaseFixSignal.Length;
                    //MatlabFigure dFig = new MatlabFigure();
                    //double[] allData = data.Select(x1 => (double)x1).ToArray();
                    //dFig.AddPlot("lockin scope", allData, false);
                    //dFig.Show();
                }
                int N = (dataLenToAverage / PointsInCycle);
                double[] avgData = new double[PointsInCycle];
                if (phaseFixDataBeforeAverage)
                {
                    for (int j = 0; j < PointsInCycle; j++)
                    {
                        for (int k = 0; k < N; k++)
                        {
                            avgData[j] += phaseFixSignal[k * PointsInCycle + j];
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < PointsInCycle; j++)
                    {
                        for (int k = 0; k < N; k++)
                        {
                            avgData[j] += data[k * PointsInCycle + j];
                        }
                    }
                }
                signalData  = avgData.Select(x=> Math.Round(x/ N)).ToArray();
                double[] lpSignal;
                SignalTranformation.SGLowPassFilter(signalData.ToArray(), 2, 21, true, out lpSignal);
                signalData = lpSignal;
                if (asyncReading)
                {
                    //reconnect locking event pooling on modulator data.
                    zilockAmp.SubscribeEventListener();
                }
            }
            return ERROR_CODES.AA_OK;
        }
        public ERROR_CODES SetupScopeRead(ScopeInputChannel inputChannel,  int pointsToRead, ScopeSampleMode sampleMode, int measurementToAverage)
        {
            ERROR_CODES status = ERROR_CODES.AA_OK;
            string command;

            command = String.Format(":DATa:SOUrce CH{0}\n", inputChannel.GetHashCode());
            status |= Write(command);
            command = ":DATa:ENCdg ASCIi\n";
            status |= Write(command);
            command = ":DATa:WIDth 2\n";
            status |= Write(command);
            command = ":HEADer 0\n";
            status |= Write(command);
            command = ":ACQuire:MODe HIRes\n";
            status |= Write(command);
            System.Threading.Thread.Sleep(10);
            if (sampleMode == ScopeSampleMode.Average)
            {
                command = ":ACQuire:MODe AVErage\n";
                status |= Write(command);
                command = String.Format(":ACQuire:NUMAVg {0}\n", measurementToAverage);
                status |= Write(command);
                System.Threading.Thread.Sleep(100);
            }
            return status;
        }
 public ERROR_CODES ReadScopeData(ScopeInputChannel inputChl, out double[] data)
 {
     ERROR_CODES status = ERROR_CODES.AA_OK;
     IEnumerable<double> tmp;
     data = new double[0];
     if (workWithLockinScope)
     {
         status = Scope.ReadScopeData(inputChl, scopeCycleSize * 541, ScopeSampleMode.Average, 512, out tmp);
     }
         else
     {
         status = Scope.ReadScopeData(inputChl, 100000, ScopeSampleMode.Average, 512, out tmp);
     }
     if (status == ERROR_CODES.AA_OK)
         data = tmp.ToArray();
     return status;
 }
        public ERROR_CODES ReadScopeData(ScopeInputChannel inputChannel, int pointsToRead, ScopeSampleMode sampleMode, int measurementToAverage, out IEnumerable<Double> signalData)
        {
            ERROR_CODES status = ERROR_CODES.AA_OK;
            String command, scopeReply = "";
            signalData = null;

            if (this.IsOpen)
            {
                command = String.Format(":DATa:SOUrce CH{0}\n", inputChannel.GetHashCode());
                status |= Write(command);
                command = String.Format(":DATa:STARt {0}\n", 1);
                status |= Write(command);
                command = String.Format(":DATa:STOP {0}\n", pointsToRead);
                status |= Write(command);
                command = ":DATa:ENCdg ASCIi\n";
                status |= Write(command);
                command = ":DATa:WIDth 2\n";
                status |= Write(command);
                command = ":HEADer 0\n";
                status |= Write(command);
                bool bSkipMode = false ;
                if (!bSkipMode)
                {
                    command = ":ACQuire:MODe HIRes\n";
                    status |= Write(command);
                    System.Threading.Thread.Sleep(10);
                    if (sampleMode == ScopeSampleMode.Average)
                    {
                        command = ":ACQuire:MODe AVErage\n";
                        status |= Write(command);
                        command = String.Format(":ACQuire:NUMAVg {0}\n", measurementToAverage);
                        status |= Write(command);
                        System.Threading.Thread.Sleep(500);
                    }
                }
                if (status == ERROR_CODES.AA_OK)
                {
                    status = Query(":CURVe?\n", out scopeReply);
                }
                if (status == ERROR_CODES.AA_OK)
                {
                    //string[] tmp = scopeReply.Split(',');
                    signalData = scopeReply.Split(',').Select(stringSample => Double.Parse(stringSample)).ToArray();
                }
                else
                {
                    status = ERROR_CODES.INSTRUMENT_READ_WRITE_FAIL;
                }
            }
            else
            {
                status = ERROR_CODES.INSTRUMENT_NOT_CONNECTED;
                UpdateErrorBuffers("Tried to read scope data while it was not connected.", "Tried to read scope data while it was not connected.");
            }
            return status;
        }