# Simple lowpass frequency response

Okay, so hopefully this isn't too hard or off-topic. Let's say I have a very simple lowpass filter (something that smooths out a signal), and the filter object has a position variable and a cutoff variable (between 0 and 1). In every step, a value is put into the following bit of pseudocode as "input": position = position*(1-c)+input*c, or more mathematically, f(n) = f(n-1)*(1-c)+x[n]*c. The output is the value of "position." Basically, it moves a percentage of the distance between the current position and then input value, stores this value internally, and returns it as output. It's intentionally simplistic, since the project I'm using this for is going to have way too many of these in sequence processing audio in real time.

Given the filter design, how do I construct a function that takes input frequency (where 1 means a sine wave with a wavelength of 2 samples, and .5 means a sine wave with wavelength 4 samples, and 0 is a flat line), and cutoff value (between 1 and 0, as shown above) and outputs the amplitude of the resulting sine wave? Sine wave comes in, sine wave comes out, I just want to be able to figure out how much quieter it is at any input and cutoff frequency combination.

-
I have voted to close, as this is clearly not related to mathematics. –  user126 Jul 21 '10 at 1:10
I think, with a little effort, the question can be written in purely mathematical language. –  AgCl Jul 21 '10 at 8:02
Moreover, my thinking in asking it was that any sort of function or proof to the problem is inherently mathematical. Sure, it's using math to actually do useful things, but I always kinda thought that was the point of math. –  Campadrenalin Jul 21 '10 at 21:55
@Campadrenalin: Could you edit your question to add a little more detail? The current wording makes it hard to understand exactly what you're after. –  Larry Wang Jul 24 '10 at 18:47
I get the feeling I could answer the question if a version was given that removed the semantics, and was stated more like an equation. –  Jonathan Fischoff Jul 25 '10 at 1:15

I don't have enough mojo to comment on Greg's answer.

1. Greg made a silly calculational mistake: The transfer function $A(\omega)$ should be $c/(1-(1-c)e^{-i\omega})$.

2. What you want is the modulus of $A(\omega)$. Note that $\sin \omega n$ is precisely the imaginary part of $e^{i\omega n}$. Because the relation between input and output is linear, the response to $\sin\omega n$ will be the imaginary part of $A(\omega)e^{i\omega n}$. That's going to be a sinusoid with some shifting and the amplitude $|A(\omega)|$. Here is a plot for $c=1/2$.

-
Oops, thanks! I'll update my answer. –  Greg Graviton Jul 30 '10 at 15:23
No problem. Just in case someone wonders how come it still looks different: Greg used a slightly different recurrence from the question, but nothing serious, the modulus of the transfer function is the same. –  rgrig Jul 30 '10 at 15:27
Ah, I should change it so that $f_n$ and $x_n$ are exactly in phase when $c=1$, that's a lot nicer. –  Greg Graviton Jul 30 '10 at 15:42

As I understand it, you are given a sequence $(x_n)_{n\in\mathbb{N}}$ of input values from which you calculate a sequence $f_n$ that is given by the following recurrence relations:

$f_0 = 0$

$f_{n+1} = (1-c)f_n + c\cdot x_{n+1}$

Your question is: given a sine wave $x_n=\sin(\omega n)$, you assert that $f_n$ is also a sine wave and you want to know its amplitude in dependence on the frequency $\omega$.

Answer: It's easier to calculate the frequency response with exponential functions instead of sine waves.

$f_n = Ae^{i\omega n}$

$x_n = e^{i\omega n}$

Since $f_{n+1} = e^{i\omega (n+1)} = e^{i\omega} e^{i\omega n} = e^{i\omega} f_n$, the recurrence relation gives

$e^{i\omega} A e^{i\omega n} = (1-c)Ae^{i\omega n} + c e^{i\omega} e^{i\omega n}$

which implies

$A(\omega) = \frac{c}{1 - (1-c)e^{-i\omega}}$

To calculate the response for sine waves, you can represent the sine function as a linear combination of two exponential functions

$x_n = \sin(\omega n) = \frac1{2i}(e^{i\omega n}-e^{-i\omega n})$

and obtain

$f_n = \frac1{2i}(A(\omega)e^{i\omega n}-\bar A(\omega)e^{-i\omega n}) = |A(\omega)| \sin(\omega n + \phi)$ where $A(\omega) =: |A(\omega)| e^{i\phi}$.

In other words, the complex frequency response $A(\omega)$ encodes both the change in amplitude and the phase shift. I'll leave the exact calculation of $|A(\omega)|$ to you, so that you can get familiar with this method of calculation.

Without exponential functions, you'd have to use the addition and subtraction theorems for sine and cosine. Put differently, exponential functions provide a very convenient formulation of the trigonometric identities.

-
Wow, this is exactly what I needed. It's also way higher math than I've ever taken, since most of my dabbling in calculus was self-taught in order to put together a simple physics engine (a project that failed pathetically anyways, though I got a bit of Math education out of it). This is gonna take me awhile to get my head around, but thank you so much for pointing me in the right direction! –  Campadrenalin Aug 1 '10 at 7:28
My pleasure. :-) And don't forget to mark your favorite answer as accepted. ;-) You mainly need to know complex numbers and Euler's formula to understand this solution. The whole business of splitting a signal into sine waves (or rather $e^{i\omega t}$ functions) is called "Fourier Transform" and commonly taught in any course about signal processing, electrical engineering, engineering in general, and many physics courses. Maybe MIT OpenCourseWare can help, like 18.013A Calculus with Applications –  Greg Graviton Aug 1 '10 at 11:04