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 home@home-desktop:$

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.

## Poll

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.

**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.*

{ 23 comments… read them below or add one }

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.

@swati: sorry, no

sir please help me in soft viterbi decoding in MATLAB

@khushi: Please take a look @

http://www.dsplog.com/2009/01/14/soft-viterbi/

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,

Prash

@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

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

pls help me

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

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.

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.

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

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.

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

g++ -lm script_ber_bpsk_convolutional_code_viterbi_decode.c

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

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

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.

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

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

http://www.sagemath.org/

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

Regards

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

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?

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

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

Very useful , thanks~!