Connect with us

PWM versus codec

Discussion in 'Electronic Design' started by Chris Becker, Sep 28, 2005.

Scroll to continue with content
  1. Chris Becker

    Chris Becker Guest

    Im starting a new design. Typically I've always used a codec to bring audio
    in/out of the system.
    The micro I'm looking at using has an onboard ADC and a PWM. It would be
    very exciting if the PWM could be used for the DAC functionality.
    I have zero experience with PWM.
    Is it reasonable to expect to be able to get 13-bit 8Khz "telephone quality"
    audio out of a PWM?
    Is it relatively straight forward or should I expect it to be quite
    difficult to get audio quality comparible to that of a dedicated codec?
    I sure appreciate any wisdon from someone who has worked with this stuff
  2. Bob Smith

    Bob Smith Guest

    I looked at the same problem. I wanted to use an
    AVR micro to build a "phone line to RS232" converter.
    The problem is that none of the cheap micros have a
    good DAC on board.

    You want the PWM frequency to be ten or so times
    greater than the sample rate, or about 80 KHz for
    telephony applications. For my design, this ruled
    out the 16 bit PWM since it could not count up fast
    enough to give even 14 bits at 80 KHz.

    The best I could come up with was a hybrid approach.
    I used both PWMs on the AVR, with both working at 6
    bits of resolution. The idea was to combine the two
    outputs with an "R-64R" resistor network to do a
    weighted sum of the PWM outputs. Luckily I found
    another way to solve the problem and never had to
    get this crazy idea to work. I look forward to
    seeing the real solutions posted in this thread.

    My ultimate goal was just an answering machine that
    would work under Linux. I found a winmodem approach
    which, if you are interested, is documented at:


  3. Ultimately what you are suggesting is certainly possible, and the basic
    concept has been successfully implemented many times before. How high the
    maximum achievable quality you can attain will depend upon your
    microcontroller's PWM capabilities, as well as your cleverness as a
    programmer and engineer.

    I've got a couple of brothers who are fans of Microchip microcontrollers who
    have done just this before. Both have produced surprisingly decent quality
    music (even in stereo) from the built in 10 bit PWM module on the higher
    speed PIC (40MHz) devices. Of course, I'm no audiophile, but I found the
    music quite acceptable to listen to.

    As the sample output rate increases , there are limitations on how many bits
    of PWM resolution that are available, I'm not sure if they were using the
    full 10 bits. I suspect they were using something more like 8, but I don't
    know that for sure.

    See also:

    Even at 8 bits, the quality attainable can be significantly better than that
    of "telephone quality." I imagine most people's ears aren't really
    sensitive to 16-bits of audio resolution (a mere 15 microvolts for a 1Vp-p
    signal range), the real benefit of using 16-bits is you get to implement
    various tricks like digital audio amplification and scaling without running
    into serious limitations. Assuming your audio source is properly scaled and
    normalized to begin with, 8-bits is enough to deliver quite high quality

    Obviously you want to run your PWM frequency as high as possible, but there
    are still practical limitations in terms of the maximum audio sample rate
    achievable, due to the low pass filter on the output (which filters the
    square wave output to an analog voltage). An "R-2R ladder" (hint, google
    search terms) poor man's DAC approach could likely significantly exceed the
    performance of the PWM module in this case in terms of maximum audio
    frequency renderable, but R-2R ladders take a bunch of I/O lines. At
    telephone quality, you shouldn't need to bother, although that does depend
    of course on what microcontroller you are using and it's capabilities (as
    well as your capabilities).
  4. Chris B

    Chris B Guest

    What I dont get is this: For a couple bucks I get a standard 13-bit codec.
    13 bits is *sort of* standard resolution for telephony when using a linear
    So to equal this with PWM i need to run at 2^13 * 8000 = 65.5MHz. Then I
    hear that as a rule of thumb you want to be 10x faster! This would mean i
    require a clock of 1/2 a gigahertz! Is this 10x really necessary?

    And say you do bump the frequency up 10x, do you need to upsample (digitally
    filter) the audio signal you are putting out? Or just keep putting out the
    same value for 10 samples...which seems pointless?

    I am worried about how hardcore the filtering needs to be after the PWM
    output. I dont have weeks to design the greatest filter ever....Are audible
    artifacts of all this switching easily removed?

    I hadn't thought of doing volume control, but that is a clever idea. I do
    need two distince volume levels and was toying with the idea of modifying
    the level of the square wave before the low pass filter.

    Thanks for your comments.

  5. It appears you may have the wrong idea about how the PWM module would
    typically be used. You don't need a 500MHz microcontroller or any kind of
    fancy digital or analog filters.

    Depending upon what your audio format is in to begin with, all you likely
    actually need is a single RC low pass analog filter. You hook the resistor
    up to the output of the PWM module. On the other side of the resistor you
    hook up a capacitor to ground. You get your analog output signal off the
    top of that capacitor.

    The PWM module is typically quite easy to use, you simply configure a couple
    of registers and it starts oscillating at some fixed frequency at some set
    duty cycle. You get to set the frequency and duty cycle by playing with the
    registers, but normally you just set it and it goes by itself. Normally,
    for DAC use, what you would do is simply configure it to output the maximum
    practical frequency available (which ideally should be at least 10X the
    *sample rate* (not sample bits) of the audio you are trying to output, the
    higher the frequency the better). So for instance, if you wanted to output
    8-kilosamples of audio per second, you would ideally configure your PWM
    module to start oscillating at least at 80kHz. This should be no sweat,
    just set up the proper registers and it will be good to go.

    Once you have the PWM module configured, you are ready to start outputting
    the audio samples. Suppose you have a 10-bit PWM module available (meaning
    that you can adjust the duty cycle of the output frequency anywhere from
    100% to 0% in 1024 discrete steps). This would allow you to output at
    maximum 10-bit audio samples. For each audio sample, you adjust the PWM
    duty cycle (but leave the frequency the same) corresponding to what the
    audio sample actually is. When you low pass filter the digital square wave
    coming out of the PWM output, you get an analog DC voltage. For example if
    you had say a 5V CMOS microcontroller, presumably the output of the PWM
    module would go from 0V to 5V. Suppose you output a "512" audio sample,
    which would correspond to 50% duty cycle on the PWM output. This would
    cause the low pass filter to produce an average DC voltage of 2.5V (half on
    at 5V, half off at 0V, average 2.5V when low pass filtered). If you
    outputted a "256" audio sample, or a 25% duty cycle output, the DC voltage
    on the low pass filter capacitor would drop to 1.25V. Etc. This is how you
    get your analog output voltage. Typically you would then AC couple this
    analog DC voltage through a capacitor to an op-amp buffer (although you can
    simply AC couple it to earphones and listen to it albeit with very weak

    Since the output of the PWM module is a digital square wave instead of a
    true analog voltage, you want to very heavily low pass filter it, lest the
    square lumps of the PWM main frequency will appear in your analog output.
    This means you want to place the low pass filter breakpoint at very low
    frequency, as low as possible without unreasonably attenuating the higher
    frequencies of your real audio signal. If you were outputting 8-kilosamples
    of audio per second, then it would seem reasonable to place the low pass
    filter breakpoint somewhere very vaguely around 8 kilohertz. Of course, if
    your main PWM frequency is a mere 80kHz, that is only one decade higher, so
    you will only get 20dBs of attenuation of the 80kHz content... That is not
    that great. The higher your PWM frequency, the better the attenuation will
    be, so set it for the maximum practical that your microcontroller will
    produce (subject to the constraint that the duty cycle resolution will
    probably decrease at high PWM frequencies, so there may be a compromise here
    as to what maximum frequency is optimal).

    The speed of your microcontroller really only needs to be as fast as you can
    handle the audio samples to make them appear in the PWM duty cycle control
    register(s). Only a modest microcontroller would be adequate for modest
    quality audio, although that depends on how clever and efficient a
    programmer you are.
  6. Hmm... Actually since 8 kilosamples per second only produces a maximum
    audio frequency of 4kHz, maybe putting the breakpoint closer to 4kHz would
    make more sense. I guess you get to play with it, see what sounds best and
    looks the nicest on the o-scope.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day