Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: Teensy 3.1 DAC high frequency sine generator

  1. #1
    Junior Member
    Join Date
    Mar 2015
    Posts
    7

    Teensy 3.1 DAC high frequency sine generator

    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!

  2. #2
    Senior Member
    Join Date
    Jan 2015
    Location
    frisia
    Posts
    285
    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.

  3. #3
    Junior Member
    Join Date
    Mar 2015
    Posts
    7
    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) .

    Click image for larger version. 

Name:	Screen Shot 2015-03-20 at 11.51.53 AM.jpg 
Views:	209 
Size:	79.9 KB 
ID:	3896

    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.

  4. #4
    Senior Member
    Join Date
    Jan 2015
    Location
    frisia
    Posts
    285
    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.

  5. #5
    Junior Member
    Join Date
    Mar 2015
    Posts
    7
    Hm... Yup that seems to be the case. Here it is @ 18kHz (offset by .5kHz).
    Click image for larger version. 

Name:	Screen Shot 2015-03-20 at 12.22.59 PM.jpg 
Views:	135 
Size:	86.5 KB 
ID:	3898

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

    Thanks for helping out

  6. #6
    Senior Member
    Join Date
    Jan 2015
    Location
    frisia
    Posts
    285
    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 by kpc; 03-20-2015 at 07:21 PM.

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Location
    Boston, MA, USA
    Posts
    1,112
    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.

  8. #8
    Junior Member
    Join Date
    Mar 2015
    Posts
    7
    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

  9. #9
    Senior Member
    Join Date
    Sep 2013
    Location
    Sonoma County, CA
    Posts
    141
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •