<?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>fft C# Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/tag/fft-c/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/tag/fft-c</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Tue, 02 Jun 2020 22:51:59 +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>FFT Performance Benchmarks in .NET</title>
		<link>https://www.centerspace.net/fft-performance-benchmarks-in-net</link>
					<comments>https://www.centerspace.net/fft-performance-benchmarks-in-net#respond</comments>
		
		<dc:creator><![CDATA[Paul Shirkey]]></dc:creator>
		<pubDate>Wed, 05 Jan 2011 20:07:46 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[FFT]]></category>
		<category><![CDATA[FFT .NET benchmarks]]></category>
		<category><![CDATA[FFT benchmarks]]></category>
		<category><![CDATA[fft C#]]></category>
		<category><![CDATA[FFT in .NET]]></category>
		<category><![CDATA[Multicore FFT]]></category>
		<category><![CDATA[NMATH FFT and FFTW]]></category>
		<category><![CDATA[Non power of 2 FFT]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=2942</guid>

					<description><![CDATA[<p>We've had a number of inquires about the CenterSpace FFT benchmarks, so I thought I would code up a few tests and run them on my machine.  I've included our FFT performance numbers and the code that generated those numbers so you can try them on your machine.  (If you don't have NMath, you'll need to download the <a href="https://www.centerspace.net/downloads/trial-versions/">eval version</a>).  I also did a head-to-head comparison with FFTW, one of the fastest desktop FFT implementations.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/fft-performance-benchmarks-in-net">FFT Performance Benchmarks in .NET</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>We&#8217;ve had a number of inquires about the CenterSpace FFT benchmarks, so I thought I would code up a few tests and run them on my machine.  I&#8217;ve included our FFT performance numbers and the code that generated those numbers so you can try them on your machine.  (If you don&#8217;t have NMath, you&#8217;ll need to download the <a href="/trial-version/">eval version</a>).  I also did a comparison of 1 dimensional real DFTs, with FFTW, one of the fastest desktop FFT implementations available.</p>
<h3> Benchmarks </h3>
<p>These benchmarks were run on a 2.80 Ghz, Intel Core i7 CPU, with 4Gb of memory installed. </p>
<pre class="code">
The clock resolution is 0.003 ns
1024 point, forward, real FFT required 4361.364 ns, Mflops 4069
1000 point, forward, real FFT required 5338.785 ns, Mflops 3235
4096 point, forward, real FFT required 21708.565 ns, Mflops 3924
4095 point, forward, real FFT required 43012.010 ns, Mflops 1980
1024 * 1024 point, forward, real FFT required 15.635 ms, Mflops 2324
</pre>
<p>I&#8217;m estimating the megaflop performance during the FFT using:<br />
<center><br />
<img decoding="async" src="http://latex.codecogs.com/gif.latex?MFlops \approx {2.5*n \ ln (n)) \over{ \textit{time in} \ \mu s} }" title="MFlops \approx {2.5*n \ ln (n)) \over{ \textit{time in} \ \mu s} }" /><br />
</center></p>
<p>This is the asymptotic number of floating point operations for the radix-2 Cooley-Tukey FFT algorithm. This FFT MFlop estimate is used in a number of FFT benchmark reports and serves as a good basis for comparing algorithm efficiency.</p>
<p>As expected we take a performance hit for non-power of 2 lengths, but due to various optimizations for processing prime length FFT kernels (3, 5, 7 &#038; 11), the performance hit is minimal in many cases. The 1000-point FFT has prime factors <code>(2)(2)(2)(5)(5)(5)</code>, and the 4095-point FFT has prime factors <code>(3)(3)(5)(7)(13)</code>, so those larger prime factors in the 4095-point FFT cost us some performance.  Typically, user&#8217;s zero pad their data vectors to a power-of-two length to get optimal performance.</p>
<h3> Side by side comparison with FFTW </h3>
<p>FFTW claims to be the &#8220;Fastest Fourier Transform in the West&#8221;, and is a clever, high performance implementation of the discrete Fourier transform.  This algorithm is shipped with all copies of MATLAB.  FFTW is implemented in C and has the reputation as being one of the fastest desktop FFT algorithm.  </p>
<p>Both the NMath FFT and the FFTW have a pre-computation setup that establishes the best algorithmic approach for the DFT at hand, before computing any FFT&#8217;s.  This pre-computational phase is not included in the times below.   In the case of the NMath FFT classes, this pre-computational phase in done in the class constructor; Therefore users must avoid constructing NMath FFT classes in tight loops for best performance (as shown in the benchmark code below).  Below is a small side-by-side comparison between FFTW and NMath&#8217;s FFT (using the numbers from above).</p>
<pre class="code">
<table><tbody>
<tr>
<th colspan="3"> Comparison of a forward, real, out-of-place FFT. </th>
</tr>
<tr> 
<th> FFT length</th> <th> FFTW </th> <th> NMATH FFT </th>
</tr>
<tr> 
<td> 1024 </td> <td> 4.14 &mu;s</td> <td> 4.36 &mu;s </td> 
</tr>
<tr> 
<td> 1000</td> <td> 5.98 &mu;s </td> <td> 5.33 &mu;s </td> 
</tr>
<tr> 
<td> 4096</td> <td> 20.31 &mu;s </td> <td> 21.71 &mu;s </td> 
</tr>
<tr> 
<td> 4095</td> <td> 49.90 &mu;s </td> <td> 43.01 &mu;s </td> 
</tr>
<tr> 
<td> 1024^2 </td> <td> 17.16 ms </td> <td> 15.63 ms </td> 
</tr>
</tbody>
</table>
</pre>
<p>Clearly NMATH is very competitive with, and at times out-performs FFTW for real FFT&#8217;s of both power-of-2 length signals and otherwise.  I chose 1D real signals as a test case because this is one of the most frequent use cases of our NMATH FFT library. </p>
<p>On a subjective scale, running a 1024-point FFT on a desktop commodity machine at around (an algorithm normalized) 4 GFlops is amazing.  That means that in a real time measurement situation, users can compute 1024-point FFT&#8217;s at around 220kHz &#8211; all with just a couple of lines of code.</p>
<p>Happy Computing,<br />
<em> Paul </em></p>
<h3> Benchmark Code </h3>
<pre lang="csharp">
 public void BenchMarks()
    {
      Double numberTrials = 10000;
      Double flops;

      Stopwatch timer = new System.Diagnostics.Stopwatch();
      Console.WriteLine( String.Format("The clock resolution is {0:0.000} ns", Stopwatch.Frequency / 1000000000.0 ) );

      // Snip one - power of two
      RandGenUniform rand = new RandGenUniform();
      DoubleForward1DFFT fft = new DoubleForward1DFFT( 1024 );
      DoubleVector realsignal = new DoubleVector( 1024, rand );

      DoubleVector result = new DoubleVector( 1024 * 1024 );

      timer.Reset();
      for( int i = 0; i < numberTrials; i++ )
      {
        timer.Start();
        fft.FFT( realsignal, ref result );
        timer.Stop();
      }
      flops = (2.5 * 1024 * NMathFunctions.Log(1024)) / (((timer.ElapsedTicks / numberTrials) / Stopwatch.Frequency) * 1000000.0 );
      Console.WriteLine( String.Format( "1024 point, forward, real FFT required {0:0.000} ns, Mflops {1:0}", ( ( timer.ElapsedTicks / numberTrials ) / Stopwatch.Frequency ) * 1000000000.0, flops ) );

      // length 1000
      fft = new DoubleForward1DFFT( 1000 );
      realsignal = new DoubleVector( 1000, rand );

      timer.Reset();
      for( int i = 0; i < numberTrials; i++ )
      {
        timer.Start();
        fft.FFT( realsignal, ref result );
        timer.Stop();
      }
      flops = ( 2.5 * 1000 * NMathFunctions.Log( 1000 ) ) / ( ( ( timer.ElapsedTicks / numberTrials ) / Stopwatch.Frequency ) * 1000000.0 );
      Console.WriteLine( String.Format( "1000 point, forward, real FFT required {0:0.000} ns, Mflops {1:0}", ( ( timer.ElapsedTicks / numberTrials ) / Stopwatch.Frequency ) * 1000000000.0, flops ) );

      // length 4096
      fft = new DoubleForward1DFFT( 4096 );
      realsignal = new DoubleVector( 4096, rand );

      timer.Reset();
      for( int i = 0; i < numberTrials; i++ )
      {
        timer.Start();
        fft.FFT( realsignal, ref result );
        timer.Stop();
      }
      flops = ( 2.5 * 4096 * NMathFunctions.Log( 4096 ) ) / ( ( ( timer.ElapsedTicks / numberTrials ) / Stopwatch.Frequency ) * 1000000.0 );
      Console.WriteLine( String.Format( "4096 point, forward, real FFT required {0:0.000} ns, Mflops {1:0}", ( ( timer.ElapsedTicks / numberTrials ) / Stopwatch.Frequency ) * 1000000000.0, flops ) );

      // length 4095
      fft = new DoubleForward1DFFT( 4095 );
      realsignal = new DoubleVector( 4095, rand );

      timer.Reset();
      for( int i = 0; i < numberTrials; i++ )
      {
        timer.Start();
        fft.FFT( realsignal, ref result );
        timer.Stop();
      }
      flops = ( 2.5 * 4095 * NMathFunctions.Log( 4095 ) ) / ( ( ( timer.ElapsedTicks / numberTrials ) / Stopwatch.Frequency ) * 1000000.0 );
      Console.WriteLine( String.Format( "4095 point, forward, real FFT required {0:0.000} ns, Mflops {1:0}", ( ( timer.ElapsedTicks / numberTrials ) / Stopwatch.Frequency ) * 1000000000.0, flops ) );


      // length 1M
      fft = new DoubleForward1DFFT( 1024 * 1024 );
      realsignal = new DoubleVector( 1024 * 1024, rand );

      timer.Reset();
      for( int i = 0; i < 100; i++ )
      {
        timer.Start();
        fft.FFT( realsignal, ref result );
        timer.Stop();
      }
      flops = ( 2.5 * 1024 * 1024 * NMathFunctions.Log( 1024 * 1024 ) ) / ( ( ( timer.ElapsedTicks / 100.0 ) / Stopwatch.Frequency ) * 1000000.0 );
      Console.WriteLine( String.Format( "Million point (1024 * 1024), forward, real point FFT required {0:0.000} ms, Mflops {1:0}", ( ( timer.ElapsedTicks / 100.0 ) / Stopwatch.Frequency ) * 1000.0, flops ) );

    }
</pre>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/fft-performance-benchmarks-in-net">FFT Performance Benchmarks in .NET</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/fft-performance-benchmarks-in-net/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2942</post-id>	</item>
		<item>
		<title>Power Spectral Density with NMath</title>
		<link>https://www.centerspace.net/power-spectral-density-with-nmath</link>
					<comments>https://www.centerspace.net/power-spectral-density-with-nmath#respond</comments>
		
		<dc:creator><![CDATA[Paul Shirkey]]></dc:creator>
		<pubDate>Wed, 13 Jan 2010 19:35:53 +0000</pubDate>
				<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[C# power spectral density]]></category>
		<category><![CDATA[C# power spectrum]]></category>
		<category><![CDATA[estimating PSD]]></category>
		<category><![CDATA[fft C#]]></category>
		<category><![CDATA[filtering c#]]></category>
		<category><![CDATA[power spectral density]]></category>
		<category><![CDATA[PSD .NET]]></category>
		<category><![CDATA[PSD C#]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=725</guid>

					<description><![CDATA[<p><img src="https://www.centerspace.net/blog/wp-content/uploads/2010/01/SGPkFndrExplPeriodogram-150x136.jpg" alt="" title="Periodogram Example" class="excerpt" /><br />
We've had several customers ask about computing the PSD in C# with NMath, so I thought it was time for a post on the subject.  The power spectral density provides an estimate of the power present within each slice of spectrum, and is presented as graph of the signal power versus frequency.  It's a common signal processing calculation across many fields from acoustics to chemistry, and can provide insight into periodicities contained within a time domain signal.  </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/power-spectral-density-with-nmath">Power Spectral Density with NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<h2>Application Note</h2>
<h2>Computing the Power Spectrum in C#</h2>
<p>We&#8217;ve had several customers ask about computing the PSD in C# with NMath, so I thought it was time for a post on the subject. The power spectral density provides an estimate of the power present within each slice of spectrum, and is presented as graph of the signal power versus frequency. It&#8217;s a common signal processing calculation across many fields from acoustics to chemistry, and can provide insight into periodicities contained within a time domain signal.</p>
<p>For stationary, square summable-signals, the PSD is expressed as,</p>
<p> </p>
<center><br /><img decoding="async" title="S_f(\omega) = \underset{T \rightarrow \infty }{lim} {\left | F_T(\omega) \right |^2 \over T}" src="http://latex.codecogs.com/gif.latex?S_f(\omega) = \underset{T \rightarrow \infty }{lim} {\left | F_T(\omega) \right |^2 \over T}" /></center>
<p> </p>
<p>where <code>F(w)</code> is the Fourier transform of the time domain signal <code> f(t)</code>, and <code>T</code> is the width of the time domain sampled signal. Naturally we can never sample the entire signal, so calculations of the PSD (power spectrum density) are all estimates. Techniques for estimating the PSD can be divided into two classes, parametric (model based), and non-parametic (non-model based). We will discuss only non-parametic techniques here. For discrete signals that are not square-summable (i.e. non-stationary signals &#8211; and so the Fourier transform does not exist), estimates of the power spectral density can be derived from,</p>
<p> </p>
<center><br /><img decoding="async" title="S_f(\omega) = F \left \{ \underset{T \rightarrow \infty} {\lim } {{1} \over {T}} (f^*(t) *_T f(t)) \right \}" src="http://latex.codecogs.com/gif.latex?S_f(\omega) = F \left \{ \underset{T \rightarrow \infty} {\lim } {{1} \over {T}} (f^*(t) *_T f(t)) \right \}" /></center>
<p> </p>
<p>Which is the Fourier transform of the autocorrelation as the correlation width of the sampled signal tends to infinity. For a concise derivation of both of these formulas read <a href="http://www.ee.nmt.edu/~elosery/lectures/power_spectral_density.pdf">these</a> short lecture notes.</p>
<h3>Computing the PSD in C# with NMath</h3>
<p>Concentrating on stationary (periodic) signals, the PSD is most efficiently computed by applying smoothing to discrete periodograms .</p>
<p> </p>
<center><br /><img decoding="async" title="Periodogram(\omega_n) = {\left | F_n(\omega) \right |^2 \over n}" src="http://latex.codecogs.com/gif.latex?Periodogram(\omega_n) = {\left | F_n(\omega) \right |^2 \over n}" /></center>
<p> </p>
<p>Where <code>n</code> is the number of signal samples. Each point in the periodogram represents the relative contribution to the variance in the time domain signal at that frequency. (Visualization provided courtesy of <a href="http://www.infragistics.com/products/wpf#Overview">Infragistics</a>.)</p>
<figure id="attachment_1163" aria-describedby="caption-attachment-1163" style="width: 360px" class="wp-caption aligncenter"><img decoding="async" loading="lazy" class="size-full wp-image-1163" title="Periodogram Example" src="https://www.centerspace.net/blog/wp-content/uploads/2010/01/SGPkFndrExplPeriodogram.jpg" alt="Periodogram of sun spot data." width="360" height="136" srcset="https://www.centerspace.net/wp-content/uploads/2010/01/SGPkFndrExplPeriodogram.jpg 360w, https://www.centerspace.net/wp-content/uploads/2010/01/SGPkFndrExplPeriodogram-300x113.jpg 300w" sizes="(max-width: 360px) 100vw, 360px" /><figcaption id="caption-attachment-1163" class="wp-caption-text">An example periodogram of sunspot data. This is smoothed in some fashion to estimate the PSD.</figcaption></figure>
<p>Another estimation technique involves computing multiple windowed periodograms and then combining these together to get a progressively more accurate estimate (Welch&#8217;s Method, similarly MTM with Slepian windows). The time domain signal should be detrended before any of these operations.<br /><span id="more-725"></span><br />The following C# code estimates the PSD by smoothing the periodogram using a Savitzy-Golay zero phase shift filter.</p>
<pre lang="csharp"><code>using CenterSpace.NMath.Core;

/* Estimate the Power Spectrum Density in C# / .NET */
public DoubleVector PSDEstimate(DoubleVector signal)
{
  // Detrend the periodic data<br /></code><code>  signal = signal - NMathFunctions.Mean(signal); </code><br /><br /><code>  // Compute the periodogram </code><br /><code>  var forwardFFT = new DoubleForward1DFFT( signal.Length); </code><br /><code>  var packedFFT = forwardFFT.FFT( signal ); </code><br /><code>  DoubleSymmetricSignalReader reader = forwardFFT.GetSignalReader( packedFFT ); </code><br /><code>  DoubleComplexVector fft = reader.UnpackSymmetricHalfToVector();<br /><br />  </code><code>// Square of the absolute value, scale the result by the length, 1/N.</code><br /><code>  DoubleVector pg = NMathFunctions.Pow( NMathFunctions.Abs(fft), 2.0) / fft.Length; </code><br /><code>  <br />  // Smooth w/ filter of width of 7, &amp; polynomial degree of 5. </code><br /><code>  var sgf = new SavitzkyGolayFilter(3, 3, 5); </code><br /><code>  DoubleVector smoothedPSD = sgf.Filter(pg);<br /><br />  </code><code>return smoothedPSD;
}
</code></pre>
<p>Or using a Daniell filter and the <code> MovingWindowFilter </code> class to smooth the periodogram.</p>
<pre lang="csharp"><code>MovingWindowFilter mwf = 
  new MovingWindowFilter(2, 2, new DoubleVector(.125, .25, .25, .25, .125));
mwf.WindowBoundaryOption = 
  MovingWindowFilter.BoundaryOption.PadWithZeros;

DoubleVector smoothedPSDaniell = mwf.Filter(data);
</code></pre>
<p>More information about NMath&#8217;s FFT class set can be found on our <a href="/topic-fast-fourier-transforms/">fft landing page</a>. <del datetime="2013-10-21T18:31:19+00:00">The class <code>SavitzkyGolayFilter</code> will be avaliable in our next release, however, current users can use the <code>MovingWindowFilter</code> class with Savitzky-Golay coefficients generated via a provided helper method.</del> The class <code>SavitzyGolayFilter</code> is available in the current release.</p>
<p>Happy Computing,</p>
<p><em> Paul </em></p>
<p><strong>References</strong></p>
<p>Haykin, S. “Adaptive Filter Theory”. Prentice-Hall, Inc. 1996.</p>


<p></p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/power-spectral-density-with-nmath">Power Spectral Density with NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/power-spectral-density-with-nmath/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">725</post-id>	</item>
	</channel>
</rss>
