Sigma-Delta Modulation Primer Part II

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);
Advertisements

Tags:

16 Responses to “Sigma-Delta Modulation Primer Part II”

  1. rob k Says:

    hello, thank you so much for this, i am not 100% it hold all the answers, although if it can point me in the right direction, i will be eternally greatfull.

  2. electronjunkie Says:

    Rob,
    Thank you for responding. If you have any questions about S-D modulation, ask me and I’ll answer, or tell you where to find more info.

    I have done 4th order 30x S-D modulation (~95dB SNR) and it has been quite fun and I have learned lots from it. Maybe I’ll do a “Part 3” soon on 3rd order and larger S-D modulation, since the structure involves weighted coefficients to stay stable. These coefficients require Control Theory based heuristic algorithms to determine. Programs for finding these coefficients are easiest to come by using full installs of Matlab at the local university.

    -Brian

  3. Nilanjan Says:

    Hi!!
    does MATLAB6.5 support this code or we need a higher version of it?

  4. electronjunkie Says:

    Hi Nilanjan,
    Short answer: I don’t think it matters as long as the “sound” function works and has access to your hardware.

    If you find it does not, please let me know. I use Gnu Octave/forge, which is available for any OS, and it works great on Gnu Octave/Forge under Linux

  5. Vivek Says:

    Hi:

    Do 3rd and 4th order SD modulators work by simply repeating the above structure? I cannot make the above code work for order>2.

    Thanks

  6. electronjunkie Says:

    Hi Vivek,
    Thanks for your interest.

    The structure for n>=2 keeps the feedback loop on the outside and the quantizer in the same spot. The rest is just a series of the noise shapers.

    For SD modulation with orders greater than two, and for some instances of 2nd order modulation, there has to be some weighting of the coefficients. Otherwise you end up with instability. This is where some of that control theory that I never took comes into play. Using heauristical control theory algorithms, one can maximize the coefficients for stability vs. noise in the pass-band.

    I have a working SD modulator in C that reads a wav file and puts it out onto the Serial Port using a 4th order SD modulator and the Serial port at 230400 bps. It sounds OK, but when I strip the start/stop bits out with a CPLD FSM it sounds great, like 90-95dB SNR. I have been meaning to post this project. Maybe I will soon. Also, I used a friends matlab controls toolbox for my 4th order coeff’s.

  7. Jayashree Says:

    Hi!
    We tried to implement first order sigma to delta modulator proposed by you in MATLAB 7.0.1. We encountered an error as auload is an undefined function. Later we got the coding for auload.m. Now we are encountering an error regarding rindex function. Is it possible to use auread or wavread function instead of auload.

    Thanks

  8. electronjunkie Says:

    The idea behind auload is just to get linear pcm audio into the x buffer. You could just as easily use auread or wavread. That would look something like:
    [x, fs, nbits] = wavread(‘whatsup.wav’);

    I use Octave under Ubuntu Linux and it works fine. Both octave and linux are free and GPL. Octave will run under windows(yuck) also. Matlab is $expensive, but more full featured.

    You could also just put a sine wave into x by replacing the auload line with:
    fs=11000;
    x_index=1:.1:1000;
    x=sin(x_index);

    You may need to tweak fs or x_index length to your needs.

  9. bernard Says:

    hai…i want to know…why the serial ouput y(n) that have value of 1 or -1 if u input in LPF can back to the original signal? can u describe how it works?maybe i know if the LPF use as integrator…but how to proof that the PLF is an integrator or just a usual LPF?thx .

    oh again once more how do u know that the order of filter is 122?how u decide the order?thx very much.

  10. electronjunkie Says:

    Hi Bernard,
    Actually a LPF is also an integrator, always no matter how its implemented. The idea is that it keeps a running total of what it has recieved from the 1 bit d/a. So think of it as a summation register.

    The order of the filter is arbitrary, the higher the better in terms of rejecting unwanted high frequency noise. You will also notice that the function “decimate” has an LPF built in so I effectively filter it twice.

  11. bernard Says:

    hi again..btw, thx for the answer before..sorry but i have some more question. From what i learn and read about SD modulation there is some noise that being input to the modulator, like quantization noise. I don’t see any of this noise that being input in this simulation, are u decided that the level of noise in this simulation is zero or what? because from what i know one of the purpose from oversampling is to reduce the level of noise or make it into the higher frequency so that if u use LPF in the demodulator it will reject the unwanted high frequency noise, am i right?so if there is no noise that been input what is the purpose of the oversampling that u use in simulation?are they just used to make a better resolution and a better output signal? can u explain to me?sorry for all the question..thx.

  12. albert Says:

    hi i want to know to…is there a quantization noise in this simulation? can anyone give me the answer..cos i’m still a newbie in this thing..thx..

  13. electronjunkie Says:

    “From what i learn and read about SD modulation there is some noise that being input to the modulator, like quantization noise. I don’t see any of this noise that being input in this simulation, are u decided that the level of noise in this simulation is zero or what?”

    The noise comes from approximating the signal with a +1 or -1. Any time you throw away information by approximating a signal you introduce noise. The majority of this noise ends up above the frequency range of the original signal. The LPF has a cutoff that eliminates most of thee noise.

    What we are simulating here is the output of a Sigma Delta 1-bit DAC. If you were to implement this algorithm here into an FPGA and put a decent RC filter network on the output, you should end up with about the same result as the simulation produces.

  14. electronjunkie Says:

    “hi i want to know to…is there a quantization noise in this simulation? can anyone give me the answer..cos i’m still a newbie in this thing..thx..”

    There is quantization noise (due to the +1 -1 quantization output) in this simulation. Otherwise would it really be a simulation? This simulation does everything a real SD modulator 1 bit DAC would do except use actual hardware. The LPF gets rid of most of it. The way to minimize quantization noise is by increasing the order of the SDM and/or increasing the over-sampling-rate. If you don’t believe me decimate* the output and try to listen to it.

    According to on slide 17:
    http://www.digitalsignallabs.com/presentation.pdf

    And using OSR, M=10 from above and order, L=1

    20log10 ( ((6L+3)/2pi^2L)M^(2L+1) )

    The SNR is 53dB this sounds about right, but if someone knows for sure let me know.

    * by only throwing away every 10 samples, not using the matlab function “decimate” which throws away samples after runing the data through an FIR LPF. Yes this means the above code is putting the data through an LPF twice. The reason is I did not know the matlab/octave “decimate” procedure also uses an FIR on the data first when I originally coded this.

  15. Jose Antonio Says:

    Hi , i am spanish, can i see the 4º order filter S-D? can you send to my mail?

    thanks

    good work

  16. wasted Says:

    Thank you so much for this code. Made me spare some time to think about my problem, instead of spending it to write a first-order DS code.

    Best Regards

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: