Kalman Filter’s Daddy!

November 13, 2009

Aaaaah! Research! Oh how much I love thee, and how much I HATE thee!

Khair… I have been trying to understand how actually Kalman filters work for a while now and Believe me when I tell you they aint easy! I went through hell to get the knots in my brain straightened out! I read a book called Introduction to Random Signals and Applied Kalman Filtering in about two days, at least the first 6 chapters! I reference a gazillion papers and other material online, talked to a mathematician, run simulations and what not… the list goes on. Heck I was so frustrated and unsure of myself that I had to relearn my 4th semester signals and systems course (the state spaces part).

Who knew in the end good old fashioned tinkering would make it work. So! I have decided that I would nt let the other people looking for a quick KF fix roam around aimlessly like particles in Brownian motion in the dark…

Ok first thing I learnt was that you need the theory, I read this fairly complicated and annoying book mentioned above… After reading the chapter like 3 times, I was able to grasp (somewhat) what it was trying to say. But the prolem is (I think that the equations dont directly apply for some reason when implementing the damn thing…. Plus most of the time you would be wondering how to calculate and use most of the variables. So yes in short its not the way to go if you are thinking of applying it in the near future.

But the reading part is very necessary, cuz you definitely need to know the why  part… for the how, go to this fairly good article on embedded.com. Ok this article also get confusing as it does nt tells you a LOT of things… like how will you be calculating the noise covariances and what is the difference between measurement noise and process noise.

To answer these very relevant questions I m writing this article.

First of I m going to show a very simple example…. nothing complicated… What I m going to do is collect data from a single accelerometer and filter its output rather than the position data calculated from it. This reduces most of our state space matrices to 1. The commonly used system eqs are

x_hat_dot     =     A_mat * x    +    B_mat * u     +     w

y              =               C_mat * x        +        z

Ok…. to explain what these are you ll need to read some basic text on state spaces, I would recommend Signals, Systems, and Transforms by C.L.Phillips, J.M.Parr and E.A.Riskin. I am not going to go through that over here… NO CHANCE IN HELL!

So moving on to Kalman filters and our simple filtering app…. What I did was simply record a lot of data from the accelerometer and calculate the mean and variance/standard deviation parameters for the data. There is a small problem where that needs to be address. most sensors give the zero force point by Vcc/2, and since the filter parameters require the mean to be near zero and the SD to be around it, a transformation is in order.The easiest say is to subtract the average and divide by the gravity value. So thats that…. Now we have our sensor data and its parameters. the measurement and process noise are supposed to be different for the system. But while playing around with the values I found that if you Q as SD ^ 2 and R as SD you get an awesomely filtered result. Now dont ask me why the works. But theoretically R should be the SD and Q should be calculated from the A matrix and some other info about the process.

So the simple program that I ran in MATLAB is:

 

phi_k = 1;
H_k = 1;
R_k = (7.29738/(3113.08-2131.33));
P_min_k (1) = 0;
Q_k = R_k ^ 2;
P_k(1) = 1;
x_hat (1) = 0;
n = size1/NumberOfColumns;
for i = 1:n -1
%     K_k(i) = P_min_k (i) * H_k * inv(H_k * P_min_k (i) * H_k + R_k);
%
%     x_hat(i) = x_hat_min + K_k(i) * (da1(i,1) - H_k*x_hat_min);
%     x_hat(i+1) = phi_k * x_hat(i);
%
%     P_k (i) = (1 - K_k(i) * H_k) * P_min_k(i);
%     P_min_k(i+1) = phi_k * P_k(i) * phi_k + Q_k;
K_k(i) = phi_k * P_k (i) * H_k * inv(H_k * P_k (i) * H_k + R_k);
x_hat(i+1) = phi_k * x_hat(i) + K_k(i) *(da1(i,1) - H_k*x_hat(i));
P_k(i+1) = phi_k * P_k(i) * phi_k + Q_k - phi_k * P_k(i) * H_k * inv(R_k + P_k(i)) * H_k * P_k(i) * phi_k;
end
figure;
hold on;
plot(K_k, '-r');
plot(P_k, '-b');
plot(P_min_k, '-g');
xlabel('Time/sample');
ylabel('Bits');
title(strFileName);
figure;
hold on;
plot(da1(:,1), '-g');
% plot(x_hat_min, '-r');
plot(x_hat, '-b');
xlabel('Time/sample');
ylabel('Bits');
title(strFileName);

 

phi_k = 1;H_k = 1;R_k = (7.29738/(3113.08-2131.33));P_min_k (1) = 0;Q_k = R_k ^ 2;P_k(1) = 1;

x_hat (1) = 0;n = size1/NumberOfColumns;
for i = 1:n -1%     K_k(i) = P_min_k (i) * H_k * inv(H_k * P_min_k (i) * H_k + R_k);%     %     x_hat(i) = x_hat_min + K_k(i) * (da1(i,1) - H_k*x_hat_min);%     x_hat(i+1) = phi_k * x_hat(i);%     %     P_k (i) = (1 - K_k(i) * H_k) * P_min_k(i);%     P_min_k(i+1) = phi_k * P_k(i) * phi_k + Q_k;    K_k(i) = phi_k * P_k (i) * H_k * inv(H_k * P_k (i) * H_k + R_k);        x_hat(i+1) = phi_k * x_hat(i) + K_k(i) *(da1(i,1) - H_k*x_hat(i));        P_k(i+1) = phi_k * P_k(i) * phi_k + Q_k - phi_k * P_k(i) * H_k * inv(R_k + P_k(i)) * H_k * P_k(i) * phi_k;
end
figure;hold on;
plot(K_k, '-r');plot(P_k, '-b');plot(P_min_k, '-g');xlabel('Time/sample');ylabel('Bits');title(strFileName);
figure;hold on;
plot(da1(:,1), '-g');% plot(x_hat_min, '-r');plot(x_hat, '-b');xlabel('Time/sample');ylabel('Bits');title(strFileName);

 

 

Keep in mind that the Data was already loaded into the MATLAB workspace. The figure of interest is

kalmanWorks like a charm.

 

Dont ask me how or why though… I m just happy it does!


Of pillows and parties

November 7, 2009

Finally bough myself a pillow… aint that grand!

008

And we had a tikka party yesterday… farewell party for a flatmate of mine. Have nt had so much fun burning my hands with my eyes all watery and red, while doing “chores”. It was a blast.

Will post the pictures later.

EDIT: Can someone please explain to me the point of a Double Cheese Burger WITHOUT meat??? I get it you are a vegitarian, so order fries, apple or some other vegitarian friendly thing! Dont be blasphemous to such a divine thing!!!


Eyes of the Beholder

November 2, 2009

I went to the doctors today, for the headaches. And out came the story of my life…. oh sorry my eyes. Well, what happened was that he suspected that the headaches were caused by my eyesight, which is significantly shortsighted. And he was about to prescribe glasses all over again albeit my fierce protests, till he actually came around to checking my eyesight. And thus came out the story of my life…. oh sorry, my medical history, or more specifically my eyes.

The story starts at my birth as always… I have a hereditary deficiency where I have  a degenerated (or degenerating) retina, and a mild squint (which is not mild at all). It comes from my mother’s side of the family and there are a fair number of people there which have tackled this all their lives.

So, back to the story, so I have this thing and its not life threatening (apart from a hereditary scare of Alzheimer, but lets skip that one), so as a toddler, one of the first books I got was this BIG book of aircrafts, especially combat aircrafts. Now this was NOT a toddlers book by any means. Its was a book that military personnel use to  reference aircraft specs and dimensions (to scale) and memorize their shapes and what-nots. Since this book had some really cool posters, which my father thought (at the time) would be cool to put around their son’s room. So my parents especially my mother used to “read” me that “book” as a “baby”.  Also, as you guys must know, I learnt to draw from my mother too. So the first thing I learnt to draw was airplanes too. And thus was born my dream of flying, becoming a pilot.

Every 3 year old wants to be a pilot and astronaut, but I was different. It was nt just a dream, it was an obsession. By the age of seven I could spit out aircraft specs just by looking at just a picture. I knew how airforce aircrafts are different from navy aircrafts, I knew what were the latest prototypes and what new technologies were being tested and what they meant for the world of flying, I knew what vortex lift was  and what fly-by-wire meant. The first blow came when I was five, I had just started school and I had trouble looking at the board. My parents took me to the doctors, and from the simple eye tests, they could nt know what was wrong. So I had to go to a specialist, who told me that I had a degenerated retina, which meant no flying, and that glasses would nt really help,.I was told not to look at bright lights and because of the squint at fast moving objects like cartoons and bright sunlight, one of the reason why I usually wear sunglasses.

For a few months I did nt even understand what happened and I thought it was just a joke or something, after that I almost forgot. I even wore glasses for a month or so, but because of “peer pressure” I left that, and since it did nt really help but make it easy for me to explain why I need to sit in front of the class (which I never got to sit cuz of my height) no one really bothered to encourage it.

By the age of ten I realized what it meant, and by that time my obsession had run deep. And when it dawned on me that I would never become a pilot. I went through the whole four steps of loss and when finally came in the acceptance, I had a new obsession… airframe design engineer, or flight dynamics engineer. And it was right about the time when I had to make the first decisions about my career, I had to choose my subjects for the tenth grade.

I was always a bad student and no one expected me to really succeed. The problem was nt math and science I was good, even great at them, the problem was everything else, urdu, english, islamiat, social studies and the works. I had a bad eyesight which meant bad handwriting and even worse speed. And since I used to sit at the back, could nt see the board which meant I went from bad to worse. So when the time came I had to really make an effort. Khair one thing a guy like me is good at are obsessions and focus. Now that I had a reason, it became an obsession and finally I aced it, ok not aced it, I barely made it through.

Then came in college and I knew the odds were not just against me, but that they were a certainty, not odds. They was just one school in the country that offer aeronautical engineering, and only three ways to get in. a) join the airforce, for which I failed the preliminary medical, again eyesight. b) Join as a PIA cadet, again the same thing, and c) join as a NUST student, I did nt get in, did nt have the grades or the scores.

So I took the next best thing I took up electronics. I was good at computers from an early age, so I figured computer engineering and computer science would be boring. I did nt take up mechanical because…. well I dont know, cuz I wanted to and I had better prospects after it. I realized good computer skills would help me while doing electronics so I took it up. Again came that dreaded medical test and I again knew what was coming. So this time I went in prepared.  I knew this was just a simple eyesight exam which did nt really matter as opposed to its air force ans PIA counterparts. So I had my specialist doctor write up a not and approve it. I attached that to the application and I FINALLY got in.

Khair so barely made it out of engineering school and got into grad school by sheer luck. Which is good. But now I think this deficiency is going to haunt me again. Cuz as I told my doctor a shorter version of all this I realized he was confused as to what to do with me. Now I have a note to consult another specialist and another bad headache. Story of my life.


headaches

October 24, 2009

My headaches are not fancy, I dont get migraines or any other name they might have for the various varieties of headache that are out there.  And I go completely out of whack when I m ill. I dont like taking medicine even though I come from a family of doctors.

So my current headaches come and go like electricity in karachi! And that is saying something! And when they come I cant do anything, not even think, its like brain paralysis. Heck I burned my hand while making chai for myself!

Ahh, these headaches make me want to be seventeen and a def leppard song (from their early years… although the later years are nt bad either, just a bit depressing, Pour some sugar on me compared to When love and hate collide, Foolin’ compared to Two steps behind, get what I mean?), When I actually thought I had the energy to change everything. And now I can even change my existence, Heck my headaches either!

