2rnroor6 ‘The Fourier Transform Part XIV— FFT Algorithm |
Home
Services
THE'FOURIER
TRANSFORM PART XIV —
FFT ALGORITHM
Home / The Fourier Transform
Demos
The Fourier Transform Part XIV — FFT Algorithm
Filming is currently underway on a special online course based on this
blog which will include videos, animations and work-throughs to
illustrate, in a visual way, how the Fourier Transform works, what all the
math is all about and how it is applied in the real world.
6 \
The module will be emailed to you the moment the course goes live.
itplwwrwthemebilestuso,nethe-fouror ransform-par-14
Podcast
PROFESSIONAL RECORDINGS=""=!
FROM THE COMFORT OF YOUR GWNiHOME f
Blog
THE MOBILE STUDIO
LINK TO PODCAST 0}
RSS FEEDS
Podcast RSS feed
Blog RSS feed
RECENT POSTS
Euler's Identity
The Fnuiriar Tranefnrm
wm2rnroor6 ‘The Fourier Transform Part XIV— FFT Algorithm |
In this post, we're going to develop an algorithm to implement all that
m going Pan aig P FFT Calculator
we have learned in the last few posts about the FFT. In developing this,
algorithm, I've started from the smallest part of the computation | could The Fourier Transform
think of and worked outwards, The basic building block of the FFT is the FET Algorithm
“Butterfly” calculation, This calculation is iterated many times over the
course of the FFT. The Fourier Transform
Numerical Example
The snippets of code that appear in this post are written in Javascript.
The Fourier Transform
Before we start, let's define some terms:
eee ae re Get Google
ae a Pit : Ch:
fag tse) tape (eon) = 0 eR Me : rome
= y= acc No at, A fast, secure, a
a ren 3k : faa browser ex all 5
ae eae es emis) fc devices. Downk
a nl Hh nse: tin) ton) (rien) now!
aan sot Seton) ‘
google.com
Any size of FFT will be broken down into stages. For example, I've
shown a 16-point FFT in the diagram above. The number of stages can
be calculated by the following formula:
log(F FT Size)
NumberO f Stages = 224 ==?)
Tog(2)
If you plug the number 16 into the FFTSize of the formula above youl
find that there are 4 stages required to calculate the FFT as shown in
the diagram above. These stages are numbered 0-3.
Each stage of the calculation has a number of groups of butterflies in it
In stage 0, there are 8 groups, each group containing 2 samples being
fed into 1 butterfly. Blog RSS feed
In stage 1, there are 4 groups, each group containing 4 samples being
fed into 2 overlapped butterflies. RECENT POSTS
In stage 2, there are 2 groups, each group containing 8 samples being Euler's Identity
fed into 4 overlapped butterflies,
The Fourier Tranefnem
pu themebilestsonete-fasir-rarstorm-par-4 an2rnroor6 ‘The Fourier Transform Part XIV— FFT Algorithm |
and so on.
The number of samples in each group can be calculated by the
following formula:
NumberOf Samples = 2StageIndex+1
The number of groups in each stage can be calculated by the following
formula:
FFTS:
pStagelnde
NumberO {Groups =
Each butterfly takes two samples and adds them together for the first
term and subtracts them for the second term. Things are complicated
slightly (but not much) by the fact that the second sample needs to be
multiplied by a twiddle factor. This addition and subtraction repeats itself
constantly throughout the computation of the FFT which means we are
going to reuse the piece of code that does it a lot so it makes sense to
program it into it's own function. The generalized butterfly for any
sample pair in the FFT can be calculated as follows.
xis the input sample
F is the output frequency term
FFTSize is the total number of samples going into the FFT
nis the sample index (0, 1, 2, 3, ..., FFTSize-1)
Nis the number of samples in each group of the current stage of the
FFT
kis the order of the twiddle factor within each group ( 0, 1, 2, 3,
(N/2)-1 )
Wis the twiddle factor
itplwwrwthemebilestuso,nethe-fouror ransform-par-14
FFT Calculator
The Fourier Transform
FFT Algorithm
The Fourier Transform
Numerical Example
The Fourier Transform
Get Google
Chrome
A fast, secure, a
browser ex all 5
devices. Downk
now!
google.com
Blog RSS feed
RECENT POSTS
Euler's Identity
The Fnuriar Tranefnrm
am2vron016 ‘The Furie Transform PartXIV-FFT Ago
However, although the butterfly lies right at the heart of the
calculation, it itself is made up of 3 even more basic calculations. These
are one complex add (to add the 2 samples together), one complex
subtract (to subtract the 2 samples one from each other) and one
complex multiply (to multiply the second sample by the twiddle factor).
So we are first going to develop 3 functions to do each of
these 3 operations. Throughout the whole algorithm, I'm going to define
my own data structure to store the complex numbers used throughout
the calculation. This is a structure made up of 2 floating point numbers,
one for the Real term and one for the Imaginary term as follows:
1{ var ComplexNumber = {Real: @, Imaginary: 0};
So lets start simple and write the function which adds together 2
complex numbers. To do this we need to add the the real and imaginary
terms separately.
function ComplexAdd(ComplexNumber®,
ComplexNumber1)
{
var ComplexResult = {Real: @, Imaginary: 0};
ComplexResult.Real = ComplexNumber@.Real +
ComplexNumber1. Real;
ComplexResult . Imaginary =
ComplexNumber®. Imaginary +
ComplexNumber1. Imaginary;
wanvauaunr
return ComplexResult;
}
The function accepts 2 complex numbers (ComplexNumber0
and ComplexNumbert), adds together the real and imaginary parts
separately and retums the result (ComplexResul)
Writing the subtract function is just as simple. We only need to change
the addition sign to a subtraction sign:
function ComplexSubtract (ComplexNumberd,
ComplexNumber1)
{
var ComplexResult = {Real: @, Imaginary: 0};
ComplexResult.Real = ComplexNumber@.Real -
ComplexNumber1. Real;
ComplexResult. Imaginary =
ComplexNumberd. Imaginary -
ComplexNumber1. Imaginary;
waVansune
itplwwrwthemebilestuso,nethe-fouror ransform-par-14
FFT Calculator
The Fourier Transform
FFT Algorithm
The Fourier Transform
Numerical Example
The Fourier Transform
Get Google
Chrome
A fast, secure, a
browser peg all 5
devices. Downk
now!
google.com
Blog RSS feed
RECENT POSTS
Euler's Identity
The Enuriar Tranefnrm
ant2rnroor6 ‘The Fourier Transform Part XIV— FFT Algorithm |
return ComplexResult
}
The third function is the complex multiply function. Remember that
multiplying two complex numbers is like multiplying 2 brackets. We have
to use the FOIL method I mentioned in the previous post.
@1| function ComplexMultiply(ComplexNumbero,
@2 | ComplexNumber1)
e3| {
e4 var ComplexResult = {Real: @, Imaginary: 0};
5 var First;
6 var Outside;
7 var Inside;
e8 var Last;
e9
18 // First - Produces real result
cel First = ComplexNumber@.Real *
12 | ComplexNumber1.Real;
B // Outside - Produces imaginary result
4 Outside = ComplexNumber@.Real *
15 | ComplexNumber1. Imaginary;
16 // Inside - Produces imaginary result
v7 Inside = ComplexNumber@. Imaginary *
18 | ComplexNumber1.Real;
19 // Last - Produces real result multiplied by
20) i-squared (i-squared = -1)
21 Last = -1 * ComplexNumber@. Imaginary *
22| ComplexNumber1. Imaginary;
ComplexResult.Real = First + Last;
ComplexResult .Imaginary = Inside + Outside;
return ComplexResult;
}
Now that we have the 3 mathematical operations, we can write the code
for the butterfly. The butterfly has to add together the first and
second samples to produce the first frequency term and subtract the
second sample from the first to produce the second frequency term
The second sample is always multiplied by the relevant twiddle factor.
Therefore the inputs to the function are the two samples and the twiddle
factor. All of these are complex numbers. The function returns an
array containing the two frequency terms, again as complex numbers.
@1| function Butterfly(Sample@, Sample1,
@2| TwiddleFactor)
e3| {
e4 var Frequency=[];
5 var TwiddledSample1=Complexmultiply(Sample1,
@6 | TwiddleFactor) ;
7
e8 Frequency[@] = ComplexAdd (Sampled,
e9 | TwiddledSample1);
tslwwrthemebilestuso,netthe-fouer ransform-par-14
FFT Calculator
The Fourier Transform
FFT Algorithm
The Fourier Transform
Numerical Example
The Fourier Transform
Get Google
Chrome
A fast, secure, a
browser ex all 5
devices. Downk
now!
google.com
Blog RSS feed
RECENT POSTS
Euler's Identity
The Fnuiriar Tranefnrm
amt2rnroor6 ‘The Fourier Transform Part XIV— FFT Algorithm |
10] Frequency[1] = ComplexSubtract(Sample®,
TwiddledSample1) ; FFT Calculator
return Frequency;
} The Fourier Transform
Next we need to have a function which calculates the twiddle factors. FFT Algorithm
The number of twiddle factors depends on which stage of the
The Fourier Transform
calculation we are currently doing and can be calculated by raising 2 to
Numerical Example
the power of the index of the current stage. So, for example, if we are in
the second stage of the FFT the stage index will be equal to 1 The Fourier Transform
(remember the index starts from zero) and there will be 2 twiddle factors
(2! = 2), Therefore we need to have a “Stageindex’ at the input to the
function to tell it which stage of the calculation we need the twiddle Get Google
factors for. The functions then returns an array of complex numbers Chrome
which are the twiddle factors. The twiddle factors are calculated using
the following formula: A fast, secure, a
i browser gey all
WE = co: ‘
‘ devices. Downk
now!
where:
google.com
Nis the number of samples in each group of the current stage of the
FFT
kis the order of the twiddle factor within the current group.
For the second stage of the FFT, k will range between 0 and 1
@1{| function CalculateTwiddleFactors(StageIndex)
e2| {
@3 var NumberOfTwiddleFactors =
04 | Math.pow(2,StageIndex) ;
eS var TwiddleFactors=Array(NumberOfTwiddleFactor
@6 var NumberOfSamples = Math. pow(2,StageIndex+1)
e7 var i;
8
89 for (4=0; isNumberOfTwiddleFactors; i++)
18 {
crn var ComplexNunber = {Real: ®, Imaginary: | Blog RSS feed
12
13 ComplexNumber.Real = Math.cos(2 * Math.PI
14) i / NumberofSamples) ;
15 ComplexNunber. Imaginary = -1 * Math.sin(2_ RECENT POSTS
16 | Math.PI * i / NumberOfSamples) ;
v7 TwiddleFactors[i] = ComplexNumber;
18 } Euler's Identity
return TwiddleFactors;
The Fnuiriar Tranefnrm
itplwwrwthemebilestuso,nethe-fouror ransform-par-14 ant2rnroor6 ‘The Fourier Transform Part XIV— FFT Algorithm |
Now we've got the butterfly sorted out with all its twiddle factors, we
need to prepare the samples that are going to be input into the first
stage of the FFT. Remember the samples have to be placed in a special
order, a bit reversed order, not the order they occur in naturally.
Therefore the following function will reorder them for us:
@1| function BitReversal(SamplesIn, NumberOfSamples)
e2| {
@3 var NewOrder;
4 var Samplesout=[]3
5 var NumberOfBits;
6 var SampleIndex;
7
e8 NumberOfBits = Math.log(NumberofSamples) /
e9 | Math. 1og(2);
18
n for (SampleIndex-0;
12| SampleIndex>
SHARE THIS:
Tags: Algorithm Complex Numbers Cosine Waves Fast Fourier Transform
FFT Fourier Fourier Series. Fourier Transform French mathematician
Javascript Joseph Fourier Phase Sine Waves
1 Comment Sort by
Add a comment,
William Mitchell : Shamanic Practitioner at Self-Employed
ilove the fact that memory is no longer a problem. in the 70's the chalk
diung a 1K FFT within 1K of memory was interesting and possible. usin
of hardware and software to produce teh required addressing. the com
created as MOSAIC with Dick Zobel as a supervisor
Like - Reply - May 25, 2016 6:20am
Mark Newman - Owner at Mobile Studio
‘When I was at University in Manchester (a few years ago now)
reconstructed one of the early computers with the old valve me
the Manchester University Computing department. It was quite
stunning to think that the smartphone in my pocket nowadays |
computing power than that huge machine did.
Like « Reply : Jun 6, 2016 3:51am
Ei Facebook Comments Phigin
itplwerw.hembilestasosneite-foue ransform-par-14
FFT Calculator
The Fourier Transform
FFT Algorithm
The Fourier Transform
Numerical Example
The Fourier Transform
Get Google
Chrome
A fast, secure, a
browser peg all
devices. Downk
now!
google.com
Blog RSS feed
RECENT POSTS.
Euler's Identity
The Enuiriar Tranefnrm
nn