Matlab or C for Viterbi Decoder?

Are you bothered by speed of the speed of the simulations which you develop in Matlab/Octave? I was not bothered much, till I ran into the Viterbi decoder. If you recall, the Matlab/Octave simulation script for BER computation with hard soft decision Viterbi algorithm provided in post Viterbi with finite survivor state memory took around 10 hours to run.

I knew C was much faster, so I coded the simulation again in C. Of Course, coding in C was much more difficult. I missed many functions (like conv(), rand(), randn() etc), the plotting capabilities and the statistical analysis capabilities of Matlab/Octave.

To have a graphical debug environment for C, I installed KDbg. I already had the gcc compiler installed, and I was all set to go. The C code, executed in 14 seconds !!!

home@home-desktop:$ gcc -g -lm script_ber_bpsk_convolutional_code_viterbi_decode.c
script_ber_bpsk_convolutional_code_viterbi_decode.c: In function β€˜main’:
script_ber_bpsk_convolutional_code_viterbi_decode.c:54: warning: incompatible implicit declaration of built-in function β€˜exp10’
home@home-desktop:$ date;./a.out; date
Fri Aug 21 05:35:09 IST 2009
|BER for BPSK in AWGN with hard/soft Viterbi decoder
|Eb/N0 | BER (sim) | BER (sim) | BER (theory) |
| (dB) | (hard)    | (soft)    | (uncoded)    |
|  0   | 0.198044  | 0.096864  | 0.078650     |
|  1   | 0.128787  | 0.045707  | 0.056282     |
|  2   | 0.072306  | 0.017853  | 0.037506     |
|  3   | 0.032038  | 0.005107  | 0.022878     |
|  4   | 0.011104  | 0.001269  | 0.012501     |
|  5   | 0.002886  | 0.000277  | 0.005954     |
|  6   | 0.000696  | 0.000054  | 0.002388     |
|  7   | 0.000100  | 0.000005  | 0.000773     |
|  8   | 0.000014  | 0.000000  | 0.000191     |
|  9   | 0.000000  | 0.000000  | 0.000034     |
Fri Aug 21 05:35:24 IST 2009

Please click here to download the C code for simulating BER for BPSK with convolutional coding with hard/soft decision Viterbi having finite survivor state memory.


I created a poll to collect your feedback on the choice of programming language. You can chose any two from Matlab/Octave/C/Scilab/Simulink/Others. If you chose “Others”, please specify some more details of the programming language in the Comments section.

[poll id=”2″]

Note: RSS/email subscribers need to visit the site for participating and seeing the results from the poll.

23 thoughts on “Matlab or C for Viterbi Decoder?

  1. Hi Krishna Sankar,
    Do you have any code in Verilog for low power Viterbi algorithm for Butterfly block?
    Please if you do, kindly help me with it?
    Thank you.

  2. Hi Krishna,
    First of all,I thank you for your efforts to help out people.

    I was trying to understand C code for viterbi decoder,you uploaded.
    Please clarify my following doubt,
    AWGN implementaion has following implementation,

    EcN0dB = (float) (ii – 3) ;
    EcN0Lin = exp10((float) -1*EcN0dB/20);

    What is ‘3’ here,Is it constraint lenght? and if we have to model AWGN channel for constraint lenght = 5 and code rate 1/5, then change ‘EcN0dB = (float) (ii – 5)’ is correct and only change required to model AWGN in your implementation ,for the mentioned case ?

    Hoping an early reply,if possible.
    Thanks & Regards,

  3. urgently i need matlab code for convolution coding and viterbi decoding using awgn channel for gmsk……
    pls help me

  4. Hi Krishna Sankar,
    Do you have any code in C++ for Viterbi algorithm for Space Time Trellis Code?
    Please if you do, kindly help me with it?
    Thank you.

  5. Thanks for this awesome tutorial and sample code. 1 question thou: How come when I simulate the results, the soft decision BER is different as shown on this page? Around 0.49 for 0-9 dB.

  6. I was told that integration of C code within Matlab is not very difficult. Then, maybe the best option is combining both.

    – Matlab to speed up code development.
    – C to speed up time consuming processes and the whole simulation.

    Keep the good work Krishna. I thank you for sharing your knowledge and make all us learn and improve.

  7. I don’t know enough about you, but have you ever tried SAGE?

    The good thing about it (IMHO) is that the language you use there is Python (which is easy to learn, and also it is a standard language), so you can get your proof of concept working easily.

    If you then need speed, you can plug in Cython code (which is some sort of modified Python) giving you already some orders of magnitude of speed. If that’s still not enough, you can write a C module and use in it.

    Moreover, you already have a lot of mathematical facilities you can interface with


    1. @bastianazzo: Thanks for the comment. I have not tried Sage, but will explore.
      In the meanwhile, if you are comfortable with Sage, can you consider writing a brief article on it, sharing your experience and example usage to the readers of It will be of benefit to the community.
      Thanks again.

  8. Krishna,

    I downloaded and ran the Matlab simulation – it took 198 seconds to run on my computer. I don’t know why you state in your article it takes 10 hours. Are you using an 8080? πŸ™‚

    1. @Randy: Uuugghh! Thanks for pointing this out.
      In my desktop (Pentium4, 2.66GHz 512MB RAM, Octave 3.0.1), the simulations took nearly 10 hours. After seeing your comment, I ran the same on laptop (Dell Latitude D620 Core Duo T2400 1.83GHz, 2GB RAM, Matlab R14), the simulations completed in 55 minutes !
      Maybe it has to do with the RAM OR it may be that Matlab is better than Octave. Let me do one more trial (run the same on Laptop with Octave) and then update the post. πŸ™‚

Leave a Reply

Your email address will not be published. Required fields are marked *