Go Down

Topic: Audio input analyzed w/ Arduino FFT (Read 13857 times) previous topic - next topic

rexhex

jremington, aliasing is nice to know about. So the nyquest frequency is set from the sampling rate/ 2. The arduinio has a sampling rate of 9600. 9600/2 = 4800hz. This is a high pitch and will probably still give me nice results in the treble range. To get rid of the aliasing noise I will make a low pass filter at about 4800hz.

pjrc

#31
Nov 24, 2015, 12:54 am Last Edit: Nov 24, 2015, 12:54 am by Paul Stoffregen
Thank you very much for that valuable resource. That explains the window function very well. It also shows me some challenges to overcome in the future. When I have some more spare time I would like to watch your whole video. What board would you recommend that lets me still use the arduino language?
Well, of course my opinion is a bit biased.  Teensy 3.2 does have excellent Arduino compatibility, of course after you install the add-on software, since it's not an official Arduino board.  In fact, many of the widely used libraries that aren't (yet) ported to Zero or Due do indeed work with Teensy.

44.1 kHz audio with properly windowed and 50% overlapped 1024 point FFT is very easy with the audio library, as you can see in the video.  If 1024 point FFT will meet your needs, it's probably worth a try.

If discontinuous sampling (where you ignore the input while computing the FFT) and 256 points is enough, an 8 bit AVR will probably be able to do the job.

pjrc

#32
Nov 24, 2015, 01:12 am Last Edit: Nov 24, 2015, 01:17 am by Paul Stoffregen
To get rid of the aliasing noise I will make a low pass filter at about 4800hz.
An analog filter with sharp transition from pass-band to stop-band isn't feasible.  You'll almost certainly have to design your filter for a lower corner frequency, if you want the higher frequencies adequately attenuated.  That sucks, but it's simply the reality of analog filter design.

Audio analog to digital converters normally use 64X to 256X oversampling.  So if the sample rate is 44.1 kHz, a 64X oversampling ADC chip actually samples at 2.82 MHz.  It's very easy to make an analog filter that attenuates 1.4+ MHz, with virtually no impact on frequencies below 20 kHz.  Those chips have low-res ADC conversion (often just a single bit) that run so fast, but they use a special "noise shaping" analog circuit which shifts all the "noise" from the low res conversion to only the higher frequencies (near 1.4 MHz).  Also inside the ADC chip, a very complex digital filter low-pass filter removes all the signal between 20 kHz to 1.4 MHz, while down-converting the data rate to 44.1 kHz.  The noise shaping was known since the late 1960s, but it wasn't until the late 1980s that such complex digital filters became feasible and the era of digital audio began.  Now you can get all that amazing audio performance in a low cost chip for just a few dollars.

Without an oversampling ADC, it's really tough to get good quality audio sampling.  Especially with slower sample rates where you have to filter away part of the audio band, it's *really* difficult.  Analog filtering is really limited.

rexhex

Well, of course my opinion is a bit biased.  Teensy 3.2 does have excellent Arduino compatibility...
That's hilarious, Mr. Stoffregen, I did not realize you were the creator of Teensy. Thank you for coming in and helping me better understand FFT and how to apply it. I have a teensy chip running a neopixle project I did a while back.   :)

Looks like I will be buying another Teensy for a better sampling rate and an expansion of bins.

An analog filter may not have a sharp transition but it is something that could easily be created in my home and would get rid of aliasing noise. Because you have mentioned better options though, I want to try them out instead of wasting time.

Interesting information on ADC. I searched the web a bit for ADC chips and saw there are many to choose from. It is good to know more about whats going on when audio enters A0 and that one could buy a better ADC than A0.

Looking at your audio board I noticed that you did not have an op amp circuit. The mic and line in goes to SGTL5000. With my current circuit a potentiometer needs to be adjusted to keep the audio between 0-5v. I bought a digital pot and better op amp to eventually make this adjustment automatic for different audio sources.

Now looking at your circuit it seems there might be a better way. Briefly looking at the at the SGTL5000 data sheet the line in goes to an ADC first. Can it process negative voltage? Does it not need to process 50% of the signal to get cd quality digital audio data?

44.1 kHz audio with properly windowed and 50% overlapped 1024 point FFT is very easy with the audio library, as you can see in the video.  If 1024 point FFT will meet your needs, it's probably worth a try.
Is this quality only achieved in combination with the audio board attachment or does the ADC on the Teensy do the trick? If the ADC on the teensy does not create this quality, could another ADC with an op amp get the same results?

Thank you for your time,

Rex

pjrc

