vinniefalco / DSPFilters

A Collection of Useful C++ Classes for Digital Signal Processing
1.83k stars 375 forks source link

Assertion failed: (!Dsp::is_nan (pole)) in Layout.h line 101 #17

Closed mgill404 closed 10 years ago

mgill404 commented 10 years ago

I am getting this Assertion failed for Butterworth low shelf, high shelf, and band shelf filters. The assertion failed happens when the order of the filter is raised above 1. I am setting up the filter using the following code snippets.

Dsp::Filter *filter = 0; filter = new Dsp::SmoothedFilterDesign Dsp::Butterworth::Design::BandShelf<50,2 > (1024);

m_params[0] = SampleRate; m_params[1] = Order; m_params[2] = Freq; m_params[3] = Gain; filter->setParams(m_params);

This causes the following statements to return false, and I cannot determine what would be making that happen.

template inline bool is_nan (Ty v) { return !(v == v); }

template <> inline bool is_nan (complex_t v) { return Dsp::is_nan (v.real()) || Dsp::is_nan (v.imag()); }

This arrangement works for many other filters, but it is not working for me if the order is greater than 1 for Butterworth shelf filters.

mgill404 commented 10 years ago

I've been digging all day and found that std::polar(theta, rho) returns NaN if theta is negative. How is this being circumvented in the demo project?

mgill404 commented 10 years ago

I am able to circumvent the NaN return from std::polar by adding std:abs() to the parameters. The fix is below.

addPoleZeroConjugatePairs (std::polar (gp, theta), std::polar (gz, theta));

addPoleZeroConjugatePairs (std::polar (std::abs(gp), theta), std::polar (std::abs(gz), theta));

pumphaus commented 9 years ago

I'm a little unsure whether this is the correct fix. While it makes the assertion go away, the minus signs seem to be intentionally put there: const double gp = -1. / g; const double gz = -g; Does the filter work as expected after this change?