chebyshev filters with nmath - centerspace blog

7
CenterSpace Blog » Blog Archive Chebyshev Filters with NMath - CenterSpace Blog http://www.centerspace.net/blog/chebyshev-filter-csharp/[13/04/2015 16:04:48] Call Us: +1 (541) 896-1301 « NMath Tutorial videos Optimal Portfolio Allocation » Chebyshev Filters in C# There are three classes of widely used IIR (recursive) filters in signal processing: Butterworth, Chebyshev, and elliptical. In this article I will give a short introduction to the Chebyshev filter, present a code implementation, and end with a usage example. The Butterworth filter was discussed in a previous blog article . Chebyshev filters come in two flavors defined by either allowing ripple in the pass-band (type 1) or ripple the stop-band (type 2). We will only discuss the type 1 filters here, as the type 2 filters are rarely used. Type 1 Chebyshev filters trade-off steeper roll-off with ripple in the pass band and in the limit of zero ripple, mimic Butterworth filters. Simply put, the more ripple allowed in the passband, the steep roll-off the filter achieves. Chebyshev filters derive their name from Chebyshev polynomials [1]. Finding Chebyshev polynomials for a given order represent a computational challenge, and when computed often exhibit various numerical instabilities (often overflow) if not done properly. For an excellent review and comparison of computational techniques for computing Chebyshev polynomials see this paper by Wolfram Koepf. Here, we will side step the entire computational issue, as legions of engineers have done before us, and simply look up the Chebyshev polynomials, by order, in a switch statement. Because Chebyshev filters with an order n above 10 or 12 would be unusual, this table look-up approach can be considered, practically speaking, complete. Wikipedia has a excellent overview article discussing the Chebyshev filter, so I’ll refer you there for a background treatment of Chebyshev filtering theory. Categories » .NET (11) » Best Practices (5) Blog

Upload: igor-gjorgjiev

Post on 13-Nov-2015

12 views

Category:

Documents


0 download

DESCRIPTION

Chebyshev Filters With NMath - CenterSpace Blog

TRANSCRIPT

  • CenterSpace Blog Blog Archive Chebyshev Filters with NMath - CenterSpace Blog

    http://www.centerspace.net/blog/chebyshev-filter-csharp/[13/04/2015 16:04:48]

    Call Us: +1 (541) 896-1301

    NMath Tutorial videos Optimal Portfolio Allocation

    Chebyshev Filters in C#There are three classes of widely used IIR (recursive) filters in signal processing: Butterworth, Chebyshev, and elliptical. In this article I will give a short introduction to the Chebyshev filter, present a code implementation, and end with a usage example. The Butterworth filter was discussed in a previous blog article.

    Chebyshev filters come in two flavors defined by either allowing ripple in the pass-band (type 1) or ripple the stop-band (type 2). We will only discuss the type 1 filters here, as the type 2 filters are rarely used. Type 1 Chebyshev filters trade-off steeper roll-off with ripple in the pass band and in the limit of zero ripple, mimic Butterworth filters. Simply put, the more ripple allowed in the passband, the steep roll-off the filter achieves. Chebyshev filters derive their name from Chebyshev polynomials [1]. Finding Chebyshev polynomials for a given order represent a computational challenge, and when computed often exhibit various numerical instabilities (often overflow) if not done properly. For an excellent review and comparison of computational techniques for computing Chebyshev polynomials see this paper by Wolfram Koepf. Here, we will side step the entire computational issue, as legions of engineers have done before us, and simply look up the Chebyshev polynomials, by order, in a switch statement. Because Chebyshev filters with an order n above 10 or 12 would be unusual, this table look-up approach can be considered, practically speaking, complete.

    Wikipedia has a excellent overview article discussing the Chebyshev filter, so Ill refer you there for a background treatment of Chebyshev filtering theory.

    Categories .NET (11)

    Best Practices (5)

    Blog

  • CenterSpace Blog Blog Archive Chebyshev Filters with NMath - CenterSpace Blog

    http://www.centerspace.net/blog/chebyshev-filter-csharp/[13/04/2015 16:04:48]

    Chebyshev FiltersThe gain expression for Chebyshev filters has a very similar structure to Butterworth filters. As with the Butterworth filter, there are the three familiar design parameters, DC Gain, cutoff frequency f0, and filter order n, with an additional parameter, epsilon, for specifying the desired pass-band ripple.

    Chebyshev gain equation

    The Tn signifies a Chebyshev polynomial of order n. So for example when n = 2, it happens that Tn = 2*x*x - 1, where when substituted into the gain equation we replace x with f/f0. The ripple factor can be equated to pass-band ripple in Db with the equation:

    Passband ripple in Db

    Typical values for epsilon would range between 0.1 and 1.

    The Chebyshev Filter in CodeWe take the identical approach to implementing the Chebyshev filter in code as we did with the Butterworth filter. We will first compute the input signals FFT, then multiply that by the above filter gain, and then take the inverse FFT of that product resulting in our filtered signal. This is a O( n*log(n)) operation.

    But before we do that, we need a way to find the Chebyshev polynomials of a given order n. To that end Ive build a simple method that returns a Chebyshev polynomial given the order; if the requested order is larger than the look-up table, a NotImplementedException is thrown. This

    Case Studies (2)

    CenterSpace (16)

    Corporate (6)

    Customer Stories (2)

    Events (1)

    Excel (6)

    Marketing (5)

    MKL (2)

    NMath (60)

    NMath Tutorial (16)

    NMath Premium (12)

    NMath Stats (27)

    NMath Stats Tutorial (7)

    Object-Oriented Numerics (1)

    Performance (7)

    Silverlight (1)

    Statistics (5)

    Student version (1)

    Support (4)

    Syncfusion (2)

    Theory (2)

    Uncategorized (6)

    Video (1)

    Visualization (9)

  • CenterSpace Blog Blog Archive Chebyshev Filters with NMath - CenterSpace Blog

    http://www.centerspace.net/blog/chebyshev-filter-csharp/[13/04/2015 16:04:48]

    look-up approach can easily be extended as far as would be needed for most any practicable Chebyshev filter.

    Chebyshev Polynomial Lookup

    private Polynomial ChebyshevPolynomial( int order) { switch( order ) { case 0: return new Polynomial( new DoubleVector( "1" ) ); case 1: return new Polynomial( new DoubleVector( "0 1" ) ); case 2: return new Polynomial( new DoubleVector( "-1 0 2" ) ); case 3: return new Polynomial( new DoubleVector( "0 -3 0 4" ) ); case 4: return new Polynomial( new DoubleVector( "1 0 -8 0 8" ) ); case 5: return new Polynomial( new DoubleVector( "0 5 0 -20 0 16" ) ); case 6: return new Polynomial( new DoubleVector( "-1 0 18 0 -48 0 32" ) ); case 7: return new Polynomial( new DoubleVector( "0 7 0 56 0 -112 0 64" ) ); case 8: return new Polynomial( new DoubleVector( "1 0 -32 0 160 0 -256 0 128" ) ); default: throw new NotImplementedException(); } }

    An excellent on-line resource for Chebyshev polynomials, or any other integer sequence can be found at OEIS, the Online Encyclopaedia of Integer Sequences. If you wish to delve into computing the coefficients of Chebyshev polynomials, a good review of techniques can be found here.

    Chebyshev Filter

    The following code implements the Chebyshev filter, following the three steps mentioned above:

  • CenterSpace Blog Blog Archive Chebyshev Filters with NMath - CenterSpace Blog

    http://www.centerspace.net/blog/chebyshev-filter-csharp/[13/04/2015 16:04:48]

    [1] FFT(signal), [2] FFT(signal)*G(f), [3] IFFT.

    private DoubleComplexVector ChebeshevFilter( DoubleComplexVector signal, double sampleFrequency, int order, double f0, double ripple, double DCGain ) { // Get the Chebyshev polynomial Polynomial T = ChebyshevPolynomial( order ); int N = signal.Length; // Apply forward FFT in place DoubleComplexForward1DFFT fft = new DoubleComplexForward1DFFT( N ); DoubleComplexVector signalFFT = fft.FFT( signal ); // Remove DC offset signalFFT[0] = 0; if ( f0 > 0 ) { double numBins = N / 2; // Half the length of the FFT by symmetry double binWidth = sampleFrequency / numBins; // Hz // Filter System.Threading.Tasks.Parallel.For( 1, N / 2, i => { double binFreq = binWidth * i; double gain = DCGain / ( Math.Sqrt( ( 1 + Math.Pow( T.Evaluate( binFreq / f0) , 2.0 ) ) ) ); signalFFT[i] *= gain; signalFFT[N - i] *= gain; } ); } // Reverse filtered signal DoubleComplexBackward1DFFT ifft = new DoubleComplexBackward1DFFT( N ); ifft.SetScaleFactorByLength(); // Needed to get the correct amplitude signal = ifft.FFT( signalFFT ); return signal; }

    Filtering Example

  • CenterSpace Blog Blog Archive Chebyshev Filters with NMath - CenterSpace Blog

    http://www.centerspace.net/blog/chebyshev-filter-csharp/[13/04/2015 16:04:48]

    To test the Chebyshev filtering code above, Ive generated a test signal which is the sum to two sinusoids of frequences 1 Hz and 8 Hz. For comparison, this is the same test signal I used to test the Butterworth filter in my previous post. The test signal is sampled at 25 Hz and then filtered using a third order Chebyshev filter with a frequency cut-off of 1.5 Hz and ripple factor (epsilon) of 0.2.

    public void ChebyshevFilterTest() { DoubleComplexVector signal = new DoubleComplexVector( 100 ); double Fs = 25; // Sample rate, 25Hz double f1 = 1; // 1 Hz Signal double f2 = 8; // 8 Hz Signal for ( int t = 0; t < 100; t++ ) { signal[t] = new DoubleComplex( Math.Sin( 2 * Math.PI * f1 / Fs * t ) + Math.Sin( 2 * Math.PI * f2 / Fs * t ), 0.0 ); } // Filter DoubleComplexVector filteredSignal = this.ChebeshevFilter( signal, 25, 3, 1.5, 0.2, 1.0 ); // Display results Chart chart = NMathChart.ToChart( new DoubleVector( signal.ToRealDataTable() ) ); chart.Titles[0].Text = "Input signal, sampled at 25 Hz"; chart.ChartAreas[0].AxisY.Title = "Input signal magnitude"; chart.ChartAreas[0].AxisX.Minimum = 0; Chart chartfft = NMathChart.ToChart( new DoubleVector( filteredSignal.ToRealDataTable() ) ); chartfft.Titles[0].Text = "Filtered signal"; chartfft.ChartAreas[0].AxisY.Title = "Filtered signal magnitude"; chartfft.ChartAreas[0].AxisX.Minimum = 0; List charts = new List() { chart, chartfft }; Chart compositeChart = NMathChart.Compose( charts, 1, 2, NMathChart.AreaLayoutOrder.ColumnMajor ); compositeChart.Width = 650; compositeChart.Height = 300; NMathChart.Show( compositeChart ); }

  • CenterSpace Blog Blog Archive Chebyshev Filters with NMath - CenterSpace Blog

    http://www.centerspace.net/blog/chebyshev-filter-csharp/[13/04/2015 16:04:48]

    This example test code, using NMath charts, generates the following chart of the unfiltered and filtered signal side by side.

    -Happy Computing,

    Paul

    [1] Chebyshev is a transliteration of a Russian name and in the past has been variously spelled as Chebychev, Tschebyscheff, Tchebysheff and Tchebichef.

    Tags: c# chebyshev, c# chebyshev filter, c# data filtering, c# iir filtering, chebyshev filter, nmath filtering, vb.net chebyshev, vb.net chebyshev filter

    This entry was posted on Wednesday, December 11th, 2013 at 7:44 pm and is filed under NMath. You can follow any responses to

    this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

    Leave a Reply

  • CenterSpace Blog Blog Archive Chebyshev Filters with NMath - CenterSpace Blog

    http://www.centerspace.net/blog/chebyshev-filter-csharp/[13/04/2015 16:04:48]

    Name (required)

    Mail (will not be published) (required)

    Website

    2009-2014 CenterSpace Software | Home | Sitemap | Contact Us

    centerspace.netCenterSpace Blog Blog Archive Chebyshev Filters with NMath - CenterSpace Blog

    VieXNoZXYtZmlsdGVyLWNzaGFycC8A: form1: query: submit:

    VieXNoZXYtZmlsdGVyLWNzaGFycC8A: form15: submit: author: email: url: comment: