thcondra42
Posts: 8
Joined: Fri Feb 06, 2015 5:54 pm

Sampling High Frequency

Wed Apr 08, 2015 1:00 am

I am looking to sample a noisy pulse input signal from a sensor. The input signal will have a maximum frequency of 50kHz, so conforming to the Nyquist Rate requires atleast 100 kSam/sec. The original idea was to use a MCP3001 ADC to convert the analog signal to a digital signal and be able to process it within the Pi (filter the signal, perform an FFT). It also involved using the Pi's clock for the ADC.

However, I've read that a 100 kSam/sec sample rate is basically impossible using this method. Is this true? And if so, what is the best way to get a digitized representation of the signal inside the Pi at that sample rate?

My apologies if this post is in the wrong place. I'm quite new to a lot of this, including the Pi itself. :)

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Sampling High Frequency

Wed Apr 08, 2015 1:49 am

You are correct, you will NOT be able to sample at 100ksps.

The best way would be to attach an external microcontroller, that can sample at 100ksps, which had Ethernet to get the data into the Pi.

Second best way is external microcontroller that can sample at 100ksps and has an SPI slave interface that can send the concentrated sample data to the Pi fast enough - or can perform the filtering and send (slower) filtered data.

I often use the MCP3208 (and MCP3008), so estimating based on that, you will need (roughly) 30 clocks to read a 12 bit ADC result, leading to 30*100000 = 3mbps SPI clock.

This sounds like it should work... however in my experience, the MCP series does not like SPI clock speeds in excess of 2MHz.
thcondra42 wrote:I am looking to sample a noisy pulse input signal from a sensor. The input signal will have a maximum frequency of 50kHz, so conforming to the Nyquist Rate requires atleast 100 kSam/sec. The original idea was to use a MCP3001 ADC to convert the analog signal to a digital signal and be able to process it within the Pi (filter the signal, perform an FFT). It also involved using the Pi's clock for the ADC.

However, I've read that a 100 kSam/sec sample rate is basically impossible using this method. Is this true? And if so, what is the best way to get a digitized representation of the signal inside the Pi at that sample rate?

My apologies if this post is in the wrong place. I'm quite new to a lot of this, including the Pi itself. :)
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

thcondra42
Posts: 8
Joined: Fri Feb 06, 2015 5:54 pm

Re: Sampling High Frequency

Wed Apr 08, 2015 2:15 am

So a microcontroller using ethernet communication would serve the same purpose as the proposed ADC, only with an increased sample rate? Is that the right way to look at it, I'm slightly confused on the actions of the microcontroller that will be communicated.

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Sampling High Frequency

Wed Apr 08, 2015 2:45 am

The basic problems are:

- the Raspberry Pi is NOT designed for high speed data collection
- the MCP series of ADC's tops out at ~2.7Mhz SPI clock at 5V
- SPI latency with the RPi

The SPI interface on the Pi is simply not capable of accurately reading 100,000 samples from an ADC at precise intervals.

I think Joan was able to get 60ksps working.

I'd use something designed for the job - like a dsPIC - which has an internal ADC capable of >1Msps, and batch transfer the data to the Pi using Ethernet or high speed SPI etc.
thcondra42 wrote:So a microcontroller using ethernet communication would serve the same purpose as the proposed ADC, only with an increased sample rate? Is that the right way to look at it, I'm slightly confused on the actions of the microcontroller that will be communicated.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Sampling High Frequency

Wed Apr 08, 2015 7:53 am

It's not clear whether the signal you are sampling is analogue or digital. If it's digital there is no need for an ADC.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Sampling High Frequency

Wed Apr 08, 2015 9:04 am

96kHz sampling is easily achived with an I2S ADC. I have 96kHz,24bit stereo input working using a simple I2S codec on a breakout board.
Higher sampling rates may be possible but the codec I'm using (PCM1803A) maxes out at 96kHz.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
experix
Posts: 204
Joined: Mon Nov 10, 2014 7:39 pm
Location: Coquille OR
Contact: Website

Re: Sampling High Frequency

Wed Apr 08, 2015 2:42 pm

Your remarks about filtering and fft make me think your problem might really need a lower sample rate, but the noise causes problems. In that case you could put a filter before the ADC.

User avatar
mikronauts
Posts: 2783
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Sampling High Frequency

Wed Apr 08, 2015 3:18 pm

I am definitely going to have to check out I2S on the Pi, and the PCM1803A - thanks!

update:

Peter's I2S PCM1803A article is at http://www.peteronion.org.uk/I2S/
PeterO wrote:96kHz sampling is easily achived with an I2S ADC. I have 96kHz,24bit stereo input working using a simple I2S codec on a breakout board.
Higher sampling rates may be possible but the codec I'm using (PCM1803A) maxes out at 96kHz.

PeterO
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Sampling High Frequency

Wed Apr 08, 2015 4:02 pm

I need to update that because I now have it working opn PI1 and PI2 and I have device tree configuration files as well.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

mung
Posts: 506
Joined: Fri Nov 18, 2011 10:49 am

Re: Sampling High Frequency

Wed Apr 08, 2015 7:44 pm

I would think using the gpio was the fastest method?

http://hackaday.com/2015/03/27/the-pi-2 ... ster-gpio/

The link seems to suggest 42MHz if you max out cpu.

I think the dma method uses least cpu overhead, but I have been finding problems above 200kHz, not sure why?

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Sampling High Frequency

Wed Apr 08, 2015 7:54 pm

I'm not convinced the OP knows whether the signal source is digital or analogue. All that seems to be known is the source is a "physical sensor" which in itself is meaningless.

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Sampling High Frequency

Thu Apr 09, 2015 9:06 am

mung wrote:The link seems to suggest 42MHz if you max out cpu.
That is a meaningless example that doesn't do anythign useful.
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Sampling High Frequency

Thu Apr 09, 2015 9:08 am

joan wrote:I'm not convinced the OP knows whether the signal source is digital or analogue. All that seems to be known is the source is a "physical sensor" which in itself is meaningless.
Th OP said "The original idea was to use a MCP3001 ADC to convert the analog signal to a digital signal". Seems pretty clear to me !

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Sampling High Frequency

Thu Apr 09, 2015 9:28 am

PeterO wrote:
joan wrote:I'm not convinced the OP knows whether the signal source is digital or analogue. All that seems to be known is the source is a "physical sensor" which in itself is meaningless.
Th OP said "The original idea was to use a MCP3001 ADC to convert the analog signal to a digital signal". Seems pretty clear to me !

PeterO
As a matter of principle I disbelieve at least 50% of all posts. I'm not often disappointed.

plugwash
Forum Moderator
Forum Moderator
Posts: 3614
Joined: Wed Dec 28, 2011 11:45 pm

Re: Sampling High Frequency

Thu Apr 09, 2015 1:38 pm

thcondra42 wrote:I am looking to sample a noisy pulse input signal from a sensor. The input signal will have a maximum frequency of 50kHz, so conforming to the Nyquist Rate requires atleast 100 kSam/sec.
What exactly do you mean by the "signal will have a maximum frequency"? Do you mean that the "worst case fundamental" will be no more than 50KHz or do you mean that there will be no frequency components over 50KHz? These are very different things.

To correctly sample a signal the highest frequency present in the input must be BELOW the nyquist frequency (half the sampling frequency). Signals at exactly the nyquist frequency have somewhat strange behaviour where the results depend on the phase relationship between the samping clock and the input signal. Signals above the nyqist frequency will be aliased (effectively converted to lower frequencies) and will therefore interfere with signals at lower frequencies.You will usually want some form of analog filtering to reduce (you can never eliminate them completely) the level of above-nyquist signals and noise that will be sampled and hence aliased.

Remember a square wave or pulse train has many harmonics. For good results you really want to capture the third harmonic and ideally the fifth harmonic too.

sokkeh
Posts: 7
Joined: Tue Mar 10, 2015 7:37 pm

Re: Sampling High Frequency

Thu Apr 09, 2015 2:04 pm

Do keep in mind that in theory you only need 2x the highest frequency you want to measure in your signal, so that would be 100kSam/s. But in practice its always a good idea to have 3-4x the highest frequency as your sample frequency. This is because when you get close to the nyquist frecuency your sampled signal will start behaving a bit strange allready and the signal will be sampled alot better if you increase the sampling frequency above the theoretical 2x.

thcondra42
Posts: 8
Joined: Fri Feb 06, 2015 5:54 pm

Re: Sampling High Frequency

Mon Apr 13, 2015 2:26 pm

