# Matlab 'filter' returns negative values (when filtering a positive signal)

I just filtered a only positive signal with the following filter which is a bandpass filter with the following characteristics:

%the filter
numer = [0.2 0.1 0 -0.1 -0.2];
denom = [1 -0.94];


I filter a only positive signal with this filter by applying:

y = filter(numer, denom, signal);


The question is: Why does a filter with only positive inputs yield negative values? What can I do about it? (Can't possibly plot the result of the filtering on a dB scale because there is no dB of negative values)

Sorry for the beginner question I searched the web extensively before posting here (but couldn't find an answer).

• @MarcusMüller: nope. The impulse response is all positive. Commented Jun 8, 2018 at 12:24

Looking at your plots, your filter has attenuation at f=0. In effect, you are subtracting a constant from your data, shifting it so that some of it negative.

Subtracting the mean from a set of numbers will centralize it on zero.

– Alon
Commented Jun 8, 2018 at 13:44
• Use low pass instead of band pass is one solution. Another is to find the minimum values of the filtered data, which will be negative and subtract ( subtract a negative ) that constant from the data. Which is better depends on your application
– user28715
Commented Jun 8, 2018 at 14:07
• In terms of where you can learn more, books are never narrowly tailed to a specific understanding, they have a lot of other things in them and the path is never direct. This place is a good place to ask questions but not a perfect place. An in the flesh teacher or consultant is the best but that isn’t free. Experiment Experiment Experiment...
– user28715
Commented Jun 8, 2018 at 14:19
• Thanks for your input Mr. Pawlukiewicz ! Do you mind explaining why it is valid to just add a offset to the filtered data? I cannot imagine how the "most negative" value of the filtered data is the correct offset to add...
– Alon
Commented Jun 8, 2018 at 14:29
• Subtracting the minimum value (which is negative) is the smallest shift that makes all the numbers positive
– user28715
Commented Jun 8, 2018 at 16:38
1. Your impulse response is all positive. You only have one negative coefficients and that's in the denominator, so it flips to positive in the difference equation
2. If you filter a positive signal with a positive impulse response, you will get a positive result. You can verify this bey evaluating $min(y)$, that should be equal or greater than zero
3. If you do simply type $plot(y)$, you should see an all positive signal
4. If you plot anything in dB you will also see negative values. That simply means the original value is smaller than 1.
5. It rarely makes sense to plot a time domain signal in dB. Typically you would do a spectral analysis first

EDIT:

The filter you show in the graph and the filter you show in the code are not the same. The code is a lowpass filter, the graph is a bandpass. The bandpass will indeed have negative values in the impulse response.

• 1. I know / 2. I know / 3. I know / 4. I know / 5. I know.. Please answer the question
– Alon
Commented Jun 8, 2018 at 13:15
• Thanks for pointing this out! I forgot the "-" sign in front of the last two coefficients!
– Alon
Commented Jun 8, 2018 at 13:55
• You say the bandpass will indeed have negative values in the impulse response, how is this possible? Tell me everything you know please!
– Alon
Commented Jun 8, 2018 at 13:55
• Why would it NOT be negative: If you have negative coefficients, you'll subtract numbers. If you subtract numbers you will occasionally get negative numbers. It's hard to help you unless you explain, why you would think this should be positive only. Nothing in the math says, it would be that way Commented Jun 8, 2018 at 16:12
• When you look at at transfer function you only see attenuation, not subtractions, am I right? The signal gets weaker or stronger but only in terms of percentages, e.g. -3dB is 50%. Or at -200dB the signal is attenuated by 99.99...%. But it never changes algebraic sign, does it?
– Alon
Commented Jun 12, 2018 at 11:14