User Tools

Site Tools


experiments_using_octave_for_b-tech_course

NOTE

  1. The tutorial here assumes that you have a Debian Lenny GNU/Linux system installed with Octave and Octave forge.
  2. There might be mistakes in this tutorial. If found please report me (e-mail address can be found at http://jeffrey.co.in/blog/about) or you may edit the Wiki.
  3. More example programs can be found in this page(More Octave Examples)
  4. This Wiki will be updated. So keep an eye…

INTRODUCTION

OCTAVE

Octave is a (mostly Matlab (R) compatible) high-level language, primarily intended for numerical computations. It provides a convenient command-line interface for solving linear and nonlinear problems numerically.

Octave uses some of the best and most respected numerical libraries as balgen, dassl, eispack, fftpack, lapack, linpack, minpack, odepack, ranlib, slatec-fn and villad. Octave can be dynamically extended with user-supplied C++ files.

OCTAVE FORGE

The octave-forge project contains over 500 contributed functions for GNU Octave which are not in the main distribution. These functions are grouped according to the following subdirectories: audio, comm, control, general, geometry, ident, image, io, linear-algebra, miscellaneous, optim, path, plot, set, signal, sparse, specfun, special-matrix, splines, statistics, strings, struct, symbolic, time.

While the main Octave distribution is conservative about accepting new functions and changes, octave-forge is very open. As a result, be prepared for some lower quality code and more rapidly changing interfaces to the functions in octave-forge.

– copied from Debian Lenny's Synaptic Package Manager

You can find more about Octave from this wiki pageGNU Octave

Why another OCTAVE tutorial ?

Read my blog post http://jeffrey.co.in/blog/2009/04/experiments-using-octave-for-b-tech-course/

A large number of tutorials are available in the internet for Octave. For more tutorials you can refer Matlab tutorials. Even the tutorials for Matlab can be used for Octave. This is because the commands used in Matlab are same as that of Octave. So any program in Matlab can be done in Octave ( If you have all the functions installed ! ) also.

Installing Octave

Its so easy to install Octave in Debian Lenny GNU/Linux with the help of Synaptic Package Manager. Select Synaptic Package Manager from the SYSTEM menu as shown.

From the search option, type 'Octave'

Install 'Octave 2.1' and 'Octave-2.1 Forge'

Launching Octave

Octave can be launched from the terminal. First I will explain how to use octave in command line and later the QT Octave (GUI version of Octave) will be explained.

Step 1

Launch terminal from APPLICATIONS > ACCESSORIES > TERMINAL

Step 2

To launch Octave from command line type “octave” and press ENTER key.

You will get a similar output in your screen.

Congrats ! You have succeeded in launching Octave from the terminal. The octave commands can be entered here and executed. Even you can run M-files of Matlab in Octave.

Lets start our journey !

Everything is ready for now. You can use your Octave as your old Matlab. To check whether our octave is working or not, type the command

a=1

You will get a similar output as shown below.

Now your Octave is working perfectly. The command prompt 'octave:1>' will change to 'octave:2>' and so on. The numbers show the line number.

Example

1) Plotting a sequence with x varying from 0 to 10 and y as the sine of x.

Solution

x=0:0.1:10;
y=sin(x);
plot(x,y)

The first command x=0:0.1:10; will input the x sequence. The second command 'y=sin(x);' will input y. The last command 'plot(x,y)' will plot the sequence with x on x-axis and y on y-axis.

This time you have got an analog plot. To get a discrete plot, use the command stem(x,y). you will get a plot as shown.

Try to use 'x=0:10;' instead of 'x=0:0.1:10;'. What happened ? Why did it happen ? Try to find out yourself.

Using M-files

M-files actually stand for Matlab files. Our Octave supports M-files. M-files is a file in which a set of commands to be executed in an order are written. Typing long number of commands is not that much easy. So to ease our life, we can save all those commands in the order to be executed in an M-file.

Launch a text editor. Type the commands of the above example and save it as a file named 'plotting.m' in your home directory. My home directory is '/home/jeffrey'. you can download the file plotting.m from here .

To find whether your M-file is present for the octave to access, type 'ls' command. you will get all the files listed in the present working directory (in this case it is /home/jeffrey.did you see the file plotting.m ? . If not, check you have properly saved it inside your home directory. Actually 'ls' is a UNIX command used to list all the files in the present working directory. You will get a similar output as shown.

To execute the M-file, you have to just type the file name. Here you need to just type 'plotting.m'.Try it. Did you notice the same output as obtained by typing the commands one by one ?

It is some time required to work in a specific directory, for example a folder named 'oct' or any other name because you may not want to mix up Octave files with other files. In that case you may create a new folder in your home directory named as 'oct'( you can use your own naming if you want because it is just a directory name ). Now to move to that directory , type 'cd oct'. This will change you to the directory named 'oct'. now you can execute the files in the folder named 'oct' by just typing the m-files name. 'cd' is also a UNIX command. find what it does..

Example

NOTE : I won't be explaining what each command of octave does. go and find it yourself…

2) Plotting two graphs using subplot.

Save these commands as an m-file and execute

