3

I am looking to use the GPIO on a Raspberry Pi to get data from an ADC for audio signal processing.

For the purposes of this forum, my project is to produce a spectrum analyzer which will display the audio spectrum of some music playing on a projector through HDMI. This is to be used as visuals for a music event at university.

The first idea I had was to use an ATMega-328 (arduino mcu) to collect audio samples using the analog inputs, and send the data to the R-Pi using the digital io pins.

I believe that unless I program the ATMega-328 using machine code (asm), the performance will be poor. For example, I understand that using digitalWrite(), peak performance is about 230 Hz, as these functions are not implemented efficiently in the arduino libraries. http://www.codeproject.com/Articles/732646/Fast-digital-I-O-for-Arduino

Okay so this link has alternative libraries, but performance isn't as high as 44100 Hz. Realistically, I would like to achieve about 8 kHz sample rate, at 10 bits per sample.

In addition, the maximum sample rate of the ADC in the ATMega-328 (which is a 10 bit ADC, or will work as one with a precision reference) is about 10 kHz.

I then thought that an I2C device might provide increased performance. This device can sample 100 kHz.

The performance of the R-Pi's io should be greater than 1 MHz, but not higher than about 22 MHz. This should be adequate to carry the required data.

I then read about I2S devices on wikipedia, and understand that the R-Pi has only I2S output, as the pins for I2S input are not connected. (So that was a dead end?)

My question comes down to:

1: Are there any alternatives or better methods I have not considered? Which method would you recommend for this application?

2: Are there any improvements I can make to the second method described (using an external ADC with I2C) to increase performance, baring in mind that FFT's are computationally intensive, and there might not be much time for computing them, even at 900 MHz / 1 GHz clock speed on the R-PI.

Also I am a Physics student, so I know sort of what I'm doing, but I don't have much training in electronics.

I considered using something like the Wolfson Audio board but I understand that it is difficult to setup and use. I am not confident that I would be able to write a program to collect 1024 audio samples and process them using this board. This is where question 3 comes in: Has anyone used this board for a project, and would it be suitable for something like what I am trying to do?

Obviously these questions are quite open-ended. If you want a closed question, then that can be "how do I go about implementing this project that I want to build."

8
  • 1
    @joan Ideally, 2 channel (left, right) 16 bit and 44100 Hz. In practice, 16 bit accuracy is not likely to be achievable. Any higher than 10 bit is likely to be considerably noisy, so a minimum of 8 to 10 bits would be acceptable. 8 might be too low. Similarly, 44100 Hz would require some speedy data transfers, so as low as 8-10 kHz would be acceptable. Commented Jun 23, 2014 at 20:12
  • 1
    If you want to hear a 8kHz signal good enough, you need to have a sampling frequency of at least 16kHz. Look up Nyquist rate.
    – Anders
    Commented Jun 23, 2014 at 20:18
  • 1
    @Anders I said 8 kHz because I know about FFT's. Commented Jun 23, 2014 at 20:18
  • 1
    The MCP3208 is actually an 8 channel SPI device. SPI can be considerably faster than I2C which is typically limited to 400kbps. You can use bit-banging to sample the MCP320X at circa 25ksps. That would probably use 50% CPU. Not sure how much you would need for your FFT. Is this meant to happen "live"?
    – joan
    Commented Jun 23, 2014 at 20:31
  • 1
    @joan By live if you mean real time, then yes - my fears are confirmed then, it might be too slow? Commented Jun 23, 2014 at 20:39

2 Answers 2

1

You should consider bit-banging SPI. This has two potential advantages.

  • the sample time can be precisely controlled, i.e. every 125 microseconds, not every 125 microseconds +/- 100 microseconds.
  • many devices can be sampled simultaneously.

You'll need to weigh those advantages against higher code complexity compared against the standard SPI interface.

An experiment in bit-banging SPI on the Raspberry Pi

1

I […] thought that an I2C device might provide increased performance. [… But I] understand that the R-Pi has only I2S output, as the pins for I2S input are not connected.

This will work! The I2S input pins are connected now: to the P5 header, introduced in PCB revision 2.0 (came out Q3/Q4 2012).

For the software side, there is a kernel module for I2S PWM audio input.

(Before that driver existed, somebody got it working with "a modified I2S [kernel] driver and a "soundcard" driver that supplies the required master clock from GPCLK0" – see. His design successfully used a WM8783 ADC on the P5 header, together with an electrete mic and a preamp board, and achieved a 32 ksps sampling rate.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.