Tuesday, 18 December 2012

Have you seen the pictures from Mars?

No? Ah, that's a shame. Here you go. First link, NASA photojournal. The photos there have already been processed and stitched, and I find the panoramas astonishing. It's just unbelievable that those pictures come from another world.

Second link, raw images. You probably want to select pictures from Mastcam or MAHLI if you want to see anything of interest. In the Mastcam photos you'll find also a few images taken in the near-infrared spectrum, if you're into multispectral imaging at all (here is a detailed description of the Mastcam, including the spectral sensitivity of the sensor and the spectral transmission of each filter).

For me, the drawback of the raw images is that for most of them only the visible-light high resolution version is available, while there's only a tiny thumbnail for the near-infrared. The reason for this is bandwidth. Yes, it doesn't take a lot of bandwidth to download a photo. But Curiosity is on Mars, and the interplanetary bandwidth is very limited. On top of this, the same antenna that communicates with Curiosity is used for all the other probes as well (or at least some of them, I'm not sure now and can't be bothered to google it). So, limited bandwidth and limited time slot too. The result is that at NASA engineers download only thumbnails of images, and then download the higher resolution version of those they deem worth the effort.

It's a shame, because I'd be very interested in putting my hands on some misty image of the horizon, where the near-infrared version is crisp and clear...

Monday, 3 December 2012

More about exponents

My previous post raised some interest (i.e., one person asked me about it), so I decided to follow up a little bit on that. In a nutshell, I suggested to use logarithms and exponents to raise large matrices to some non-integer power because it's faster. That is, exp(log(A)*x) is faster than A.^x, subject to the constraint that A>0 everywhere. Referring to this, I can confirm that it works on Octave as well as in Matlab. I wouldn't know about other programming languages though, if you feel like trying and letting me know in the comments, I'll create a new post about it.

To that, I wanted to add something else. If we want to raise a large matrix to some integer power, it's worth using the exponentiation by squaring. The following Matlab code is significantly faster than the .^ operator already when the exponent is 4.

function y = pow(x,p)

y = ones(size(x));

while (p > 0)
  if (mod(p,2) == 1)
    y = y.*x;
  end
 
  p = floor(p/2);
  x = x.*x;
end

Interestingly, this does not work for Octave, which probably implements a similar algorithm when it realises that the exponent is an integer. So, if you use Octave keep going with your .^ operator.

In Matlab the code doesn't look great, probably because most of its commands are native for double precision floating-point arithmetic, while here we're dividing the exponent which is an integer. Somehow, the same function written in C looks a little bit more neat.

float powN(float x, unsigned int p) {
  float y = 1.0;
 
  while (p > 0) {
    if (p & 1)
      y *= x;
  
    x *= x;
    p >>= 1;
  }
 
  return y;
}

Here we can use directly bitwise operators in the condition and in place of the division by 2. That said, this is a very low-level operation which is likely to be implemented more efficiently in whichever API you're using (except for Matlab, it seems). So I'd recommend you to double-check first, because it might very well be that you can use a vectorised version that outperforms this one.

Wednesday, 28 November 2012

.^ operator vs. logs in Matlab

Quick comparison test. Say you have a large matrix A, what's faster, A.^2.4 or exp(log(A)*2.4)?

Obviously, the fact that I'm asking should kind of give away the answer and the latter is faster. There's however a catch in all this, so it really depends on what you're doing, because you can't take log(0). But, for what I want to do this isn't a problem.

Let's say that our big matrix is actually an image in sRGB colour space, and I want to convert it to linear RGB, that is, I want to remove the gamma correction. From the Wikipedia page I find the formula, and the straightforward implementation is:

linear = srgb;
small = linear < 0.04045;
linear(small) = srgb(small) / 12.92;
linear(~small) = ((srgb(~small) + 0.055)/1.055) .^ 2.4;

But if the image is big (or your computer is slow) we can save seconds using logs.

small = srgb < 0.04045;
linear = exp(log((max(srgb,1e-3)+0.055)/1.055) * 2.4);
linear(small) = srgb(small) / 12.92;

This code, if in a function, is about three times as fast as the previous.

