## Friday, 20 November 2015

### Prime Spirals in Python

I watched this really fascinating video on prime spirals recently and decided to see if I could write my own code to generate them. It's not terribly efficient but it works! Code is on github! :D

### Machine Learning: Implementing Linear Regression and Gradient Descent in Python

It's been a while since I went through Andrew Ng's Machine Learning course on Coursera. The course is very good at introducing the basic concepts but it lacks the depth of knowledge that I need if I really want to understand how to implement and use some of the algorithms on my own. So I decided to work through a more difficult textbook on machine learning. One of the first things I decided to do was implement the machine learning algorithms from scratch using my language of choice: Python.

The most simple idea out there that can be called machine 'learning' is linear regression. I first learned about linear regression in high school math class. So when I saw that linear regression was the first topic in the machine learning course, I was a bit skeptical. I felt that linear regression was too simple to be called machine learning. But it turns out that linear regression is quite a powerful tool to make predictions from data. The term 'linear' in linear regression is a bit misleading because it makes you think about straight line graphs and gives you the impression that trying to fit straight lines to data can't really be that useful. The trick to levelling up the power of linear regression lies in how you choose your variables (or features)! So you can have features that are crazy functions of your initial variables.

To understand this think about how we can use a straight line to fit a set of data points that you think might be exponential in nature. Say yoususpect that output $y$ is related to the variable $x$ by $y = ke^{cx}$. You can rearrange this equation to $log(y) = cx + k_1$ by taking the natural logarithm of both sides of the equation. Now we can plot $log(y)$ vs $x$ on a graph and try to fit a straight line to it. Here the parameter that we 'learn' is $c$ and $k_1$. The variable $x$ is called a feature.

In a similar way you can add features that are functions of the existing features if you think that the relationship is not linear. In a sense we are choosing a 'basis' of functions which we can mix together in different amounts to (hopefully) accurately model the output $y$. The way a Fourier Series builds up a periodic sequence using sine waves of different frequencies is exactly the same as how linear regression works with non linear basis functions.

So I decided to make the system 'learn' the square wave in my first implementation of linear regression. First some math:

The output prediction $\hat{y}$ can is represented by the linear model below. Here 'n' is the number of feature vectors.

$$\hat{y} = \sum_{i=1}^{n} \theta_i x_i$$

To use gradient descent to train this model we need two things: a cost function and a gradient function.

The cost function is a function that takes the values of theta, and the features and the data samples and calculates a cost based on the error between the prediction that the model makes and the actual value. The most commonly used cost function is the mean square error cost function.

In this equation, m is the total number of training samples that we have and $x^{(i)}$ represents a feature vector that is ith training example. The cost function sums over all the training examples available to give one real number that represents the cost of the current values of the parameters.

$$J = \frac{1}{m} \sum_{i=1}^{m}(\hat{y}^{(i)} - y^{(i)})^2 \\ \frac{\partial J}{\partial \theta_j} = \frac{2}{m} \sum_{i=1}^{m}(\hat{y}^{(i)} - y^{(i)})x^{(i)}_j$$

The gradient of a function is a vector that points in the direction in which the function changes the most. So if I want to find the place where the cost is minimum all I have to do is to start out at a point, find the gradient at that point and go in the opposite direction. I have to keep doing this until the gradient is zero (or very close to it). Think of it like a ball rolling down slopes until it comes to rest at a place where there is no slope. So in Python I have to make a loop and each time the loop is run, I update the parameters theta using the rule. Here alpha is the learning parameter.
$$\theta_j := \theta_j - \alpha \frac{2}{m} \sum_{i=1}^{m}(\hat{y}^{(i)} - y^{(i)})x^{(i)}_j$$

So I got a large set of points that were in the shape of a square wave and I used sine wave basis functions as features. After running gradient descent for a thousand iterations I got coeffecients that were really close to the actual Fourier Series expansion of a square wave!
 The learned function superimposed over the original square wave

 How the error decreases over time.

You can find my implementation on my github page.

## Sunday, 15 November 2015

### The One Thing that my College Gets Wrong About Exams

I'll be honest. Despite it's shortcomings, many of the courses that are offered actually do contain material that I find interesting. In fact, when you're learning something at the level of a college course, there are few subjects that aren't interesting. It's like Richard Feynman said; everything is interesting if you go deep enough into it. My biggest complaint about the system isn't the lack of depth in the courses or for that matter the professor's depth of knowledge (although I have had a fair share of instructors who had a less than optimal grasp of the course material themselves). My biggest complaint is the focus of the examinations that they conduct that are supposed to evaluate how well the students know the material. The exams  - in addition to having a low correlation to the students' grasp of the material - also actively discourage them from gaining a true understanding of the course material.

Typically, the exams I've written (particularly exams of courses that are highly mathematical) test only the ability of the students to memorize two things: derivations and formulae. I'm not saying that people should be exempted from memorizing formulae. There are plenty of formulae that are so important that they must be memorized. The problem is when you have to memorize formulae that look like this:

$L_F = -10 log(\cos{\theta} \{\frac{1}{2} - \frac{1}{\pi} p(1-p^2)^{\frac{1}{2}} - \frac{1}{\pi}arcsin(p) - q[\frac{1}{\pi}y(1-y^2)^{\frac{1}{2}} + \frac{1}{\pi}arcsin(y) + \frac{1}{2}]\})$

