Test and Measurement Forum

Sample rate in streaming mode

Post general discussions on using our drivers to write your own software here

Sample rate in streaming mode

Postby lyurealm » Tue May 03, 2016 10:00 am

Hi! I have a question regarding your PS4000 API. I'm a C# beginner programmer and I'd like to make a capture software that allows the final user to select sample rate and the total capture time.

I'm looking at your C# Console Example, but I don't really understand how to configure the sample rate and the total capture time.

Code: Select all
uint sampleInterval = 1;
(...)
status = Imports.RunStreaming(_handle, ref sampleInterval, Imports.ReportedTimeUnits.MicroSeconds, preTrigger, 1000000 - preTrigger, true, 1000, sampleCount);


Let's say I want to capture 100 seconds at 10 kS/s. Could you please explain to me how to should I setup the timebase (maybe 999 for 10e-04?), the sampleInterval, the ReportedTimeUnits, ratio, etc?

I don't think I'm getting it, even though I've been reading about this since yesterday.

Have a good day, thank you in advance!
lyurealm
Newbie
 
Posts: 0
Joined: Wed Nov 11, 2015 1:10 pm

Re: Sample rate in streaming mode

Postby lyurealm » Tue May 03, 2016 2:39 pm

Okay, I think I know partially the answer to my question. I'll post here just in case it may help others.
sampleInterval is the interval between one sample and another, and sampleIntervalTimeUnits it's the units of time used for that time gap.

So if you want a sample rate of 10000 samples/second, you can select as sampleIntervalTimeUnits MicroSeconds and as sampleInterval 100, so you have a sample every 100 MicroSeconds, 1e-04 seconds, and then 1/1e-04 = 10.000 samples/second.

So, for my example, I can set the downSamplingRatio to 1 and the Max Samples to 100.000 so I will get 100.000/10.000 = 10 secs:

Code: Select all
uint sampleInterval = 100;
(...)
status = Imports.RunStreaming(_handle, ref sampleInterval, Imports.ReportedTimeUnits.MicroSeconds, preTrigger, 100000 - preTrigger, true, 1, sampleCount);


I think this is pretty much correct, the only question left is about the timeBase. Can someone please explain me how this affects the streaming? Also, my program is working fine if I use the following code:

Code: Select all
status = Imports.RunStreaming(_handle, ref sampleInterval, Imports.ReportedTimeUnits.MicroSeconds, preTrigger, 1000 - preTrigger, true, 1, sampleCount)


But I get an exception if I increase from 1.000 to 10.000:

Code: Select all
status = Imports.RunStreaming(_handle, ref sampleInterval, Imports.ReportedTimeUnits.MicroSeconds, preTrigger, 10000 - preTrigger, true, 1, sampleCount)


The exception is: IndexOutOfRangeException. Do I need to initialize the buffer array to the number of samples I'm capturing?

Thank you in advance.
lyurealm
Newbie
 
Posts: 0
Joined: Wed Nov 11, 2015 1:10 pm

Re: Sample rate in streaming mode

Postby Hitesh » Wed May 04, 2016 3:48 pm

Hi lyreualm,

The ADC can sample data at certain sampling intervals - these are defined by timebase indices and formulae outlined in the Programmer's Guide for block mode captures. Streaming mode captures will also use the same sampling intervals but the differences are that the fastest sampling intervals will not be available due to the bandwidth of the USB bus and also as you mentioned, you specify the interval and time unit when calling the RunStreaming() function (there is no need to provide a timebase index).

Please note that the driver will return back the actual sampling interval used if the sampling interval specified is not one that the device supports. If you find that you are getting 0 returned for sampleInterval, multiply the requested interval by 1000 and change the time unit to the next smallest (e.g. from Imports.ReportedTimeUnits.MilliSeconds to Imports.ReportedTimeUnits.MicroSeconds), then check the value again.

