Showing posts with label SD Card. Show all posts
Showing posts with label SD Card. Show all posts

Saturday, October 2, 2021

Tympan at High Speed (Ultrasonic!) Sample Rates

While we designed the Tympan as a platform for trying hearing aid algorithms, it's flexible enough to be used for many different audio tasks.  For example, by increasing the Tympan's sample rate, you can see signals above the range of human hearing...to explore ultrasound!  The question is, how high into the ultrasonic range can the Tympan go?


Sample Rate and Nyquist.  The Tympan is a digital audio device; it samples the voltage of a signal at discrete moments in time.  It acquires audio samples at a constant rate, the "sample rate".  If you wish to sense a certain frequency of audio (say 10 kHz), you need a sample rate that is fast enough to capture this frequency.  Thanks to Nyquist, we generally say that the sample rate needs to be at least twice the frequency of the signal that you want to sense.  So, to sense 10 kHz, our sample rate must be *at least* 20 kHz.  Typically, digital audio systems run at 44.1 kHz or 48 kHz so that they can comfortably span the 20 kHz maximum range of human hearing.

//set the sample rate and block size
const float sample_rate_Hz = 48000.0; //for audible sound
const int audio_block_samples = 128

Ultrasound.  For sensing ultrasound, we need to sense frequencies higher than 20 kHz.  Many inexpensive ultrasonic range-finders, for example, operate near 40 kHz.  If we want to explore these signals, we need to increase our sample rate to 80+ kHz.  Some rats and bats make vocalizations that extend up to 80 kHz, so that would require a sample rate of 160+ kHz.  Can the Tympan sample this fast?

Changing the Sample Rate.  Changing the sample rate of the Tympan is easy.  Near the top of every Tympan example, you can see where to change the sample rate.  This example is even called "ChangeSampleRate".  So, that part is easy; simply write in a sample rate that is higher!  The question is whether the Tympan produces useful data when running at these higher speeds.  Let's test it!

//set the sample rate and block size
const float sample_rate_Hz = 96000.0; //for ultrasound
const int audio_block_samples = 128; 

Test Setup.  As shown in the photo at the top of this post, I used a function generator to make a sine wave.  I then ran its signal through an attenuator to make sure that I wasn't overdriving the input of the Tympan.  I used a Tympan RevE and inserted the signal via its pink input jack.  

Tympan Software.  On the Tympan, I used one of the example sketches that records audio to the SD card.  In the code, I made two changes: (1) I told it to record from the pink jack as line-in and (2) I changed the sample rate to whatever I was testing.

Test Method.  For each test, I started the Tympan's SD recording and then I manually turned the knob on the function generator to sweep up through the frequency range.  I then stopped recording, pulled out the SD card, and made a spectrogram of the recording on my PC.  I used Matlab, but you could also use Python or Audacity for your spectrograms.

Results, Clean Audible Signal (fs = 48 kHz).  I started with a known-good traditional audio sample rate.  The figure below shows my frequency sweep when using a sample rate of 48 kHz.  The spectrogram shows that we could see frequencies up to 24 kHz, as expected based on Nyquist.  The spectrogram looks great; the signal is clean and the background noise looks like background noise.  This is what "clean" and "good" look like.

Results, Clean Ultrasound Signal (fs = 96 kHz).  I then turned up the sample rate to 96 kHz and repeated the measurement.  The spectrogram below is the result.  It looks great.  We see our signal up to 48 kHz, as expected.  There's a little bit of aliasing as the input signal continued past 48 kHz (we see that the signal's line in the spectrogram bounces downward a little bit when it hits 48 kHz).  The aliasing stops quickly, so this seems fine.  I think that this spectrogram looks great.

Results, Marginal Quality (fs > 96 kHz).  When I increase the sample rate beyond 96 kHz, the results start to look less good.  Below are the results for 100 kHz, 105 kHz, and 110 kHz.  As you can see, the signal itself looks OK, but strange artifacts start to appear in the background noise.



