How to smooth Array values

Hi all,

I am wondering, what is the best approach to smooth out values in array. One can say to low-pass filter them.

I have made a function which does the thing: x(n) = ( x(n)+x(n+1) ) / 2, but after some recursion, there is a quite shift of data.

Maybe there exists some method to acomplish this.
Does anybody have a better solution?

thanks

You could implement a basic one-pole filter as follows (I just took this from the OnePole help file). Adjust the coefficient closer to 1 for more feedback==more smoothing.

(
	var coef = 0.5, last = 0;
	
n = Signal.periodicPLNoise(1024); 

	m = n.collect{|item, i| 
	var val = ((1 - abs(coef)) * item) + (coef * last);
	last = val;
};

[n, m].plot
	
)

Sam

1 Like

Yes! thank you Sam. I should have look into doc more precisely.

I am attaching a quick extension to Collection class, maybe somebody is interested in.


+ Collection {
	smooth { |coef=0.5|

    this.every({ |item i| item.isNumber}).if({
			var last = 0;
			var r = this.collect{|item, i|
				var val = ((1 - abs(coef)) * item) + (coef * last);
				last = val;
			};
			^r;
		},{
			"Not a countable collection".error
		});
	}
}
1 Like