# Why does low highpass in fir filter cause NaN?

Started by August 20, 2006
```I am using a butterworth filter on a 1843200 point signal, sampled at
2048/second:

n = 2;
Wn = [lo hi]/(samplerate/2);
[buttercoeffsB,buttercoeffsA] = butter(n,Wn);

to bandpass a signal:

filtereddata = filter(buttercoeffs(1,:),buttercoeffs(2,:),data);

When lo and hi equal .2 and 80, or .3 and 60, the filtering works fine.
But, if I try .2 and 60, or .3 and 80, only part of the filtered data
is produced, and the rest of the filtered data is NaN.

Clearly I don't have a deep enough understanding of filtering. Any
comments about why this happens or how to get around it would be much
appreciated.

Many thanks.....

```
```charlesblackstone1@hotmail.com wrote:

> I am using a butterworth filter on a 1843200 point signal, sampled at
> 2048/second:
>
>    n = 2;
>    Wn = [lo hi]/(samplerate/2);
>    [buttercoeffsB,buttercoeffsA] = butter(n,Wn);
>
> to bandpass a signal:
>
>    filtereddata = filter(buttercoeffs(1,:),buttercoeffs(2,:),data);
>
>
> When lo and hi equal .2 and 80, or .3 and 60, the filtering works fine.
> But, if I try .2 and 60, or .3 and 80, only part of the filtered data
> is produced, and the rest of the filtered data is NaN.
>
> Clearly I don't have a deep enough understanding of filtering. Any
> comments about why this happens or how to get around it would be much
> appreciated.
>
> Many thanks.....
>
Perhaps letting us know what the filter coefficients are would help --
not everyone here uses MatLab to generate filters.

Even having not used MatLab for a long time, I can tell you that the
'filter' function as you're using it implements an infinite impulse
response filter, _not_ a FIR filter.  That's an important difference to
keep straight.

I would suspect that some internal state of your filter is unstable, and
you get real numbers until that state exceeds the size of MatLab's
floating point representation, at which point you start getting NaNs.

--

Tim Wescott
Wescott Design Services
http://www.wescottdesign.com

"Applied Control Theory for Embedded Systems" came out in April.
See details at http://www.wescottdesign.com/actfes/actfes.html
```
```On 20 Aug 2006 01:51:06 -0700, charlesblackstone1@hotmail.com wrote:

>I am using a butterworth filter on a 1843200 point signal, sampled at
>2048/second:
>
>   n = 2;
>   Wn = [lo hi]/(samplerate/2);
>   [buttercoeffsB,buttercoeffsA] = butter(n,Wn);
>
>to bandpass a signal:
>
>   filtereddata = filter(buttercoeffs(1,:),buttercoeffs(2,:),data);
>
>
>When lo and hi equal .2 and 80, or .3 and 60, the filtering works fine.
>But, if I try .2 and 60, or .3 and 80, only part of the filtered data
>is produced, and the rest of the filtered data is NaN.
>
>Clearly I don't have a deep enough understanding of filtering. Any
>comments about why this happens or how to get around it would be much
>appreciated.
>
>Many thanks.....

Hello Charles,

are you sure your filter is working properly
when lo = 0.2 and hi = 80?

With a sample rate of 2048 Hz, I question whether
a fourth-order IIR filter can achieve the
incredibly low frequency of 0.2 Hz for the

When I apply your coefficients to MATLAB's
freqz() command and plot the filter's mag
response I see a truly strange response.
I'm guessing that your "lo" value is
pushing MATLAB's algorithms beyond the
parameter range where they operate properly.

Do you really need the beginning of the
passband to be 0.2 Hz?
(It seems to me you're trying to implement
some sort of super-duper "DC cancelation" filter.)

Regards,
[-Rick-]

```