#34
Nov 24, 2015, 11:05 am Last Edit: Nov 24, 2015, 11:12 am by Paul Stoffregen
Looking at your audio board I noticed that you did not have an op amp circuit. The mic and line in goes to SGTL5000. With my current circuit a potentiometer needs to be adjusted to keep the audio between 0-5v. I bought a digital pot and better op amp to eventually make this adjustment automatic for different audio sources.
Like most of these CODEC chips, the SGTL5000 has software adjustable gain.  Internally it's probably a circuit similar to an opamp and epot, but of course they don't tell you exactly what circuit they used to design the chip.

Quote
Now looking at your circuit it seems there might be a better way. Briefly looking at the at the SGTL5000 data sheet the line in goes to an ADC first. Can it process negative voltage? Does it not need to process 50% of the signal to get cd quality digital audio data?
The audio signals go through a 2.2 uF capacitors before reaching the SGTL5000 input pins.  You're almost certainly doing something similar with the A0 pin, probably also with a pair of resistors to set the DC voltage to the center of the ADC's range?

For the SGTL5000, only a capacitor is needed.  The chip internally has the resistors (or something like a pair of resistors) to establish a 1.6 volt DC level at the input pin.  So the input signal can go as far as about -1.5V and +1.5V before the capacitor and still be measured properly after it's raised up to 1.6VDC after the cap.  Inside, the chip has an amplifier with software-controlled gain, so you can control the signal range that maps to the full 16 bit numerical range.  The default is 1.3 volts peak-to-peak.  The adjustment lets you configure from as much as a 3.1Vp-p signal to as low as a 0.24Vp-p signal.

Quote
Is this quality only achieved in combination with the audio board attachment or does the ADC on the Teensy do the trick? If the ADC on the teensy does not create this quality, could another ADC with an op amp get the same results?
The SGTL5000 has very good analog input quality.  It's certainly not the very best on the market and won't win any kudos from audiophiles or professional recording studios, but it does sound quite good.  Internally, it uses oversampling and digital filtering.  The datasheet isn't clear (at least to me) on the details, but it's definitely using those techniques.  I've tried sweeping the input with a sine wave and indeed the response drops very rapidly starting around 19 kHz and pretty much ignores sine waves or other signals above 21 kHz.  I've driven its input with full scale sine waves from 30 to 44 kHz and hear no noticeable aliased audio output.  Whatever digital filter they put inside the chip works very well.

Pretty much all the dedicated ADC and CODEC chips for audio have similar performance, or better as you spend more in dollars and power.  Many of them like the SGTL5000 are loaded with lots of extra features like software adjustable gain which are meant to save you the cost of adding you own amplifier.  Some others have more spartan feature sets, especially the really high-end ones with top quality levels.  There are many hundreds of these chips on the market from several manufacturers.... you could spend a long time reading all their datasheets!  They're all quite good.  They all use basically the same oversampling, noise shaping and digital filtering for excellent quality.  Sure the specs vary, but even the worst & cheapest ones on today's market sound very good.