Ok, here comes another one… so see you after a break.


sick

October 23, 2009

Oh I know I m sick…. but this time I m really sick…

My eyes are burning, my ears are red, my face is pale, I ache all over as if I ve been run down by a truck! And especially my neck and the back of my head for some reason, I think I have a slight fever, but I can find the energy to find a thermometer are measure it,  and I cant sleep. Last night when I did I woke up after half an hour. AND I NEVER HAVE TROUBLE SLEEPING!!!! Something is VERY wrong.


25 Random things…

October 21, 2009

about the world as I see it… Its too boring to talk just about oneself.

Anyway lets start.

  1. I have trouble deciphering 5 with 7, 1 with 11, 2 with 10, and 8 with four on a watch with hands but no numbers!
  2. I m strictly NOT a fruitarian…. the only fruit I can eat are apples and bananas, and I dont like either very much.
  3. I hate mangoes, I m probably the only Pakistani who does that!
  4. The only times I screw up are the times when I dont double check, and double check is one of my many OCDs, I need to check my keys, the locked door, my phone before living the room or house.
  5. I m a print freak! I need to print everything and anything, even the stuff I have already read! And I never throw away paper! I have manuals and datasheets dating back to the third semester.
  6. I can never choose ice creams or cakes! There are so many of them and I want them all! So when it comes to ice cream I always end up eating chocolate and strawberry, or chocolate cakes when cakes are in question.
  7. I use scorching hot water to bath a bath… even in summer when its 49C outside!
  8. Even now, when I have access to wire strippers and cutters, I peel wire jackets with my teeth.
  9. Talking about teeth, I top right incisors is fake, broke it while playing in the ninth grade at school. Ergo my hate for dentists (There is another reason too though, I think)
  10. I HATE social sciences people… and I am stuck with five of them! Argh! And the reason I hate them is because I think “social science” is a wrong term, as science implies empirical as well as theoretical proofs.
  11. I day dream constantly, although its pretty much the same dream everyday… it has evolved over the years.
  12. I have a very steep learning curve, or atleast I think I do, but I fail to excel, I just end up doing them very normally.
  13. I m terrible at verbal and visual cues and reading between the lines.
  14. I dont find Angelina Jolie attractive, beautiful, sexy, hot, pretty or any other such adjective. And my definition of all these terms differ from most guys
  15. I HAVE to cut my mails every friday! I can NOT stand big nail, even on other people.
  16. I hate doing numbered lists, I either go off course, or dont know what to put in halfway through.
  17. No matter how hard I try I cant make routines.
  18. I still have those old LEGO set from when I was 6 years old, and I still play with them.
  19. I m arachnophobic, I hate creepy crawlers and buzzers of all types. But I freeze only for a moment and then kill them straight away.
  20. As a child I was scared of human skeletons, the root cause can be traced back to the terminator 2 for some reason. The fear vanished when I was 12.
  21. I want to visit Antarctica, I m probably the few people who actually want to go there, I even tried applying, contrary to common belief, I learnt that there are a lot of people wanting to go there, and getting a job is very competitive.
  22. I sleep like a log…. people have told me that I have slept through thunderstorms and imbicile friends trying to wake me up. Although I wake up to my mothers voice at the first call, and she does nt even have to be loud about it.
  23. In my house, every remote control there is has its lid for the battery compartment broken, and they say its my fault.
  24. I m very possessive about my stuff. And one thing I dont like sharing with anyone is my computer.
  25. I hate going out in slippers, I can go bare foot but not in slippers.

Thats an hour wasted… I hate tags, damn you Huda!


Last of the wilds

October 14, 2009

Not exactly wilds, but geeks. Yes our generation is probably the last of the old school geeks, and yes there is a huge difference. For your convenience let me iterate you through some of them. The old school geeks were a derivation of hippies. Brilliant minds with the courage to do things, if not the energy or the upper body strength to do em.In fact most of the REAL old school geeks were actually hippies. I m not going to cite examples here but take my word for it.Khair the difference between the old school and new school is subtle but when your know it, it seems like the great divide.

The Old school geeks were not just bookish and computerish, they believed in stuff. their likes and dislikes were not just because of their desire to fit in or the new trend in geekiness, but because of their opinions. And their opinions were driven by the various beliefs that they had. This not only set them apart from the new school geeks but the majority of the society itself. The new school geeks are geeks because they want to be portrayed as such. They are not the popular of the handsome or the bad asses of the world, so to fit in they form their own niche which is closely bounded. Now the old school geeks would not feel the desire to fit in in the first place. And their social network is as porous  and wide ranging as anyone else’s. Infact for the old school geeks, would not be called geeks if you met them with another bunch of people, as there are other facets to their personality. If you met them with a gearhead, you ‘d find them to be a gearhead, If you met them with a metalhead you ‘d find them to be a metalhead, but what is common in each of their facets is their firm opinions and their firm principles and beliefs, not the best trends. They are nostalgic people as any, wanting to go back to the good old days of the eight track tapes and pony cars!

