Friday, February 8, 2013

Digital Filter University - Course II: The Math

As you might have already noticed, I love everything about digital filters. And I love math too, especially filter math. It's a great myth that engineers are good at math. Most of us aren't. Even the math we studied we forget. So, if you are an engineer, don't feel bad about looking this over as a refresher. If you're not an engineer or an otherwise mathy person, let me see if I can give you adequate background for use in digital filters. If there is ever anything you cannot follow, let me know and I'll help. Your questions will help others, so fire away.





I once took a math class where the professor said he would write on the board all the things we needed to know from our previous math courses. He wrote a y on the board, and then stood in front of what he was writing so we couldn't see. I imagined that he was writing something like y = mx+b, which is the equation for a line, and an important element of math. But, when he finished and moved aside, he had written "everything!" 

Well, never fear. I would never do such a thing to you, would I? Actually, there are only a few mathematical concepts needed for digital filtering. Much less than you would expect. I'll show you:





Let's start by talking about the real numbers. You already know all this; but in case you are a little rusty (no pun) on the details, let's review. Numbers can be plotted on a number line or on what we can an x-y axis, as shown at right below. When the numbers are in (x, y) pairs, we find the location on the x-axis, and then move up (or down, if the y value is negative) to the location on the y-axis. Make sure you now how to plot the points plotted on the axis below.

It's also useful to recall a few properties of real numbers. A number multiplied by itself is the number squared. However, if we take the square root of a number, we are looking for a number that, when squared, will equal the original number. Remember that a negative number times a negative number gives a positive result. So the square root of a number can be either positive or negative and, when squared, the original number will return like magic.

If we multiply a number by itself three times, we get the cube. The cube root, however, has to have the same sign as the original number, because three negatives multiplied return a negative. Can you follow all the stuff on this next chart? 











I suppose "imaginary numbers" is a weird name since these numbers are actually "real" in the sense that they do exist. But we already used up the name real on the previous type of numbers, so these numbers got stuck with a name that makes you think you don't need to worry about them, as in the case of the imaginary monsters who used to live in your closet or under your bed. Not so. Our imaginary friends of the numerical variety actually make digital filters tick. So quit drowsing off and pay a little attention for once, would ya!?

We, appropriately, in my view, use the letter i to indicate imaginary numbers. Some'a you electrical engineers may also be used to using j. That practice was started to avoid confusion with the i that's used for current. There never would have been such a problem had something reasonable been used for current in the first place... But, though I do have a small stack of electrical engineering degrees, I'm a digital filter guy. I don't deal much with current except when I go fishing. And I haven't been fishing in at least a decade. So, forgive me if I use i. I prefer it.

Now, i is actually defined as the square root of negative 1. You may have heard that the square root of a negative number is not defined or doesn't exist. Just one of many lies you've been told throughout your life. Get over it.

Given that definition for i, it's clear that it's square has to be -1. It's cube is actually -i since the cube is the square times i. Makes sense, right?

Look over the notes below and make sure you follow. Ask if you have a question.











Now, whenever an imaginary number gets together with a real number, the result is a complex number. This is another unfortunate name, since complex numbers really aren't all that complex. It might have been nice had they been called composite numbers or something instead.

One key thing to remember about complex numbers is that the real and imaginary parts must be kept separate. Notice below how we have a number 2 + 3i. That is the simplest form for that number.

This is what happens when a real number is multiplied by an imaginary number: 4 x 5i = 20i. See what happened there? 

What about when two imaginary numbers are multiplied? 4i x 5i = -20 because the two is combine to make i squared, which is -1. Slick ain't it?

Look over the chart below to make sure you can follow both the plotting and the arithmetic. Recall the FOIL rule from algebra: when we multiply any pair of numbers, like x + y, or 2 + 3i with another pair, we multiply the First elements with each other, then the Outside elements, then the Inside elements, and then the Last elements. Work through to make sure you get the same result.









I'll wait while you work out the examples below. Let me know when you've finished.











How'd that go? Did you get them right? Here's what I got:







Now that we're experts on that, it's time to move on to a new topic: polynomials. If you are familiar with the filter song, you know that polynomials are highly revered in the filter world.

Although polynomials can be written in terms of other variables, the ones below are polynomials in x. In the first one, we have x, or x^1 if you prefer. So this is a first-order polynomial. Notice how the order is the highest power of x in the polynomial. 








The roots of polynomials are those values of x that make the polynomial go to 0. The first order case is simple, as seen below. Check it, to make sure it's correct.








Some second order polynomials are easy to factor, as you see in the next chart. If x + 3 is a factor of the polynomial, and we plug in -3 for x, that factor goes to 0 right? And, of course, if we multiply that 0 by the other factor, the result is still 0. So we see that we can simply get the roots from the factors.




Try your hand at factoring these.








Here's what I got for the first three. But the last one doesn't factor simply.






There's nothing that says polynomials have to have simple, integer roots. In fact, they don't even have to be real. So, when something doesn't factor simply, or even if it does, we can use the quadratic equation to factor 2nd order polynomials.






Now, follow along as I plug the roots back in to make sure they're correct.







As Peter Gabriel would say, "don't give up." One more slide to go (for this example, anyway).









This next one turns out to be real, but still irrational. That is, it's not a simple integer number, but it has no imaginary part. Follow along.









There is something magic about 2nd order polynomials. As we've said, their roots are not always complex. In fact, the only way they can be complex is if they occur in what we call complex conjugate pairs. This is explained below. Awesome, ain't it?










Unfortunately, there is no such simple formula for factoring higher order polynomials. But, at least there are dozens of ways - websites, calculators, computer software - to get the job done. Note that we usually need decent precision for filter work, and some websites that I've tried didn't have it. Look for something similar to the number of decimal places we have below.








Some people get concerned when they look at filter formulas because they notice a tangent or a cosine here and there. Fortunately for both you and for me, we don't have to know (or remember) all those trigonometric identities. Don't get me wrong, I like trig. But that doesn't mean I remember them either.

Generally speaking, for filter work, we really only need to evaluate the trigonometric functions. When I was a kid this meant looking in a book for a table of values, and then interpolating the table to get the answer. But don't worry, I'm not a kid anymore. As with factoring higher order polynomials, evaluating trigonometric functions is now the domain of software. Just plug in the input (argument), and get the output. That's all there is to it. These days a $10 calculator will even do it, as will the one on your computer (if you select scientific calculator mode).











The inverse of the tangent function is called the inverse tangent or the arctangent. Similarly with the other trig functions. Exactly what this means is shown below.

I once developed an algorithm that was full of trigonometric functions. In an effort to prove that I am not a unidimensional human being, I named it Death Comes for the Arctangent (after a similarly named Willa Cather novel). But now I really am off on a tangent.












Let's refresh our memories about degrees and radians below. Got it?













Now, of course we can plot the points on an axis, as we have discussed previously. But sometimes we need to know the angle from the positive x-axis to the line segement between the origin and these points, as shown in the chart below?









As you may have guessed, the answer involves the arctangent. But, did you ever hear about the quadrants of two-dimensional plots? We call the upper right one quadrant I, and then number them counter clockwise from there, I - IV. Unfortunately the regular arctangent doesn't understand about quadrants. It gives you part of the information you need, but then you sometimes have to compensate it to get the quadrant correct, as shown below.










An alternative to this is something called the two input arctangent or atan2. (Some math software abbreviates arctangent as atan, and the two input version as atan2.) This version does know about quadrants and can, therefore, give you the angle you need without compensation.














Now, this might seem like a big shift from where we were, but the final note for this unit is about polar coordinates. When we plot x and y values, we are using rectangular or cartesian coordinates. (Named for Rene Descartes who thought, therefore he was.)

But we can also specify a point ono the grid by what we call polar coordinates, which tell us the angle, as we have already discussed, and the magnitude, or length of the line segement from the origin. Note that either set of coordinates can describe the same point. And, we can convert back and forth between them, as described below.

The exponetial of itheta might look a little unusual. But just think of this notation as describing the angle to the point. (Or study it more. It's actually quite fascinating.)














Now, if you ever encounter a polar bear, you can quicky convert him into a cartesian bear. Then, if you don't think about him, maybe he'll cease to exist? If you try it, let me know how it comes out.

We'll introduce a few more mathematical concepts as needed in the course of the remaining lectures. But generally speaking, if you can get through this, you've got the foundation you need to deal with digital filters. If you can't get through this, don't give up. Let me know and I will help you. You can do it, for sure.

No comments:

Post a Comment