Sampling rate problem when using picosdk library of Python

Post general discussions on using our drivers to write your own software here
Post Reply
bzy
Newbie
Posts: 0
Joined: Wed Feb 22, 2023 6:44 am

Sampling rate problem when using picosdk library of Python

Post by bzy »

Now I have a picoscope 4425A device, I copied the code by following the python example of github and want to run block mode.
As the equation of sampling rate in picoscope-4000-series-a-api-programmers-guide.pdf,
Sampling frequency (fS) = 80 MHz / (timebase+1),
I want to set frequency as 20000Hz, so timebase is 3999, am I right?
But if I give 100Hz input audio and capture 200 samples by 20000Hz sampling rate, the waveform of these 200 samples is not a full cycle.
Is there something wrong with my code?

Code: Select all

    chandle = ctypes.c_int16()
    status = {}
    powerStatus = 0
    status["openunit"] = ps.ps4000aOpenUnit(ctypes.byref(chandle), None)
    try:
        assert_pico_ok(status["openunit"])
    except: # PicoNotOkError:
        powerStatus = status["openunit"]

    if powerStatus == 286:
        status["changePowerSource"] = ps.ps4000aChangePowerSource(chandle, powerStatus)
    elif powerStatus == 282:
        status["changePowerSource"] = ps.ps4000aChangePowerSource(chandle, powerStatus)
    else:
        pass

    setCh = "setChA"                    
    setDataBuffers = "setDataBuffersA"
    chRange = 10
    status[setCh] = ps.ps4000aSetChannel(chandle, 0, 1, 1, chRange, 0)
    assert_pico_ok(status[setCh])

    preTriggerSamples = 100
    postTriggerSamples = 100
    maxSamples = preTriggerSamples + postTriggerSamples

    timebase = 3999
    timeIntervalns = ctypes.c_float()
    returnedMaxSamples = ctypes.c_int32()
    oversample = ctypes.c_int16(1)
    status["getTimebase2"]= ps.ps4000aGetTimebase2(chandle,timebase,maxSamples, ctypes.byref(timeIntervalns), ctypes.byref(returnedMaxSamples), 0)
    assert_pico_ok(status["getTimebase2"])

    status["runBlock"] = ps.ps4000aRunBlock(chandle, preTriggerSamples, postTriggerSamples, timebase, None, 0, None, None)
    assert_pico_ok(status["runBlock"])

    ready = ctypes.c_int16(0)
    check = ctypes.c_int16(0)
    while ready.value == check.value:
        status["isReady"] = ps.ps4000aIsReady(chandle, ctypes.byref(ready))

    bufferMax = (ctypes.c_int16 * maxSamples)()
    bufferMin = (ctypes.c_int16 * maxSamples)() 
   
    status[setDataBuffers] = ps.ps4000aSetDataBuffers(chandle, 0, ctypes.byref(bufferMax), ctypes.byref(bufferMin), maxSamples, 0 , 0)
    assert_pico_ok(status[setDataBuffers])

    overflow = ctypes.c_int16()
    cmaxSamples = ctypes.c_int32(maxSamples)

    status["getValues"] = ps.ps4000aGetValues(chandle, 0, ctypes.byref(cmaxSamples), 0, 0, 0, ctypes.byref(overflow))
    assert_pico_ok(status["getValues"])

    maxADC = ctypes.c_int16(32767)

    adc2mVChAMax =  adc2mV(bufferMax, chRange, maxADC)


    # Create time data
    time = np.linspace(0, (cmaxSamples.value) * timeIntervalns.value, cmaxSamples.value)
    
    # plot data from channel A
    plt.plot(time, adc2mVChAMax[:])
    plt.xlabel('Time (ns)')
    plt.ylabel('Voltage (mV)')
    plt_name = "CHA"
    plt.title(plt_name)
    plt.show()

    status["stop"] = ps.ps4000aStop(chandle)
    assert_pico_ok(status["stop"])

    status["close"] = ps.ps4000aCloseUnit(chandle)
    assert_pico_ok(status["close"])
