Lowpass filter formula question.

POAj Nei's icon

Is anyone willing to explain to me why this equation: y(n) = 1/2 x(n) + 1/2 x(n-1) resorts to a lowpass filter and not an highpass filter or something else?
Im trying to learn dsp but am struggeling on understanding filters.
Maybe you guys know a good source to learn more about filters?


Roman Thilenius's icon


say you have a sound of 4 samples

0.0 , 0.9 , 0.5 , 0.0

and you apply above formula on it - which calculates the mean of each sample to its neighbour - you will end up with something like this:

0.0 , 0.475 , 0.24125 , 0.120625

the more far from eath other two sucessive values are, the more they get approached to each other.

or in other words: the higher a frequency is, the more it gets suppressed.

an input of

0.3 , 0.303 , 0. 306 , 0.309

will not get changed so dramatically like the first example does.

that is why [slide~] or [rampsmooth~] are already a form of "low pass filter".

-110

POAj Nei's icon

Thanks for taking your time to help me. I have two questions. The first one is about the math and the equation itself. My math is awful and when i try to calculate the equation with your samples my output isnt the same as yours. I will try to type out my thought process so you can see where im going wrong.

The equation can be translated as follows: the sample output by the filter, y(n), is defined as the sum of its input sample x(n) multiplied by coefficient a0 and the previous input sample x(n-1) multiplied by the coefficient a1.

lets take your sample list and put it trough the equation.

0.0 , 0.9 , 0.5 , 0.0

0.0 goes in:

0(n) = 1/2 0(n) + 1/2 0(n-1)

0 comes out

0.9 goes in :
0.0 as the previous input sample.

0.45(n) = 1/2 0.9(n) + 1/2 0(n-1) ( 0.9 x 0.5 = 0.45)

0.5 goes in
0.9 as the previous input sample.

0.7(n) = 1/2 0.5(n) + 1/2 0.9(n-1) ( 0.5 x 0.5 = 0.25 ) ( 0.9 x 0.5 = 0.45) 0.25+ 0.45 = 0.7

etc
so my output samples would be:

0 , 0.45 , 0,7

compared to yours which are:

0.0 , 0.475 , 0.24125

so what am i doing wrong here?

my second question is about this thing you said:

or in other words: the higher a frequency is, the more it gets suppressed.
an input of 0.3 , 0.303 , 0. 306 , 0.309

will not get changed so dramatically like the first example doe

Those sample values correspond to the amplitude value of those samples right?

So how do they relate to a higher or lower frequency?

Thanks again for taking your time to help me.




Roman Thilenius's icon


it might be that my values are not correct (i calculated the mean mto both the last and the next sample) - and it is hard to tell in what situation the first value reacts, because sometimes there is a sample number -1 and sometimes not. :)

just look at only the two middle values 0.9 and 0.5. they represent a spectral content which includes more high frequencies than the second example.

yes, sample values represent amplitude. if you are able and willing to learn abstract stuff from books (i am not) you should look up "sampling theorem".

the values in the second example, 0.303, 0.306 ... could be, for example a small part of a 16 Hz sinewave.

as opposed to a sinewave at 5.5 kHz, which would look like that: 0. 0.5 1.0 0.5 0. -0.5 -1. -0.5 0.

now imagine you would have a sound sample which is a mix of both. (a+b)

0.303 , 0.806 , 1.309 , 0.9.01 ...

what would happen if you average all the samples with the next one?

Roman Thilenius's icon


i just tried to make a patch but of course with that formula and scope it can only fail. :)

let me exaggerate the example a bit more:

when you perform that formula to 0.303 , 0.306 , 0.309 you basically only delay the incoming signal for half a sample.

but when you performa that to a sine wave at samplingrate/2, which is -1. 1. -1. 1, you will get 0. 0. 0. 0. -> i.e. silence.

that is maybe not a "correctly explained" but a working example why higher frequencies are affected more by interpolation than lower ones. (sinewaves are all but ideal to test and explain algorithms in real life)

