PS3000A_ID_Streaming_Example application fft

Post your MATLAB discussions here
Post Reply
SG95
Newbie
Posts: 0
Joined: Tue Nov 26, 2019 10:04 am

PS3000A_ID_Streaming_Example application fft

Post by SG95 »

Hi

For my thesis i'm using matlab on a picoscope. I am using the example code PS3000A_ID_Streaming_Example provided with picotech-picosdk-ps3000a-matlab-instrument-driver-9c2a427. I am trying to do a fast fourier transformation in real time so i think its best to use the example code PS3000A_ID_Streaming_Example. For that i want to understand how precisely the code works. So when the fast fourier is performed on the live data with for example is a sinus. I want to see the peak of the fft change when the amplitude changes of the live data or shift towards the frequency when i change the frequency of the sinus. Can someone help me to explain how the live streaming data works? so how does it saves the data, which variables should i take into account, etc.

thank you in advance

Gerry
PICO STAFF
PICO STAFF
Posts: 650
Joined: Mon Aug 11, 2014 11:14 am

Re: PS3000A_ID_Streaming_Example application fft

Post by Gerry »

Hi SG95,

If an FFT was calculated using individual, or small groups of samples to plot small parts of the spectrum, gradually building up the complete spectrum over time, then it would be convenient to calculate and plot streamed data as it is being received. But when you perform an FFT you are transforming a complete buffer of Time domain data into a complete buffer of Frequency domain data, and to minimize data loss, you need to ensure that you can plot the FFT faster than the rate that you are capturing a buffer of data. So, the time to (a) transmit all of the samples required for the number of bins you will transform them into, (b) transform them using a method that includes calculating the FFT, and then (c) display the data, should not be longer than the time to capture the data. For this reason block mode would be better than streaming mode, as you can be doing (a), (b) and (c) while you are capturing the next buffer of data (if you are streaming the data then you will have significantly less time and therefore be more likely to lose data).

For real-time display you need to also ensure that you are not using too many samples or bins, and/or not sampling too slowly, so that the time to capture the data you need is short enough for the spectrum plots to appear continuous.