Khair, the new geeks are the game controller wielding, fat people who dont do squat and dont know squat. Sure their are a few old school geeks in the new generation as the school is not bound with when you were born. But then they are few in numbers which are dwindling fast.

The old geeks were and are outspoken and they are uncompromising in their belief of the right and wrong. while the new ones are pretty much couch potatoes, or worse wannabes like me.


Square 1

October 11, 2009

The “back to the drawing board” thing is really really frustrating… Since I have to redo my design because of the fair amount of glitches and inadequate response time, now I find my brain wandering around from time to time, and I m prone to stupid mistakes.

and example is:

if ( ADC_index % 2)
{
    Packet [ADC_index + (ADCindex / 2)] = (byte)(dADCTempBuffer & 0x000000FF);
    Packet [ADC_index + (ADC_index / 2) + 1] = (byte)((dTempADCTempBuffer 
                                                        >> 8 ) & 0x0000000F);
}
else
{
    Packet [ADC_index + (ADC_index / 2)] |= (byte)((dADCTempBuffer 
                                                    << 4) & 0x000000F0);
    Packet [ADC_index + (ADC_index / 2) + 1] = (byte)((dADCTempBuffer 
                                                    >> 4) & 0x000000FF);
}

I wrote this eight lines code code in about an hour! An hour!!!! I tried to motivate myself to work by giving myself some extra goals which was implementing the USB protocol in my firmware. But I think I ll need to delve into assembly and probably that wacky gcc inline stuff, which makes it infeasible to do it in the time that I have. So thats going down the drain…. days of soldering connectors to the board and making cables! Argh!!!!

I need to complete this housekeeping stuff quickly so that I can concentrate on the juicy part, but these damn chores dont seem to be ending… they are dragging on and on like a bad rap song!

EDIT: There are days when I m a complete ass, and then there are days when I m this, and I dont know what *this* is called. But I think it is the opposite of ass!

I wanted to post the video for “Leaving you for me” but embedding is disabled on it. Tarja Turunen is the best female rock/metal vocalist ever, but she does nt sound as good as she did with Mightwish… and vice versa Nightwish does nt sound the same without her, now they actually sound very Evanescency, which is good, but not great. I hope they reconcile their differences and get back together and make great music like they used to, no offense to Annette Olsen, she is great and all, but she is not Tarja! Man that voice is angelic… coupled with symphonic elements and fist clenching drums and bass, and beautiful guitar, you ve got a formula for heavenly music.


Sleep deprived and hungry (Read poor)

October 11, 2009

Yes I m poor, I hardly have dough to get stuff that I desperately need, A bicycle for one, and a pillow for another. Plus the matress in my new place is…  well I have no idea whats wrong with it, but I wake up with a backache, so yes I need to replace that too. And I dont want to ask my dad! Man does recession suck or what? Damn finance guys and accountant fucks and MBA dumbasses and Auditor assholes for not doing their FUCKIN job!!!

Khair so what else is new? nothing, I need money, I want money, and I aint got any way to do that, which happens to have a quick payday… Damn NTU finance guys for NOT GIVING ME MY FUCKING CHECK FOR MOVING OUT! And why the fuck cant they pay me monthly for my Labs!!!! Argh!!!!!!

Damn I need a job! And for that I need this recession to dissipate… And I need to finish my Masters ASAP!!!

Shit! I just realized I have no idea how to do any of these!


Uncharted territory

October 9, 2009

Can I play with madnes??? He said you re blind, Too blind to see!

Khair! yes I did the inevitable today… I cooked, and more importantly I made parathas!!! And for a first time effort, they were nt so bad, a bit “crisp” but as I was the one eating them, it did nt matter.

083

085

I guess everything tastes good when you are hungry!