Function Approximation – 1 (Radial Basis Network)

Here I give an example about the function approximation technique in MATLAB, than can be used in many areas, e.g., machine learning, robotics, artificial intelligence, optimization. The function approximation in this tutorial is accomplished using Radial Basis Networks. There are 5 simple steps that I explain individually.

1- first we make a sine wave with variable domain that will be the target of function approximation. (This is similar to MATLAB documentation)

X=-1:0.1:1;
T1 = [-.9602 -.5770 -.0729 .3771 .6405 .6600 .4609 ...
.1336 -.2013 -.4344 -.5000 -.3930 -.1647 .0988 ...
.3072 .3960 .3449 .1816 -.0312 -.2189 -.3201];

2- to make it different from the MATLAB example, I add the same wave in reverse to the data. This makes the function approximation task more interesting.

X=-1:0.1:3.1;
T2 = T1(1,end:-1:1);
T = [T1 T2];

3- I want to start from a number of points, not all of them (say the 10 first points), accomplish the function approximation and then add a new point each time and update the function approximation. This would help to understand how we can use function approximation inside an algorithm at each iteration. So we have to define the followings:

eg = 0.02;           % sum-squared error goal
sc = 1;                  % spread constant
o = 10;                 % number of points to start from
iii = 1;                  % counter-1
jjj = 2;                 % counter-2
c = hsv(size(T,2));
h = figure;

4- Now the main loop is as follows:

for ii=1:size(T,2)-o+1
TT = T(1,1:o+ii);                                                        % add one point at each iteration
XX = X(1,1:o+ii);                                                      % add one point at each iteration
figure(h);plot(XX,TT,'+','color',c(ii,:));hold on
saveas(h,['x' num2str(iii) '.png']);                        % save figure
iii = iii + 1;
net = newrb(XX,TT,eg,sc);                                     % train a radial basis network
Y = net(XX);                                                              % use the trained network
figure(h);plot(XX,Y,'color',c(ii,:));hold on
saveas(h,['x' num2str(jjj) '.png']);
jjj = jjj + 1;
end

5- the result can be seen in the following animated Gif:

4126986

Posted in Linux, Machine Learning, MATLAB, Neural Networks, Optimization, programming, Robotics, SIMULINK, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Extract numeric data from MATLAB figures

In order to extract numeric data from a MATLAB figure you can do the following steps:

1- open the fig file

2- execute the following lines of code

h=get(gca,'Children'); %get the handle of the object
xData=get(h,'XData'); %get the x data
yData=get(h,'YData'); %get the y data

3- if the extracted xData and yData vectors/Matrices are cells then you can easily convert them as follows:

xDataNum = cell2mat(xData);
yDataNum = cell2mat(yData);
Posted in Linux, MATLAB, programming, SIMULINK, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Kashi (Kashani) the Mathematician

Al-Kashi was one of the best mathematicians in the Islamic world. He was born in 1380, in Kashan, in central Iran.

In order to determine sin 1°, al-Kashi discovered the following formula often attributed to François Viète in the 16th century.

sin(3\theta) = 3sin(\theta) - 4sin^3(\theta)

This is just one of his amazing mathematical discoveries.

A method algebraically equivalent to Newton’s method was known to his predecessor Sharaf al-Dīn al-Tūsī. Al-Kāshī improved on this by using a form of Newton’s method to solve x^P - N = 0 to find roots of N. In western Europe, a similar method was later described by Henry Biggs in his Trigonometria Britannica, published in 1633.

Posted in Thoughts | Tagged , , , , , , , | Leave a comment

Simple Object Detection in 3 lines of Code (OpenCV/Python)

img_filt = cv2.medianBlur(cv2.imread('f.jpg',0), 5)
img_th = cv2.adaptiveThreshold(img_filt,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,11,2)
contours, hierarchy = cv2.findContours(img_th, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

figure_1

Posted in Linux, Machine Learning, OpenCV, programming, Python, Robotics, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , , , , , , , , , , | Leave a comment

Read, write, show images in OpenCV (Python)

In order to load or read an image you can use imread function.

>>> import numpy
>>> import cv2
>>> img = cv2.imread('filename.jpg',0)

In order to display an image you can use imshow function.

>>> cv2.imshow('image',img)
>>> cv2.waitkey(0)
>>> cv2.destroyAllWindows()

After you applied some changes to an Image you can write it into the disk. In order to write an image you can use imwrite function.

>>> imwrite('newfilename.png',img)

Finally you can do all above in the following snippet:

import numpy as np
import cv2
img = cv2.imread('myImage.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27:
    cv2.imwrite('newImage.png',img)
    cv2.destroyAllWindows()

 

 

 

 

Posted in Linux, Machine Learning, OpenCV, programming, Python, Robotics, Software, Ubuntu | Tagged , , , , , , , , , , , , , , , , , , , | Leave a comment

Installing OpenCV for Python (windows)

The good thing about OpenCV is that you can use it in Python ;)

Here I give you some hints for installing it.

0- If you haven’t installed python first you have to do that. So download (python 2.7.x) and install it. The installation is straight forward also you can find good hints here and here.
1- You have to download and install the following packages before installing and using OpenCV: Numpy, MatPlotLib.
2- Open a terminal (cmd.exe) and check if the numpy module has been installed correctly:

>>> import numpy
>>> import matplotlib

3- You shouldn’t see and error or warning, and that means that everything is Ok.
4- Download the OpenCV package from here. And extract it somewhere on hard disk (e.g. c:\).
5- Go to ‘c:\opencv\build\python\2.7′ and copy ‘cv2.pyd’ (ctrl+c)
6- Go to ‘c:\python27\Lib\site-packages’ and paste the file (ctrl+v). Hint: I assume that you have installed your python in ‘c:\python27‘, if not, you have to replace the correct path.
7- If you perform all the steps correctly, you can open a terminal and load the module to see if it has been installed correctly.

>>> import cv2

 

 

 

 

Posted in Machine Learning, programming, Python, Software | Tagged , , , , , , , , , , , , , , , , | Leave a comment

6 habbits you have to change when using TOR

You need to change some of your habits, as some things won’t work exactly as you are used to.

  1. Use the Tor BrowserTor does not protect all of your computer’s Internet traffic when you run it. Tor only protects your applications that are properly configured to send their Internet traffic through Tor.
  2. Don’t torrent over TorTorrent file-sharing applications have been observed to ignore proxy settings and make direct connections even when they are told to use Tor. Even if your torrent application connects only through Tor, you will often send out your real IP address in the tracker GET request, because that’s how torrents work.
  3. Don’t enable or install browser pluginsThe Tor Browser will block browser plugins such as Flash, RealPlayer, Quicktime, and others: they can be manipulated into revealing your IP address.
  4. Use HTTPS versions of websitesTo help ensure private encryption to websites, the Tor Browser includes HTTPS Everywhere to force the use of HTTPS encryption with major websites that support it.
  5. Don’t open documents downloaded through Tor while onlineYou should be very careful when downloading documents via Tor (especially DOC and PDF files) as these documents can contain Internet resources that will be downloaded outside of Tor by the application that opens them. This will reveal your non-Tor IP address.
  6. Use bridges and/or find companyBy default, it does not prevent somebody watching your Internet traffic from learning that you’re using Tor. If this matters to you, you can reduce this risk by configuring Tor to use a Tor bridge relay rather than connecting directly to the public Tor network.

Read more about it here!

Posted in Software, Thoughts | Tagged , , , , , , , , , , , , , , , , , , , , , | Leave a comment