$p = \frac{\cos{(\theta_c)}(1-\cos{(\theta)})}{\sin{(\theta_c)}\sin{(\theta)}}$

$q = \frac{\cos^3(\theta_c)}{(\cos^2(\theta_c) - \sin^2(\theta_c))^\frac{1}{2}}$

$y = \frac{\cos^2(\theta_c)(1 - \cos \theta) - \sin^2(\theta)}{\sin{(\theta_c)} \cos{(\theta_c)} \sin{(\theta)}}$

For anyone interested it's a formula that tells you the coupling loss between two fiber optic cables when they're misaligned by an angle theta. The book doesn't even attempt to derive this formula because the authors know that the derivation on its own doesn't really add to a person's understanding of fiber optics. It may be a nice exercise that tests the student's algebraic manipulation but that's pretty much it. In the real world, if there is a situation in which this formula needs to be used, it's pretty much guaranteed that the engineer will just look it up. It's completely pointless to memorize it.

Why is this focus bad? Firstly, it does not actually test understanding and leads to students not getting the score that they deserve. There have been countless instances where I knew  how to solve a problem but didn't get any credit because I had to remember some stupidly long equation. Secondly, it forces examiners to limit their questions to really simple ones because they know that remembering the formula or it's contrived derivation is half the question. So the number of questions that test my understanding of the material is next to zero. Thirdly, it actively discourages students from trying to understand the material. Once people figure out that they can get scores by just memorization, they will start focusing on memorization. Sometimes, there is so much to memorize that there isn't enough time to try and understand even if I wanted to.

If I were the person who was in charge of the setting the examinations, I would do the sensible thing and give every student a booklet full of the formulae that need to be used for the questions. This allows the questions to be far more interesting and test how the student is able to use the information at his disposal to solve actual problems that practicing engineers face. The brain is a processor with a limited amount of cache! It's not a hard disk! Introducing this simple change of focus will go a long way towards courses that are actually useful. It will also give the professors the freedom to ask tougher questions!

Now I know that this approach actually works! Before college I did my Cambridge IGCSEs and A-Levels. Both these exams were very focused on understanding rather than recall. Exams either had all the required formulae included as part of the questions or (as was the case for Further Mathematics at A-Levels) a large booklet of formulae. This didn't affect the difficulty of the exam at all. It's no use knowing formulae if you don't know how to use it. This might sound a bit strange but I actually enjoyed writing my IGCSEs and my A-Levels. The exams were designed well enough that sometimes I came out of the exam hall with a new understanding of what I had learned. I still feel that I understood and learned more in class during my high school than I did in most of my college courses. Any understanding that I have about my course material is a result of my independent reading and efforts rather than the course instruction.

Now I'm not the first person to talk about this. People have been complaining about the education system for a very very long time. And many have even tried to change it by approaching the administration. Invariably though, I've seen that - at least in my college - the people in charge are very reluctant to bring about any of these changes. The biggest roadblock is probably the fact that a lot the of the professors in my college disagree with what I have said above. They believe that memorization is very important perhaps because that is how they were taught. I've seen that things are changing, but not nearly fast enough. I don't really think that the changes that I've suggested will be implemented any time soon. So why am I writing all this down? I really just really needed to vent. XP

## Thursday, 15 October 2015

### Simulating a Double Pendulum in Mathematica

I've been playing around with Mathematica's Non-Linear Control Systems toolbox over the past few days and it's been brilliant! One of the first systems that I tried to simulate is the double pendulum since it's such a commonly used sample problem in non-linear controls.

The first step is to write down the total kinetic and potential energies of the system and find the Lagrangian. Once you have that, you just need to run the Lagrangian through Mathematica's EulerEquations function and then set up a non-linear state space model using the differential equations.

Once you have the system model it's really simple to get the response and make a simple simulation.

And out pops a wonderful demonstration of chaos! Here are are three simulations with slightly different initial conditions showing diverging trajectories.

 Initial conditions: {160, -160}
 Initial Conditions: {161, -160}

 Initial Conditions: {161,-161}

The next thing I tested was whether I could make a simple controller to stabilize this double pendulum pointing straight up. I went with a standard Linear Quadratic regulator. In this simulation there is only one actuator at the elbow joint. So this is also an example of an underactuated robotic system. Works great! :D

## Sunday, 11 October 2015

### Simulating Mechanical Systems in Mathematica

I've been trying a lot of different software for simulating mechanical systems for my project. By far, the most fun I've had is when I was simulating it on Mathematica. Mathematica is a seriously cool piece of software. I got a simulation of the simple pendulum up and running almost 3 times faster than I did when I was working with Python and Matplotlib. Although, to be fair, the time I did it in Python was the first time I was doing the simulation. So I was learning about how to do the simulation as well as trying to make it. So that might have affected the amount of time I took.

## Friday, 9 October 2015

### Movie Review - Interstellar

Interstellar is a movie that I had been looking forward to for quite a while. After watching the movie I started going through a lot of the movie reviews that people started posting. I was quite disappointed to see so much negativity and nitpicking directed towards the movie.

The movie is set in a dreary post apocalyptic earth ravaged by dust storms. Food is scarce and blight is creeping in on the last of humanity's food supplies. To survive, people are forced to take up farming. In one of the farms Cooper - a NASA engineer turned unwilling farmer reminisces about the time humanity used to look up at the stars and wonder about our place in the universe.

Cooper soon discovers that all hope is not lost when a gravitational anomaly leads him to a hidden NASA base full of people who are working on one last mission to save humanity. A wormhole has appeared near Saturn that put a new galaxy full of potentially habitable planets within the reach of humans. Cooper is asked to help NASA with visiting the most promising of these worlds to investigate. They visit a world with tidal waves the size of mountains and a world of ice and snow all the while having to deal with the time dilation of a super massive black hole.

The movie excelled in its depiction of space travel and the realities of time dilation. It very realistically depicted the science of gravitational time dilation. The graphics of the worm hole was brilliant, dwarfed only by that of the absolutely gorgeous black hole.

The AI of the robots TARS and CASE were an unexpected delight! The movie did not try to make the AI look humanoid or impossibly advanced. The shape and movements of TARS were practical and realistic. The highly advanced natural language processing and fluid intelligence that the robots displayed was truly remarkable. As a person who is reasonably familiar with robotics, I know that these are areas where active research is going on. In fact, quite recently robotics research has started shifting from conservative control systems - that are very slow and careful - to control systems that make full use of the dynamics of the body of the robot - very similar to how TARS controls itself - to create robots that can make quick and acrobatic movements. The humor settings were a nice touch.

The harshest criticisms were leveled at the slightly unusual finale with Cooper floating around in the black hole and sending messages back in time. Many say that this ending was unsatisfactory and ruined what was otherwise a good movie. I disagree. Science fiction movies and novels can be characterized as a spectrum with hard science fiction on one end (like the work of Arthur C. Clarke) and soft science fiction on the other (like Doctor Who). Maybe people were complaining because most of the movie can be classified as hard science fiction while the black hole scene is more 'soft'. However, I find the idea of an advanced species manipulating a black hole to send messages back in time no more outlandish than the idea of them creating a wormhole at will. If there are species that are capable of the kind of engineering required to construct a stable wormhole, I think it's safe to assume that they have also figured out the mechanics of a black hole.

Also, a lot of the reviews that I've seen of interstellar keep looking at it from the  point of view of a movie critic. They look at the movie as though it was designed to give them some sort of intense entertainment experience and that the movie fell short of giving it to them. I think it's unfair to expect Interstellar to be like that. Movies like interstellar are not good because they are packed full of action or witty dialog. Movies like interstellar are good because they make us dream about a better future. Just like Carl Sagan's COSMOS and Neil deGrasse Tyson's awe inspiring monologues on space, this movie has the power to inspire us. Perhaps one day in the future we'll become a space faring civilization and some of the people working on it will talk about how this really old movie about space travel captured their imagination.

So Interstellar is not a movie to watch if you're looking for action or comedy (although the movie has its fair share of both). It's a philosophical movie that is intended to make us think and wonder; and at least in my case, I think it succeeded in doing exactly that judging by the unusually long post-film trance I was in.

## Thursday, 8 October 2015

### The Magical Euler-Lagrange Equation and the Calculus of Variations

I've been learning a lot about simulating and controlling mechanical systems for one of my projects. Of all the math that I learned, the most amazing was the Euler-Lagrange equation.

$\frac{d}{dt} \frac{\partial L}{\partial \dot{q_i}} - \frac{\partial L}{\partial q_i} = Q_i$

The overarching theme of the control systems that I've been reading about is optimality. And wherever we need optmization, Calculus of variations tends come into the picture. Calculus of Variations is a branch of mathematics that studies the problem of finding functions that minimize the value of a functional (or cost function) in a particular range. As an example, take the range $x \in [0,1]$ on the real line and assume that $f(x)$ is a function that is defined in this range. There are an uncountably infinite different possible functions $f(x)$. A functional is an operation that maps the function $f(x)$ on to a real number. Think of this as a "cost" for the path that the function draws on the graph. The main problem that calculus of variations tries to solve  is finding the function $f(x)$ that minimizes the "cost".

This area of mathematics has applications in a huge variety of fields and arguably the most important of which is in the field of Physics. Long back some physicists found that the Newtonian Mechanics that everyone knows and loves can be reformulated as an optimization problem. They found that there is a quantity known as "action" (which is defined as kinetic energy - potential energy) that is minimized as particles and other things in the real world go about their daily business. This is commonly known as the "Principle of Least Action". Coming back to the Euler-Lagrange equation, mathematicians figured out that plugging in the functional into this equation gives the constraints of the system that cause it minimize the functional. This means that every single dynamic problem in Newtonian physics can be reformulated as an optimization problem! Pretty neat.

So say you have a simple system like a simple pendulum and you want to figure out the equations of motion of system. The way people are used to doing it is drawing a diagram that shows the net forces acting on the system and then deriving the equations of motion using Newton's  three laws of motion and energy constraints. Using the Euler-Lagrange equation, the procedure is slightly different and arguably simpler. The first step is to write down the Kinetic and Potential energies of the system.

$T = \frac{1}{2} m l^2 \dot{\theta}^2$
$V = -m g l \cos{\theta}$
$L = \frac{1}{2} m l^2 \dot{\theta}^2 + m g l \cos{\theta}$