x= [1 2 3 4];
t=0:3; 
subplot(2,1,1);
plot(t,x);
subplot(2,1,2);
stem(t,x)

You will get a similar output.

3) Input a sequence and plot it.

Save these commands as an m-file and execute

x=input('Enter the sequence')
t=input('Enter the time')
subplot(2,1,1);
plot(t,x);
subplot(2,1,2);
stem(t,x)

while you execute the above M-file, octave will ask for inputs. When asked 'Enter the sequence' type '[1 2 3 4]' or type any other sequence as you wish. And when asked 'Enter the time' type 't=0:3'. Try inputing different values.

4) Linear Convolution of two sequences

Save these commands as an m-file and execute

x1=input('Enter the first sequence');
x2=input('Enter the second sequence');
t1=input('Enter the starting time of first sequence');
t2=input('Enter the starting time of second sequence');
y=conv(x1,x2);
a=length(x1);
b=length(x2);
n1=t1:t1+a-1;
n2=t2:t2+b-1;
t=t1+t2:t1+t2+a+b-2;
subplot(3,1,1);stem(n1,x1);
xlabel('Time--->');
ylabel('Amplitude--->');
title('First Sequence')
subplot(3,1,2);stem(n2,x2);
xlabel('Time--->');
ylabel('Amplitude--->');
title('Second Sequence')
subplot(3,1,3);stem(t,y);
xlabel('Time--->');
ylabel('Amplitude--->');
title('Convoluted Sequence');

while you execute the above M-file, octave will ask for inputs.

When asked 'Enter the first sequence' type '[1 2 3 4]' When asked 'Enter the second sequence' type '[5 6 7 8 9]' When asked 'Enter the starting time of first sequence' type '0' When asked 'Enter the starting time of second sequence' type '0'

You will get a similar output for the above values.

Try inputing different values.

5) DFT and IDFT

Save these commands as an m-file and execute

x=input('Enter the sequence');
y=fft(x);
a=abs(y);
b=angle(y);
r=ifft(y);
subplot(4,1,1);
stem(x);
title('input Sequence');
subplot(4,1,2);
stem(a);
title('Magnitude plot');
subplot(4,1,3);
stem(b);
title('Phase plot');
subplot(4,1,4);
stem(r);
title('Reconstructed Sequence')

while you execute the above M-file, octave will ask for inputs.

When asked 'Enter the sequence' type '[1 2 3 4]'

you will get a similar output for the above values.

6) IIR filter - Butterworth low Pass filter Design

Save these commands as an m-file and execute

wa=input('Enter pass band frequency');
wb=input('Enter stop band frequency');
rp=input('Enter pass band ripple');
rs=input('Enter stop band ripple');
nc=2*max(wa,wb);
wp=wa/nc;
ws=wb/nc;
[n,wn]=buttord(wp,ws,rp,rs);
[b,a]=butter(n,wn)
freqz(b,a,521,2*nc);
title('Butterworth low Pass filter')

while you execute the above M-file, octave will ask for inputs.

When asked 'Enter pass band frequency' type '200' When asked 'Enter stop band frequency' type '300' When asked 'Enter pass band ripple' type '4' When asked 'Enter stop band ripple' type '10'

You will get a similar output for the above values.

7) IIR filter - Chebyshev high pass filter Design

Save these commands as an m-file and execute

wa=input('enter pass band frequency');
wb=input('enter stop band frequency');
rp=input('enter pass band ripple');
rs=input('enter stop band ripple');
nc=2*max(wa,wb);
wp=wa/nc;
ws=wb/nc;
[n,wn]=cheb1ord(wp,ws,rp,rs);
[b,a]=cheby1(n,rp,wn)
freqz(b,a,521,2*nc);
title('Chebyshev high pass filter')

while you execute the above M-file, octave will ask for inputs.

When asked 'Enter pass band frequency' type '300' When asked 'Enter stop band frequency' type '200' When asked 'Enter pass band ripple' type '4' When asked 'Enter stop band ripple' type '10'

you will get a similar output for the above values.

8) FIR filter - Hamming window low pass filter Design

Save these commands as an m-file and execute

wa=input('Enter pass band frequency');
wb=input('Enter stop band frequency');
rp=input('Enter pass band ripple');
rs=input('Enter stop band ripple');
nc=2*max(wa,wb);
wp=wa/nc;
ws=wb/nc;
neum=-20*log10(sqrt(rp*rs))-13;
deno=14.6*(wa-wb)/nc;
n=round(neum/deno);
n=abs(n);
wn=(wp+ws)/2;
	if(rem(n,2)==0)
		m=n+1;
	else
		m=n;
		n=n-1;
	end
w=hamming(m);
b=fir1(n,wn,'low',w);
freqz(b,1,500,nc)

while you execute the above M-file, octave will ask for inputs.

When asked 'Enter pass band frequency' type '200' When asked 'Enter stop band frequency' type '300' When asked 'Enter pass band ripple' type '4' When asked 'Enter stop band ripple' type '10'

You will get a similar output for the above values.

This tutorial is still under construction. Please contribute if you can…

experiments_using_octave_for_b-tech_course.txt · Last modified: 2012/02/04 19:08 (external edit)