Old 01-07-2009, 12:03 PM   #1
cern.th.skei
Human being with feelings
 
cern.th.skei's Avatar
 
Join Date: Sep 2007
Location: trondheim, norway
Posts: 375
Default simplistic filter questions

some simple js code like:

@init
prev = 0;
@sample
out = (spl0 + prev) * 0.5;
prev = spl0;
spl0 = out;

averaging the current sample and the previous one, would result in a kind of lowpass filter, wouldn't it? at frequency (samplerate/2)?

and the same with, for example, averaging the last four samples? (samplerate/4?)

then, my main question: what if i crossfade between those two?
might need some kind of logarithmic, instead of just linear interpolation i guess?

this is for some experimental stuff with haar-wavelets, not meant to be correct (or academic) in any way, but trying to make some interesting sounds and effects with various non-traditional methods...

- ccernn
cern.th.skei is offline   Reply With Quote
Old 01-07-2009, 03:45 PM   #2
dub3000
Human being with feelings
 
dub3000's Avatar
 
Join Date: Mar 2008
Location: Sydney, Australia
Posts: 3,955
Default

that'll probably do what you want but there are much better filter algorithms - if you're looking for info http://www.musicdsp.org/ is a very good place to start.
dub3000 is offline   Reply With Quote
Old 01-07-2009, 06:15 PM   #3
cern.th.skei
Human being with feelings
 
cern.th.skei's Avatar
 
Join Date: Sep 2007
Location: trondheim, norway
Posts: 375
Default

thanks,
but i'm not really looking for the filtering itself.
i'm trying to better understand what the haar wavelet transform does,
(http://en.wikipedia.org/wiki/Haar_wavelet)
and what it can be (ab-)used for.
more analysis/process/resynthesis, than filtering i guess.
most of the stuff i've found is either very academic, or very superficial,
(i have a bunch of url/links if anybody is interested)
so i'm trying to look at it from different perspectives,
from the code, etc.

- ccernn
cern.th.skei is offline   Reply With Quote
Old 01-08-2009, 03:56 PM   #4
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by cern.th.skei View Post
some simple js code like:

@init
prev = 0;
@sample
out = (spl0 + prev) * 0.5;
prev = spl0;
spl0 = out;

averaging the current sample and the previous one, would result in a kind of lowpass filter, wouldn't it? at frequency (samplerate/2)?

and the same with, for example, averaging the last four samples? (samplerate/4?)

then, my main question: what if i crossfade between those two?
might need some kind of logarithmic, instead of just linear interpolation i guess?

this is for some experimental stuff with haar-wavelets, not meant to be correct (or academic) in any way, but trying to make some interesting sounds and effects with various non-traditional methods...

- ccernn
Averaging is similar to lowpassing, but different, see:


If you want to crossfade between averaging and lowpass, well probably best would be to process both methods in parallel and then crossfade (level) those 2's output signals. Same goes for when you want to crossfade between 2 sample and 4 sample averaging.
Though I might have misunderstood you.

EDIT: Tough if you want to increase (dynamically) 2 sample to 4 sample averaging, you could just oversample and then slowly go from 2 over 2.1 to 2.2, etc ... sample averaging.
LOSER is offline   Reply With Quote
Old 01-08-2009, 08:33 PM   #5
cern.th.skei
Human being with feelings
 
cern.th.skei's Avatar
 
Join Date: Sep 2007
Location: trondheim, norway
Posts: 375
Default

i think i might have described my question wrong, or possibly been quite confused myself, heh

let's say we have audio input: ABCDEF
(letters indicate individual samples)
then some pseudo-code
x=(A+B)/2
y=(C+D)/2
z=(E+F)/2
and we reconstruct an audio output like this: xxyyzz

that would be a lowpass filter, wouldn't it? similar to blurring in graphics. by half the sample-rate? what kind of step response (q/bandwidth?)

(and probably some aliasing and stuff too? but, well..)

and subtracting that from the original would leave the (opposite) highpass-filtered audio?

and what if i repeat the same process, on the already averaged buffer?

then, if the above is correct, what happens if i cross-fade between two of these filtered versions?

part of the haar wavelet transform is such recursively averaging, storing the averages (and difference/coefficients) between two and two samples. in the end you are left with a number of 'layers', each being half the size of the previous one, different number of such repeatedly averaging.

i just updated the haar wavelet analyser,
(http://cernthskei.wordpress.com/2009/01/09/haarvester/)
and is thinking about ways to experiment, and do weird stuff with it.

- ccernn

btw: the example code i gave in the first post was acually wrong. it's not a moving average, we're using the unmodified neighbour sample from a buffer, not the previously calculated average.
cern.th.skei is offline   Reply With Quote
Old 01-08-2009, 09:21 PM   #6
dub3000
Human being with feelings
 
dub3000's Avatar
 
Join Date: Mar 2008
Location: Sydney, Australia
Posts: 3,955
Default

Quote:
Originally Posted by cern.th.skei View Post
i think i might have described my question wrong, or possibly been quite confused myself, heh

let's say we have audio input: ABCDEF
(letters indicate individual samples)
then some pseudo-code
x=(A+B)/2
y=(C+D)/2
z=(E+F)/2
and we reconstruct an audio output like this: xxyyzz

that would be a lowpass filter, wouldn't it?
not necessarily. let's say your input stream is 0,0,1,1,1,1 (step input)
then x=0, y=1, z=1, so your output stream would still be:
0,0,1,1,1,1

if you did: (A+B)/2, (B+C)/2, etc, your output stream would be:
0,0.5,1,1,1,? (you incur one sample delay on that filter).

edit: actually, yeah - that initial xxyyzz algorithm isn't time-invariant, i think (?? it's been a long time since uni..), so all sorts of weird stuff would happen depending on the input.

Last edited by dub3000; 01-08-2009 at 09:29 PM. Reason: (it is actually linear, i think, just not time invariant)
dub3000 is offline   Reply With Quote
Old 01-09-2009, 03:58 AM   #7
liteon
Human being with feelings
 
liteon's Avatar
 
Join Date: Apr 2008
Posts: 510
Default

Hi ccern. I think there is some sort of misunderstanding and a combination of methods here. :-)

