2
$\begingroup$

Can anyone tell what kind of filter is this? I can see it's smoothing/averaging the input signal, but why specifically with [1,2,1] instead of something else? What is so special about this coef=[1,2,1]? Thanks

# Python code
length = 1000
signal = np.random.rand(length)
output = np.zeros(length-2)
coef= np.array([1,2,1])
for i in range(length - 2):
   output[i] = np.sum(signal[i:i+3] * coef / 4)
$\endgroup$
  • 1
    $\begingroup$ You could plot the frequency response and try to find out. $\endgroup$ – Ben Apr 17 '18 at 21:52
  • $\begingroup$ I'll propose the opposite: there's nothing "special" about this filter. You can't ask us "why it's being used" without telling us what for it's being used! $\endgroup$ – Marcus Müller Apr 18 '18 at 6:39
  • $\begingroup$ It's cheap to implement $\endgroup$ – Hilmar Apr 18 '18 at 13:57
  • $\begingroup$ I found this example from a tutorial, which the author use to smooth a noisy ECG signal. The moment I saw this example I was wondering why not using something else, such as a 5-point moving average with coef = [1,1,1,1,1]/5, ... etc $\endgroup$ – Scoodood C Apr 21 '18 at 0:39
  • $\begingroup$ that having coefficients as integers and nothing else... $\endgroup$ – Fat32 Dec 4 '18 at 23:44
5
$\begingroup$

It is a symmetric odd-sized FIR smoothing kernel, belonging to the class of Pascal or binomial filters that somehow sample a Gaussian kernel. Plus, its coefficients are simple dyadic integers, that can be implemented as bit-shifts 1/4 1/2 1/4. The coefficients sum to one, hence it is unit gain at DC.

In simpler word: (one of) the simplest real smoother preserving symmetric feature location. It's efficiency is however limited

$\endgroup$
  • 2
    $\begingroup$ I'd argue that's not even a nicely designed filter under the constraint of being that simple – a normalization could've made it unit-gain in passband, since this is all floating point math anyways. $\endgroup$ – Marcus Müller Apr 18 '18 at 6:41
  • $\begingroup$ Agreed, this filter is not deeigned. Except for the Gaussian likeness. Yet there is a normalization by 4 $\endgroup$ – Laurent Duval Apr 18 '18 at 13:01
  • $\begingroup$ Hi Laurent, do yo mind to further elaborate the significant of Gaussian kernel? Why not simply use 5-points moving average with coef = 1/5 1/5 1/5 1/5 1/5? Besides, what do you mean by "preserving symmetric feature location"? $\endgroup$ – Scoodood C Apr 21 '18 at 0:44
3
$\begingroup$

It is the adjacent average of the adjacent average.

$$ x1[n] = ( x0[n] + x0[n-1] ) / 2 $$

$$ x2[n] = ( x1[n] + x1[n-1] ) / 2 $$

$$ x2[n] = ( ( x0[n] + x0[n-1] ) / 2 + ( x0[n-1] + x0[n-2] ) / 2 ) / 2 $$

$$ x2[n] = ( x0[n] + 2 x0[n-1] + x0[n-2] ) / 4 $$

Where $x0$ is your source signal, $x1$ a simple average, and $x2$ the average of the average.

Now, change it to (1, -2, 1) and you get the discrete analog of the 2nd derivative.

Ced

$\endgroup$
  • $\begingroup$ wow this is an incredible insight... but why would someone do 2-points averaging instead of with a longer samples... such as 5-points or 10-points...? $\endgroup$ – Scoodood C Apr 21 '18 at 0:50
  • $\begingroup$ @ScoodoodC, I should have looked at your code a little closer. The minuses would have been plusses in my answer. If they were centered you could also say the result is the average of the sample with the average of its two neighbors. It is actually three point smoothing, which may be adequate. Longer intervals may be to "smeared", depends on the application. I have actually used [[1,2,1],[2,4,2],[1,2,1]] centered 2D smoothing on webcam images to improve the quality. Being Pascal triangle coefficients they are rough approximations of a Gaussian distribution. $\endgroup$ – Cedron Dawg Apr 21 '18 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.