Apologies in advance for asking something so basic, this is not my field at all so I'm a bit lost where else to find this info. I'm trying to understand this piece of code that detects sudden drops in a signal, it goes something like this:

filter = [ones(1,10)*-0.05 ones(1,10)*0.05];

filterResponse = conv(exampleSignal, filter, "same");

Can anyone tell me what type of filter this is and why does it centers the signal at 0? Just anything to get me started onto more googling would be really helpful. Thanks in advance.

signal and filter response example


This is a moving difference filter which acts as a discrete time derivative and is less sensitive to high frequency noise. When all the samples in the waveform are the same (DC) the last ten samples will cancel out the first 10 samples for DC cancellation but when a change occurs within a 10 sample interval compared to 10 samples prior the difference will be maximized.

This filter is identical to a 10 sample moving average followed by a difference over 10 samples and provides a filtered estimate of the time derivative of the waveform as shown in the diagram below ($z^{-10}$ in the diagram refers to a 10 sample delay and MAF refers to a "Moving Average Filter" which is the sum of the prior 10 samples):

equivalent filter

This equivalence form helps provide a more intuitive understanding of the filter with further details provided below:

The basic form of a differencing filter is $y[n] = x[n]-x[n-1]$, with coefficients given as [1 -1] and is a discrete time approximation of a derivative (as the Forward Euler mapping of $s$ which is the Laplace Transform of a time derivative):

differencing filter

Also consider how the time derivative is $\lim_{T \rightarrow 0}\frac{x(t+T)-x(T)}{T}$ in comparison to the form given above.

This filter has a simple high pass frequency response with the magnitude response shown below, given as the response from DC to the sampling rate where the frequency axis has been normalized by the sampling rate. Like the derivative which has a frequency response that increases as a function of f (given the Laplace transform of a time derivative is $s$), this filter is most sensitive to the highest frequency components in the signal (at the Nyquist frequency at half the sampling rate given by the normalized frequency of $0.5$):

freq response

If we insert 9 zeros to upsample the filter by 10, to get coefficients given as [1 0 0 0 0 0 0 0 0 0 -1], (which is the differencing stage in the first diagram provided above) such a zero insert creates periodicity in the frequency response, repeating the same frequency response ten times over the frequency range from DC to the sampling rate as shown in the magnitude response below (creating the classic "comb filter"):

interpolated response

The final response of the OP's filter is found from the convolution (or cascade) of the interpolated differencing filter with a 10 sample moving average filter (which removes the high frequency sensitivity as a low pass filter).
A moving average filter over 10 samples with coefficients given as [ones(1,10)] would have a Dirichlet response which is an aliased Sinc function. This is clear when we consider the coefficients of the filter is the impulse response of the filter, and the Fourier Transform of the impulse response is the frequency response. The coefficients are a sampled pulse, and the Fourier Transform of a pulse is a Sinc function. The response of a filter with coefficients [ones(1,10)] is shown below:

moving average

And the final form of the filter as the convolution of the coefficients of the interpolated differencing filter and the 10 sample moving average filter is shown with the response below (omitting the scaling by $0.05$). Convolution in time is multiplication in frequency, and we see that the resulting response is the product of the responses given above:

final frequency response

  • 1
    $\begingroup$ Thank you very much for this detailed response! I understand a lot better now what the filter is doing. Thanks for taking the time $\endgroup$
    – BlueBird
    May 13 at 19:33

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.