With your original code:
Code:
@sample
out = (spl0 + out)*0.5;
spl0 = out;
...you are averaging between 2 samples. If you observe freq/magnitude plot you will see similarities between filtering and averaging. LOSER's first graphic explains it quite well.

Take a look at these:

CODE1:
Code:
@init
//X = (A+B)/2;
//Y = (C+D)/2;
//Z = (E+F)/2;
//out is XXYYZZ
@init
i = 0;
step = 12;
@sample
i += 1;
i == step ? (
  out = (out+spl0)/2;
  i = 0;
);
spl0 = out;
This is the 'XXYYZZ' algorithm and its some sort of combination of downsampling + averaging.
We repeat a sample N times (step) and we do a 2 step average between this and the last sample.

CODE2:
Code:
//X = (A+B)/2
//Y = (B+C)/2
//Z = (C+D)/2
//out is Z
@sample
x = (out+spl0)/2;
y = (x+out)/2;
z = (y+out)/2;
out = z;
spl0 = out;
This is a 4 point averaging example (I believe). And it does work like a filter in a way. But in filters more complex functions are used such as: cos, atan. If you look at LOSER's second graph example you will notice the curve.

Here is the simplest of LP filters. An exponent is used:
Code:
slider1:20<20,20000,1>F
@slider
//f - freq (hz), k - coefficient
f = slider1;
k = exp(-2*$pi*f/srate);
@sample
//(1-k) - unity gain
out = (1-k)*spl0+k*out;
spl0 = out;
Edit: I took it from here - http://web-ee.com/tutorials/digital_...ner/index.html

