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!