Dear (apparently) James Newtons Massmind, Interesting name. Spehro Pefhany has done a good job answering exactly the question you asked ( http://techref.massmind.org/techref/postbot.asp?by=time&id=piclist\2005\10\15\214600a&tgt=post ). Pefhany used the technique known as "fixed-point math", which is useful whenever you want to do math on fractions, but you only have integer math available. There's been a lot written about fixed-point math, most of it making it *sound* far more complicated than it really is. http://en.wikipedia.org/wiki/Fixed-point Microchip AN617 Fixed Point http://microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en010962 (Is there an article on Massmind.org dedicated to explaining fixed point in general? Massmind has lots of PIC code for *doing* fixed point math, but they all seem to assume you already know all about fixed point math and things like the 7:8 notation.) I am sure you are sincere and well-meaning, but whenever I hear that someone is planning to teach students something technical, I have 2 simultaneous reactions: * That's wonderful. Better-educated students are nearly always a good thing. * I hope this teacher teaches how things really work, rather than one of the popular "Science Myths"[*] that confuse people and take forever to unlearn. > The idea is that the student can put in the actual > values of the R and the C in a basic RC type low pass filter and the code > will load the literal values appropriate for the digital simulation of that > filter. That is certainly one way to do it. I failed to see how that is any improvement on allowing students to put in actual resistors and capacitors into a basic RC low pass filter, then look at the output on an o'scope, until you clarified: >> This is about educating people who understand analog systems >> and showing them that the same basic thing >> can be done in a digital system. OK then, now it makes more sense -- they already how to make a low pass filter from actual resistors and capacitors, and you're trying to teach them digital filtering based on their previous experience (rather than starting from scratch). > I am working from this text: > > Let us take for example the Simple RC (resistor-capacitor) low pass filter. > This circuit passes frequencies in the input voltage that are lower than > some critical frequency (called the cutoff frequency) determined by the > resistor and capacitor values, while severely attenuating any higher > frequencies. Good so far. > The output voltage Vout, then, is simply a selectively reduced version of > Vin (input voltage). The resistor drops the output voltage, as does the > voltage that goes into charging C. At first, Vout = a * Vin (where the > constant a is the amount of attenuation caused by R and C). It can be shown > that a=1/RC (where R is in ohms and C is in farads). To quote Bob the Angry Flower, No! Wrong! Totally wrong! Where'd you learn this? -- http://www.angryflower.com/bobsqu.gif It's not even dimensionally consistent. For the very special case where we have been applying a sine wave to the input for a long time, Vout(t) = a * Vin(t-p) (where the constant a is the amount of attenuation, p is the phase lag), but that constant a depends on R, C, *and* the frequency f of the sine wave. For any other input (square waves, triangle waves, white noise, etc.), the output is *not* a scaled version of the input, but a distorted ("rounded-off") version of the input. When we apply a slow square wave to the input (one far longer than 10 time constants), the output is *not* an attenuated square wave. When the input switches from Vhi to 0, I see that the output starts at Vout(0) = Vhi . and decays like Vout(t) = Vhi * exp( -t / (R*C) ). until the output is practically 0. When the input switches from 0 to Vi, the output starts at Vout(0) = 0 and rises like Vout(t) = Vhi *( 1 - exp( -t / (R*C) ) ). until the output is practically the same as Vhi. (There are very small deviations from this theory caused by the non-ideal parasitic ESR of the capacitor, but the ESR (and the deviations which depend on it) cannot be calculated from R and C.) > The voltage change with time across a capacitor is an exponential function. > If the voltage at time zero is V, then the voltage at time t is given by > Ve^(-at) (a is the Same constant as above). > I need to find e^(-at) given the value of at. I think someone has unnecessarily confused you. While the output voltage (while the input is zero) may decay like Vout(t) = Vhi * exp( -t / (R*C) ), that is *because* * electrons are leaking out of one side of the capacitor, through the resistor, and back to the other side of the capacitor. * When there is a higher voltage across the resistor, the electrons flow faster. * When there is a lower voltage across the resistor, the electrons flow faster. No matter what the input voltage does, if use very small time steps h, and keep track of the number of the number of excess electrons Q across the capacitor, we can approximate the situation as Vout(t) = Q(t)/C ; output voltage i(t) = (Vin(t) - Vout(t) ) / R ; current through resistor into capacitor Q(t+h) = Q(t) + h*i(t) ; next count is the old count plus however many new electrons came in (or out) over that time increment. In C, one could write this as something like const conductance = 1/R; const reactance = 1/C; for(;;){ Vin = get_next_input_voltage(); current = ( Vin - Vout )*conductance; time = time + h; charge = charge + h*current; Vout = charge * reactance; cout << "Output voltage at time" << time << "was" << Vout; } Did you notice that there are no exponentials in this code? Some people would "optimize" the inner calculations of that loop into: for(;;){ Vout = Vout + FF*( Vin - Vout ) ; time = time + h; cout << "Output voltage at time" << time << "was" << Vout; } which is astonishingly similar to the very first response you got (from olin): FILT <-- FILT + FF * (NEW - FILT) . Have I given you enough information to be able to calculate FF? Forgive me for rambling so long. [*] "Science Myths" * http://www.ems.psu.edu/~fraser/BadScience.html * http://www.eskimo.com/~billb/miscon/miscon4.html * http://www.amasci.com/miscon/miscon.html -- David Cary -- http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist