Octave quick tutorial
Octave is an open source software based on the well-known Matlab. Furthermore, most of the scripts working on Matlab will also work on Octave.
We mainly use this software for signal processing simulations and calculations.
This page is a kind of memorandum of the tips we learned when we started to work with that software. Instead of keeping them on a sheet of paper, we prefer keeping them on the wiki. Maybe it would be useful for someone.
Quick tips
- It is important to add the packages "signal" and "communications" at
the beginning of the script. These packages allow the user to access
some useful functions for signal processing simulations. For example
in the "signal" package one can find the
awgn()
function, useful to test the effect of additive noise on the signals.
pkg load signal
pkg load communications
- The comments can be put using both the '' and the '#' symbols. If you plan to use your script with Matlab also, the '' should be preferred as the '#' will not be recognized. Here is the syntax to put some multi-line comments:
%{
Some comments
%}
- Do not forget to put a
clear all
at the beginning of the script. It is important to remove the ancient variables that stays in the memory before executing a new script. Indeed, it is possible that some variables have the same names in different scripts and if they are not removed from the memory, they could interfere with the new ones). Theclc
command is useful to clean the Command Window. Finally, theclose all
can be used to close all the plot windows that were opened. A good idea of things to put at the beginning of your script would be:
clear all; % Clean the memory
close all; % Close all the plots
clc; % Clean the Command Window
- The
close all
command is really important when you plot things in your script. The main commands to plot results areplot(X,Y)
and thehold on
/@hold off@ commands. These two last ones are used to add other data points on the same chart. If you only use thehold on
command in your script, you should add theclose all
. Otherwise, each time you run the script, the data points would be accumulated on the same chart even if you did some changes in the script.
close all;
x = 0:100; % generate an array of points with the points 0,1,2,3, ..., 98,99,100
y = sin(2*pi*x/length(x)); % compute the points for a sine
y2 = sin(2*pi*5*x/length(x)); % compute the points for an other sine with a frequency 5 times higher
plot(x,y) % Displays the first sine
hold on; % All the following plots will be added to the first plot until the command hold off is executed even if it is not in the same script
plot(x,y2) % Displays the second one on the same chart as the first one. It is possible thanks to the previous command.
- Arrays of points are created using the syntax
x = f:s:l
wheref
is the first point of the array,s
the step size andl
the last point. Ifs
is not specified and the syntaxx = f:l
is used, the default step size is 1.
x = 1:0.5:10; % Create an array of 19 numbers : 1, 1.5, 2, ..., 9, 9.5, 10
x = 1:10; % Create an array of 19 numbers : 1, 2, 3, ..., 8, 9, 10
If these lines are written in a script, keep in mind that after the execution of these lines the only array that will stay in the memory is the second one, just because the variable names are the same.
- Like Matlab, the big power of Octave is the ease with which it is
possible to handle matrices. To create them the syntax is
x = [11 12 ; 21 22]
. This principle can be extended to create bigger matrixx = [11 12 13; 21 22 23; 31 32 33]
and so on.
- In the scripts, it is also very useful to add the ';' at the end of the lines. Thanks to that, you can keep the Command Window clean. If you do not do that, all the results would be plotted in this window. Just remove the semicolon for the results that you want to display.
x = 0:100; % Not displayed
y = 100:200; % Not displayed
z = x.*y % Displayed
- A very important thing about Matlab and Octave is the use of the '.' before a classic operator such as '*','/', ... The point means "do the operation element by element and not the operation between the matrix". For example :
x = [1 0; 0 1];
y = [2 3; 0 1];
x*y % Gives the result [2 3; 0 1]
x.*y % Gives the result [2 0; 0 1]
- To access the elements of an array or a matrix in Octave or Matlab, you have to use the following syntax :
x = [5 4 3 2 1]; % Create the array
w = x(5) % Displays "w = 1"
y = [1:10;10:-1:1]; % Create a matrix
e = y(2,3) % Displays "e = 8"
x = [1 1 1 1 1];
x(2:0) = 0 % Displays "x = 1 0 0 0 0"
- How to to simulate quantification? This method is one of the easiest we found to quantize signals.
bits = 14;
Vd = linspace(-1,1,2^bits); % Create an array of 2^bits element going from -1 to 1.
samples = interp1(Vd,Vd,y,'nearest'); % Quantize the data
- It is possible to plot different charts on the same figure. This can
be done using the
subplot()
functions. The parameter for this function is a number made of 3 different digits. The first digit represents the number of lines of charts you want to have on the figure. The second one is the number of column. And the third one is used to select the chart you want to implement. After calling the subplot, you have to put a plot function to implement the chart.
subplot(311);
plot(x,y); % Plot the x,y chart on the top of a 3 lines figure
subplot(312);
plot(t,signal); % Plot the t,signal chart on the middle of a 3 lines figure
subplot(339);
plot(z, distance); % Plot the z,distance chart on the chart at the bottom right of a 9 charts figure
- If you want to plot the data on a logarithmic chart, you can use the
functions
loglog(x,y)
or thesemilogy(x,y)
. The first one is used to plot data on a full-logarithmic graph and the second one is used to create a semi-logarithmic graph with the y-axe on a logarithmic scale. Thesemilogx(x,y)
function does the same but in that case, the x-axe is on a logarithmic scale.
x = logspace(5,15); % Create an array of 50 points from 1e5 to 1e15
plot(x) % Create an exponential
semilogy(x) % Create a strait line
- After the graph has been plotted, you can name the different axes and give a title to your chart.
plot(x,t)
xlabel('Time [s]')
ylabel('Voltage [V]')
title('myGraph')
- It is also possible to add a legend to your graph. It is useful when you plot several data on the same graph. It can be done by this way :
p1 = plot(x,t)
hold on;
p2 = plot(x2,t)
xlabel('Time [s]')
ylabel('Voltage [V]')
title('myGraph')
legend([p1,p2], 'plot of x1', 'plot of x2')
- It would be probably interesting to add some text on the chart. It
can be done by using the function
text()
. This one can be used like this :
p1 = plot(x,t)
hold on;
p2 = plot(x2,t)
xlabel('Time [s]')
ylabel('Voltage [V]')
title('myGraph')
legend([p1,p2], 'plot of x1', 'plot of x2', 'location', 'northwest') % Place the legend at the top left corner of the chart.
- It is possible to export the plots for Latex. This can be done in
EPS or in tikz draw for example. First, plot the charts, add axis
names, etc. When the chart is ready to be exported, type the
print
command.
plot(x,y)
xlabel('Time [s]')
ylabel('Voltage [mV]')
text(5,7,'One important point', 'HorizontalAlignement','Left') % Put a label with the text 'One important point' at the left of the point 5,7. The point and the label are horizontally aligned.
- Useful functions for signal processing :
-
awgn(x, SNR)
: Used to add Gaussian white noise on a signal (x). The Signal/Noise ratio is given by SNR in dB. -
quantiz(x, grid, values)
: Associate each interval of the grid with a value coming from values. It is useful to digitize samples while simulating and ADC. -
stem(x,y)
: Has the same use asplot(x,y)
but withstem(x,y)
only the data points are represented. It is better to represent the sampled data. -
resample(x,j,k)
: Change the sample rate of the signal x by a factor j/k. -
downsample(x,n)
: Create a signal composed of each n points of the x signal. -
xcorr(x,p)
: Used to compute the correlation between signal x and signal p. -
histogram(x)
: Display an histogram of the data. -
conv(x,y)
: Compute the convolution of x and y. -
fft(x)
: Compute the Fast Fourier Transform of the signal x. -
polyfit(x,y,n)
: Return the coefficient of a polynomial P (X) of degree N that minimize the least-squares-error of the fit to the points.
-
We recommend to check the help of these functions before using them. To
do it, type help Function_Name
in the Command Window.
Useful links
- Official website
- Download link
- Tutorial wiki
- Just type "Octave tutorial" on youtube, there is plenty of them