Tuesday, August 30, 2016

I now hate scilab

Fig 1. Circle
The goal of this activity is to master manipulating matrices in Scilab. I think.
The task is to create different types of images using basic matrix operations.
Given here first is a circle as provided by the initial code.

Next up is a square which is done by taking the intersection of two white strips, one vertical and one horizontal.
Fig 2. Square and Cross
 If instead of an intersection, we take the union, then we get a cross.
 Fig 3. Sinusoidal and Grating

The shortest codes I've written are for the periodic figures. These are much easier because no matrix operations are actually performed. The matrices are done just about as they are initialized. ez.

Fig 4. Annulus and Ellipse
A little bit longer but far easier to code are the circle derivatives. The annulus is just a black circle inside a white circle. How hard could that be? And the ellipse uses exactly the same code as the circle except we put different dividers on the X and Y matrices.

Finally the gaussian transparency which made me to learn how to define a function in scilab. Yeahp totally forgot to check if a gaussian function already exists (which probably does... in all languages).
I basically just set each matrix element value based on the gaussian function with respect to distance from the center. Then I blacked out everything outside some radius r.
Fig 5. Gaussian filter

I've been staring at this for about 3 hours and I still have no idea how functions actually work in Scilab. I was not able to do any combination pattern because I keep getting an error about about redefining functions and nooooo I'm not gonna brute force my matrices.

It took me around 30 minutes to do the basic figures and 3 hours to do absolutely nothing. Scilab is really frustrating right now. I remember how when I first looked at the activity I thought "I can do this in 10-15 minutes" but NOPE. More frustrating is that I can't seem to find proper/complete documentation of even basic stuff. Not even in stackoverflow can I find answers to simple questions. Or maybe I'm too stupid that my questions shouldn't even be asked. I hope not.

Gawd  I can probably do all this in Python in 10 minutes. Scilab is looking like Matlabs retarded brother whom no one really pays attention to.

Close enough



The goal of this activity is to extract a hand-drawn (or X-Y plotter) graph from an "ancient" journal.
From the scanned image, the graph equation will be estimated to be used in reconstructing the graph numerically.

For this activity I used a plot from Dr. Caesar Saloma's 1986 Ph.D. dissertation paper.
Fig 1. Original scan
I rotated the image in GIMP, taking advantage of the guide tools to make sure that the x- and y- axes are as horizontal and vertical as possible.

From the extracted graph i randomly picked several points (got 82). Instead of manually noting down the pixel coordinates of the selected points, i drew over them on a separate layer. I loaded the new image in Python, converted it into a matrix and saved the pixel coordinates from that.
Fig 2. Extracting data points

From the extracted points, I used numpy polyfit to obtain an equation of the curve. Why polyfit? Because I have no idea what form the equation might be like and polynomial fitting is like cheating when estimating equations. It doesn't fit? Just add more terms. EZ GAME.

 Fig 3. First result using 5th degree polynomial

At first I did not know how to deal with the logarithmic y-axis so I left it as is. The scaling of the x-axis turned out to be pretty good where 155 x-pixels = 1 micrometer. I observed that a 5th order polynomial looks close enough and is actually better than having a 6th term.

After 2 hours of manually adjusting my parameters, I finally got a decent looking semilog reconstruction.
Fig 4. Final reconstruction using 10th degree polynomial.

I actually used a 10-term polynomial to fit this which I feel is way beyond cheating. I really have no idea how to deal with logscale axes. 
What I did:
  1. normalized y.
  2. converted y to magnitudes via y = 10^(ypix/ky);  where ky is the number of ypixels between 1 magnitude. I used 130 pixels.
  3. renormalized the converted values.
  4. multiplied by 12 (because i think the highest value is around 12 -- definitely above 10)
  5. subtracted a constant value such that the lowest value is 0.25 (which i think is the start of the x axis) 
It's really hard to estimate values in log scale. Problem here is that I dont have a nice equation for the curve. 10^(ay^10+by^9...+k) is just not legit.
Welp close enough.


Tools used:
  1. GIMP for image conditioning(?)
  2. Python 2.7 with scipy.ndimage and numpy
  3. Microsoft Excel for plotting
  4. Garena Plus Messenger -- my favorite screen shot tool that lets you highlight/draw on the spot (plus most of my friends that I want to talk to are online here. Who needs facebook.)
The screenshot interface looks like dis
 
Sige advertise pa.

Pabonus:

Fig 5. To overlay (backgroundlay? derp) image in Excel 
Nuff said.

Self evaluation
  1. Technical correctness    -    4    (close enough but dem logscale)
  2. Quality of presentation  -    3    (It's 5:40 and I still have to blog activity 3. Goodluck)
  3. Inititative                       -    1     (Bonus pls)
  4. Total                               -    8