Ejemplo n.º 1
0
        //----------------------------------------------------------------------------
        //
        // Function    :  ConfigureBoard
        //
        // Description :  Configure sample rate, input, and trigger settings
        //
        //----------------------------------------------------------------------------

        static public unsafe bool ConfigureBoard(IntPtr boardHandle, IntPtr fftHandle, UInt32 recordLength_samples)
        {
            UInt32 retCode;

            // TODO: Specify the sample rate (in samples per second),
            //       and appropriate sample rate identifier
            samplesPerSec = 4000000000.0;
            UInt32 sampleRateId = AlazarAPI.SAMPLE_RATE_4000MSPS;

            // TODO: Select clock parameters as required.
            retCode =
                AlazarAPI.AlazarSetCaptureClock(
                    boardHandle,
                    AlazarAPI.INTERNAL_CLOCK,
                    sampleRateId,
                    AlazarAPI.CLOCK_EDGE_RISING,
                    0
                    );
            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarSetCaptureClock failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }


            // TODO: Select channel A input parameters as required
            retCode =
                AlazarAPI.AlazarInputControlEx(
                    boardHandle,
                    AlazarAPI.CHANNEL_A,
                    AlazarAPI.DC_COUPLING,
                    AlazarAPI.INPUT_RANGE_PM_400_MV,
                    AlazarAPI.IMPEDANCE_50_OHM
                    );
            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarInputControlEx failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }


            // TODO: Select channel B input parameters as required
            retCode =
                AlazarAPI.AlazarInputControlEx(
                    boardHandle,
                    AlazarAPI.CHANNEL_B,
                    AlazarAPI.DC_COUPLING,
                    AlazarAPI.INPUT_RANGE_PM_400_MV,
                    AlazarAPI.IMPEDANCE_50_OHM
                    );
            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarInputControlEx failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }



            // TODO: Select trigger inputs and levels as required
            retCode =
                AlazarAPI.AlazarSetTriggerOperation(
                    boardHandle,
                    AlazarAPI.TRIG_ENGINE_OP_J,
                    AlazarAPI.TRIG_ENGINE_J,
                    AlazarAPI.TRIG_CHAN_A,
                    AlazarAPI.TRIGGER_SLOPE_POSITIVE,
                    150,
                    AlazarAPI.TRIG_ENGINE_K,
                    AlazarAPI.TRIG_DISABLE,
                    AlazarAPI.TRIGGER_SLOPE_POSITIVE,
                    128
                    );
            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarSetTriggerOperation failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }

            // TODO: Select external trigger parameters as required
            retCode =
                AlazarAPI.AlazarSetExternalTrigger(
                    boardHandle,
                    AlazarAPI.DC_COUPLING,
                    AlazarAPI.ETR_TTL
                    );

            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarSetExternalTrigger failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }

            // TODO: Set trigger delay as required.
            double triggerDelay_sec     = 0;
            UInt32 triggerDelay_samples = (UInt32)(triggerDelay_sec * samplesPerSec + 0.5);

            retCode =
                AlazarAPI.AlazarSetTriggerDelay(
                    boardHandle,
                    triggerDelay_samples
                    );
            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarSetTriggerDelay failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }

            // TODO: Set trigger timeout as required.

            // NOTE:
            // The board will wait for a for this amount of time for a trigger event.
            // If a trigger event does not arrive, then the board will automatically
            // trigger. Set the trigger timeout value to 0 to force the board to wait
            // forever for a trigger event.
            //
            // IMPORTANT:
            // The trigger timeout value should be set to zero after appropriate
            // trigger parameters have been determined, otherwise the
            // board may trigger if the timeout interval expires before a
            // hardware trigger event arrives.

            double triggerTimeout_sec    = 0;
            UInt32 triggerTimeout_clocks = (UInt32)(triggerTimeout_sec / 10E-6 + 0.5);

            retCode =
                AlazarAPI.AlazarSetTriggerTimeOut(
                    boardHandle,
                    triggerTimeout_clocks
                    );
            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarSetTriggerTimeOut failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }

            // TODO: Configure AUX I/O connector as required
            retCode =
                AlazarAPI.AlazarConfigureAuxIO(
                    boardHandle,
                    AlazarAPI.AUX_OUT_TRIGGER,
                    0
                    );
            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarConfigureAuxIO failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }

            // FFT Configuration
            UInt32 dspModuleId;
            UInt16 versionMajor;
            UInt16 versionMinor;
            UInt32 maxLength;
            UInt32 reserved0;
            UInt32 reserved1;

            retCode = AlazarAPI.AlazarDSPGetInfo(
                fftHandle,
                &dspModuleId,
                &versionMajor,
                &versionMinor,
                &maxLength,
                &reserved0,
                &reserved1
                );
            if ((AlazarAPI.DSP_MODULE_TYPE)dspModuleId != AlazarAPI.DSP_MODULE_TYPE.DSP_MODULE_FFT)
            {
                Console.WriteLine("Error: DSP module is not FFT");
                return(false);
            }

            UInt32 fftLength_samples = 1;

            while (fftLength_samples < recordLength_samples)
            {
                fftLength_samples *= 2;
            }

            // TODO: Select the window function type
            AlazarAPI.DSP_WINDOW_ITEMS windowType = AlazarAPI.DSP_WINDOW_ITEMS.DSP_WINDOW_HANNING;

            // Create and fill the window function
            float[] window = new float[fftLength_samples];
            fixed(float *pWindow = &window[0])
            {
                retCode = AlazarAPI.AlazarDSPGenerateWindowFunction(
                    (UInt32)windowType,
                    pWindow,
                    recordLength_samples,
                    fftLength_samples - recordLength_samples
                    );
                if (retCode != AlazarAPI.ApiSuccess)
                {
                    Console.WriteLine("Error: AlazarDSPGenerateWindowFunction failed -- " +
                                      AlazarAPI.AlazarErrorToText(retCode));
                    return(false);
                }

                // Set the window function
                retCode = AlazarAPI.AlazarFFTSetWindowFunction(
                    fftHandle,
                    fftLength_samples,
                    pWindow,
                    (float *)0
                    );
                if (retCode != AlazarAPI.ApiSuccess)
                {
                    Console.WriteLine("Error: AlazarFFTSetWindowFunction failed -- " +
                                      AlazarAPI.AlazarErrorToText(retCode));
                    return(false);
                }
            }

            // TODO: Select the background subtraction record
            Int16[] backgroundSubtractionRecord = new Int16[recordLength_samples];

            // Background subtraction
            fixed(Int16 *pBackgroundSubtractionRecord = &backgroundSubtractionRecord[0])
            {
                retCode = AlazarAPI.AlazarFFTBackgroundSubtractionSetRecordS16(
                    fftHandle,
                    pBackgroundSubtractionRecord,
                    recordLength_samples
                    );
                if (retCode != AlazarAPI.ApiSuccess)
                {
                    Console.WriteLine("Error: AlazarFFTBackgroundSubtractionSetRecordS16 failed -- " +
                                      AlazarAPI.AlazarErrorToText(retCode));
                    return(false);
                }
            }

            retCode = AlazarAPI.AlazarFFTBackgroundSubtractionSetEnabled(
                fftHandle,
                true
                );
            if (retCode != AlazarAPI.ApiSuccess)
            {
                Console.WriteLine("Error: AlazarFFTBackgroundSubtractionSetEnabled failed -- " +
                                  AlazarAPI.AlazarErrorToText(retCode));
                return(false);
            }

            return(true);
        }