Troisième: Scilab <3

Besides electromagnetism, programming is the topic I am most scared of. That is why I automatically panicked after hearing that we are to produce something using Scilab, a new programming language (for me). I was like: ‘heck, it took me two semesters to even get used to programming in python, how am I to do this in 9 hours?’ Gladly though, we were provided with a sample code and I didn’t have to google some help in stackoverflow. XD

Figure 1. I was surprised Kappa

Our guide code is shown below:

nx = 100; ny = 100
x = linspace(-1, 1, nx)
y = linspace(-1, 1, ny)
[X,Y] = ndgrid(x, y)
r = sqrt(X.^2 + Y.^2)
A = zeros (nx, ny)
A (find(r<0.7)) = 1
f = scf()
grayplot(x, y, A)
f.color_map = graycolormap(32)

Like what sir Vinni and sir Pang taught us, I analyzed how the provided program works line by line. I apologize in advance if I misexplain some of it, I am really new with Scilab. The first three lines creates a list of nx and ny numbers between -1 to 1. The following line uses this list to create a 2D grid denoted by [X, Y] with size equal to x by y. Next, it ‘transforms’ the X Y grid to the radial coordinate r. It then creates a matrix of zeros with size nx by ny and finds the values of r (which is based from X and Y) less than 0.7; therefore a circle with radius 0.7 and equates it to 1. Finally, it specifies a figure (f = scf()), and plots x vs y with the condition given by A as shown below.

Figure 2. Circle
Figure 2. Circle

Next, we are tasked to produce 7 different images that could be used as filters for images. I will discuss them in the following paragraphs two at a time, depending on their similarities. I used the original code given to us as a base, only modifying the lines 5 to 7.

The first two I did was the ellipse and the annulus since they are can both be derived directly from the circle. For the ellipse, all I had to do was to change the formula of r to that of an ellipse; that is, add a denominator equal a factor squared on both x and y as shown below:

b = 2.5; c = 2
r = sqrt(X.^2/b^2 + Y.^2/c^2)
A = zeros (nx, ny)
A (find(r<0.30)) = 1

For the annulus (funny, the wordpress doesn’t actually have the word annulus in its dictionary and the first suggestion for correction was ‘annul us’), which is actually just a ring-like region, all I did was to add a condition on line 7:

A (find(r>0.2 & r<0.4)) = 1

The images of both plots are shown below:

Ellipse
Figure 2. Ellipse
Annulus
Figure 3. Annulus

The next two are the easiest; the centered square and the cross. For both cases, I removed the line defining the radial coordinate r.  I then add a condition locating the x values which is less than |0.3| which is actually a centered vertical strip and the y values which is less than |0.3|, a horizontal strip:

A(find(abs(X)<0.3)) = 1
A(find(abs(Y)<0.3)) = 1

Combining this together, we get the cross shown in figure 4.

Cross
Figure 4. Cross

For the centered square, I just combined both conditions: meaning I located points that are x < |0.3| AND y < |0.03|.

A(find(abs(X)<0.3 & abs(Y)<0.3)) = 1

Fortunately, my first trial worked and I have produced the centered square plot.

Centered Square
Figure 5. Centered square

The last plots I paired up was the sinusoidal (corrugated roof) and the grating along the x – direction. Similar to the previous pair, I removed the line defining r, and instead replace it with A = sin(X). The first result was a singular sinusoidal wave, producing a picture with a single gradient from white to black. I didn’t know how to fix this at first, and luckily my classmate Jesli Santiago helped me by suggesting that I increase the value of X in my function. True enough, after I multiplied matrix element by 24 I was able to make a corrugated roof plot:

Sinusoid
Figure 6. Sinusoidal along x ( A = sin(X. * 24) )

To produce the grating along x-direction, my initial idea was to use the modulo operator. By dividing the each matrix positions along x (1, 2, 3, 4… 1000 in this case) by 2 and returning the remainder (thus the modulo operator), a series of 0’s and 1’s can be produced. My only problem with this is I can’t find the correct way how to do this.

10517493_10201632836377720_8231927184152787665_n

I eventually gave up this idea and just thought of a way on how to modify my sinusoidal grating since it somehow looks similar to the result I want, save the changing gradient. I realized that since the sine wave returns values from -1 to 1 I can modify my program using this range to produce a grating. Eventually, I succeed by inputting these conditions:

f = sin(X.*5.5)
A (find(f<0)) = 0;
A (find(f>0)) = 1

Here is my final plot, and shout-out to my colleague Karol Giuseppe Jubilo for suggesting that I increase the increments in my linspace function to produce my plot.

Figure 7. Grating along x
Figure 7. Grating along x

The last plot I made was the circular gradient (Gaussian) plot because I got intimidated by the word Gaussian. XD Not that I have no idea on how it supposed to look like, but because I know it has some exponential and square root functions involved so I decided to do it last. With the help of my colleague Jaime Olivares, I was able to find the proper syntax for some operations involved in my plot. The code, like the others shown above, involved only the modification of lines 5-7 of he sample code:

r = sqrt(X.^2 + Y.^2)
G = 1/sqrt(2*%pi)*exp((-r.^2)/2)
G (find(r>0.7)) = 0

Finally, I have finished this activity with this last plot:

Circular gradient
Figure 8. Circular gradient

Overall, I felt that I was able to learn and understand all the necessary things that will allow me to be called a ‘Scilab novice.’ Though scared, it is always a pleasant thing to learn new things and expand our knowledge so I am quite thankful for this activity. For now, I will give myself a perfect 10 since I was able to reproduce all the required plots. Again, shout-outs to my colleagues in 186 who have helped me pulled off this activity. Cheers!!

P.S. I will try to do additional plots as an added challenge soon!

Finally!
Finally!

P.P.S Hi! Here are some additional plots produced from the combination of the initial plots given:

First is the combination of a corrugated roof and an ellipse. This was done by multiplying the two individual filters element by element (.*)

Ellipse + Sinusoid
Figure 9. Combination of a sinusoid filter and an ellipse

Next is an ellipsoid gradient. I purposely increased the radii limit to better show the gradient feature.

Ellipse Gradient
Figure 10. Ellipsoid gradient

Last but not the least is a rotated cross. Aesthetically, nothing’s different except for the tilt but what I want to stress is the usage of the translation matrix in order to produce this. We have:

X’ = XcosΘ + YsinΘ

Y’ = -XsinΘ + YcosΘ

where X’ and Y’ are the translated coordinates and theta is the angle of translation. We then apply the same conditions I stated above to produce a rotated cross:

Cross Rot
Figure 11. Rotated cross using the 2D translational matrix

And with that, I end my blog post. I believe with this extra plots, I could garner an additional points, giving myself a total of 12 points for this entry. :-bd

 

 

References:

[1] Soriano, M., “Scilab Basics” 2015.

One thought on “Troisième: Scilab <3

Leave a comment