To get the equations of motion of the system,

$\frac{d}{dt}\frac{\partial L}{\partial \dot{\theta}} - \frac{\partial L}{\partial \theta} = -b\dot{\theta} + u$
Where U is the torque an actuator (motor) at the base of the pendulum gives to the pendulum.

This gives:

$I\ddot{\theta} + b\dot{\theta} + m g l \sin{\theta} = u$

No fiddling around with forces and no need to try and figure if the net force is zero or not. Just works with pure energies! This works every time as long as you write down the Lagrangian correctly. The first time I saw this, I couldn't believe it worked! For the following few weeks I'd take Lagrangians of random things for fun and check to see if it was giving the correct answers.

This also makes me think about the amount of information we need to know 'everything' about a system. Before I learned about the Lagrangian, I had a 'gut-feeling' that in physics, fields were everything. Maybe this is because school and engineering level education in physics focuses a lot on field theories in physics. However, the fact that we can get the complete behaviour of a system from an expression that just encodes the total energy of the system implies that somehow, the energy of the system encodes all the necessary information to describe it completely. The equation suggests to me that potentials and energies are more 'fundamental' in a sense than fields.

## Monday, 5 October 2015

### Movie Review - Real Steel

Two weeks ago I discovered pure awesomeness in the form of Battle Bots. I spent my whole evening watching every episode of the 2015 season. Seeing my excitement, my friend suggested that I watch this movie called Real Steel which was released in 2011.

The movie is set in a future where human boxing is outlawed (Good!). Instead people watch insanely cool robots go all out against each other for entertainment (Fantastic!). A retired human boxer (Charlie Kenton)  who is trying to make it big in the robot boxing (and failing miserably at it) has to spend two months looking after his son (who he hasn't seen in years). Though they are initially cold to each other, the duo eventually learn to work together with a little scrapyard bot called Atom and make it to the Real Steel Tournament, repairing their relationship along the way. The movie is surprisingly heart warming despite the fact that it is about robots fighting violently to their deaths while people cheer (Or maybe it's heartwarming because of that :D ).

Of course, what I liked most about the movie was were the robots! Being a robotics enthusiast and having built quite a few robots myself, it's really nice to see more realistic robots being depicted in film. Far too many movies get robots wrong. Most of them vastly over estimate their abilities or completely ignore the laws of physics. I liked the fact that the robot fighters in the movie were controlled via joysticks and had the sort of jerky, non-human movements that characterize robots. I get the feeling that the movie was aiming for realism - which is a goal that I really appreciate, especially when the movie is about a subject like robotics which I'm really passionate about.

The fight scenes between the robots were brilliantly choreographed and genuinely exciting. The urge to cheer on little Atom during the final fight was completely overwhelming.

The movie also gave me a lot to think about. It set my mind a-buzz with ideas about how to implement a robot boxing tournament like that in real life. It also got me thinking about how I would go about designing robots like that. The problem is harder than most people think. This amusing compilation of robots attempting the DARPA Challenge gives you a good idea of how good our robots are in real life. The movie was a satisfying blend of science fiction, robots and very human, emotional moments. Definitely a worthwhile watch.

## Friday, 28 August 2015

### Book Review - The Martian by Andy Weir

Fantasy books were my gateway drug into the world of reading. Starting with Harry Potter, The Inheritance Cycle and the Lord of the Rings, fantasy was a staple of the steady diet of books. Then I discovered Famous Five, Secret Seven and other children's adventure novels. I started reading science fiction quite late into my life. Although I read a few books here and there I didn't really get deep into the genre until I read Douglas Adams' "The Hitchhiker's Guide to the Galaxy". Nowadays I tend to prefer science fiction over other genres. So when I found out the the upcoming movie "The Martian" was based on a novel by the same name, I immediately hunted down the book so I could read it before the movie came out. Turns out the book was pretty short and I finished it by reading on and off during my free time over the past couple of days. So here are my thoughts on the book.

Mark Watney is an astronaut on the third manned mission to Mars. Due to an unfortunate and unexpectedly strong dust storm five days into the mission, NASA had to abort. When the crew was making an emergency evacuation from the artificial habitat, the dust storm blows a piece of debris into Mark Watney, impaling him with a piece of metal and taking out the electronics that monitored his vital signs. With no vital signs from Mark's suit and the storm threatening to tip over the vehicle that they needed to lift off from the martian surface, the crew had no choice but to leave his (supposedly) dead body behind. Mark wakes up on Mars after the storm with a slightly leaky suit and the stark realization that he is now stranded on an alien planet. He needs to use his knowledge and resourcefulness to survive on a dusty red wasteland till someone can send help from Earth.

The first thing I noticed about the book was how realistic a lot of the mission terms and procedures seemed to be. The actual structure of the Mars Mission had a lot of the idiosyncrasies of a real mission. The science presented in it appeared to be accurate as well. I decided to look up the author after finishing the novel. It turns out the that the author is a computer programmer who put quite a lot of effort into making sure that the science in the book was reasonably accurate. From his pictures on Wikipedia I think he probably hung around NASA a lot and learned as much as he could about the procedures they follow and how they handle communication with the media and the general public before writing in those parts of the book. The book really gave me an insight into how difficult it can be to communicate science and technology effectively with the public and  how important it is to do it right.

