Question

I of course know that filters filter certain kind of noise. I just want to understand how filters work in matlab. Consider the code:

[x,y] = pol2cart(0:0.01:2*pi, 1);
nx = filter(.2*(1-.3), [1 -.3], cumsum(randn(size(x))));
x=x+nx;

Some deformation noise is added to x-coordinate. How does it work? I also do not clearly understand the interpretation of parameters. The matlab documentation was not that helpful either.

How do filters work? and How do I interpret the different parameters?

Was it helpful?

Solution

The documentation states

FILTER One-dimensional digital filter. Y = FILTER(B,A,X) filters the data in vector X with the filter described by vectors A and B to create the filtered data Y. The filter is a "Direct Form II Transposed" implementation of the standard difference equation:

a(1)*y(n) = b(1)*x(n) + b(2)*x(n-1) + ... + b(nb+1)*x(n-nb)
                      - a(2)*y(n-1) - ... - a(na+1)*y(n-na)

Perhaps the simplest example of a filter is a running average. In that case, because the signal in your data is not random (not uncorrelated) but the noise IS random (at least white noise is) then averaging has the effect of attenuating noise in favor of signal.

In your case the filter is used as follows:

y = filter(scale*(1-f), [1 -f], cumsum(randn(size(t))));

Here y(n) is the noise (deformation) you will add to your signal. In this case b = scale*(1-f), a = [1 -f], so we can write

y(n) = scale*(1-f)*x(n) + f*y(n-1)

A few things are worth noting:

(1) cumsum(randn(size(t))) represents a random walk and is equal to the filter input x(n) in the equation above. This is plotted in red in the figure below. Adding a random walk rather than just noise (generated say with randn) leads to a deformation rather than, well, just noise.

(2) scale affects only the first term in the sum and as the name implies it is most important setting the overall magnitude of the deformation of the original signal.

(3) f favors one term over the other. When f is large the contribution of the first term decreases, that of the second increases. The result of decreasing f is to increase the "choppiness" of the data. Therefore you can think of f as a smoothing parameter.

Here are the results of some experiments (red = random walk, blue = sine (signal), green = signal+filtered random walk):

enter image description here

Another way to undertand the effect of the filter is to consider its frequency response, computed with freqz, as follows:

[h,w]=freqz(b,a,10000);

From the following plots of the frequency response (frequency in the abscissa, amplitude scaling factor in the ordinate) for different scaling and smoothing parameters, the effect of the parameters on the behavior of the lowpass filters becomes clearer:

enter image description here

Thanks to Peter for contributing the original expression for the filter.

OTHER TIPS

I agree that the description in the filter help file is not that easy to read.

One important thing to realize is that he filter is not what I commonly think of as a filter in daily life. It is not typically used to remove some (bad) things from the data.

Instead, it loops over the data, and performs an operation on each element and its predecessors.

For what its worth, if you click on 'Algorithms' in the documentation file, you can see how it actually works. It contains a graph and a written out transfer function.


To conclude: If you just want to add noise there is probably an easier way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top