<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>c# chebyshev Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/tag/c-chebyshev/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/tag/c-chebyshev</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Wed, 14 Oct 2020 22:52:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>
<site xmlns="com-wordpress:feed-additions:1">104092929</site>	<item>
		<title>Chebyshev Filters in C#</title>
		<link>https://www.centerspace.net/chebyshev-filter-csharp</link>
					<comments>https://www.centerspace.net/chebyshev-filter-csharp#respond</comments>
		
		<dc:creator><![CDATA[Paul Shirkey]]></dc:creator>
		<pubDate>Thu, 12 Dec 2013 03:44:53 +0000</pubDate>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[c# chebyshev]]></category>
		<category><![CDATA[c# chebyshev filter]]></category>
		<category><![CDATA[c# data filtering]]></category>
		<category><![CDATA[c# iir filtering]]></category>
		<category><![CDATA[chebyshev filter]]></category>
		<category><![CDATA[nmath filtering]]></category>
		<category><![CDATA[vb.net chebyshev]]></category>
		<category><![CDATA[vb.net chebyshev filter]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=5062</guid>

					<description><![CDATA[<p>There are three classes of widely used IIR (recursive) filters in signal processing: Butterworth, Chebyshev, and elliptical. In this postI will give a short introduction to Chebyshev filters, 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 [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/chebyshev-filter-csharp">Chebyshev Filters in C#</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>There are three classes of widely used IIR (recursive) filters in signal processing: Butterworth, Chebyshev, and elliptical. In this postI will give a short introduction to Chebyshev filters, present a code implementation, and end with a usage example. The Butterworth filter was discussed in a previous <a title="Butterworth filter blog article" href="/butterworth-filter-csharp/">blog article</a>.</p>
<p>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.<br />
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 <a href="http://www.mathematik.uni-kassel.de/~koepf/cheby.pdf">this</a> 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 <code>switch</code> statement. Because Chebyshev filters with an order <code>n</code> above 10 or 12 would be unusual, this table look-up approach can be considered, practically speaking, complete.</p>
<p>Wikipedia has a excellent overview article discussing the <a href="https://en.wikipedia.org/wiki/Chebyshev_filter">Chebyshev filter,</a> so I&#8217;ll refer you there for a background treatment of Chebyshev filtering theory.</p>
<h2>Chebyshev Filters</h2>
<p>The 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, <code>DC Gain</code>, cutoff frequency <code>f0</code>, and filter order <code>n</code>, with an additional parameter, <code>epsilon</code>, for specifying the desired pass-band ripple.</p>
<p><center></p>
<figure id="attachment_5119" aria-describedby="caption-attachment-5119" style="width: 182px" class="wp-caption alignnone"><img decoding="async" loading="lazy" class="size-full wp-image-5119" src="https://www.centerspace.net/blog/wp-content/uploads/2013/12/CodeCogsEqn7.gif" alt="Chebyshev gain equation" width="182" height="45" /><figcaption id="caption-attachment-5119" class="wp-caption-text">Chebyshev gain equation</figcaption></figure>
<p></center></p>
<p>The <code>Tn</code> signifies a Chebyshev polynomial of order <code>n</code>. So for example when <code>n = 2</code>, it happens that <code>Tn = 2*x*x - 1</code>, where when substituted into the gain equation we replace <code>x</code> with <code>f/f0</code>. The ripple factor can be equated to pass-band ripple in Db with the equation:</p>
<p><center></p>
<figure id="attachment_5119" aria-describedby="caption-attachment-5119" style="width: 114px" class="wp-caption alignnone"><img decoding="async" loading="lazy" class="alignnone size-full wp-image-5070" src="https://www.centerspace.net/blog/wp-content/uploads/2013/12/CodeCogsEqn81.gif" alt="Passband ripple equation expressed in decibles" width="114" height="20" /><figcaption id="caption-attachment-5119" class="wp-caption-text">Passband ripple in Db</figcaption></figure>
<p></center></p>
<p>Typical values for epsilon would range between 0.1 and 1.</p>
<h2>The Chebyshev Filter in Code</h2>
<p>We take the identical approach to implementing the Chebyshev filter in code as we did with the <a href="/butterworth-filter-csharp/">Butterworth</a> filter. We will first compute the input signal&#8217;s 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 <code>O( n*log(n))</code> operation.</p>
<p>But before we do that, we need a way to find the Chebyshev polynomials of a given order <code>n</code>. To that end I&#8217;ve build a simple method that returns a Chebyshev polynomial given the order; if the requested order is larger than the look-up table, a <code>NotImplementedException</code> is thrown. This look-up approach can easily be extended as far as would be needed for most any practicable Chebyshev filter.</p>
<h3>Chebyshev Polynomial Lookup</h3>
<pre lang="csharp"> 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();     
      }
   }
</pre>
<p>An excellent on-line resource for Chebyshev polynomials, or any other integer sequence can be found at <a href="http://oeis.org/A053120">OEIS</a>, 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 <a href="http://www.mathematik.uni-kassel.de/~koepf/cheby.pdf">here</a>.</p>
<h3>Chebyshev Filter</h3>
<p>The following code implements the Chebyshev filter, following the three steps mentioned above: [1] FFT(signal), [2] FFT(signal)*G(f), [3] IFFT.</p>
<pre lang="csharp">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 &gt; 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 =&gt;
        {
          double binFreq = binWidth * i;
          double gain = 
            DCGain / ( Math.Sqrt( ( 1 + ripple * 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;
    }
</pre>
<h2>Filtering Example</h2>
<p>To test the Chebyshev filtering code above, I&#8217;ve 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 <a href="/butterworth-filter-csharp/">post</a>. 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.</p>
<pre lang="csharp">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 &lt; 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 );

    }
</pre>
<p>This example test code, using NMath charts, generates the following chart of the unfiltered and filtered signal side by side.</p>
<p><img decoding="async" class="alignnone size-full wp-image-5087" src="https://www.centerspace.net/blog/wp-content/uploads/2013/12/Chebyshev.png" alt="Chebyshev filtering example" width="600" srcset="https://www.centerspace.net/wp-content/uploads/2013/12/Chebyshev.png 650w, https://www.centerspace.net/wp-content/uploads/2013/12/Chebyshev-300x138.png 300w" sizes="(max-width: 650px) 100vw, 650px" /></p>
<p>-Happy Computing,</p>
<p>Paul</p>
<p>[1] Chebyshev is a transliteration of a Russian name and in the past has been variously spelled as Chebychev, Tschebyscheff, Tchebysheff and Tchebichef.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/chebyshev-filter-csharp">Chebyshev Filters in C#</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/chebyshev-filter-csharp/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5062</post-id>	</item>
	</channel>
</rss>
