function mandelFrac
% MATLAB and Octave code to generate
%a Mandelbrot fractal
% Number of points in side of image and
% number of iterations in the Mandelbrot
% fractal calculation
npts=1000;
niter=51;
% Generating z = 0 (real and
% imaginary part)
zRe=zeros(npts,npts);
zIm=zeros(npts,npts);
% Generating the constant k (real and
% imaginary part)
kRe=repmat(linspace(-1.5,0.5,npts),npts,1);
kIm=repmat(linspace(-1,1,npts)',1,npts);
% Iterating
for j=1:niter
% Calculating q = z*z + k in complex space
% q is a temporary variable to store the result
qRe=zRe.*zRe-zIm.*zIm+kRe;
qIm=2.*zRe.*zIm+kIm;
% Assigning the q values to z constraining between
% -5 and 5 to avoid numerical divergences
zRe=qRe;
qgtfive= find(qRe > 5.);
zRe(qgtfive)=5.;
qltmfive=find(qRe<-5.);
zRe(qltmfive)=-5.;
zIm=qIm;
hgtfive=find(qIm>5.);
zIm(hgtfive)=5.;
hltmfive=find(qIm<-5.);
zIm(hltmfive)=-5.;
end
% Lines below this one are commented out when making
% the benchmark.
% Generating plot
% Generating the image to plot
ima=log( sqrt(zRe.*zRe+zIm.*zIm) + 1);
% Plotting the image
imagesc(ima);
i.e. some stuff and junk about Python, Perl, Matlab, Ruby, Mac X, Linux, Solaris, ...
Monday, June 25, 2007
Matlab: Mandelbrot set
Recently I read article on Free Software Magazine titled Generating cool fractals. I was very pleased to find source code for generation of Mandelbrot set.
Labels:
MATLAB
Sunday, June 24, 2007
Lenne picture
Sunday, June 17, 2007
Ruby: Tk, select directory
Just quick code for selecting input/output directory
Below an example that takes a directory and lists all txt files that contain string '_data' in their name. Than contents of each such file is copied to one output file ('allData.txt'):
#!/usr/bin/env ruby
require 'tk'
bdir=Tk.chooseDirectory('initialdir'=>'./')
puts bdir
Below an example that takes a directory and lists all txt files that contain string '_data' in their name. Than contents of each such file is copied to one output file ('allData.txt'):
#!/usr/bin/env ruby
require 'tk'
bdir=Tk.chooseDirectory('initialdir'=>'./')
fout= open('allData.txt', 'w')
Dir.foreach(bdir) { |x|
next if x !~ /_data/
fpath=bdir+'/'+x
File.open(fpath).each_line {|l| fout << l}
}
fout.close
Thursday, June 14, 2007
Excel: Visual Basic; mean value
Simple code in Microsoft VBA as a function in Excel. This function takes a range (x) and calculates mean value of the number lower than 0.
Function mymeanLr(x)
n = x.Count
ReDim xx(n + 1)
Dim ind As Integer
ind = 0
For i = 1 To n
If x(i) < 0 Then
xx(i - 1) = x(i)
ind = ind + 1
End If
Next i
mymeanLr = WorksheetFunction.Average(xx)
End Function
Labels:
Excel
Monday, June 11, 2007
bash: use sed and awk to calculate an awarage value in the file
#!/bin/bash
FD=`cat $1 | sed -e 's/^[0-9.]*//g' -e 's/^\t//g' -e 's/^M$//g' \
| awk '{for (i=1; i<=NF; i++) s=s+$i}; END{print s/9/12}'`
echo $1 $FD
The script calculates avarage of values in a txt without values from the first column.
Example txt file:
0 2.7792 2.7876 2.8023 2.8248 2.8413 2.8551 2.8679 2.8593 2.8537
18.281 2.7619 2.7723 2.7864 2.7888 2.8105 2.8314 2.8697 2.8695 2.8615
26.719 2.749 2.7464 2.7622 2.7811 2.8145 2.8276 2.8359 2.8438 2.8482
45 2.7596 2.7601 2.7614 2.779 2.803 2.803 2.8207 2.8391 2.8374
63.281 2.778 2.7809 2.8013 2.8071 2.8408 2.8423 2.8478 2.8514 2.826
71.719 2.7871 2.7927 2.8171 2.819 2.8366 2.8581 2.876 2.8826 2.8648
90 2.8007 2.8038 2.8219 2.8371 2.851 2.8628 2.8683 2.879 2.8719
108.28 2.781 2.7835 2.7901 2.7982 2.8214 2.8314 2.8488 2.852 2.8416
116.72 2.7731 2.7749 2.7784 2.7852 2.8077 2.8155 2.8226 2.8274 2.832
135 2.7744 2.7826 2.7854 2.7998 2.8153 2.8154 2.8358 2.8627 2.8775
153.28 2.7866 2.8011 2.8079 2.8253 2.8517 2.862 2.8727 2.8728 2.8644
161.72 2.7906 2.8071 2.8278 2.8424 2.8674 2.8679 2.8709 2.8716 2.8635
Labels:
bash
Saturday, June 09, 2007
Ruby: reading Microsoft/OpenOffice spreadsheets
Reading of Excel files (*.xls) can be done using roo package for Ruby.One function that I write for myself was function that reads Excel file (fragment seen on the below picture) to a Hash table, where first column (image file name) is a key. Moreover, a key is only base name of the file,i.e., no extension. The line #(1) is very interesting, as I found out that roo reads x-rays in a way that each character is separated by '0'. Thus, to get correct String, it is necessary to remove zeros from the string. This is I think due to encoding shame. Excel uses Unicode 16, instead of ASCI; hence, each character is represented by two bites: firt byte is a character, and the second byte is '0'. Hence, to read the file name to ASCI, it is necessary to skip ever second byte. It must be done, because otherwise gsub does not want to work.
def readExcelToHash f
oo = Excel.new(f)
oo.default_sheet = 1
xcl=Hash.new
0.upto(oo.last_row) { |line|
r=oo.row(line)
next if r[0].to_s.length==0
rs= String.new r[0].to_s
a=String.new
rs.each_byte{|x| a+= x.chr if x!=0} #(1)
next if rs =~ /^#/
xcl[a.gsub(/\.tif+$/,'')]=r
}
xcl
end
As far as creating spreadsheets is concerned, roo does not provide this functionality.
Labels:
Excel
Monday, June 04, 2007
Noise and blur effect on the image
Adding noise to an image adds hight frequency components to it. High frequency components are those that are responsible for image sharpness, i.e., pixel to pixel distances. In other words, the smallest components of the image like edges. On the other hand, blur removes high frequency components from the image. Bellow, the picture presents image (left column) and its Fourier transforms (right column).
P.S
Above Fourier transforms were calculated using free ImageJ software.
P.S
Above Fourier transforms were calculated using free ImageJ software.
Saturday, June 02, 2007
Ruby: Plotting with gnuplot in Mac X
To plot in Ruby, one can use rgplot interface to gnuplot. To install it, just download gnuplot-gem and install by
If you have gnuplot already installed it is good, if not install it by
Assuming that everything went well, one can now plot and save figures using Ruby.
Simple example of Ruby code that plots simple data and saves (do not display) it to png file.
This is especially useful when one want to plot a lot of figures contained in a data file. Of corese, this is the simples example, and more complicated things require to do some experimenting before desired plots are obtained. Nonetheless, this is good start.
To plot two data on one graph and to save it as png one can do as below:
Results is (graph.png):
sudo gem install ./gnuplot-2.2.gem
If you have gnuplot already installed it is good, if not install it by
sudo port install gnuplot
.Assuming that everything went well, one can now plot and save figures using Ruby.
Simple example of Ruby code that plots simple data and saves (do not display) it to png file.
#!/usr/bin/env ruby
#testGnuplot.rb
require 'rubygems'
require 'gnuplot'
outFname='graph.png'
xData=[1, 2, 3, 4,5]
yData=[2, 3 ,4, 2,3]
#xData=(0..10).collect { |v| v.to_f }
#yData= xData.collect { |v| v ** 2 }
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
plot.output outFname
plot.terminal 'png'
plot.title "Array Plot Example"
plot.ylabel "y"
plot.xlabel "x"
x= xData
y= yData
plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
ds.with = "linespoints"
ds.notitle
end
end
end
This is especially useful when one want to plot a lot of figures contained in a data file. Of corese, this is the simples example, and more complicated things require to do some experimenting before desired plots are obtained. Nonetheless, this is good start.
To plot two data on one graph and to save it as png one can do as below:
#!/usr/bin/env ruby
#test.rb
require 'rubygems'
require 'gnuplot'
outFname='graph.png'
xData=[1, 2, 3, 4,5]
yData=[2, 3 ,4, 2,3]
yData2=[1, 2 ,3, 3,4]
Gnuplot.open do |gp|
Gnuplot::Plot.new( gp ) do |plot|
plot.output outFname
plot.terminal 'png'
plot.title "Array Plot Example"
plot.ylabel "y"
plot.xlabel "x"
x= xData
y= yData
plot.data =[ Gnuplot::DataSet.new( [x, y] ) do |ds|
ds.with = "linespoints"
ds.notitle
end,
Gnuplot::DataSet.new( [xData, yData2] ) do |ds|
ds.with = "linespoints"
ds.notitle
end
]
end
end
Results is (graph.png):
Subscribe to:
Posts (Atom)