Pedro Santos's icon

Roman, I don't think it's the "last and the next sample".
It's n and n-1, so to calculate the current output sample value we use an average of the current sample and the previous one.

POAj Nei's icon

I forgot to give the full context my bad. Pedro would be correct see the attached image this is where i got the equation from i hope that clears more up.

Would my calculations be correct in this context? i never have gotten algebra in school sadly. I think i might pick up a book on it since i have read people saying linear algebra is very important in dsp.


And about the frequency

the values in the second example, 0.303, 0.306 ... could be, for example a small part of a 16 Hz sinewave.
as opposed to a sinewave at 5.5 kHz, which would look like that: 0. 0.5 1.0 0.5 0. -0.5 -1. -0.5 0.

So the more amplitude values the higher the frequency? Otherwise i find it kind of odd?
dont all Full amplitude sine waves (for example) have the amplitude values 0.303 and 0.306.

Would the bottom part of this text ( see link ) correspond to the more amplitude values the higher the frequency? or has that nothing to do with it.

https://gyazo.com/9acd9202a8f6e5b6a1bd275f3eb47438

Thanks for trying to help.

LSka's icon

I think what Roman meant is: the bigger amplitude variation between samples, the higher the frequency. This is due to sampling.
See this image:
https://upload.wikimedia.org/wikipedia/commons/5/50/Signal_Sampling.png

Sampling happens at a costant pace (44100 Hz, for example), so the "grid" in which you measure amplitude is regular. You can deduce that a lower frequency would produce more "similar" adjacent values, while a higher frequency would likely have higher differences between adjacent samples.
Hence, a low-pass filter will smooth higher "jumps" between samples, resulting in a lower frequency wave.

Roman Thilenius's icon


actually it is the same for analog signals. ;)

POAj Nei dont worry about these numbers beein right or wrong. the priciple is (almost) always the same: digital frequency filters perform operations on a bunch of successive samples/time in order to be able to distinguish between different frequency content.

see the helpfile of slide~ for a similar kind of algorithm. it is birectional and supports sub-sample resolution, but basically it is also just a form of interpolation.

y(n) = y(n-1) + ((x(n) - y(n-1))/slide)

if you feed it symetrically with F/samplingrate you can control the filterfrequency in hertz: *)

#N comlet AUDIO;
#P outlet 47 180 15 0;
#N comlet Hz f;
#P inlet 122 48 15 0;
#N comlet AUDIO;
#P inlet 47 48 15 0;
#P window setfont "Sans Serif" 9.;
#P window linecount 1;
#P newex 157 72 62 196617 dspstate~;
#P newex 122 104 62 196617 !/ 44100.;
#P newex 47 134 56 196617 slide~;
#P connect 2 1 1 1;
#P connect 4 0 1 0;
#P connect 1 0 0 2;
#P connect 1 0 0 1;
#P connect 0 0 5 0;
#P connect 3 0 0 0;
#P window clipboard copycount 6;

*) actually F/samplingrate/2 would be correct, but i had reasons to design my object like that

POAj Nei's icon

Im not that well versed in max yet so the slide thing is kinda overwhelming . If just been trying to learn dsp/ max for 3 weeks now. I will check out the slide part later.
But i have been thinking about the frequency / amplitude values and came to this Is this correct ?:

lets take a sine wave of 2000hz
2000 hz completes 2 cycles every milisecond.
at a sample rate of 44100 u sample u get 44.1 samples every milisecond.
so if you only have to spread out those 44.1 samples across 2 cycles the amplitude values will be closer together to lets say a 10,000hz wave which has 10 cycles every milisecond. Which causes the 44.1 samples to be more spread apart which result in amplitude samples that are further apart because of the regular grid LSKA was talking about?
And if u you put lets say the amplitude samples of a 10,000hz sine wave trough the equation which averages the samples and puts them closer together there by you take away the higher frequencies?

