Teensy 3.1 DAC high frequency sine generator

Status
Not open for further replies.

jmzjmzjmz

Member
Hi there!

I'm looking to output high frequency sine waves (~18kHz - ~20kHz) through this Adafruit mono Class D Amplifier (https://www.adafruit.com/product/2130). Here's the very basic preliminary code to test the output, generating random frequencies in that range once every second: (note, I am not using the audio shield)

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>

AudioSynthWaveformSine   sine1;          
AudioOutputAnalog        dac1;           
AudioConnection          patchCord2(sine1, 0, dac1, 0);

long freq = 18500;

void setup() {
  AudioMemory(20);
  sine1.frequency(freq);
  sine1.amplitude(1);
}


void loop(){

sine1.frequency(int(random(0,1500)) + freq);
delay(1000);
  
}

I have a DC blocking capacitor (10uf) on the output, still doesn't remove the intense harmonic distortion occurring at the lower frequencies. I know the speaker is capable of cleanly outputting the high frequency because I get great results when directly plugging it into my iPhone's tone generator (those high frequencies are completely inaudible to human ears, which is exactly what I want). Is this a matter of the teensy's analog 12-bit resolution not being high enough to produce a smooth sine wave?

Thanks in advance for any help!
 
What do you mean with 'intense harmonic distortion'? Audibly, or do you look at the picture on an oscilloscope?
What can also be the case is that since neither board has an reconstruction filter/antialising filter, due to class-D operation, the harmonics might fold back into the audio band.
 
Thanks for the response--- by intense harmonic distortion, I mean that there are very audible frequencies being heard. On an o-scope it looks ok, just has some discrete steps in the sine wave. Attached is an image of what my computer mic picks up from the external speaker (Teensy generating 18.5kHz sine) .

Screen Shot 2015-03-20 at 11.51.53 AM.jpg

For now, I'm skipping the mono class D amp. For testing, the speaker I'm using is a small battery powered mono speaker, which again reproduces the tone excellently (inaudible to humans, but LOUD) when using my iPhone as a sine generator.
 
It looks indeed like harmonics folding back, due to the missing filter.
To verify, you could generate a second spectrum with the 18.5 kHz tone offset by 0.5 kHz, then the tone at 4.4 khz should shift lets say 1.5 kHz and the one at 11.2 should shift say 2.5 kHz. The 1.5 and 2.5 kHz are just examples, but the shift should be an integer multiple of the fundamental shift of 0.5 kHz.
 
Hm... Yup that seems to be the case. Here it is @ 18kHz (offset by .5kHz).
Screen Shot 2015-03-20 at 12.22.59 PM.jpg

What would you suggest the best (hopefully simplest) way to fix this?

Thanks for helping out
 
Since you anly require a small bandwidth you might try with a simple LC resonant circuit.
Either series R and parallel LC in parallel, or series LC in series and R to ground.
You can play with the value of R to get reasonable L and C values.

Design equations.

For the first case:
Q = R * sqrt(C/L)
for the second case:
Q = 1/R * sqrt(L/C)

Q = f/BW
f = 1/(2*pi*sqrt(L*C))
 
Last edited:
I would suggest a third-order (18dB/oct) low-pass filter at around 20.5 kHz. Since you are using sine waves there shouldn't be any issues from the signal not being bandlimited, so just a post-DAC reconstruction filter to remove the sampling frequency should suffice.
 
Aye, now I'm not so sure it's because of the anti-aliasing.... I have a 3rd order low pass filter @ 19.1K (easy values: .1uF & 83ohm) and the harmonics are showing up just as badly & audibly. Unfortunately I don't have any inductors on hand or else I'd try the LC resonant circuit today. Any other suggestions for me to try while I wait for those to come in? Was really hoping to get this running by this weekend
 
If all the problems that you're hearing are much lower frequency than your output frequencies, then I'd try approximating a bandpass filter by reducing the capacitance of your coupling cap by factors of 10 or so until you get something you like.

The corner frequency for the highpass response created by the coupling capacitor is 1/(2*pi*C*R), where C is your coupling cap, and R is your series resistance (DAC source resistance + any series resistance in your filters + the resistance of the speaker or load). Without any exact knowledge of the Rs, you can't set this perfectly, but you can easily play around with it to find a good response.
 
Status
Not open for further replies.
Back
Top