Hope that sums things a little. Please correct me if I've made any mistakes. Good job with the wavelet plugin so far :-)

Last edited by liteon; 01-09-2009 at 04:06 AM.
liteon is offline   Reply With Quote
Old 01-09-2009, 04:46 AM   #8
cern.th.skei
Human being with feelings
 
cern.th.skei's Avatar
 
Join Date: Sep 2007
Location: trondheim, norway
Posts: 375
Default

Quote:
Originally Posted by dub3000 View Post
not necessarily. let's say your input stream is 0,0,1,1,1,1 (step input)
then x=0, y=1, z=1, so your output stream would still be:
0,0,1,1,1,1

if you did: (A+B)/2, (B+C)/2, etc, your output stream would be:
0,0.5,1,1,1,? (you incur one sample delay on that filter).

edit: actually, yeah - that initial xxyyzz algorithm isn't time-invariant, i think (?? it's been a long time since uni..), so all sorts of weird stuff would happen depending on the input.
yeah, probably not a 'correct' filter
perhaps a 'lowpass-like function' would be more suitable.
depending on buffer-sizes vs samplerate and stuff like that.

i've seen this averaging described as lowpass in some of the wavelet papers, and didn't think too much about what kind of filtering, until now that i want to do things with the result..

- ccernn
cern.th.skei is offline   Reply With Quote
Old 01-09-2009, 07:44 AM   #9
cern.th.skei
Human being with feelings
 
cern.th.skei's Avatar
 
Join Date: Sep 2007
Location: trondheim, norway
Posts: 375
Default

Quote:
Originally Posted by liteon View Post
Hi ccern. I think there is some sort of misunderstanding and a combination of methods here. :-)

...

Edit: I took it from here - http://web-ee.com/tutorials/digital_...ner/index.html

Hope that sums things a little. Please correct me if I've made any mistakes. Good job with the wavelet plugin so far :-)

first, thanx for the link and explanations, i think and hope i've learned a bit about filters during these experimentations.

isn't (iir) filters generally made from variations of very, very short delay lines (for example just 1 samples) with feedback. but the coefficients (add/multiply values) calculated from your freq/q/bandwidth and whatnot... (certainly seemed so when experimenting with another js effect i made: frac.delay (find it in my blog)). for example, delay line of one sample, with feedback of 0.5.

i'm not after getting 'correct' filtering out of the haar wavelets, but trying to understand it properly. so i'm trying to see if this averaging can be seen and handled as a special kind of filter.

other wavelet transform function uses other wavelet shapes (daublet, morlet, ..), and would probably have better filtering properties, i guess.
i hope to look more closely at these a later, when i understand things
a bit better.

want to learn the rules, so i know how to break them, hehe

- ccernn

now it's time for some test plugs, prototypes, etc... i'll post things and issues as they appear..
cern.th.skei is offline   Reply With Quote
Old 01-09-2009, 09:06 AM   #10
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by cern.th.skei View Post
i think i might have described my question wrong, or possibly been quite confused myself, heh

let's say we have audio input: ABCDEF
(letters indicate individual samples)
then some pseudo-code
x=(A+B)/2
y=(C+D)/2
z=(E+F)/2
and we reconstruct an audio output like this: xxyyzz

that would be a lowpass filter, wouldn't it? similar to blurring in graphics. by half the sample-rate? what kind of step response (q/bandwidth?)
Normal way of averaging (e.g. image blurring) would be:
input: x1,x2,x3,x4,x5
output: y1,y2,y3,y4,y5
calculation:
y1 = (x0+x1)*0.5; (with x0 = 0)
y2 = (x1+x2)*0.5;
y3 = (x2+x3)*0.5;
y4 = (x3+x4)*0.5;
y5 = (x4+x5)*0.5;