As I was reading the book it struck me that Matt Damon is the perfect person to play Mark Watney. With the incessant swearing and wisecracking, it was almost as though the character of Mark Watney was written with Matt Damon in mind. Of course, this is probably confirmation bias on my part but that's what was going through my mind as I read the book.

The Martian is also in the Goldilocks Zone with regards to the level of technical detail that the author decided to add in - not too much to turn away a general audience and just enough juicy detail to draw in the science nerds. Being a science nerd myself, I thought the density of science was ideal.

A lot of the story was written from the perspective of Mark Watney as excerpts from his log. The reader is for the most part experiencing the story from the point of view of the man stuck on Mars. This made the novel feel very personal and all the more thrilling. Those brief moments where the writing switched to third person were absolutely spine chilling because in general, the book followed the pattern of switching to third person when things are about to go south.

'The Martian' is an optimistic and very heartwarming exploration of what humans can do together if they really put their minds to it. A lot of people sacrificed a lot of things and a lot of money so that one man could come back to earth from Mars. Rivalries were set aside when the life of another human being was at stake. Would the events have played out similarly in real life? I don't know. But I believe - as the author probably did - that most humans are good at heart. So if asked about the most important thing I learned from the book, I'd say it is the maxim: "Be excellent to each other!". :D

## Thursday, 20 August 2015

### My Thoughts on the Ashley Madison Hack

The Ashley Madison website was hacked about a month ago and hackers stole gigabytes of user data including their full names, addresses and credit card data. On August 18, the hackers released all this data on the web. Now all this sounds really bad until you find out what Ashley Madison actually does. It's a website for men and women in a committed relationship to find other men and women in a committed relationship and have an affair. So while many disagree with the amount of information that the hackers actually released, I'm sure that a lot of people around the world are secretly happy that the data was stolen.

Infidelity is a despicable thing. I know how incredibly hurt and betrayed I would feel if I found out that my partner was cheating on me. The people who run the site are not doing anything illegal but they are complete assholes. Yes, it's true that the activities of the company are completely legal (as their public statements keep reminding us) but I think that in a way, this hack is the internet's way of telling Ashley Madison what gigantic dickheads they are for providing a platform for people to break promises.

However, despite the obvious reprehensibility of the target of the hack I don't condone the actions of its perpetrators. In fact, I would say that stealing personal information and violating the privacy of thousands of people is just as bad - if not worse. Releasing personal information on the internet often leads to online mobs carrying out witchhunts similar to what happened on reddit in the aftermath of the Boston Bombing incident. Two wrongs don't make a right.

Freedom of speech is a wonderful thing. But it doesn't protect you from criticism. In fact when you're doing appalling things, people around you have a right to call you out on it. The idea that I'm trying to explain is beautifully conveyed by this xkcd comic.

 Alt text: I can't remember where I heard this, but someone once said that defending a position by citing free speech is sort of the ultimate concession; you're saying that the most compelling thing you can say for your position is that it's not literally illegal to express.

So the people at Ashley Madison should take this as a hint. No, what you're doing is not illegal but a lot of people (even some evil people) think it's contemptible. And they're showing you the door.

## Tuesday, 18 August 2015

### Dung Beetles are Extremely Cool

I've noticed little bugs scurry across the road quite a lot during my time at this college. Most of the time I've been too busy to actually go down and examine these bugs. Today, when I saw one of these critters crawling beside the road I decided to take a closer look.

What I found was the Dung Beetle in the process of rolling a ball of dung and trying to find a nest. Dung beetles are extremely cool creatures. From Wikipedia:

The "rollers" roll and bury a dung ball either for food storage or for making a brooding ball. In the latter case, two beetles, one male and one female, stay around the dung ball during the rolling process. Usually it is the male that rolls the ball, while the female hitch-hikes or simply follows behind. In some cases, the male and the female roll together. When a spot with soft soil is found, they stop and bury the ball, then mate underground. After the mating, both or one of them prepares the brooding ball. When the ball is finished, the female lays eggs inside it, a form of mass provisioning. Some species do not leave after this stage, but remain to safeguard their offspring. The dung beetle goes through a complete metamorphosis. The larvae live in brood balls made with dung prepared by their parents. During the larval stage, the beetle feeds on the dung surrounding it.

Here's a video from National Geographic that shows what the Dung beetles do.

## Tuesday, 28 July 2015

### Creating a Simple Bot using the Telegram Bot API

So Telegram recently released an API for creating bots on Telegram! It's quite cool. Already, people have released nice Python libraries for it. For this bot I'm using one of them.

I decided to create a simple Telegram bot that I can use to control the music on my laptop. It's a testament to the power and simplicity of Python and the convenience of the linux terminal that it only took me an hour to develop this. Head over to my repo at gihub if you want to look at the code!

Here's a video of it working!

### Movie Review - Whiplash

Note: This post may contain spoilers.

I used to watch sci-fi and animated films exclusively but now every once in a while I watch a random movie just for the fun of it. Yesterday that random movie turned out to be Whiplash - a movie about an aspiring drummer and his well intentioned but abusive mentor.