As much as I'd like to tout Teensy, the honest truth is it's on-chip ADC is pretty similar to the ones on regular Arduino.  Sure, it's faster and higher res, but it's most certainly not a massively oversampled and noise shaped and digital filtered design like these chips meant for audio.  The Teensy Audio Library does have an object for the on-chip ADC.  It runs the ADC at 44.1 kHz, so if your signal has content above 22 kHz, it will alias back into the audio band (a real problem with large LED projects where the data signals to the LEDs tend to couple into the analog signal, and alias to some audio frequency even though they're hundreds of kHz data).  The on-chip ADC is also only about 12-13 bits resolution.  The library does use efficient DMA transfers, so you get the 44100 samples/sec into the chip and audio lib paths with very low overhead.  If you listen to both signals, either by recording to a SD card or by output to the DAC pin or the SGTL5000's high quality output, you can hear the on-chip ADC isn't nearly as good.  It kinda sounds like a 1980s cassette tape.. not terrible but a noticeable static or hiss that's almost impossible to hear from a the SGTL5000 if you listen at a reasonable volume setting.  Some of that noise might be aliasing, the rest the ADC's resolution or coupled noise from the rest of the digital stuff on the chip (which aliases if over 22 kHz).



rexhex

You're almost certainly doing something similar with the A0 pin, probably also with a pair of resistors to set the DC voltage to the center of the ADC's range?

For the SGTL5000, only a capacitor is needed.  The chip internally has the resistors (or something like a pair of resistors) to establish a 1.6 volt DC level at the input pin.
Yes I have the middle DC offset to 2.5v with a +-2.5 going to 5 or 0v.

I have decided to make a large purchase, at least for my current income, through adafruit to make a completely new set up. Teensy 3.2 + the audio board, audio input jack, microphone, and some neopixels. One thing about this purchase that makes me very happy is that I will be able to work on this in other places than on my studio floor... I was using a control box from a previous project that goes to my rooms LED lamps. It forced me to the floor and my back is killing me...

Pretty much all the dedicated ADC and CODEC chips for audio have similar performance, or better as you spend more in dollars and power.  Many of them like the SGTL5000 are loaded with lots of extra features like software adjustable gain which are meant to save you the cost of adding you own amplifier.  Some others have more spartan feature sets, especially the really high-end ones with top quality levels.  There are many hundreds of these chips on the market from several manufacturers.... you could spend a long time reading all their datasheets!  They're all quite good.  They all use basically the same oversampling, noise shaping and digital filtering for excellent quality.  Sure the specs vary, but even the worst & cheapest ones on today's market sound very good.
I spent some time going through CODEC chip datasheets and decided that it will just be easier and better for me to start out with your audio board. There are so many different types of CODEC chips on the market for a few bucks! I eventually want to come up with something at a lower cost for this CODEC part of my final circuit.

Thank you for helping me out with this. Also thank you for producing an affordable micro controller board and audio board that works so well with LEDs. On top of that thank you for making a library and producing quality information on how to apply it to your boards! I am excitedly waiting for my new set up to arrive.



rexhex

Mr. Stoffregen,

Does your audio board work with your teensy 3.2. It says that it was made for 3.1 and 3.0 but you recommend the 3.2. I have been spending the afternoon attempting to get it wired up to my new teensy 3.2. Im testing to see if the audio board is working with the microphone test example off of the youtube video. Nothing is going to the headphones. 

Code: [Select]
// Advanced Microcontroller-based Audio Workshop
//
// Part 2-1: Using the Microphone


///////////////////////////////////
// copy the Design Tool code here
///////////////////////////////////


#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioInputI2S            i2s2;           //xy=189,118
AudioOutputI2S           i2s1;           //xy=388,101
AudioConnection          patchCord1(i2s2, 0, i2s1, 0);
AudioConnection          patchCord2(i2s2, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=64.5,308
// GUItool: end automatically generated code


void setup() {
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  sgtl5000_1.inputSelect(AUDIO_INPUT_MIC);
  sgtl5000_1.micGain(36);
  delay(1000);
}

void loop() {
  // do nothing
}


The problem must be in hooking the audio board to the teensy. It is confusing to find a way to accomplish this properly. Here is the best resource found (http://www.pjrc.com/store/teensy3_audio.html).

 "The audio chip, part number SGTL5000, connects to Teensy using 7 signals. The I2C pins SDA and SCL are used to control the chip and adjust parameters. Audio data uses I2S signals, TX (to headphones and/or line out) and RX (from line in or mic), and 3 clocks, LRCLK (44.1 kHz), BCLK (1.41 MHz) and MCLK (11.29 MHz). All 3 clocks are created by Teensy3. The SGTL5000 operates in "slave mode", where all its clock pins are inputs." --> diagram of audio board.

I connected the I2c Pins SDA (pin 18 teensy) and SCL (pin 19 teensy). I also connected I2S to TX1(pin 1 teensy) and RX1(pin 0 teensy). What I cant find is how to hook up LRCLK, BCLK, and MCLK to the teensy. Maybe this is why nothing is coming through the headphones?

I have the 3.3v hooked to the audio boards pin 24 marked 3v. And of ground to pin 25 audio board ground.

the mic is hooked up to the mic and ground slots designated for the mic.

I see that you soldered these boards stacked on top of each other but I don't see how that works out with the pin lay out. Are there any good resources created on connecting them without stacking the two boards? Any alternative resources on hooking these boars together?

side note. I am using mac 10.6.8. Teensy 3.2 says it only works with 10.7 but i was able to get it to work with the neo pixels just fine. I had no issues with the software downloads but maybe this is the problem with the audio? I don't know why it would be an issue with the audio board.

here is a link to the neopixel set up I created this weekend powered by the teensy 3.2.

https://www.instagram.com/p/-s37V6n-tK/


pjrc

Those 2 boards are meant to stack.  Both have all the pin numbers labeled on the bottom sides.  Just line up the GND pins on the corner and everything should match up.

Yes, 3.2 works.  The page has been updated to mention 3.2.  Click refresh if it's still showing you only 3.1.

rexhex

The pins have already been soldered on the boards. Desoldering then resoldering pins on a board is probably my least favorite thing to do in epectronics. I want to hook them up with out stacking. If the are made to stack then the clock pins on the audio board should match with the teensy.

rexhex

Yesterday after work I wired up all of the pins on both boards. Microphone test code worked. I could play music in and out (sound quality was terrible with bass and there was a humm so I have a Y connecter out of my computer).

The examples for the audio library is fantastic!

if any one ends up reading this and is frustrated with the fft library just buy these boards and use the audio library.

I spent hours upon hours messing with sound and light last night. Its so much fun and these libraries are great!

 :D

Go Up