With regards to the IndexOutOfBoundsException, what is the size of the buffers that you are setting? Are you copying data from the driver buffers to temporary application buffers in the callback function? Where in the code is the exception thrown?

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2008
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Sample rate in streaming mode

Postby lyurealm » Thu May 05, 2016 9:44 am

Thank you very much for your answer, Hitech.

So, as far as I can understand, I don't need timebase for streaming, right? The sample rate is determined only by the sampleInterval. If the sampleInterval selected is one supported by the Oscilloscope, then it will use it, and if not, it will return a 0.

With regards to the buffers, I'm using the Console C# example provided by the PicoSDK.

In this example, there's a line of code, which, I guess, sets the appBuffers/buffers sizes.

Code: Select all
uint sampleCount = BUFFER_SIZE * 10;
(...)
appBuffers[ch] = new short[sampleCount];
appBuffers[ch + 1] = new short[sampleCount];

buffers[ch] = new short[sampleCount];
buffers[ch + 1] = new short[sampleCount];


Of course, if (maxSamples / downsamplingRatio) exceeds sampleCount, I will get IndexOutOfRangeException. The C# Console Example uses the same size for buffers and appBuffers, but I guess that I only need appBuffers to have the same size as samples to be captured, right?

I will have, however, to modify this from the StreamingCallBack:

Code: Select all
Array.Copy(buffers[ch], _startIndex, appBuffers[ch], _startIndex, _sampleCount); //max
Array.Copy(buffers[ch + 1], _startIndex, appBuffers[ch + 1], _startIndex, _sampleCount);//min


Because this assumes that buffers and appBuffers sizes are the same.

Thank you in advance for your help, have a good day!
lyurealm
Newbie
 
Posts: 0
Joined: Wed Nov 11, 2015 1:10 pm

Re: Sample rate in streaming mode

Postby Hitesh » Thu May 05, 2016 2:54 pm

Hi lyurealm,

That's correct, you do not need to use a timebase index for streaming.

Just to clarify, the PICO_INVALID_SAMPLE_INTERVAL status code (refer to the PicoStatus.h file) will be returned from the ps4000RunStreaming() function call.

Which device are you using?

With regards to sampleInterval being returned as 0, we can use the (now obsolete) PicoScope 4226/7 models as an example. From the Timebases section, we can determine that a 1 us interval is not possible, so if you were to set the parameters for the ps4000RunStreaming() function for 1 microsecond, this would return 0 for the sampleInterval but the driver would select the next fastest sampling interval (992 ns).

Based on this you can set the sampleInterval value to 1000, and the time units to Imports.ReportedTimeUnits.NanoSeconds, and the driver should report back 992 for sampleInterval.

With regards to the value of sampleCount, this should be large enough to accommodate the number of samples that will be collected per channel in one iteration of the loop that calls the ps4000GetStreamingLatestValues(). It does not have to be the same as maxSamples (it should really be smaller if you are collecting a lot of data).

If you wish you can change the appBuffers to be a larger buffer and in the callback copy to the correct position in the appBuffers array for the channel as you collect the data.

Regards,
Hitesh

Technical Specialist
Pico Technology
Hitesh
Site Admin
Site Admin
 
Posts: 2008
Joined: Tue May 31, 2011 3:43 pm
Location: St. Neots, Cambridgeshire

Re: Sample rate in streaming mode

Postby lyurealm » Thu May 05, 2016 3:43 pm

Thank you again for your answer Hitech.

I see. I have developed software with your driver in Matlab, so I knew that AppBuffers and Buffers are different things. I think that I should rewrite that code so I have a small buffer that gets the data collected in each iteration and a "big" buffer (AppBuffer), with as much points as data I want to collect, to accommodate all the data.

Thank you for all your help, much appreciated. I hope you have a good day.
lyurealm
Newbie
 
Posts: 0
Joined: Wed Nov 11, 2015 1:10 pm


Return to General Software Discussions

Who is online

Users browsing this forum: No registered users and 0 guests