I loved the movie but I disagree with the opinions of Terence Fletcher. There is a scene in the movie where he is asked if there is a line. If there is a limit which if he crosses he might actually discourage his student from becoming the next great musician that the world loves. And he replies that there is no such limit because the next great won't be discouraged. While that's a cute sentiment, I think he's wrong. The world isn't divided into 'The Greats' and talentless people. There are plenty of people who fall in between the spectrum. And I think that by not caring about those who are not destined to be 'Great' and even actively doing things that could damage them mentally he is doing more harm than good. Additionally, I think that there are a lot of people who respond much better to constructive criticism and positive reinforcement than to abuse hurled at them. For every person with a story about an abusive mentor who made them who they are, I think there are a few dozen people with stories about how their incredibly supportive and positive mentor inspired them to be their best.

So while the story was inspiring, I really hope that people don't use Terence as an inspiration.

## Saturday, 18 July 2015

### Maker Faire Singapore

The day before yesterday I found out that there was a Maker Faire going on in Singapore this weekend! I immediately scrapped all my other plans for the weekend to get there. It was definitely worth it. I owe a lot to the maker movement. It's what got me interested in engineering and I think it's a big part of the reason I am the person I am today!

The highlight of the Maker Faire was definitely the whole hall dedicated to Intel Edison projects! There were so many cool things in that hall! I have to admit that I only had a vague idea of what the Intel Edison did before saw all those demos. It's a pretty capable SoC. My favorite demo were these dancing hexapods. They were really cool to watch. The second best demo was the robot with omni directional wheels.

 The Dancing Hexapods

There was also quite an impressive display of 3D printing at the Faire. There were so many exhibits related to them! Apart from the conventional designs using steppers to move the extruder in the X-Y plane I also saw a 3D printer based on a delta robot.

 Saw this 3D printer that could print huge parts

 The delta robot 3D printer

### Why Self-Driving Cars are the Future of Personal Transport

So Google's self driving cars are getting really good at self driving. A lot of people I know are skeptical about self driving cars. Some say that they will never use them because they just love to drive. I think that once self driving cars are actually on the road, they won't have a choice.

From everything the the testing of the self driving cars have shown so far, they seem to be much safer than human drivers. They don't get tired and they never break the rules. Human drivers on the other hand, cause millions of deaths every year due to carelessness and driving while sleepy, drunk or under the influence of drugs. Even if self driving cars only cut down the number of car accidents by half we have a moral obligation to switch over. By making the choice to continue driving manually we are making the choice for millions of deaths to continue happening every year.

I think that sometime in the distant future people will talk with horror about the time that people were allowed to drive their own cars. Just like we now talk with horror about the horrifying state of medical practices before we knew about germs and how exactly the human body worked.

## Friday, 3 July 2015

### Terminator Genisys – Review

I watched the movie a few days ago. And I thought I’d write down some thoughts I had about the movie.

First of all, I have to admit that although I roughly know what happens in the earlier movies I have only watched one of the previous three movies.So this is the first Terminator film the I’ve watched fully.

For me, the movie was just OK. Nothing spectacular. Visual effects were nice and that was probably the best part of the movie.

Apart from the fact that the movie uses a completely overused plot of using time travel to reset what happened in the earlier movies, my main complaint about the film is the portrayal of the new enhanced symbiote John Connor as evil. By merging with the machines, John Connor has finally solved two of humanity’s greatest problems: aging and disease! Why does the movie portray that as bad?
In fact, one of the most most interesting thing researchers in medicine are working on right now is using nanobots for targeted drug delivery. Imagine having an army of nanobots inside you making sure you don’t age and never get any disease.

If everyone were to merge with the machines like John Connor did there would actually be peace again and human civilization would have taken the next logical step of using their now immortal bodies to explore the vastness of interstellar space!

For once the AI in the movie has a goal to work together with humans and they just had to portray that as evil. I’d love to see a Terminator film where the humans and the AI (it’s supposed to be super intelligent!) finally realize that the best thing to do is cooperate.

## Monday, 22 June 2015

### Another Weekend in Singapore

Now I’m working for a lab I guess I have a good idea of what working a 9 to 5 job will be like. I barely have time to do anything on weekdays. Free time has become a very precious resource. So weekends are the only time I get to actually do stuff. And most of the time, after a busy week all I feel like doing on the weekend is relax.

I’ve started going to the movies almost every week now. I didn’t have an opportunity to do that in Trichy. The theaters there are horrible. They tend not to show the kind of movies that I want to watch and when they do they either run it at a really inconvenient time or they dub it in Tamil. This place is heaven compared to Trichy. There are so many awesome places to see and visit! And so many good theaters!

Last Saturday, I decided to go visit the library. I felt like sitting in a quiet place and relaxing by reading something. I hadn’t done that in a really long time. I browsed around till I found a book that caught my eye. It was “Contact” by Carl Sagan. And I just sat down there and read for the next 4 hours. I really missed doing that. I’d do that a lot when I was a little kid. But after starting college I didn’t have much time to do that.

I went back to the library again on Sunday. This time I thought I’d do some reading on machine learning. There were these tables with power sockets on them set aside for people who wanted to do some work in the silence of the library. It was nice to sit there and work. The girl sitting beside me was also studying machine learning apparently. She had this book called “Supervised learning With Complex Valued Neural Networks” on her desk.
I think I’ll visit the library at least once a week from now on. It was nice to take a break from the general noise of the city.