i hope im somewhat right lol
i thought maybe writing stuff down some dots get connected in my brain better.

Pedro Santos's icon

Think about it this way: if every incoming sample is processed in such a way that it only partially affects the output, with the other part being related to previous values (memory), the processing system is having some sort of inertia, reacting slowly to new values and always lagging behind. Depending on this weighting of current vs past samples, this can effectively get rid of certain high frequencies, because the system becomes too slow/heavy to vibrate at those frequencies. Does it make sense?

Max Patch
Copy patch and select New From Clipboard in Max.

Roman Thilenius's icon


i dont want to kick you from your current track, but i ´d like to point out that you dont have to understand the math behind slide~ in order to use it with success. of course you might have good reasons.

but sooner or later everyone hits a border, usually with fft, but also when you need to make up your own filter. my last attempt failed here https://cycling74.com/forums/filter-types-for-biquad it is still not finished. :)

POAj Nei's icon

I really dont know what im not getting here lmao.
is everything i said before wrong ?
i really thought i had it with my last comment.
Thanks for all the effort. Some people are just a lost cause lol.

https://www.youtube.com/watch?v=jYmn3Gwn3oI

i know what and how to implement things inside max if i want a lowpass for example or something. It just really annoys me that i dont get the mathzzz.
Im just gonne pick up a book about it and hope that helps me understand it .
any stuff you guys reccomend ?

what kind of things did you guys create with max / do you guys have jobs in dsp ? or anything related to it.

does this help you in anyway roman :
https://www.native-instruments.com/fileadmin/ni_media/downloads/pdf/VAFilterDesign_1.1.1.pdf

To my understanding this contains alot of filter designs.

Roman Thilenius's icon


if you are a visual person then maybe experimenting with waveforms and display them can be a good way to get a feeling for the relation between amplitude and spectrum? more than reading a book which presumes you already studied math, that is.
synthesizing a bandlimited triangle wave by layering cosines could also be a nice exercise in that direction.

the usual audio filter algorithms, however, are far more complex than just getting the average between two values. and while i understand that you sometimes want to understand things from ground up, my own experience is that you can often use stuff without understanding them on the level of a math professor.

you would be suprised how many well known vendors of multimedia software dont have any idea of how to do things or why they work. often the physicist and the coder is two persons and even big companies buy more code than they write themselves.

(did i just say "NI"? no i didnt say "NI", or did i.)

POAj Nei's icon

Hey so if been thinking about it for a bit and was wondering if my shitty analogy makes some sense/would be right. Say you have someone taking pictures at a rate of 44100 pictures a sec of two people running track.

Person 1 runs at a rate of 1000 steps a sec. (1000hz)
Person 2 runs at a rate of 500 steps a sec. (500hz)

Because of the difference in speed between the two runners. The distance they each travel between pictures is going to be different.

Person 1 picture/sample is going to be further apart compared to the slower guy Person 2 his picture/sample.
That is because the faster guy is going to have traveled further in the time that each picture gets taken ( samplerate ) compared to the slower guy who traveled less distance so his samples are closer together.

So samples further apart = higher frequency / speed
Samples closer together = lower frequency / speed

So if you put the the fast guy trough a filter his samples would be closer together. Meaning he would be slower and have lost his speed( high frequencies).

i hope you can make some sense of what im trying to say. Its probably not really an analogy lol but i tried my best

Roman Thilenius's icon


this reminds me that until now we didnt mention that filter algorithms (or electric curcuits) are not able to fully distinguish between frequencies over and and under 1000 Hz.

filters are not only endlessly steep, most filters are simply having only 3 or 6 db per octave.

one could say that they only have a "tendencial effect".

plus, they are never perfect; you could call that their "quality limit".

the original question, how to find out if an algorithm is a higpass or lowpass, can not really be answered like this.
however, highpass and lowpass are often very similar structured. the most extreme exmaple is that you can take any lowpassfilter and [- ] it from the input to get a form of a highpass version of it. the code will be almost identical then.