what do µ-law and A-law do?

Thread Starter

adav84

Joined Apr 28, 2012
17
Hello,

I need to store a short audio sample in flash memory, to be played back using a PWM DAC (lowpass and all) for a hobby microcontroller project and I thought about storing it as µ-law, given that it's a relatively plain sample-level algorithm.

On W***pedia, it says something about companding. I did a small test: I generated a file with all sample values, converted it to µ-law (using these routines) and back to PCM and plotted input sample values vs output sample values. The thing is, the line was straight! So I thought, perhaps µ-law is not meant to be decoded before playback. I plotted compressed µ-law too (see attached image.) It looks weird. Most importantly though, if I open the µ-law file as raw data, as though it was PCM samples, the audio sample doesn't look (or sound) anything like the source material.

I don't understand what µ-law does. Where is the companding? How is it different than if one simply truncated the 16-bit source PCM material to 8 bits?
 

Attachments

Last edited:

RichardO

Joined May 4, 2013
2,270
Companding is short for compressing/expanding. This is a way of increasing dynamic range without increasing the data rate. The mu-law and A-law are nonlinear transfer curves and refer to the shape of the curve. Playing a compressed file using an player that does not expand using the proper curve will not sound very good.

I am already beyond my depth of knowledge so I will let the more learned take over...
 

MrAl

Joined Jun 17, 2014
13,667
Hello,

I need to store a short audio sample in flash memory, to be played back using a PWM DAC (lowpass and all) for a hobby microcontroller project and I thought about storing it as µ-law, given that it's a relatively plain sample-level algorithm.

On W***pedia, it says something about companding. I did a small test: I generated a file with all sample values, converted it to µ-law (using these routines) and back to PCM and plotted input sample values vs output sample values. The thing is, the line was straight! So I thought, perhaps µ-law is not meant to be decoded before playback. I plotted compressed µ-law too (see attached image.) It looks weird. Most importantly though, if I open the µ-law file as raw data, as though it was PCM samples, the audio sample doesn't look (or sound) anything like the source material.

I don't understand what µ-law does. Where is the companding? How is it different than if one simply truncated the 16-bit source PCM material to 8 bits?

Hi,

Are you inputting a ramp? If so, then after operating on it with a transform and then using the inverse transform on that you SHOULD get the same ramp back again, which means you did it right. If you dont get the same ramp back again then you did not do something right :)
 

Thread Starter

adav84

Joined Apr 28, 2012
17
Hi,

Are you inputting a ramp?
exactly!

If so, then after operating on it with a transform and then using the inverse transform on that you SHOULD get the same ramp back again, which means you did it right. If you dont get the same ramp back again then you did not do something right :)
then I don't understand how this does anything for the sound...
 

MrChips

Joined Oct 2, 2009
34,628
Your interpretation of the encoded μ-law data is not correct.
For negative input values, subtract 32768 from the PCM data.
For positive input values, use 2's complement of PCM data (i.e. 65536 - pcm_value).
 

MrAl

Joined Jun 17, 2014
13,667
exactly!



then I don't understand how this does anything for the sound...
Hi,

Well understanding how this works depends on understanding other concepts that are outside of the view of input=output so there appears to be nothing gained when really there is.

A way to understand this better i think is to look up the old recording standards where certain frequencies were emphasized because they recorded worse than others, and during playback they would be de-emphasized to get the original sound back.

So basically it has to do with the medium and the analog to digital conversion process.
The goal is to get output=input even after being processed, and the processing is what changes things in undesirable ways. You want to find ways to reduce the impact of the processing. The actual improvement may not be much or it may even make it worse depending on what is wrong and how the processing is changed. So we can not just generate an analog signal and process it with high precision, then process it again with high precision to understand what is happening because we always have limited precision.

A quick example would be where we have an analog input that ranges say 0 to 1.25v. We have a system where we have 8 bits of precision and storage of 8 bits per sample, and the 5v ADC is 8 bits and the 5v DAC is 8 bits so it's a complete 8 bit system running at 5v.
What we do next is look at any deficiencies we can find. One that stands out right away is that the input is max 1.25v and the system is 5v. Using an 8 bit ADC at 5v means that we only will be using 1/4 of the precision available, which is only 6 bits. So already we are wasting 2 bits which means all of our samples will range from codes 0x000 to 0x040 when we have the capability of 0x000 to 0x0FF. That also means that we will be wasting 2 bits for every byte of storage. That also means that the resolution of the signal itself will only be able to jump from say code 0x001 to 0x002 at best, while if we had for the same signal a jump from 0x001 to 0x008 that would mean we have 8 levels available for storage and reproduction.
Once we see this problem we notice that if we AMPLIFIED in the input by a factor of about 4, then we would be using the ADC, the DAC, and the storage to full potential, as well as picking up 4 times the original level step meaning better quality sound. So all we do is introduce an analog amplifier that amplifies the original signal by 4. Note we can not do this digitally, it has to be analog. Once done, we get levels from 0x000 to 0x0FF or close to that which means we get better quality sound.
On the output however, the signal will be 4 times louder, so we might use an attenuator that drops the level down by four times so we get back the original sound volume.

That's one simpler example. If we try to understand the first system with output=input we see the same thing as with the second system output=input, but there are details internal to the system that were taken into account for the second system that make the second system output better.
 
Last edited:
Top