Results, Bad Quality (fs > 110 kHz).  Finally, by the time we get to a sample rate of 115 kHz, the recorded audio is bad.  Bascially, any sample rate above at 115 kHz and above is unusable.




Conclusion.  The Tympan is good for recording at sample rates up to 96 kHz.  You can maybe even run up to 110 kHz.  But, at 115 kHz and above, your signal will be corrupted.

Improving High-Frequency Performance.  The audio codec used to do the sampling is a very complicated device.  There are many settings and many ways of clocking the device.  It is possible that there is a different combination of settings that would provide good-looking data at sample rates higher than 96 kHz.

96kHz is Still Good!  Luckily, 96 kHz is still a very useful sample rate for ultrasound.  Running at 96 kHz is fast enough to give good access to signals around 40 kHz.  This is a very important region for ultrasound in air.  There are many ultrasonic range finders and motion sensors that operate in the 40 kHz range.  So, you can explore and do many fun things running your Tympan at 96 kHz.  Furthermore, we also know that the Tympan's on-board microphones are sensitive up into this region, so you don't even need any additional hardware to sense the ultrasound!  You can just change the system's sample rate and then go have fun!

Tuesday, March 14, 2017

Tympan Electronics and Its Self-Noise

After measuring the audio performance of the Teensy Audio Board (see my previous post), I felt that I needed something better.  If I wanted my open-source hearing aid ("Tympan") to sound good, I needed a quieter audio interface with a bigger dynamic range.  So, with help from friends and colleagues, I decided that we should build our own.  Today's post gives a quick overview of its design and then I'll present some measurements of its performance.  Were we successful?  Were we able to get better dynamic range?  Let's find out...


The Audio Codec is the Heart:  The picture above shows the Tympan audio interface.  The heart of the board is the audio codec at the top-center.  An audio codec is a highly-integrated chip that, among other duties, does all of the amplification and digitization of the incoming analog audio signals.  Choosing the right codec and then properly designing the circuit board around it are both key elements to achieving a low-noise design with maximum dynamic range.

Choosing an Audio Codec:  The Teensy Audio Board uses the SGTL5000 audio codec.  Presumably it was chosen because it is small, low-cost, low-power, and has a built-in headphone driver.  For my Tympan audio interface, I want all these same features, but I also want it to be quieter.  After looking at a bunch of options, and after talking with colleagues who have experience with a variety of TI parts, we chose to go with the Texas Instruments TLV320AIC3206 (product page here).  It's got many of the same features as the SGTL5000 but promises better audio performance, though at the cost of a few extra bucks per chip.  If it gets me the wider dynamic range that I want, I'll be very happy with that trade-off.

Circuit Design:  Like with the SGTL5000 on the Teensy Audio Board, the TI 3206 needs both the I2C and I2S buses to communicate with the host processor (the host processor being a Teensy 3.5 or 3.6).  Also like the Teensy Audio Board, we will run the TI 3206 in "slave" mode, where all clocking is provided by the host processor.  In other words, our connections to the TI 3206 parallel the connections used by the SGTL5000.  Therefore, in designing our Tympan circuit, the schematic for the Teensy Audio Board was a great help.  Yay for open source!  And, to continue the sharing, our own schematic is available on the Tympan GitHub here.


Software Driver:  After laying out the PCB and getting it fabricated, we had to write software to allow the Teensy and the TI 3206 to talk to each other.  Since I wanted to fit within the Teensy Audio ecosystem, we needed to write a "AudioControl" module that configures the TI 3206 to be in the proper I2S mode so that Teensy's existing I2S functions can successfully transfer audio data to and from the codec.  Luckily, I've got a buddy (Brendan, of FlexVolt fame!) who dived in and figured it all out.  His Arduino/Teensy compatible "AudioControl" module is now on the Tympan GitHub (h-file is here, cpp file is here).  Thanks, Brendan!

