a simple and efficient fft implementation in c++ part i

4
5/25/2014 A Simple and Efficient FFT Implementation in C++:<br> Part I | Dr Dobb's http://www.drdobbs.com/cpp/a-simple-and-efficient-fft-implementatio/199500857 1/4 Subscribe Newsletters Digital Library RSS Cloud Mobile Parallel .NET JVM Languages C/C++ Tools Design Testing Web Dev Jolt Awards C/C++ Permalink A Simple and Efficient FFT Implementation in C++: Part I By Vlodymyr Myrnyy, May 10, 2007 An efficient implementation of the Cooley-Tukey fast Fourier transform (FFT) algorithm using C++ template metaprogramming This article describes a new efficient implementation of the Cooley-Tukey fast Fourier transform (FFT) algorithm using C++ template metaprogramming. Thank to the recursive nature of the FFT, the source code is more readable and faster than the classical implementation. The efficiency is proved by performance benchmarks on different platforms. Introduction Fast Fourier Transformation (FFT) is not only a fast method to compute digital Fourier transformation (DFT)— having a complexity O(Nlog(N)) (where N must be power of 2, N=2 P ), it is a way to linearize many kinds of real mathematical problems of nonlinear complexity using the idiom "divide and conquer." The discrete Fourier transform f n of the N points signal x n is defined as a sum: Example 1. where i is the complex unity. Put simply, the formula says that an algorithm for the computing of the transform will require O(N 2 ) operations. But the Danielson-Lanczos Lemma (1942), using properties of the complex roots of unity g , gave a wonderful idea to construct the Fourier transform recursively (Example 1). Example 2. where f k e denotes the k -th component of the Fourier transform of length N/2 formed from the even components of the original x j , while f k o is the corresponding transform formed from the odd components. Although k in the last line of Example 2 varies from 0 to N -1, the transforms f k e and f k o are periodic in k with length N/2 . The same formula applied to the transforms f k e and f k e reduces the problem to the transforms of length N/4 and so on. It means, if N is a power of 2, the transform will be computed with a linear complexity O(Nlog(N)) . More information on the mathematical background of the FFT and advanced algorithms, which are not limited to N=2 P , can be found in many books, e.g. [3,4]. C/C++ Recent Articles Lambdas in C++11 Understanding C++ Function Try Blocks Debugging 256 GPU Threads in Visual Studio 2013 Basic Turns 50 Debugging GPU Code in Microsoft C++ AMP Search: Site Source Code Home Articles News Blogs Source Code Dobb's on DVD Dobb's TV Webinars & Events Tweet Share Stories Blogs Most Popular Lambdas in C++11 Understanding C++ Function Try Blocks Jolt Awards: The Best Books A Simple and Efficient FFT Implementation in C++: Part I Logging In C++ View All Videos This month's Dr. Dobb's Journal

Upload: aadatti

Post on 22-Nov-2015

53 views

Category:

Documents


2 download

DESCRIPTION

Dr Dobb's