I took a picture of the library building! It looks pretty cool!

## Saturday, 20 June 2015

### Using IFTTT for blog syncing.

So since I decided to keep both my blogs running in parallel, I’ve only posted two blogposts and I’m already starting to find it annoying to update both blogs every time I get a new idea for a post. So I decided to try out this thing that I signed up for a long time ago, but never really used till now. IFTTT (IF This Then That). It’s a web service that allows you to automate your social media accounts by having certain actions triggered automatically when you do something. For example, you can set it up so that whenever you like a video on youtube, it’s automatically tweeted or shared on Facebook or posted on your blog. It’s actually quite brilliant!
Now I signed up for this service when it was just starting out because I thought it was a really cool idea. But at that time I didn’t really have much going on in the web. So even though I had an account, I never really used a single recipe till now. Now I’ve finally created one so that whenever there’s a new post in my wordpress blog on my website, the same thing is automatically posted to my blogger blog as well.

## Friday, 19 June 2015

### Analysing sound in Python

I'm trying to build a simple word recognition system in python. As a first step, I needed to find a way to get audio sample data from my microphone and store it in a numpy array in Python. After a lot of searching and experimenting I finally found a library that works well for this task: pyalsaaudio.

This small piece of code records roughly two seconds of audio from the default microphone and plots the spectrogram. This was actually a bit tricky to figure out so I thought I'd share the code for anyone out there who might be trying to do this.
#!/usr/bin/python

import struct
import alsaaudio as aa
import numpy as np
import time
import matplotlib.pyplot as plt
from pylab import *

SAMPLERATE = 8000
PERIODSIZE = 160
CHANNELS   = 1
CARD = 'default'

inp = aa.PCM(aa.PCM_CAPTURE, aa.PCM_NONBLOCK, CARD)
inp.setchannels(1)
inp.setrate(SAMPLERATE)
inp.setformat(aa.PCM_FORMAT_S16_LE)
inp.setperiodsize(PERIODSIZE)

sound = np.array([0])

if __name__=='__main__':
ctr = 20000
while ctr > 0:
ctr -= 1
if l:
samples = struct.unpack('h'*l, data);
sound = np.append(sound, np.array(samples))
time.sleep(0.0001)

print(sound.size)

#plt.plot(sound)
#plt.ylabel('amplitude')
#plt.xlabel('time')
#show()

Pxx, freqs, bins, im = specgram(sound, NFFT=1024, Fs=8000, noverlap=900, cmap=cm.gist_heat)
show()


## Tuesday, 16 June 2015

### I have a new website!

So it's summer holidays again! This time I'm spending my holiday as a research intern at NUS in Singapore. I love the labs here! They're absolutely amazing. I got an opportunity to see one of those huge KUKA robotic arms up close. Me and my friends are working on a four legged robot that uses a lot of compliance and underactuation. It's been quite a fun experience. Singapore is a really nice place. I had a little bit of trouble adjusting initially because the rooms we were staying in were quite small. Then I realized that that's a consequence of living in a big congested city like Singapore. Real estate is expensive and cheap accommodation will be small in size.

Moving on, I've been thinking of setting up a website for myself for quite some time. But I never had a big enough block of free time to set it up. So I finally decided to do it during these holidays. It took me almost a week to tweak it to my satisfaction and even then there's a lot of room for improvement. But I think it's finally time to release my website to the internet! It's live at www.ashwinnarayan.com! I've documented a lot of the projects that I've done over the last few years there.

I'm having a little bit of trouble deciding what to do with my Blogger blog. My new website has a blog and I've already imported most of the posts to it. I'm slowly publishing the older posts after fixing some formatting issues. But I'm not sure that I want to leave this blog either. I've been writing in here for years and years. I think I'll keep both the blogs alive and post in both of them. Maybe I will use the blog on my website to post exclusively about technical stuff. Or maybe I'll post on both blogs in parallel for a while.

## Monday, 2 February 2015

### VGA Output from an FPGA

So after the simple 8 bit counter on the fpga I decided to get started with generating VGA output from my FPGA. Because that seemed to be a reasonably simple yet really cool looking project. Initially I thought I'd go the cheap way and solder up my own VGA circuit. But I just ended up wasting a lot of time and effort to get a really substandard circuit that would die on me all the time. So I got myself one of these VGA modules from the Numato Labs website.

First a bit about VGA signals. VGA was originally intended to work with CRT monitors. So the standard was built around the working of a CRT monitor. These type of monitors use a Cathode Ray Tube(CRT). Inside every CRT is an electron gun that produces a beam of really fast electrons. Electric fields are used to deflect this beam so that it can fall on different positions on a screen. The screen has dots(pixels) with phosphors that glow when hit by electrons and when all of these dots glow.

CRT monitors build up an image by scanning this electron beam horizontally across the screen in one line, then moving the beam a short distance downward and scanning again and again. The image is built up line by line.

So a VGA signal has two clocks called Vertical Sync (VS) and Horizontal Sync (HS). The HS pulses every time the screen starts scanning a new line. And the VS pulses every time the whole screen is scanned once. For more details on VGA signals, visit this page.

The R, G and B, signals have nominal values of 0.7 volts. So 0 volts on the R line means no Red component and 0.7 volts means maximum red component. The inputs have impedances of 75 ohms.