plugwash wrote:
thcondra42 wrote:I am looking to sample a noisy pulse input signal from a sensor. The input signal will have a maximum frequency of 50kHz, so conforming to the Nyquist Rate requires atleast 100 kSam/sec.
What exactly do you mean by the "signal will have a maximum frequency"? Do you mean that the "worst case fundamental" will be no more than 50KHz or do you mean that there will be no frequency components over 50KHz? These are very different things.

To correctly sample a signal the highest frequency present in the input must be BELOW the nyquist frequency (half the sampling frequency). Signals at exactly the nyquist frequency have somewhat strange behaviour where the results depend on the phase relationship between the samping clock and the input signal. Signals above the nyqist frequency will be aliased (effectively converted to lower frequencies) and will therefore interfere with signals at lower frequencies.You will usually want some form of analog filtering to reduce (you can never eliminate them completely) the level of above-nyquist signals and noise that will be sampled and hence aliased.

Remember a square wave or pulse train has many harmonics. For good results you really want to capture the third harmonic and ideally the fifth harmonic too.

I was just referring to 50kHz as the highest frequency I am looking to be able to measure. I plan to use an analog low pass filter to attenuate the frequencies above 50kHz. Also, I figured 100kHz would be too slow. That only increases the problem by having to increase the sample rate.

terilars
Posts: 5
Joined: Sat Jan 23, 2016 2:34 pm

Re: Sampling High Frequency

Sat Apr 16, 2016 9:47 pm

Peter O, that Sparkfun board referred to in your blog is apparently retired. Any decent alternatives?

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Sampling High Frequency

Sun Apr 17, 2016 6:54 am

I've got one of these to play with (when I find the time). Looking in the kernel sources it seems that it may well be partially supported already.
http://www.rs-online.com/designspark/el ... dec-module
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

terilars
Posts: 5
Joined: Sat Jan 23, 2016 2:34 pm

Re: Sampling High Frequency

Sun Apr 17, 2016 7:54 am

Thanks Peter.

My intent is to sample I and Q signals from a radar module, output range is 40 Hz to 15 kHz. Do I need to go the I2S way [1], or look for simpler solution like mentioned earlier in this thread?

Kindest regards, Erik

[1]: viewtopic.php?f=44&t=8496&start=625

ronnyek42
Posts: 1
Joined: Sun Jan 29, 2017 6:49 pm

Re: Sampling High Frequency

Sun Jan 29, 2017 6:53 pm

Seems like even the mbed audio codec board is somewhat difficult to get your hands on these days...

Peter, I read your I2S page, and Im intrigued. I wondered if there were new recommendations for i2s breakouts... or perhaps better ways if the pi3 brought anything new with it.

I'd like to get an ADC on order... found lots that supported i2s, but not really much in the form factor of a little breakout board.

mfa298
Posts: 1387
Joined: Tue Apr 22, 2014 11:18 am

Re: Sampling High Frequency

Mon Jan 30, 2017 11:03 am

The other high speed interface that's not been mentioned yet is USB. You'd still need something (micro-controller) to process the samples as with the other solutions, but use USB as the transport rather than SPI/I2S etc.
terilars wrote: My intent is to sample I and Q signals from a radar module, output range is 40 Hz to 15 kHz. Do I need to go the I2S way [1], or look for simpler solution like mentioned earlier in this thread?
I've got a USB dongle that does something very similar but not up to radar frequencies. This is an SDR (Funcube dongle) that provides the I and Q samples over USB by acting as a 192Khz audio device. You might find some inspiration from how this works, I think there's a fair bit of technical documentation around. Starting point should probably be http://www.funcubedongle.com/

User avatar
PeterO
Posts: 5878
Joined: Sun Jul 22, 2012 4:14 pm

Re: Sampling High Frequency

Tue Jan 31, 2017 1:40 pm

ronnyek42 wrote:Seems like even the mbed audio codec board is somewhat difficult to get your hands on these days...

Peter, I read your I2S page, and Im intrigued. I wondered if there were new recommendations for i2s breakouts... or perhaps better ways if the pi3 brought anything new with it.

I'd like to get an ADC on order... found lots that supported i2s, but not really much in the form factor of a little breakout board.
I haven't worked on my project for over a year. I did get a different I2S codec to play with but I could never get a device tree configuration to work so I stopped. Maybe one day I'll get back to it....

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “General discussion”