The easiest way to understand how block mode is implemented would be to
1/ Look through the description on page 10 of the Programmer's Guide (here: https://www.picotech.com/download/manuals/picoscope-3000-series-a-api-programmers-guide.pdf) and the sequence of functions on Page 11
2/ Download the example code from Github (see here: https://github.com/picotech), go through the code of the functions mentioned on page 11, and then step through the relevant parts of the code so that you can see what it does.

Regards,

Gerry
Gerry
Technical Specialist

SG95
Newbie
Posts: 0
Joined: Tue Nov 26, 2019 10:04 am

Re: PS3000A_ID_Streaming_Example application fft

Post by SG95 »

hi

so if i understand this, the first thing that the script does is set up the device. like it says in the sequence. for capturing multiple blocks after each other to make it like real time you have to repeat: ps3000aRunBlock, ps3000ablockready, ps3000asetDataBuffer and ps3000aGet values. is this possible with a while loop where the argument represents the number of data blocks you want to capture? so in the script its a while loop between capture data and plot data?

best regards
Attachments
code.PNG
flowchart_blockmode.PNG

NeilH
PICO STAFF
PICO STAFF
Posts: 201
Joined: Tue Jul 18, 2017 8:28 am

Re: PS3000A_ID_Streaming_Example application fft

Post by NeilH »

What exactly are you trying to do in MATLAB?
Neil
Technical Support Engineer

SG95
Newbie
Posts: 0
Joined: Tue Nov 26, 2019 10:04 am

Re: PS3000A_ID_Streaming_Example application fft

Post by SG95 »

hi neil

i am trying to capture an elektromagnetic spectrum. so the main objective is to read data for like1 or 2 minutes. i understand that for in block mode the best thing tot do is to capture multiple blocks with a duration between block as low as possible(idealistic is 0) beceause to determine the exposure to elektromagnetic fields you need to capture every peak. so when the time between 2 blocks is to big it could be that it didnt capture a peak. for this exposer i need e method called weighted peak method. this method consist of an fft. so for eacht block of data i need a fft. then i will try to transfer the information of this fft(amplitude, phase) to an other script(CSV) to calculate the exposure. and repeat this for every block. the first step is to make it real time i guess. but I don't quite understand how to do this. I hope you understand what im trying to do?

Gerry
PICO STAFF
PICO STAFF
Posts: 650
Joined: Mon Aug 11, 2014 11:14 am

Re: PS3000A_ID_Streaming_Example application fft

Post by Gerry »

Hi SG95,

Sorry, I overlooked something earlier, I didn't ask you how many bins you were going to need for your FFT plot, or what sample rate you need to use.

Ideally, you need to be displaying new updated data at a minimum rate of every 40ms for real-time display. So, if you have a requirement for too many bins, performing the transfer to the computer will take too long (in any mode) which would mean that the display will start to appear more like a series of snapshots than real-time.

Also, just to set the right expectation, there will always be a delay between blocks. For a very small amount of bins it will be, primarily, the time required for the computer to switch resources being used to PicoScope 6 plus the time required for the USB host to establish communication with the PicoScope (anywhere between 10 milliseconds to 10's of milliseconds, also depending, to some extent, upon the PicoScope series). For a medium to large amount of bins it will depend upon the number of bins (which will extend the transfer and processing time) as you can see when you step through the Number of bins used in the drop down list in PicoScope 6.

So, the important questions are, how many bins, and what sample rate do you need?

If your sample rate is not too fast, your number of bins required is not too many, your computer is powerful enough and has enough resources, and your code is efficient enough then it is possible that you could stream the data, convert it and display it in real-time (however you could spend a long time going down this route only to find that it isn't possible).

If real-time gap-less capture and display is not possible, then you will have 2 options:

1/ Sacrifice continuous data, and Collect/transfer data in Block Mode while transforming and displaying it.

2/ Sacrifice the real-time display, and collect all of the data at once over the 1 or 2 minutes and then perform transforms on the data to display Spectrum plots (with no data gaps between the time periods that they represent) using Streaming Mode, if 125MS/s (maximum streaming rate) is a fast enough sample rate to capture the data for your requirements. Note, that Streaming Mode has some disadvantages, including a varying number of values at any time of asking (making the handling more cumbersome) (also note that PicoScope 6 uses Block Mode for its Spectrum Plot).

Regards,

Gerry
Gerry
Technical Specialist

SG95
Newbie
Posts: 0
Joined: Tue Nov 26, 2019 10:04 am

Re: PS3000A_ID_Streaming_Example application fft

Post by SG95 »

Hi Gerry

Thanks for the explenation Gerry, i now have a better view of what i have to do. The frequencys im working with have a maximum value of 10MHz wich means a sample frequency of 20MHz is needed. I understand from your explenation i can use the streaming example. A 125MS/s will therefore be fast enough to represent the signals because of the Nyquist criteria? The number of bins i need, i dont know for now because i dont quite understand fft good enough. for now i can continue to work from here. thanks a lot for the help!

regards

Gerry
PICO STAFF
PICO STAFF
Posts: 650
Joined: Mon Aug 11, 2014 11:14 am

Re: PS3000A_ID_Streaming_Example application fft

Post by Gerry »

Hi SG95,

Unfortunately with a lot of Scopes Nyquist doesn't apply, because applying the criteria assumes that you have a brick-wall filter that suddenly reduces the signal bandwidth from the rated signal level to undetectable almost instantaneously, at the Nyquist frequency. In the practical world you need some bandwidth to be able to roll off the frequencies that would cause aliasing, which is why we say that the sample rate should be at least 5 times the bandwidth that you need. So 125MS/s will be good for signals that have bandwidths up to 25MHz.

Learning about FFTs can be pretty complex and frustrating at times (because, most references focus on the FFT maths and not the method of implementing an FFT, and there is a lot of confusing and sometimes conflicting information out there). So, here are some references that imho are worth looking at:

One of the most common methods (if not the most common method) of implementing FFT's is the Welch Method of averaged Periodograms, and this text is the authoritative text on it (explains how to implement the method in detail, from start to finish): https://www.researchgate.net/publication/267956210_Spectrum_and_spectral_density_estimation_by_the_Discrete_Fourier_transform_DFT_including_a_comprehensive_list_of_window_functions_and_some_new_flat-top_windows

Here is another great reference that refers to the different spectrum types and their uses:
https://www.crystalinstruments.com/dynamic-signal-analysis-basics

Lastly, here are a couple of good visual references on windowing, and why we use the technique:
https://community.sw.siemens.com/s/article/windows-and-spectral-leakage
https://community.sw.siemens.com/s/article/window-correction-factors

Regards,

Gerry
Gerry
Technical Specialist

Post Reply