Attachments
waveform.png
equation.PNG

Tom@THU
Newbie
Posts: 0
Joined: Wed Nov 24, 2021 3:51 pm

Re: Sampling rate problem when using picosdk library of Python

Post by Tom@THU »

What a coincidence. I have stumble over a very similar problem today!

I have a Picoscope5203 which I use in an automated measurement setup.
Today, I have noticed an inconsistency of the sampling rates set in ps5000RunBlock via the timebase variable.
My box can do up to 1Gsps and rates of 1Gsps/2^n.
Down to 62,5Msps=1Gsps/16 all is well. But if I attemt to sample at half that rate, i.e. 31,25Msps=1Gsps/32 that's not what I get. Looking at the sampled stream I see that my signal is sampled with around 41,7Msps=1Gsps/24 instead of 31,25Msps, which is drastically off! HOW COME?
The other slower sample rates appear to be off, too.
Is there a list of the real sample rates available?

Best regards,
Tom

bzy
Newbie
Posts: 0
Joined: Wed Feb 22, 2023 6:44 am

Re: Sampling rate problem when using picosdk library of Python

Post by bzy »

@Martyn
Hi Martyn,
Could you please help me to comfirm the timebase value?
if use 20000Hz sampling frequency, What the value of the timebase should be? by using picoscope 4425A.
Or I need to adjust some parameters else?

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: Sampling rate problem when using picosdk library of Python

Post by Martyn »

You are correct a timebase of 3999 should give a sampling speed of 20000Samples/sec for a 4425A
I am not in the office and don't have that device to hand so can't check. I would suggest trying to set this up in PicoScope 7 and see what you get, the rule of thumb is that if it works in PicoScope then it should work in your own application.

For the 5203 I would suggest using the ps5000GetTimebase call and see what is returned for the sample interval.
Martyn
Technical Support Manager

Martyn
Site Admin
Site Admin
Posts: 4491
Joined: Fri Jun 10, 2011 8:15 am
Location: St. Neots

Re: Sampling rate problem when using picosdk library of Python

Post by Martyn »

For the PicoScope 5203
Attachments
timebases_5203.png
Martyn
Technical Support Manager

bzy
Newbie
Posts: 0
Joined: Wed Feb 22, 2023 6:44 am

Re: Sampling rate problem when using picosdk library of Python

Post by bzy »

Martyn wrote:
Wed Jun 07, 2023 9:24 am
You are correct a timebase of 3999 should give a sampling speed of 20000Samples/sec for a 4425A
I am not in the office and don't have that device to hand so can't check. I would suggest trying to set this up in PicoScope 7 and see what you get, the rule of thumb is that if it works in PicoScope then it should work in your own application.

For the 5203 I would suggest using the ps5000GetTimebase call and see what is returned for the sample interval.
Thanks for your description.
On normal case, if I give 100 Hz input audio and then capture 200 samples when timebase is 3999(sampling rate is 20000Hz), I would get a full sine waveform of 100Hz if plot the samples, right?
But I got not a full sine waveform of 100Hz as I attached waveform.
Is something wrong in my code? or I need to adjust something eles?

It is different with acquisition device of other brand.
I used NI device to capture 200 samples with 20000Hz sampling rate and plotted the samples, it was a full sine waveform.I have attached the waveform.
Attachments
NI plot.png

bzy
Newbie
Posts: 0
Joined: Wed Feb 22, 2023 6:44 am

Re: Sampling rate problem when using picosdk library of Python

Post by bzy »

By the way, I tried streaming mode and set
sampleInterval = ctypes.c_int32(50) #50 us
sampleUnits = ps.PS4000A_TIME_UNITS['PS4000A_US'] # unis is us
It got correct sine waveform.

Post Reply