I took a look at the schematic of the VGA module.

You'll notice that there are three pins connecting to the red and green input signal and 2 connecting to the blue input signal. The pins are connected through resistors whose values rise exponentially. This is a simple way to perform DAC. I decided to work through the equations to see what input voltage levels were available to me.

For the Red and Green channels, I took the value of the smallest resistor as $R$ and the input voltage as V and went from there.

$\frac{V-V_1}{4R} + \frac{V-V_2}{2R} + \frac{V-V_3}{R} + \frac{V}{R_L} = 0$ (Using KCL)

Rearranging in terms of $V$ we get:

$V = \frac{V_{cc}}{14 + \frac{4R}{R_L}} (2b_0 + 4b_1 + 8b_3)$ where I set $V_i = V_{CC} b_i$

So after substituting all the numerical values. $(V_{CC} = 3.3V, R_L = 75 \varOmega R = 500 \varOmega)$ I get:

$V = 0.09802(b_0 + 2b_1 + 4b_2)$ where $b_2 b_1 b_0$ is a 3 digit binary number. This allows me to have 8 different shades of red! Following a similar procedure I can get expressions for the other colors. And in total I need 8 bits (3 for red, 3 for green, 2 for blue (poor blue :( (I like using nested parantheses. (I should probably stop now.)))) to represent the color of one pixel. So I have a palette of 256 colors to work with.

Now it's time to get started with generating the VGA sync signals.

I'm learning from this textbook called FPGA Prototyping by Verilog Examples by P. Chu. It has a brilliant section on generating VGA output. However, I couldn't just copy the code in there and check if the circuit was working. I have a small external monitor which I use for testing purposes. It turns out that this monitor (because it's a very cheap monitor) only supports one video mode. 1368 x 768 @ 60 Hz refresh rate. Nothing else works. Following the signal timing information about this resolution from the tinyVGA website, I modified the code (actually I typed it out line by line on my own so I could understand what was going on in each line. I highly recommend this technique when you're learning a new language.)

It took quite a while for me to get the output working properly. First I tried using the ipcore wizard to generate a pixel clock that was exactly the same as the recommended one. But when I tried to get the ISE to compile the code, it complained about timing issues. So I used the ipcore wizard to generate a clock that was exactly twice that of the pixel clock and divided this clock by 2 inside the VGA synchronization module.

Here's a video of the working VGA output:

Now I think I'll get started on generating something interesting on the fpga. Maybe some fractals?

## Monday, 26 January 2015

### i3 Window Manager - After 7 months.

So I've been using the  i3 window manager for quite a while now. And once in a while I miss some of the functionality that certain programs in Ubuntu provided. Especially some of the functionality of the network settings tool. So just for one evening I decided to switch over to the Gnome Desktop Environment.

I had to switch over to i3 within a few hours because after the wonderfully customizable and workflow friendly key combinations that I had set up in i3 fiddling about with the mouse on the desktop environment felt really clunky and frustrating. It took me more than twice as much time to do a simple task like open a text editor and start writing some C code.

Rich desktop environments like Gnome and Unity are good if you're a beginner or if you don't plan on using your computer for anything other than web browsing. But if you're into programming or doing anything even remotely technical with your computer, keyboard friendly tiling window managers like i3 will definitely increase your productivity.

## Monday, 5 January 2015

### Setting Up a Cool Dr. Who Wallpaper That Changes Every Hour on Linux

I came across this really cool idea by someone on reddit to take this awesome wallpaper of the 12 regenerations of the doctor and highlight each Doctor by the hour. This is easy enough to do on Windows which has wallpapers which can change every minutes. Being a user of linux, I was interested in trying to get a similar thing to work on linux.

First here is the set of all the wallpapers.

https://imgur.com/a/av5TP

Download the wallpapers and put it all in a folder somewhere on your system. Say you put it in /home/ash/Pictures/whopaper/

Rename each picture so that the filename corresponds the the regeneration number. The first doctor's highlighted picture should be named 01.jpg and the second doctor's should be named 02.jpg and so on till 12.jpg.

Then set up a cronjob that is executed every hour that checks the hour on the clock and sets the background to that. For doing this I installed a program called "feh" (sudo apt-get install feh) which allows me to set my desktop wallpaper from the terminal. I also installed a program called gnome-schedule (sudo apt-get install gnome-schedule) which allows me to easily edit the crontab file.

Run gnome-schedule and click on "New" to add a new cronjob. Select "A task that launches recurrently". Add a short description (like "Cool Doctor Who Wallpaper" and in the command field enter the following command:

env DISPLAY=:0 /usr/bin/feh --bg-scale "/home/rationalash/Pictures/Wallpapers/DRWHO/\$(date +\%I).jpg">>sys.log

Replace the path to the wallpaper in the command to the folder in which you saved the wallpapers.

Under the  "Time & Date" section select "Basic" and set the task to run every hour.

And that's it! Now if you don't keep your computer on all the time the wall paper might be wrong for some time because the cronjob only runs every hour. If you want to be sure that the wallpaper is always the same as the hour on the clock even if your turn your computer on a lot edit the frequency of the cronjob so that it runs more often. (Say once a minute).  The wrong wallpaper won't be there for longer than a minute.