What you described would be a low-fi down and upsampler with a very crude anti-aliasing filter. Because you'd calculate the same as I described above ["lowpassing" == anti-aliasing], but then leaving out all even samples (y2,y4) [downsampling, since you reduce the sample rate]. Now you use point sampling (also known as sample and hold) to upsample again [sort of interpolation the 3 values to 5 again].

As for the response of that filter: roll of will start (reach 3dB that is) at nyquist/2 (samplerate/4), with a steepness that increases and reaches maximum at nyquist (so a nyquist sine wave (values = {-1,1,-1,1,-1,...}, would be totally blocked since (-1+1)/2 = 0). AFAICT anyway.
Hope that helps.
LOSER is offline   Reply With Quote
Old 01-09-2009, 09:33 AM   #11
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by liteon View Post
Hi ccern. I think there is some sort of misunderstanding and a combination of methods here. :-)

With your original code:
Code:
@sample
out = (spl0 + out)*0.5;
spl0 = out;
...you are averaging between 2 samples. If you observe freq/magnitude plot you will see similarities between filtering and averaging. LOSER's first graphic explains it quite well.
That's not his original code from the original post. Your code is a recursive filter (with feedback, using previous outputs) while ccern's was simple averaging (not using any previous OUTPUTS, but previous inputs).
Though I don't know what he is trying to do so maybe using recursion is the correct way.

Quote:
Originally Posted by liteon View Post

CODE2:
Code:
//X = (A+B)/2
//Y = (B+C)/2
//Z = (C+D)/2
//out is Z
@sample
x = (out+spl0)/2;
y = (x+out)/2;
z = (y+out)/2;
out = z;
spl0 = out;
This is a 4 point averaging example (I believe). And it does work like a filter in a way. But in filters more complex functions are used such as: cos, atan. If you look at LOSER's second graph example you will notice the curve.
From my understanding a moving average filter doesn't use recursion (it is a FIR filter). http://en.wikipedia.org/wiki/Moving_average
While yours is some sort of, well, it looks weird to me anyway.
I think a correct 4 sample moving average would look like this:
Code:
desc:4 spl avg
@sample
out = (spl0 + x1 + x2 + x3) * 0.25;
x3 = x2;
x2 = x1;
x1 = spl0;
spl0 = out;
Hope this is correct and good luck with that wavelet.
LOSER is offline   Reply With Quote
Old 01-09-2009, 10:15 AM   #12
liteon
Human being with feelings
 
liteon's Avatar
 
Join Date: Apr 2008
Posts: 510
Default

Quote:
Originally Posted by LOSER View Post
That's not his original code from the original post. Your code is a recursive filter (with feedback, using previous outputs) while ccern's was simple averaging (not using any previous OUTPUTS, but previous inputs).
Though I don't know what he is trying to do so maybe using recursion is the correct way.
I took the liberty to correct the original code after seeing this sentence by ccern:
"btw: the example code i gave in the first post was acually wrong. it's not a moving average, we're using the unmodified neighbour sample from a buffer, not the previously calculated average."

Quote:
From my understanding a moving average filter doesn't use recursion (it is a FIR filter). http://en.wikipedia.org/wiki/Moving_average
While yours is some sort of, well, it looks weird to me anyway.
I think a correct 4 sample moving average would look like this:
Yes my code is for a different filter! Its is for some sort of 3rd order averaging or something. Yours looks correct.
I had no idea that its a FIR filter in the first place...
"A moving average filter averages a number of input samples and produce a single output sample."

@ccern - here is more down-to-earth info on haar wavelets:
http://www.cs.ucf.edu/~mali/haar/

But tbh I can't see much use of the wavelet with audio unless you want to compress the signal. What exactly do you want to use if for? :-)

