Gaps in data from Picoscope 2204A

Post your .Net discussions here
Post Reply
kuro
Newbie
Posts: 0
Joined: Tue Dec 20, 2016 2:42 pm

Gaps in data from Picoscope 2204A

Post by kuro »

Hi.

I'm seeing odd gaps in the data I receive from a Picoscope 2204A every now and then. I am sampling repeatedly and most of the buffers I get are fine, but occasionally I see odd gaps:

Image

I extracted this data from Visual Studio via debugging. By buffer is 80k samples. I converted the samples to volts, but I checked and the original 16 bit values have the same -32768 gap.

The signal if from the Picoscope's own AWG. Checking it on an oscilloscope does not show any glitches, although looking at the data it's obvious it's a digital issue as it happens in the space of one sample with no over/undershoot or ringing.

My code:

Code: Select all

		unsafe void StreamingCallback(short **overviewBuffers, short overFlow, uint triggeredAt, short triggered, short auto_stop, uint nValues)
		{
			autostop = auto_stop != 0;

			if ((nValues > 0) && (sample_count < buffer.Length))
			{
				int max = (int)nValues + sample_count;
				if (max > buffer.Length)
					max = buffer.Length;
				
				for (int i = sample_count; i < max; i++)
					buffer[i] = overviewBuffers[0][i - sample_count];

				sample_count = max;
			}
		}

		unsafe public bool StreamBuffer(uint num_samples, out double[] output_buffer, uint sample_interval_us)
		{
			output_buffer = null;	

			if (Imports.ps2000_run_streaming_ns(handle, sample_interval_us, Imports.ReportedTimeUnits.MicroSeconds, num_samples, 1, 1, num_samples) == 0)
				return false;

			autostop = false;
			buffer = new short[num_samples];
			sample_count = 0;

			while (!autostop && (sample_count < num_samples))
			{
				Imports.ps2000_get_streaming_last_values(handle, StreamingCallback);
			}
			Imports.Stop(handle);

			double multiplier = inputRanges[(int)range] * (1 / (double)Imports.MaxValue);
			output_buffer = new double[num_samples];
			for (int i = 0; i < num_samples; i++)
				output_buffer[i] = (double)buffer[i] * multiplier;

			return true;
		}

Hitesh

Re: Gaps in data from Picoscope 2204A

Post by Hitesh »

Hi Kuro,

The PicoScope 2204A is a low memory device so will be susceptible to data being lost during fast streaming data collection, particularly if the application is not able to collect data from the driver fast enough.

In tests that we carried out here, we found that there is an improvement if a large buffer is set up for each channel prior to data collection and the callback mechanism then puts the data in the appropriate section of the array as required as shown below:

Code: Select all

/****************************************************************************
* StreamingCallback
* used by data streaming collection calls, on receipt of data.
* used to set global flags etc checked by user routines
****************************************************************************/
unsafe void StreamingCallback(short** overviewBuffers,
                                    short overFlow,
                                    uint triggeredAt,
                                    short triggered,
                                    short auto_stop,
                                    uint nValues)
{
    // used for streaming
    _autoStop = auto_stop != 0;

    // flags to show if & where a trigger has occurred
    _trig = triggered;
    _trigAt = triggeredAt;
    _nValues = nValues;

    if (nValues > 0 && !_appBufferFull)
    {
        try
        {
            for (int i = (int)_totalSampleCount; i < nValues + _totalSampleCount; i++)
            {
                for (int channel = 0; channel < _channelCount; channel++)
                {
                    if (Convert.ToBoolean(_channelSettings[channel].enabled))
                    {
                        _appBuffer[channel][i] = overviewBuffers[channel * 2][i - _totalSampleCount]; //Only copying max data from buffers
                    }
                }
            }
        }
        catch (Exception) // If trying to place data 
        {
            _appBufferFull = true;
            Console.WriteLine("Appbuffer full collection cancelled");
        }
    }

    _totalSampleCount += nValues;
}
This approach uses a counter to keep track of the total number of samples collected as the callback function could be called twice during a single iteration of the data collection loop.

The alternative, if you do not need to process the data while it is being collected is to call the ps2000_get_streaming_values_no_aggregation() function post-capture to retrieve the raw data values from the driver.

If you have installed the PicoSDK, you should find the example in the C:\PicoSDK\PS2000\C# console folder.

Regards,

kuro
Newbie
Posts: 0
Joined: Tue Dec 20, 2016 2:42 pm

Re: Gaps in data from Picoscope 2204A

Post by kuro »

Thanks for the advice, Hitesh.

I tried using ps2000_get_streaming_values_no_aggregation() instead, but the result is the same. On a hunch I tried inserting a Thread.Sleep(10), which reduced the frequency of the glitches greatly. However, they are still there occasionally. The frequency increases when other tasks are active, such as typing into this browser window.

In other words the driver seems to be very sensitive to CPU load. I suppose that is unsurprising since, as you say, it is a low memory device and the buffer is only enough for 4ms of data.

I tried a different approach. PS2000_LOST_DATA is missing from PS2000Imports.cs (as is ps2000_get_streaming_values_no_aggregation() and a few other functions) so I added it. I can now detect when a buffer is partially lost and simply do another measurement.

Hitesh

Re: Gaps in data from Picoscope 2204A

Post by Hitesh »

Hi kuro,

The PS2000_LOST_DATA constant has been added to the PS2000Imports.cs file here.

What sampling rate are you using? The CPU load will have an impact on the data collection.

Using deeper memory PicoScope models might be better as they also use a different driver model which would give relatively better performance in streaming mode data collection.

Regards,

Post Reply