Measuring the Self Noise:  Once Brendan got the software side of things working [and, in the process, finding errors in the Tympan design -- we erroneously swapped "DIN" and "DOUT" by accident!  Oops!  The schematic above has been corrected.], I turned my attention to measuring the new system's audio performance.  My primary concern was the noise floor of the new hardware.  Was it better than the Teensy Audio Board?  To find out, I used a raw 3.5 mm stereo plug in the Tympan's input jack and shorted both the left and right inputs to ground.  Now, when I start recording, I should only see the Tympan's own self-noise.


Arduino Sketch:  Now I need some software to do the actual recording.  So, starting from the Arduino sketch used to record the noise for the Teensy Audio Board (here), I swapped it over to use the Tympan audio board instead of the Teensy audio board (new version here).  This sketch digitizes the input audio (which has been shorted to ground) and sends the digital samples over USB to be recorded on the PC.

Results, USB Audio:  With the inputs shorted on my Tympan board, and with me recording the audio via USB in Audacity (as discussed here), I recorded the self-noise of the Tympan.  The spectrum of the self-noise is shown below.  It's a pretty flat spectrum, which is always nice to see.  The only unexpected feature is the increase in the noise seen at the highest frequencies.   What is that?
It's a pretty nice spectrum, except for the bump up at the high frequencies.  What's that?!?
USB vs SD:  The recording above was taken via USB.  Therefore, the USB cable was attached to the Teensy/Tympan.  USB cables are notorious for injecting noise.  Frankly, I surprised that the spectrum shown above is as low and as flat as it is.  To see if the USB connection was the cause in the bump at the higher frequencies, I revised the sketch to record the audio to the Teensy's SD card instead of sending it over USB (new sketch is here),  Re-running my test, I see that the high-frequency hump is gone!  Now that's a beautifully flat spectrum...
By disconnecting the USB cable and recording the audio via SD card, the bump in the spectrum goes away.  Now the self-noise is nice and flat.  Excellent.
Noise Floor Comparison:  My primary goal for the Tympan board was to have a lower the noise floor (and thereby increase the dynamic range) than I saw with the Teensy Audio Board.  To see if I was successful, I used my recordings to compute the total self-noise across the frequency range of 125-8000 Hz. This is the frequency range most relevant for my hearing aid work.  I assessed this broadband self-noise value for the Teensy and Tympan boards across a range of analog gain settings.  The result of this noise analysis is shown in the figure below.
Apples-to-Apples:  The Teensy and Tympan boards have different ways of specifying the input gain.  Ideally, I'd be able to set the same amount of gain for each board, but that wasn't possible.  So, to  align the data in the most fair way, you can see that plot the values as function of the "maximum allowed input signal".  As expected, increasing the gain decreases the max allowed input.  So, at any given value for max allowed input, it is a fair to compare between the two systems.  As can be seen, the Tympan audio board does indeed have lower noise than the Teensy audio board.

Dynamic Range Comparison:  Another way to express this same data is to show the dynamic range of the system.  The dynamic range is the difference between the max allowed input signal and the system's noise floor.  I want as wide a dynamic range as possible, so as to better mimic the human ear.  As can be seen below, the Tympan audio board does indeed provide greater dynamic range than the Teensy audio board.  Specifically, the Tympan is getting 92.4-94.6 dB of dynamic range (in the 125-8000 Hz band) versus 80-81 dB for the Teensy Audio Board.  This is quite an improvement!
Keeping Perspective:  While I am very pleased with the performance of the TI 3206 on the Tympan audio board, it's important to remember that the Teensy audio board has some significant advantages in other areas.  First, you can go buy your own Teensy Audio Board right now, whereas you can't (yet) get a Tympan.  Second, the Teensy Audio Board is remarkably inexpensive.  It's hard to see the Tympan board ever being that inexpensive.  Yes, PJRC did a fantastic job making a good piece of hardware at a fantastic price.  That's for sure.

Next Steps:  I'm going to use the Tympan as a platform for open source hearing aid experiments.  But, a hearing aid is more than just electronics.  So, my next steps are to start adding in other elements like microphones and earphones.  And, I have to get back to making audio processing algorithms!  That's where the fun really happens!