1 Star2 Stars3 Stars4 Stars5 Stars (8 votes, average: 4.25 out of 5)
Print Print

Matlab or C for Viterbi Decoder?

by Krishna Sankar on August 21, 2009

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.

Which programming language do you prefer? (can chose any two)

View Results

Loading ... Loading ...

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

D id you like this article? Make sure that you do not miss a new article by subscribing to RSS feed OR subscribing to e-mail newsletter. Note: Subscribing via e-mail entitles you to download the free e-Book on BER of BPSK/QPSK/16QAM/16PSK in AWGN.

{ 24 comments… read them below or add one }

paramesh March 30, 2015 at 2:26 pm

Sir..I want MATLAB code for viterbi decoder…in the given code some errors are there…plz help me.


swati January 21, 2013 at 2:13 pm

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.


Krishna Sankar January 23, 2013 at 5:28 am

@swati: sorry, no


khushi October 23, 2012 at 7:24 pm

sir please help me in soft viterbi decoding in MATLAB


Krishna Sankar October 24, 2012 at 7:52 am
prash March 24, 2012 at 9:10 pm

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,


Krishna Sankar March 26, 2012 at 5:33 am

@prash: The -3 is for rate 1/2 coding (~= 10*log10(1/2))
For rate 1/5 coding, it should be 10*log10(1/5) ~= -6.99dB


baljit April 26, 2010 at 10:37 am

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


Krishna Sankar April 27, 2010 at 4:56 am

@baljit: I have not done MSK decoding the Viterbi way, however by using two symbols for demodulation, got comparable performance to BPSK. http://www.dsplog.com/2009/06/16/msk-transmitter-receiver/ Hope this helps.


Walter Gallegos December 31, 2009 at 12:32 am

Try to use Component-Pascal (http://www.oberon.ch/blackbox.html) I use this family of languages since more than 15 years, easy to learn, clean, safe, strong typing, fast compilation, run time type check, no dangling pointers, etc
In my opinion one of the best choices to scientific programing.


Oluwafemi November 3, 2009 at 4:49 pm

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.


Krishna Sankar November 8, 2009 at 8:41 am

@Oluwafemi: Sorry, I have not discussed space time trellis code.


Victor October 28, 2009 at 2:10 am

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.


Krishna Sankar November 8, 2009 at 7:38 am

@Victor: Well, there should not be much difference… can it be some problem with the rand() number generation?


kaneda October 15, 2009 at 11:17 am

g++ -lm script_ber_bpsk_convolutional_code_viterbi_decode.c

is better. when I use gcc, same warning occurred.


Krishna Sankar October 17, 2009 at 4:03 am

@kaneda: Oh, ok. The warning which came with gcc can be ignored though, I think.


Newbie September 18, 2009 at 11:23 pm

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.


Krishna Sankar September 22, 2009 at 5:42 am

@Newbie: Thanks, I ve been thinking along these lines.


bastianazzo September 15, 2009 at 1:56 pm

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



Krishna Sankar September 18, 2009 at 5:40 am

@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 dsplog.com? It will be of benefit to the community.
Thanks again.


Randy September 2, 2009 at 9:47 pm


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? :)


Krishna Sankar September 8, 2009 at 5:39 am

@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. :)


Randy September 1, 2009 at 2:34 am

I think you had something else wrong. The difference shouldn’t be this great.


Wig August 21, 2009 at 10:52 am

Very useful , thanks~!


Leave a Comment

Previous post:

Next post: