📄 fast fourier transform in c# (cooky-turkey) - adrian's tech blog - 博客园.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://www.cnblogs.com/Dah/archive/2007/08/10/850904.html -->
<HTML><HEAD id=Head><TITLE>Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8">
<META id=metaKeywords content=Fast,Fourier,Transform,in,C,Cooky,Turkey,
name=keywords>
<META
content=C#codesnippetbelowisanillustrationoftheCooky-Turkeyalgorithm,theperformancemaysuckwhenprocessinghugedatasets,butyoucanusearrays
name=description><LINK id=CommondCss
href="Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files/common.css"
type=text/css rel=stylesheet><LINK id=MainCss
href="Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files/style.css"
type=text/css rel=stylesheet><LINK id=SecondaryCss
href="Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files/customcss.css"
type=text/css rel=stylesheet><LINK id=RSSLink title=RSS
href="http://www.cnblogs.com/Dah/rss" type=application/rss+xml rel=alternate>
<SCRIPT
src="Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files/common.js"
type=text/javascript></SCRIPT>
<META content="MSHTML 6.00.2900.3354" name=GENERATOR></HEAD>
<BODY>
<FORM id=Form1 name=Form1 onsubmit="javascript:return WebForm_OnSubmit();"
action=850904.html method=post>
<DIV><INPUT id=__EVENTTARGET type=hidden name=__EVENTTARGET> <INPUT
id=__EVENTARGUMENT type=hidden name=__EVENTARGUMENT> <INPUT
id=" __VIEWSTATE" type=hidden name=__VIEWSTATE> </DIV>
<SCRIPT type=text/javascript>
//<![CDATA[
var theForm = document.forms['Form1'];
if (!theForm) {
theForm = document.Form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
//]]>
</SCRIPT>
<SCRIPT
src="Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files/WebResource.axd"
type=text/javascript></SCRIPT>
<SCRIPT language=JavaScript>
function ctlent(evt,id)
{
if(evt.ctrlKey && evt.keyCode == 13)
{
try
{
TempSave(id);
}
catch(ex)
{
}
finally
{
__doPostBack('AjaxHolder$PostComment$btnSubmit','')
}
}
}</SCRIPT>
<SCRIPT language=JavaScript>function SetReplyAuhor(author){document.getElementById('AjaxHolder_PostComment_tbComment').value+="@"+author+"\n";document.getElementById('AjaxHolder_PostComment_tbComment').focus();return false}</SCRIPT>
<SCRIPT
src="Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files/ScriptResource.axd"
type=text/javascript></SCRIPT>
<SCRIPT
src="C:\Documents and Settings\406\桌面\c#_GDI\FFT\Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files\ScriptResource(1).axd"
type=text/javascript></SCRIPT>
<SCRIPT
src="C:\Documents and Settings\406\桌面\c#_GDI\FFT\Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files\ScriptResource(2).axd"
type=text/javascript></SCRIPT>
<SCRIPT
src="Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files/js"
type=text/javascript></SCRIPT>
<SCRIPT type=text/javascript>
//<![CDATA[
function WebForm_OnSubmit() {
if (typeof(ValidatorOnSubmit) == "function" && ValidatorOnSubmit() == false) return false;
return true;
}
//]]>
</SCRIPT>
<DIV style="LEFT: 300px; POSITION: absolute; TOP: 20px"></DIV>
<DIV id=container>
<DIV id=header>
<DIV id=top>
<H1><A class=weblogtitle id=Header1_HeaderTitle
href="http://www.cnblogs.com/Dah/">Adrian's Tech Blog</A></H1>
<P></P></DIV>
<DIV id=navstats></DIV>
<DIV id=nav>
<UL>
<LI><A id=Header1_MyLinks1_HomeLink href="http://www.cnblogs.com/">博客园</A>
<LI><A id=Header1_MyLinks1_MyHomeLink
href="http://www.cnblogs.com/Dah/">首页</A>
<LI>
<LI><A id=Header1_MyLinks1_ContactLink accessKey=9
href="http://www.cnblogs.com/Dah/contact.aspx?id=1">联系</A>
<LI><A id=Header1_MyLinks1_Admin
href="http://www.cnblogs.com/Dah/admin/EditPosts.aspx">管理</A> </LI></UL><SPAN><A
id=Header1_MyLinks1_XMLLink href="http://www.cnblogs.com/Dah/rss"><IMG
style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px"
alt=订阅
src="Fast Fourier Transform in C# (Cooky-Turkey) - Adrian's Tech Blog - 博客园.files/xml.gif"></A> <A
id=Header1_MyLinks1_Syndication
href="http://www.cnblogs.com/Dah/rss">订阅</A></SPAN> </DIV></DIV>
<DIV class=clr></DIV>
<DIV id=content>
<SCRIPT type=text/javascript>
//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('AjaxHolder$scriptmanager1', document.getElementById('Form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tAjaxHolder$UpdatePanel1'], [], [], 90);
//]]>
</SCRIPT>
<DIV class=post>
<DIV class=posthead>
<H2><A class=singleposttitle id=AjaxHolder_ctl01_TitleUrl
href="http://www.cnblogs.com/Dah/archive/2007/08/10/850904.html">Fast Fourier
Transform in C# (Cooky-Turkey)</A> </H2></DIV>
<DIV class=postbody>C# code snippet below is an illustration of the Cooky-Turkey
algorithm, the performance may suck when processing huge datasets, but you can
use arrays of double instead of arrays of complex number structure to reduce the
performance impact by object initializations and method invocations(overloaded
operators).<BR>Furthermore, you can use "Butterfly" computation(<A
href="http://www.cmlab.csie.ntu.edu.tw/cml/dsp/training/coding/transform/fft.html">http://www.cmlab.csie.ntu.edu.tw/cml/dsp/training/coding/transform/fft.html</A>)
to gain a much better performance.<BR><BR>
<DIV
style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><SPAN
style="COLOR: #0000ff">private</SPAN><SPAN
style="COLOR: #000000"> Complex[] FFT(Complex[] input,</SPAN><SPAN
style="COLOR: #0000ff">bool</SPAN><SPAN
style="COLOR: #000000"> invert)<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000"> (input.Length </SPAN><SPAN
style="COLOR: #000000">==</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">)<BR> {<BR> </SPAN><SPAN
style="COLOR: #0000ff">return</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> Complex[] { input[</SPAN><SPAN
style="COLOR: #000000">0</SPAN><SPAN
style="COLOR: #000000">] };<BR> }<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> length </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> input.Length;<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> half </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> length </SPAN><SPAN
style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">2</SPAN><SPAN
style="COLOR: #000000">;<BR> Complex[] result </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> Complex[length];<BR> </SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN
style="COLOR: #000000"> fac </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">-</SPAN><SPAN style="COLOR: #000000">2.0</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> Math.PI </SPAN><SPAN
style="COLOR: #000000">/</SPAN><SPAN
style="COLOR: #000000"> length;<BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000"> (invert)<BR> {<BR> fac </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">-</SPAN><SPAN
style="COLOR: #000000">fac;<BR> }<BR><BR> Complex[] evens </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> Complex[half];<BR> </SPAN><SPAN
style="COLOR: #0000ff">for</SPAN><SPAN
style="COLOR: #000000"> (</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">0</SPAN><SPAN
style="COLOR: #000000">; i </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #000000"> half; i</SPAN><SPAN
style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">)<BR> {<BR> evens[i] </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> input[</SPAN><SPAN
style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> i];<BR> }<BR> Complex[] evenResult </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> FFT(evens,invert);<BR><BR> Complex[] odds </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> evens;<BR> </SPAN><SPAN
style="COLOR: #0000ff">for</SPAN><SPAN
style="COLOR: #000000"> (</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">0</SPAN><SPAN
style="COLOR: #000000">; i </SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN
style="COLOR: #000000"> half; i</SPAN><SPAN
style="COLOR: #000000">++</SPAN><SPAN
style="COLOR: #000000">)<BR> {<BR> odds[i] </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> input[</SPAN><SPAN
style="COLOR: #000000">2</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> i </SPAN><SPAN
style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">1</SPAN><SPAN
style="COLOR: #000000">];<BR> }<BR> Complex[] oddResult </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> FFT(odds,invert);<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">for</SPAN><SPAN
style="COLOR: #000000"> (</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> k </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -