# Frequency and phase response of various filters

I have been set the task, by my Computing teacher, to write a program to simulate a variety of fixed electronic filters (the circuits are fixed.) The goal is to plot amplitude and phase against frequency.

The problem is, a lot of the math involves complex numbers. For example simulating even a simple RC filter requires an understanding of complex impedance - something I don't understand (yet - still in college electronics education.)

Are there any "simple" formulas which can compute the response of a filter accurately?

Is there a good web resource for how to understand this?

A list of formulas for frequency vs phase and amplitude?

Ideally, I could extend it to all types RC as well as LC filters and other types like Butterworth, but that might get really complicated.

-
The complex number formulas are the simple ones. The trigonometric forms are more complex and harder to manipulate! – Kevin Vermeer Jun 29 '11 at 12:44
– Kellenjb Jun 29 '11 at 13:00
@Thomas O learning what the complex numbers mean will be a huge benefit to your overall understanding of the relationship of time domain, frequency domain, and phase. If you are being asked to write a program to do this I would assume it is expected for you to understand this material and not just find the easy way out. – Kellenjb Jun 29 '11 at 13:32
What programming language and what library restrictions are there? You could put together a program in python with scipy handling all the complex math and matplotlib to make pretty plots. Shouldn't take more than a couple days and you don't really need to know much of anything about complex numbers, just solve for Vout using s-domain models for the parts and use scipy to do the complex math and spit out a vector for matplotlib to display as the result. – Mark Jun 29 '11 at 20:13
@Mark Python would be great if I can get it to work as I know it inside-out. But the college only has Delphi installed by default, so I'd need to create some kind of Python exe. – Thomas O Jun 29 '11 at 22:04

The normal way to solve these problems is as stevenvh described. LaPlace transforms and complex analysis are used because such problems are easier to solve by humans in that space.

However, this was all before there were computers with essentially unlimited and free compute cycles. It is possible to get meaningful answers from purely time domain analysis. It will take the computer way more cycles, but so what? Nowadays what you want to conserve are your time and effort.

Of course everything can be solved in the time domain somehow. After all, the real signals exist there. Imaginary components are, well, imaginary. They are useful mathematical aids in understanding and manipulating signals where both the frequency and phase are relevant. Using such mathematical abstractions greatly reduces the computation, but they are not neccessary.

Programs like Spice do pretty much everything in the time domain, I think. They look at the voltages and currents at any one time, then use that to decide what the voltages and currents are a little tiny step further in time. The computer does this a few million times in a row and now you've got real signals as a function of time. The tricky part here is to decide how long those computational time steps are. Too long, and the computation will miss details and get the wrong answer. Too short, and intermediate increments will be so small they will be lost when added to the existing values. This latter part depends on the number precision used inside the computer. For example, with normal IEEE 32 bit floating point, if increments are 65000 times smaller than the thing they are adding to, the increment is effectively limited to 8 bits resolution. To get around this, the simple answer is to use the highest precision floating point the computer has. This is usually called something like "double precision" in most languages. That may take a little more time in the computer to manipulate the wider numbers, but again that's pretty much irrelevant.

If you try this path, you need to model the various resistors, capacitors, and inductors in incremental form inside the computer. Or you can model the whole filter incrementally in some cases. For example, the algorithm for a single pole RC low pass filter is:

FILT <-- FILT + FF(NEW - FILT)

This is very common inside microcontrollers where it is applied to real world readings with some noise on them, for example.

Once you have code that models the filter of interest, you create a higher level that successively sends it sine waves of different frequencies. After waiting to make sure the filter has reached steady state for each frequency, you look at the amplitude and phase of a output cycle relative to the input cycle.

To be clear, I actually agree with stevenvh that using complex impedance is the best way to analyze such things. My main intent was to point out that you don't need complex math to solve this problem, or any problem in electronics for that matter, only that it can make it much simpler. I also wanted to point out that in today's world trying to conserve computation is wrong headed. That may have applied 20 years ago, or perhaps even 10 years ago, but not today.

Another point is that you need to learn complex math. You've got no business trying to be a electrical engineer, or pretty much any type of engineer, without it. So instead of finding ways to avoid complex analysis, dig in and learn it. This is a important tool every EE must have at his disposal.

-
@Thomas - Olin is absolutely right: you've got to learn complex math. (Also comes handy if you meet a girl engineer; female engineers can be very complex! ;-)) – stevenvh Jun 29 '11 at 15:14
@stevenvh that joke was so bad I just had to laugh. – Kellenjb Jun 29 '11 at 15:29
@Kellenjb - it can't always be caviar! – stevenvh Jun 29 '11 at 15:38
@stevenh At least we're not purely imaginary... – Aphaea Jun 29 '11 at 15:41
@stevenvh @Kortuk: Is this really about breaking a 9-paragraph post into titled sections? Seems a little over the top to me. The entire posts fits on my screen at one time. – Olin Lathrop Jun 29 '11 at 20:03

This is often done in the s-domain, via the Laplace transform. Every engineering formulas compendium has a list of time-domain functions mapped to their s-domain equivalent. (In DSP this will be done in the Z-domain, the Z-transform being the discrete (sampled) form of the continuous Laplace.)

Transforms like the the Laplace transform are not uncommon in (engineering) mathematics. You convert (Laplace) a problem to a simpler one (s-domain), solve it there and convert it back (inverse Laplace). A common example are logarithms, which were used by every engineer for hundreds of years until the digital computer. A multiplication becomes a sum if you take the logarithm, and that's easier to solve than a multiplication. Same for division and exponentiation. Taking the logarithm requires some effort, so it isn't worthwhile for simple products. But since the discovery of logarithms by Napier in the 17th century "computers" (people who compute) have compiled logarithm tables, and the slide rule was the engineer's computer until electronic calculators arrived. If you have some experience with a slide rule you can do some calculations almost as fast as with a calculator, which would be more difficult without the transform.
Disclaimer: no, I'm not that old that I've worked with slide rules. My career in high school started with one of the first programmable calculators, the TI-57.

If you're uncomfortable with that, you can also remain in the time-domain. You don't give examples of the circuits you have to analyze, I presume RLC circuits. You find the transfer function just like you would for a linear circuit which just uses resistors, by calculating parallel and series equivalents, using tools like Kirchhoff's laws. Of course you'll have to carry $j\omega$ around all the time, but your final result should be a complex number with a real and an imaginary term. Convert this to polar notation using Euler's identity $e^{j\omega t}=cos(\omega t) + j sin(\omega t)$ and it gives you magnitude and phase.
You could use a spreadsheet program like Calc or Excel to plot the function by varying the frequency $\omega$ over the desired spectrum.

edit
Olin mentions SPICE, and that's of course a great way to get the graphs, but you'll have a hard time convincing your teacher that you did anything yourself, less show that you understand it.

-
RLC circuits are linear in the technical sense. That's why the Laplace transform works! – Aphaea Jun 29 '11 at 15:15
@Aphaea - Yeah, that's the problem with words like "linear", they can mean anything :-(. What it means here and to me is that resistors have a fixed, and linear!, relationship between voltage and current... except for the non-linear resistors, that is :-/ – stevenvh Jun 29 '11 at 16:52
Linear and time invariant, none of my favorite problems are. But as long as they stay deterministic I can live with it. – Kortuk Jun 29 '11 at 17:55
@Kortuk: and causal! – Federico Russo Jun 29 '11 at 18:25
kortuk has got it with LTI - Linear time invariant - that is that all the values remain the same with respect to time! so a 1 ohm resistor is a 1 ohm resistor, today, tomorrow and next year. Of course long term drift, temperature coefficients and initial tolerance play a part to make it "non linear" but for circuit analysis all that is needed is a monte carlo analysis to work out the extremes of fc, phase respone and stop band (and so on) but I am getting of topic - LTI is a perquisite for performing circuit analysis of passive components – smashtastic Jun 29 '11 at 18:44

It might help you to think of linear circuits as essentially solving differential equations in hardware. Since you want to be in frequency space anyway for the Bode plots, (amplitude and phase vs. time) it really does make the most sense use the complex impedances to find the transfer function. With practice, you can make a Bode plot by hand immediately from the poles and zeros, with some amount of fiddling at the corner frequencies. MATLAB also has built in functions to generate bode plots, pole-zero maps, etc. from arbitrary transfer functions.

However, if you don't want to use complex impedances, you're left with a set of differential equations. Which you can solve using an ODE solver, but you'll still need to transform to the frequency domain. So you really might as well get comfortable with Laplace and complex impedance.

-

The best web resource is Wofram Alpha. For example plots can be calculated online, using seardch page like this http://www.wolframalpha.com/input/?i=Butterworth+filter

-
 @RS - Alpha is good, but it's like what I said about SPICE: it doesn't help you to convince the teacher that you did actually do any work, let alone understand it. – stevenvh Jun 29 '11 at 16:49