In this case, clipping the image to 0.001 works perfectly fine whether you use 8 or 16 bit precision images, because all those small values are only transformed linearly by this gamma function. Obviously, if you're not doing image processing this may not be good enough for you and you might have to stick to the .^ operator.

Anyhow, this works also for applying the gamma to a linear RGB image. The code is the following:

small = linear < 0.0031308;
srgb = exp(log(1.055) + log(max(linear,1e-3)) / 2.4) - 0.055;
srgb(small) = linear(small) * 12.92;

Monday, 26 November 2012

City of Norwich Half Marathon, the aftermath

I made it. I ran my first half marathon, 21 km (or 13 miles). I ran it all, with a very steady pace, just over 5'30" per km on average. Just under two hours (1:58'23") from start to finish, of which I'm very happy given my poor training. The arrival time was a couple of minutes longer because I started pretty far behind.

My legs ache though. They're better now, but yesterday shortly after the race I was limping around like an 80-year-old man (in fact I've seen 80-year-old men in a better shape than that!).

What's next? Who knows. I think I'll keep running, I'm not sure when I'm going to race next time. I'm really tempted to give the City of Norwich Half Marathon another go next year though!

Saturday, 24 November 2012

City of Norwich Half Marathon

Tomorrow I'm running the City of Norwich Half Marathon. I had the idea just over three months ago, and I read that three months are enough to prepare for a half marathon. They would be, if you're able to maintain a good training regime.

If, like in my case, you can't, three months become a very short amount of time. Unfortunately, I had two pretty long breaks from running over this time.

The first was when I bought some running shoes that weren't appropriate for me and my legs ended up aching so much that I wasn't even able to run. Two weeks down the drain there.

The second is that my left shin started to hurt and I was afraid I had shin splints (and no, I didn't learn about them through Wikipedia). I stopped running again, until I finally went to a physio who assured me that I didn't have shin splints. Still, three weeks down the drain there, and unfortunately they were the last before the actual race.

Anyhow, the time has come, three months flew by and after a 5 mile run on Wednesday I'm just glad that I still have a few hours to rest before the race. It's going to be very, very tough for me.

Friday, 23 November 2012

New website

Welcome to my new website! It's about time. I hadn't updated my old one for ages. I was managing the CMS myself, which is some extra faff that I really didn't need. I'm now on blogger, which does all I need and is simple to maintain. At this point, I'll probably switch my hosting plan to a domain-only plan.

Some people might know that I also had a blog on tumblr. This new website, hopefully, will be the union of both my previous personal website and my old blog, but without all the "noise" of tumblr. Now I'd like to close that blog but I can't without deleting my tumblr account all together (which I might do, eventually), but to give a sense of continuity I reposted here its last entry, which you should see below this one.

So ultimately, the purpose of this website is to have somewhere to host my CV and provide a way for people to contact me; but I'd also like to use it as blog. Knowing myself I'm genuinely not sure how often I'll be able to update it. But hey, still worth a try. What will I talk about? I'd say anything that crosses my mind. Work-related things, hobby-related things... I guess I'll play it by the ear!

Friday, 27 July 2012

Problems with MacBook and Bluetooth speakers

I recently bought a Bluetooth speaker (Creative D100), and was really annoyed about how badly it worked with my Macbook. As soon as the sound stopped for a little while (even 30 seconds), the pairing would drop and the Mac wouldn’t be able to reestablish it on its own, and would start playing sounds from the embedded speakers. I had to reconnect the speakers manually a few times before they’d start working again.

Conversely, on my Android phone they work very smoothly - the pairing would start as soon as I enable the Bluetooth.

After a little bit of googling, I found the solution. It helped a lot, although I have the impression the connection isn’t quite as smooth as with my Android phone yet. But then, I haven’t tried it much so I might be wrong.

For the lazy who don’t want to click the link:

  1. Find the file ~/Library/Preferences/com.apple.BluetoothAudioAgent and double click on it. It should open on the plist editor.
  2. Change the value “Apple Bitpool Min (editable)” to 50.
  3. Save and close.

That’s it. Should this cause problems, just follow the same steps again and change the value back to the original (35 I think, or whatever it was on your system).

Edit: Mountain Lion seems to have solved these issues.