Last edited by liteon; 01-09-2009 at 10:18 AM.
liteon is offline   Reply With Quote
Old 01-09-2009, 10:59 AM   #13
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by liteon View Post
But tbh I can't see much use of the wavelet with audio unless you want to compress the signal. What exactly do you want to use if for? :-)
I guess he's trying to conquer new grounds and come up with a new kind of effect never heard before. That can be quite interesting.
LOSER is offline   Reply With Quote
Old 01-10-2009, 08:23 AM   #14
cern.th.skei
Human being with feelings
 
cern.th.skei's Avatar
 
Join Date: Sep 2007
Location: trondheim, norway
Posts: 375
Default

Quote:
Originally Posted by LOSER View Post
I guess he's trying to conquer new grounds and come up with a new kind of effect never heard before. That can be quite interesting.
yeah, that's part of the mission :-)

after doing the wavelet transform, we have all the various 'levels' available at the same time, correlated in time. and, we also have the differences (coefficients) from the averages and the original, so that we can restore the original signal.

there's as many levels/stages as there are number of bits in the transform buffer (each level exactly half the size of the previous), so i hope to interpolate (non-linear, i guess) somehow between these. this is where the filtering come into play. but i think i need to understand more what kind of filtering is actually happening with this averaging.

- ccernn

ultimately, i hope we can analyze the audio for frequency or energy content, with timing info, and perhaps modify the coefficients before transforming back (ala fft)
cern.th.skei is offline   Reply With Quote
Old 01-10-2009, 08:45 AM   #15
cern.th.skei
Human being with feelings
 
cern.th.skei's Avatar
 
Join Date: Sep 2007
Location: trondheim, norway
Posts: 375
Default

Quote:
Originally Posted by liteon View Post
@ccern - here is more down-to-earth info on haar wavelets:
http://www.cs.ucf.edu/~mali/haar/

But tbh I can't see much use of the wavelet with audio unless you want to compress the signal. What exactly do you want to use if for? :-)
yeah, i have seen that one.
some other links, more related to audio:

multi resolution audio transforms
http://www.grahamwakefield.net/MAT-F04/201b/

wavelet signal processing of digital audio:
http://eamusic.dartmouth.edu/~corey/...tml/title.html

the wavelet transform as a musical score
http://home.swiftdsl.com.au/~tomrs/music/music.htm

i have tons more links, but most of them are much more abstract and/or academic, or apply to other stuff like engineering, stock market analysis, or, as you mentioned, bitmap compression

- ccernn
cern.th.skei is offline   Reply With Quote
Old 03-07-2009, 03:47 AM   #16
liteon
Human being with feelings
 
liteon's Avatar
 
Join Date: Apr 2008
Posts: 510
Default

Quote:
Originally Posted by LOSER View Post
From my understanding a moving average filter doesn't use recursion (it is a FIR filter). http://en.wikipedia.org/wiki/Moving_average
Quote:
Originally Posted by liteon View Post
I had no idea that its a FIR filter in the first place...
"A moving average filter averages a number of input samples and produce a single output sample."
Apparently there is a recursive implementation, after all:

liteon is offline   Reply With Quote
Old 03-07-2009, 03:27 PM   #17
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by liteon View Post
Apparently there is a recursive implementation, after all:

As far as I can see is this only a computational benefit, because you can write it as:

y[i] = x[i-(M-1)/2 + 0] + x[i-(M-1)/2 + 1] + x[i-(M-1)/2 + 2] + ... + x[i+(M-1)/2];

So yes they can be implemented recursively but must yield a finite impulse response.
LOSER is offline   Reply With Quote
Old 03-09-2009, 08:13 AM   #18
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 15,746
Default

Going back to the original post, it looks an awful lot like a very small (2 point) FIR..
Justin is offline   Reply With Quote
Old 03-09-2009, 09:25 AM   #19
LOSER
Human being with feelings
 
Join Date: May 2006
Posts: 2,373
Default

Quote:
Originally Posted by Justin View Post
Going back to the original post, it looks an awful lot like a very small (2 point) FIR..
Yes, but we were discussing the filter(s) liteon posted.
LOSER is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 02:19 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.