TRANSCRIPT

  • 5/25/2014 A Simple and Efficient FFT Implementation in C++: Part I | Dr Dobb's

    http://www.drdobbs.com/cpp/a-simple-and-efficient-fft-implementatio/199500857 1/4

    Subscribe

    Newsletters

    Digital Library

    RSS

    Cloud Mobile Parallel .NET JVM Languages C/C++ Tools Design Testing Web Dev Jolt Awards

    C/C++Permalink

    A Simple and Efficient FFTImplementation in C++:Part I

    By Vlodymyr Myrnyy, May 10, 2007

    An efficient implementation of the Cooley-Tukey fast

    Fourier transform (FFT) algorithm using C++ template

    metaprogramming

    This article describes a new efficient implementation ofthe Cooley-Tukey fast Fourier transform (FFT) algorithmusing C++ template metaprogramming. Thank to therecursive nature of the FFT, the source code is morereadable and faster than the classical implementation. Theefficiency is proved by performance benchmarks ondifferent platforms.

    Introduction

    Fast Fourier Transformation (FFT) is not only a fastmethod to compute digital Fourier transformation (DFT)having a complexity O(Nlog(N)) (where N must be power

    of 2, N=2P), it is a way to linearize many kinds of realmathematical problems of nonlinear complexity using theidiom "divide and conquer."

    The discrete Fourier transform fn of the N points signal xnis defined as a sum:

    Example 1.

    where i is the complex unity. Put simply, the formula saysthat an algorithm for the computing of the transform will

    require O(N2) operations. But the Danielson-LanczosLemma (1942), using properties of the complex roots of

    unity g, gave a wonderful idea to construct the Fouriertransform recursively (Example 1).

    Example 2.

    where fke denotes the k-th component of the Fourier

    transform of length N/2 formed from the even components

    of the original xj, while fko is the corresponding transform

    formed from the odd components. Although k in the last

    line of Example 2 varies from 0 to N-1, the transforms fke

    and fko are periodic in k with length N/2. The same

    formula applied to the transforms fke and fk

    e reduces the

    problem to the transforms of length N/4 and so on. Itmeans, if N is a power of 2, the transform will becomputed with a linear complexity O(Nlog(N)). Moreinformation on the mathematical background of the FFT

    and advanced algorithms, which are not limited to N=2P,can be found in many books, e.g. [3,4].

    C/C++ Recent Articles

    Lambdas in C++11Understanding C++ Function Try BlocksDebugging 256 GPU Threads in Visual Studio 2013Basic Turns 50Debugging GPU Code in Microsoft C++ AMP

    Search: Site Source Code

    Home Articles News Blogs Source Code Dobb's on DVD Dobb's TV Webinars & Events

    Tweet Share

    Stories Blogs

    Most Popular

    Lambdas in C++11Understanding C++ Function Try BlocksJolt Awards: The Best BooksA Simple and Efficient FFT Implementation in C++:Part ILogging In C++

    View All Videos

    This month's Dr. Dobb's Journal

  • 5/25/2014 A Simple and Efficient FFT Implementation in C++: Part I | Dr Dobb's

    http://www.drdobbs.com/cpp/a-simple-and-efficient-fft-implementatio/199500857 2/4

    I would like to start with the simplest recursive form of thealgorithm, that follows directly from the relation in Example2:

    FFT(x) { n=length(x); if (n==1) return x; m = n/2; X = (x_{2j})_{j=0}^{m-1}; Y = (x_{2j+1})_{j=0}^{m-1}; X = FFT(X); Y = FFT(Y); U = (X_{k mod m})_{k=0}^{n-1}; V = (g^{-k}Y_{k mod m})_{k=0}^{n-1}; return U+V;}

    This algorithm should give only a first impression of theFFT construction. The FFT(x) function is called twicerecursively on the even and odd elements of the sourcedata. After that some transformation on the data isperformed. The recursion ends if the data length becomes1.

    This recursion form is instructive, but the overwhelmingmajority of FFT implementations use a loop structure firstachieved by Cooley and Tukey [2] in 1965. The Cooley-Tukey algorithm uses the fact that if the elements of theoriginal length N signal x are given a certain "bit-scrambling" permutation, then the FFT can be carried outwith convenient nested loops. The scrambling intended isreverse-binary reindexing, meaning that xj gets replaced

    by xk, where k is the reverse-binary representation of j.

    For example, for data length N=25, the element x5 must be

    exchanged with x{20}, because the binary reversal of

    5=001012 is 101002=20. The implementation of this data

    permutation will be considered later, since it has been aminor part of the whole FFT. A most important observationis that the Cooley-Tukey scheme actually allows the FFT

    to be performed in place, that is, the original data x isreplaced, element by element, with the FFT values. This isan extremely memory-efficient way to proceed with largedata. Listing One shows the classical implementation ofthe Cooley-Tukey algorithm from Numerical Recipes in C++

    [5], p.513.

    Listing One

    void four1(double* data, unsigned long nn){ unsigned long n, mmax, m, j, istep, i; double wtemp, wr, wpr, wpi, wi, theta; double tempr, tempi;

    // reverse-binary reindexing n = nnm) { j -= m; m >>= 1; } j += m; };

    // here begins the Danielson-Lanczos section mmax=2; while (n>mmax) { istep = mmax>

    More >>

    Featured Reports

    Strategy: The Hybrid Enterprise Data CenterSaaS and E-Discovery: Navigating Complex WatersResearch: Federal Government Cloud ComputingSurveySaaS 2011: Adoption Soars, Yet DeploymentConcerns LingerResearch: State of the IT Service Desk

    Featured Whitepapers

    DDoS and Downtime - Considerations for RiskManagementBuild a Business Case: Developing Custom AppsA Practical Guide to Understand and Benefit fromthe Consumerization of ITAdvanced Endpoint and Server ProtectionData Quality Issues in the ConfigurationManagement Database (CMDB)

    This month, we focus in on Testing, outlining therelationship between testability and good design. Plus,two lightweight testing frameworks make it easy to unittest C code. Enjoy!, and much more!

    Download the latest issue today. >>

    Live Events WebCasts

    Upcoming Events

    Come to Interop New York, Sept 29 - Oct 3, 2014 -Interop New YorkJoin the Conference for 4G/LTE Professionals atCTIA - 4G WorldLearn to Maximize Your Next-Gen NetworkInvestments - 4G WorldEnterprise Connect Lync Tour - EnterpriseConnect Lync TourTower & Small Cell Summit - Tower & Small CellSummit

    More Live Events>>

    Digital Issues

    Most Recent Premium Content

  • 5/25/2014 A Simple and Efficient FFT Implementation in C++: Part I | Dr Dobb's

    http://www.drdobbs.com/cpp/a-simple-and-efficient-fft-implementatio/199500857 3/4

    data[i-1] += tempr; data[i] += tempi; } wtemp=wr; wr += wr*wpr - wi*wpi; wi += wi*wpr + wtemp*wpi; } mmax=istep; }}

    The initial signal is stored in the array data of length2*nn, where each even element corresponds to the realpart and each odd element to the imaginary part of acomplex number.

    1 2 3 Next

    Related Reading

    NewsCommentary

    SAP's Third Iteration Mobile Developer PlayTesting So Good It Could Change A Release

    Cycle?Free Facebook Testing For FbStartDevelopersOnsen CSS Components For UICustomization

    More News

    SlideshowVideo

    Jolt Awards 2014: The Best Programmer

    LibrariesJolt Awards: Coding ToolsJolt Awards 2013: The Best ProgrammerLibrariesJolt Awards: Utilities

    More Slideshows

    Most Popular

    Developer Reading List: The Must-HaveBooks for JavaScriptA Simple and Efficient FFT Implementation inC++:

    Part IMongoDB with C#: Deep DiveState Machine Design in C++More Popular

    More InsightsWhite Papers

    Consolidation: The Foundation for IT BusinessTransformationSelect the Right Cloud-Based ITSM Solution

    More >>

    Reports

    Research: State of the IT Service DeskDatabase Defenses

    2014Dr. Dobb's Journal

    May - TestingFebruary - Languages

    Dr. Dobb's Tech DigestWindows and .NET programmingThe Design of Messaging Middleware and 10 Tips fromTech WritersParallel Array Operations in Java 8 and Android on x86:Java Native Interface and the Android NativeDevelopment Kit

    2013January - Mobile DevelopmentFebruary - Parallel ProgrammingMarch - Windows ProgrammingApril - Programming LanguagesMay - Web DevelopmentJune - Database DevelopmentJuly - TestingAugust - Debugging and Defect ManagementSeptember - Version ControlOctober - DevOpsNovember- Really Big DataDecember - Design

    2012January - C & C++February - Parallel ProgrammingMarch - Microsoft TechnologiesApril - Mobile DevelopmentMay - Database ProgrammingJune - Web DevelopmentJuly - SecurityAugust - ALM & Development ToolsSeptember - Cloud & Web DevelopmentOctober - JVM LanguagesNovember - TestingDecember - DevOps

    2011

  • 5/25/2014 A Simple and Efficient FFT Implementation in C++: Part I | Dr Dobb's

    http://www.drdobbs.com/cpp/a-simple-and-efficient-fft-implementatio/199500857 4/4

    Pow ered by Zend/PHP

    FEATURED UBM TECH SITES: InformationWeek | Network Computing | Dr. Dobb's | Dark Reading

    OUR MARKETS: Business Technology | Electronics | Game & App Development

    Working With Us: Advertising Contacts | Event Calendar | Tech Marketing Solutions | Corporate Site | Contact Us / Feedback

    Terms of Service | Privacy Statement | Copyright 2014 UBM Tech, All rights reserved

    INFO-LINK

    Login or Register to Comment

    More >>

    Webcasts

    Big Data Analytics: Are You Ready?HTML5 Roundtable Series: Inside the Brackets

    More >>

    Dr. Dobb's Home Articles News Blogs Source Code Dobb's on DVD Dobb's TV Webinars & Events

    About Us Contact Us Site Map Editorial Calendar