Archive for the ‘Audio Codecs’ Category

Sigma-Delta Modulation Primer Part II

June 30, 2007

UPDATE! Code now bypasses so16() function which would have caused errors for you.

Well here is the first order Sigma-Delta Modulation code for matlab or octave . You will need to download the wav file, or use one of your own that has audio near 16kHz sample rate, and has audio between the first 2000 and 10000 samples. I cannot currently post wav files because wordpress will not let me. If you don’t want to mess with wav’s then you can use the square wave version.

Click For Larger View:
Sigma-Delta Modulation

Go to: http://www.dailywav.com/0502.php Right click on the Space Ghost clip (whatsup.wav) file and do a save as to the same directory as the code. I would provide a direct link but they don’t seem to allow hotlinking.

Here is the first order SDM code for a wav file (GNU Octave):

Created with Vim 7.0 command :TOhtml
%    firstOrderDSM.m
%    Implements a first order Sigma-Delta Modulator
%    Copyright 2007 Brian R Phelps
%    This program is free software; you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation; either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program.  If not, see http://www.gnu.org/licenses/clear;

[x,fs,fmt]=auload('whatsup.wav');
x=x(2000:10000);
NumSamps=2000;
OSR=10;         % The over sampling rate
NumSamps=length(x)
z1km1q = 0; % Initialize variables
z1km1 = 0;
% Scale the data to 16 bit "integers", hardware 
% in real life is integer or fixed point math
%returns scaled integers
x=round(x*32766);
for n=1:NumSamps
   z1(1)=z1km1;
   xn=x(n);
   for k=1:OSR  % Each sample is Oversampled OSR times
      % please see the diagram for an explanation for the following:
      z1(k) = z1km1;
      z1km1 = z1(k) + xn  - z1km1q;
      z1km1q = (z1km1 > 0) * 32766 - (z1km1 <= 0) * 32766;
      y(k+(n-1)*OSR) = (z1km1 > 0) - (z1km1 <= 0);
   end
end

b=fir1(121,1/(OSR*2));  % A low pass filter is also an integrator (summer), 
                        % either way it is neccessary to recover the original signal
y=filter(b,1,y);        % This gets rid of the noise, which 
                        % most of which is moved out of the passband
y=decimate(y,OSR);      % Keep only 1/10 samples to get the 
                        % sample rate back down to original
plot(1:length(y),y );
sound(y/3,fs);

Sigma-Delta Modulation Primer Part I

June 24, 2007

Also known as Delta-Sigma modulation. Ever wonder how most sound cards and DACs and even many ADC’s work? The higher quality ones use Sigma Delta Modulation. The theory behind this algorithm is that you can approximate an analog waveform using just a one bit output, and an integrator (Low Pass Filter). This is similar to the way PWM works, but is actually capable of much better resolution for a given set of hardware constraints. Higher order SDM’s require multiplication and addition, which makes the hardware more expensive than a simple PWM. See Figure1 for a low order SDM:

A 1st order noise shaper

This is a first order DSM. It uses an integrator to do noise-shaping and feedback to approximate the input. Basically it tries to match the output y[n] with the input x[n] by oversampling. The higher the oversampling rate, the higher the Signal to Noise ratio (SNR). A typical 4th order DSM can achieve nearly 90-100dB SNR if the oversampling rate (OSR) is around 45. So for 44100 kHz audio we would oversample at 1984500 Hz to achieve this SNR. Keep in mind the y[n] must be fed into a low pass filter.

The SDM has two interesting characteristics that make it useful for audio:

1. It has a dynamic range that is basically only limited by OSR.

2. It works on Digital PCM (sampled) streams or analog data.

Think of it as a guesser that can either output y[n] as a +1 or -1. The +1 or -1 is fed into an integrator many times faster than samples are arriving depending on the OSR. The integrator can be thought of as a summing device, in reality it is a low pass filter. Say your input sample is a 2 and the OSR is 5.

(Previous Value)+1+1-1+1-1+1=2

The next sample is a 1 so:

(PV = 2)-1+1-1+1-1=1

I also used used a friends account at a university to model a 4th order SDM with an OSR of 45. Octave does not yet model SDM’s, but it obviously simulates them if you write the code. Modeling Sigma-Delta Modulators is difficult and involves some control system theory. Basically higher order SDM’s run into stability issues. Again, modeling is more complicated than simulation and implementation, which is what these articles focus on

I have some matlab/octave code that does a 1st order SDM on a wav file. I will post some of my code later in another post. I will post the block diagram for this soon on a follow up. The 4th order model and code/coefficients will also be included.