Monday, July 18, 2011

OFDM Simulation using GNU Octave

Download all the files

To install GNU Octave, go to http://octave.sourceforge.net/index.html and download the Windows Installer (Octave and Octave-Forge). At the time of writing, the latest version of GNU Octave is 3.4.2, while the installer is only 3.2.4. I prefere to wait for an installer! When running the installation, you have to choose packages, I can't remember what I selected, I think at least audio and signal processing. GNU Octave is a command line program, so it is best to install a GUI, I installed GUIOctave, from http://sites.google.com/site/guioctave/download. Finally, get the Matlab 5 OFDM simulation code by Eric Lawrey: http://www.skydsp.com/resources/ofdmmatlab5.zip After installing GNU Octave and GUIOctave, unzip the zip file to somewhere convenient, e.g. C:\Octave\ofdmmatlab5. I like using 7zip for that.

Trying to run the first time

Run GUIOctave., then cd to the directory where you unzipped the code, e.g. cd C:\Octave\ofdmmatlab5.
To execute the programs, you need to add the directory to the path, e.g.
> addpath C:\Octave\ofdmmatlab5.

Now you are ready to try:

> imagetx

Oops!

warning: flops is a flop, always returning zero
error: `transmit' undefined near line 110 column 14
error: called from:
error: d:\DOWNLO~1\RDS\OFDMMA~1\imagetx.m at line 110, column 12

Do not wory about "flops is a flop". This is something not really important. The real problem is running the 'transmit' function. If you open Transmit.m, e.g. in Notepad++, notice that the function name is defined in lower case, but the filename starts on a capital. Change the file name to be all lower case.

At this point, you could try again, but Channel.m has the same problem, so rename it to channel.m.

Now we try again and get an ugly error:

error: wavwrite: Y has more than 32767 columns (too many for a WAV-file)

This puzzled me for a long time, thinking each sample you want to put in the wav file is a column. How can they expect you to have wav files only up to 32767 samples?

Try the following

>> who

You will see a variable TimeSignal. Type
>> TImeSignal
You will see it typing out to column 185600

Now try this:

>> TimeSignal'

It types out one neat column! This is it! Each column is a channel in the wave file. We want to make a mono file. One channel. I don't know yet where the problem is. In GNU Octave or this OFDM code? I don't have Matlab to test it. For now, let's just fix it.

Getting it to work.

Edit imagetx.m, again I recommend Notepad++. On line 174, add the apostrophe after TimeSignal, so it reads:
wavwrite(TimeSignal',Fs,res,txwavfile);
While you are inside this file, search it for all occurences of 'flops', and seeing it does not work anyway, comment these lines with a % in the first character. Save the file.
imagerx.m has the same kind of problem. In this case it is a bit more complicated. In line 236, add a set of brackets around the first argument, adding an apostrophe after the closing bracket, so it reads:
wavwrite((Datarx/(2^(OutWordSize)+1))',fs,OutWordSize,outfile);\
Again, comment all the lines containing 'flops'.

Running the simulation

>> imagetx
Max Signal Level: 0.95
RMS Signal Level: 0.2177
Peak to RMS power ratio : 12.797dB
Total Time: 0.34802sec

>> imagerx
Decoding Frame : 1
Decoding Frame : 2
Decoding Frame : 3
Decoding Frame : 4
BER = 0 RMS phase error = 0.0020119 Total number of errors = 0
Total Time: 0.78104sec

Along the way, imagerx will pop up a plot of the last frame. You can look at it and close it.

Check the output

Note that imagetx produced a file named imagetx.wav. The best way to look at it is using Audacity.
imagerx uses imagetx.wav as input and produces out.wav.
Now you can listen to the input file Corrs11.wav and out.wav. They are the same!
(Note: In settings.m, line 109, you can change the datatype to send. There seems to be a problem with 3, but 2 works)

I want to understand OFDM!

I have had the opportunity to try to learn OFDM since about 1998. However, I was involved more in encoding and multiplexing, up to where the signal goes into the modulator as a baseband multiplex. For me that was mostly a satellite (DVB-S) modulator. So I resisted OFDM.

Having to learn about DVB-T2 and in particular DVB-T2 MI (Modulator Interface) changed the scene somewhat, since frames and things already appear in the DVB-T2 Gateway. So I really want to try to learn.

With electronics and computers I have found that I can only understand something when I can touch and feel it, tinker with it... So I found this simulation, and although there is a nice mathematical paper OFDM Transmission simulation using GNU Octave, by Rodolfo I. Ledesma, I could not find any code that works in Octave. I don't have Matlab and am not planning to buy it soon. In my next post, I will explain where to download everything you need and slightly modify the code to make it work in GNU Octave.