Sunteți pe pagina 1din 6

Proceedings of the 5th National Conference; INDIACom-2011

Computing For Nation Development, March 10 – 11, 2011


Bharati Vidyapeeth‘s Institute of Computer Applications and Management, New Delhi
Generation of 1D and 2D FFT Function in MATLAB
Parul Goyal
Associate Professor
Department of Electronics & Communication Engineering,
Uttaranchal Institute of Technology, Dehradun - 248001, Uttarakhand, India
parulgoyal1973@gmail.com

ABSTRACT
This paper proposes the generation of the code for the X(n) = k=0 N-1 x(k)e-j2nk/N n=0, 1,…..N-1
algorithm of 1D and 2D FFT and the methods for the (1.1)
recognition of faces using various methods of Joint Transform
Correlation techniques. Various codes were written in Eq (1.1) describes the computation of N equations. For eg. If
MATLAB for the correlation and recognition of various N=4 and we let
images. Comparison between DFT and FFT Computation
Speeds is made in this paper From results it is evident that for W= e-j2n/N
a couple of samples of various lengths the time taken by normal (1.2)
DFT is higher than the time taken by FFT. Thus the speed of Then Eq (1.1) can be written as
FFT is beneficial when large calculations are required.
X (0) = x (0) W0 + x (1) W0 + x (2) W0 + x (3) W0
1. INTRODUCTION
FFT is simply an algorithm (i.e., a particular method of X (1) = x (0) W0 + x (1) W1 + x (2) W2 + x (3) W3
performing a series of computations) that can compute the
discrete Fourier transform much more rapidly than other X (2) = x (0) W0 + x (1) W2 + x (2) W4 + x (3) W6
available algorithms. For this reason, our discussion of the FFT (1.3)
addresses only the computational aspect of the algorithm. A
DFT decomposes a sequence of values into components of X (3) = x (0) W0 + x (1) W3 + x (2) W6 + x (3) W9
different frequencies. This operation is useful in many fields
(see discrete Fourier transform for properties and applications The above equations can be represented in the matrix form.
of the transform) but computing it directly from the definition
is often too slow to be practical. An FFT is a way to compute 3. PROPOSED ALGORITHM
the same result more quickly: computing a DFT of N points in By far the most common FFT is the Cooley-Tukey algorithm.
the obvious way, using the definition, takes O(N 2) arithmetical This is a divide and conquer algorithm that recursively breaks
operations, while an FFT can compute the same result in only down a DFT of any composite size N = N1N2 into many smaller
O(N log N) operations. DFTs of sizes N1 and N2, along with O(N) multiplications by
The difference in speed can be substantial, especially for long complex roots of unity traditionally called twiddle factors (after
data sets where N may be in the thousands or millions—in Gentleman and Sande, 1966). This method (and the general
practice, the computation time can be reduced by several orders idea of an FFT) was popularized by a publication of J. W.
of magnitude in such cases, and the improvement is roughly Cooley and J. W. Tukey in 1965, but it was later discovered
proportional to N/log(N). This huge improvement made many (Heideman & Burrus, 1984) that those two authors had
DFT-based algorithms practical; FFTs are of great importance independently re-invented an algorithm known to Carl
to a wide variety of applications, from digital signal processing Friedrich Gauss around 1805 (and subsequently rediscovered
and solving partial differential equations to algorithms for several times in limited forms).
quick multiplication of large integers. A simple matrix- The most well-known use of the Cooley-Tukey algorithm is to
factoring example is used to intuitively justify the FFT divide the transform into two pieces of size N / 2 at each step,
algorithm. The factored matrices are alternatively represented and is therefore limited to power-of-two sizes, but any
by signal flow graphs. From these graphs, we construct the factorization can be used in general (as was known to both
logic of an FFT computer program theoretical development of Gauss and Cooley/Tukey). These are called the radix-2 and
various forms of the FFT algorithm. mixed-radix cases, respectively (and other variants such as the
split-radix FFT have their own names as well). Although the
basic idea is recursive, most traditional implementations
2. MATRIX FORMULATION rearrange the algorithm to avoid explicit recursion. Also,
The discrete Fourier transform is given by: because the Cooley-Tukey algorithm breaks the DFT into

Copy Right © INDIACom-2011 ISSN 0973-7529 ISBN 978-93-80544-00-7


Proceedings of the 5th National Conference; INDIACom-2011

smaller DFTs, it can be combined arbitrarily with any other


algorithm for the DFT, such as those described below.
FFT ALGORITHMS SPECIALIZED FOR REAL AND/OR
SYMMETRIC DATA:
In many applications, the input data for the DFT are purely
real, in which case the outputs satisfy the symmetry

and efficient FFT algorithms have been designed for this


situation (see e.g. Sorensen, 1987). One approach consists of
taking an ordinary algorithm (e.g. Cooley-Tukey) and
removing the redundant parts of the computation, saving
roughly a factor of two in time and memory. Alternatively, it is
possible to express an even-length real-input DFT as a complex
DFT of half the length (whose real and imaginary parts are the Figure (b)
even/odd elements of the original real data), followed by O(N) STEPS INVOLVING IN THE COMPUTATION OF THE
post-processing operations. FFT:
It was once believed that real-input DFTs could be more The FFT of an input sequence of N samples is given by
efficiently computed by means of the discrete Hartley X(n)= WP x(k)
transform (DHT), but it was subsequently argued that a (1.5)
specialized real-input DFT algorithm (FFT) can typically be Here X(n ) is the FFT, WP is the twiddle factor and x(k) is the
found that requires fewer operations than the corresponding value of the input sequence.
DHT algorithm (FHT) for the same number of inputs. Bruun's For the computation of the above equation the value of the
algorithm (above) is another method that was initially proposed Twiddle Factor must be known
to take advantage of real inputs, but it has not proved popular. WP = e-j2p/N
There are further FFT specializations for the cases of real The value of p is determined by following the steps as
data that have even/odd symmetry, in which case one can gain explained under:
another factor of (roughly) two in time and memory and the (a) Writing the index k in binary form with  bits where  is the
DFT becomes the discrete cosine/sine transform(s) power of 2 corresponding to the value of N.
(DCT/DST). Instead of directly modifying an FFT algorithm (b) Scaling or sliding this binary number -l bits to the right and
for these cases, DCTs/DSTs can also be computed via FFTs of filling in the newly opened bit position on the left with zeros,
real data combined with O(N) pre/post processing. Here l is the value determining the computational array which
SIGNAL FLOW GRAPH: varies from 1 to .
The signal flow graph shown below is given for 8 samples. The (c) Then reversing the order of the bits. This bit reversed
figure shows the various computation arrays represented by ‗l‘ number is then converted to decimal. This number is the value
and it also represents the nodes to be skipped. The logic of of p.
skipping nodes is done to reduce the number of computation. After the calculation of the values of X(n) the values are
As we can see from the signal flow graph that the value of x1(8) unscrambled as they are obtained in reversed order according to
and x1(0) can be calculated from the same set of inputs the Cooley tukey algorithm.
according to eq(1.4) shown below. As we can see that each To unscramble the output the vector X(n) is to write n in
node has a dual node(a dual node is a node which has the same binary and reverse or flip the binary number. We show in fig
set of inputs)eg x(0) and x(8) are dual nodes which are (a)_ the results of this bit reversing operation : terms x(k) and
separated by N/2l. Hence is xl(k) is a node then its dual node is x(i) have been interchanged , where I is the integer obtained by
xl(k + N/2l ).The computation of any dual node pair is done by bit reversing the integer k.
xl(k) = xl-1(k) + WP xl-1(k + N/2l ). Note that a situation occurs when we encounter a node that has
xl(k + N/2l )= xl-1(k) - WP xl-1(k + N/2l ). previously been interchanged. For example in Fig (b1) node
(1.4) k=0 remains in its location, nodes k=1, 2, 3 are interchanged
For the signal flow array we can see that as we proceed with nodes 8, 4 and 12 respectively. The next node to be
downward for the computational array l=1 starting from node considered is node 4, but this node was previously been
k=0 for node k=4 we see that we have already computed the interchanged with node 2. to eliminate the possibility of
value of that node and it can be skipped. Similarly other nodes considering a node that has previously been interchanged, we
can also be skipped (k= 5, 6, 7). Thus we only compute for the simply check to see if i(the integer obtained by bit reversing k)
1st N/2l nodes and skip the next N/2l nodes. We know to stop is less than k. if so, this implies that the node has been
skipping when we reach a node index grater than N-1. interchanged by a previous operation . With this check, we can
ensure a straight forward unscrambling procedure.
Considering the input sequence of 16 elements

Copy Right © INDIACom-2011 ISSN 0973-7529 ISBN 978-93-80544-00-7


Generation of 1D and 2D FFT Function in MATLAB

FFT Computation Flowchart: vector x(k) is assumed to be complex and is indexed as k=0,1,
Using the discussed properties of the FFT we can easily … N-1. if x(k) is real then the imaginary part should be set to
develop a flowchart for programming the algorithm on a digital zero. The number of sample points must satisfy the relationship
computer. N= 2 where  is integer valued.
We know from the previous discussions that we first compute Initialization of the various program parameters is
array l=1 by starting at node k=0 and working down the array. accomplished in Box 2. Parameter l is the array number being
At each node k, we compute the pair of eq(1.4) where p is considered. We start with array l=1,N2= N/2 is initialized as
determined by the described procedure. We continue down the such. Parameter NU1 is the right shift required when
array computing the equation pair of eq(1.4) until we reach a determining the value of p in eq(1.4); NU1 is initialized to -1.
region of nodes that must be skipped over. We skip over the The index k of the array is initialized to k=0 ; thus we will
appropriate nodes and continue until the new have computed work from the top and progress down the array.
the entire array. We then proceed to compute the remaining
arrays using the same procedures. Finally, we unscramble the
final array to obtain the output.

Figure(c)
Box3 checks to see if the array l to be computed is greater than
. If yes, then the program branches to box23 to unscramble the
computed results by bit inversion. If all arrays have not been
computed, then we proceed to Box 4. Box 4 sets a counter I=1.
This counter monitors the number of dual node pairs that have
been considered. Since it is necessary to skip certain nodes in
order to ensure that previously considered nodes are not
encountered a second time, Counter I is the control for
determining the when the program must skip. Boxes 5 and 6
perform the computation of eq(1.4). Because k and I have been
initialized to 0 and 1, respectively, the initial node considered is
the first node of the first array. To determine the factor p for
this node, we must first scale the binary number k to the right
by -1 bits. To accomplish this, we compute the integer value
of k/ 2NU1 and set the result to M as shown in box 5. According
to the procedure for determining the p, we must bit reverse M,
where M is represented by  = NU bits. The function IBR(M)
Figure (b1) denoted by box 5 is a special function routine for bit inversion.
Figure (c) represents the flowchart of the computer
programming. Box1 describes the necessary input data. Data

Copy Right © INDIACom-2011 ISSN 0973-7529 ISBN 978-93-80544-00-7


Proceedings of the 5th National Conference; INDIACom-2011

Box 6 is the computation of eq (1.4). We compute the product l=l+1;


WP xl-1(k + N2 ) and assign the result to a temporary storage N2=N2/2;
location. Next, we add and subtract this term according to eq NU1=NU1-1;
(1.4). The result is the dual output. We then proceed down the k=0;
array according to the next node. As shown in the Box 7, k is end
incremented by 1Toavoidrecomputing a dual node that has [y,ic]=bitrevorder(x);
been considered previously, we check Box 8 to determine if the
counter I is equal to N2. For array 1, the number of nodes that V. Results:
can be considered consecutively without skipping is equal to
N/2 = N2. Box 8 determines this condition. If I is not equal to Considering and input sequence of N= 8 samples. The FFT of
N2 then we proceed down the array and increment the counter the sequence can be calculated using the above function in
I, as shown in Box 9. Since we have already incremented k in MATLAB as shown
Box 7. Boxes 5 and 6 are then repeated for the new value of k. x=[1 2 3 4 5 6 7 8]; %%%%Generates an input
if I=N2 in box 8, then we know that we have reached a node sequence%%%%
previously considered. We then skip N2 nodes by setting k=k + y= FFT_func(x);%%%% Takes the FFT of the
N2. Because k has already been incremented by 1 in Box 7, it is sequence%%%%
sufficient to skip the previously considered nodes by disp(y)%%%%Displays the output%%%%
incrementing k by N2. Before we perform the required Then the output is displayed as
computations indicated by Boxes 5 and 6 we must first check to Columns 1 through 6
see that we have not exceeded the array size. As shown in Box 36.0000 -4.0000 + 9.6569i -4.0000 + 4.0000i -
11, if k is less than N-1 then we reset the counter I to 1 in Box 4 4.0000 + 1.6569i -4.0000 -4.0000 - 1.6569i
and repeat Boxes 5 and 6. If k> N-1 in Box 11, we show that Columns 7 through 8
we must proceed to the next array. Hence, as shown in Box 12, -4.0000 - 4.0000i -4.0000 - 9.6569i
l is indexed by 1. The new spacing N2 is simply N2/2. NU1 is Multidimensional FFTS:
decremented by 1 and k is reset to zero. We then check Box 3 As defined in the multidimensional DFT article, the
to see if all arrays have been computed. If so then we proceed multidimensional DFT
to unscramble the final results. This operation is performed by
Box 13.
Code for 1-D FFT function:
The code for 1-D FFT is written in MATLAB as shown under.
function y = FFT_func(x) transforms an array with a d-dimensional vector of indices
a=size(x); by a set of d nested summations
N=a(2);
n=log2(N); (over for each j), where the division
l=1; , defined as ,
N2=N/2; is performed element-wise. Equivalently, it is simply the
NU1=n-1; composition of a sequence of d sets of one-dimensional DFTs,
k=0; performed along one dimension at a time (in any order).
while(l<=n) This compositional viewpoint immediately provides the
while(k<=N-1) simplest and most common multidimensional DFT algorithm,
I=1; known as the row-column algorithm. That is, one simply
while(I<=N2) performs a sequence of d one-dimensional FFTs (by any of the
M=fix(k/(2^NU1)); above algorithms): first you transform along the n1 dimension,
b=dec2bin(M,n); then along the n2 dimension, and so on (or actually, any
q=seqreverse(b); ordering will work). This method is easily shown to have the
P=bin2dec(q); usual O(NlogN) complexity, where
W=exp(-2*i*P*pi/N);
T1=W*(x(k+1+N2));
is the total number of data points transformed. In particular,
x(k+1+N2)=x(k+1)-T1;
there are N / N1 transforms of size N1, etc., so the complexity of
x(k+1)=x(k+1)+T1;
the sequence of FFTs is:
k=k+1;
I=I+1;
In two dimensions, the can be viewed as an
end
matrix, and this algorithm corresponds to first
k=k+N2;
performing the FFT of all the rows and then of all the columns
end
(or vice versa), hence the name.

Copy Right © INDIACom-2011 ISSN 0973-7529 ISBN 978-93-80544-00-7


Generation of 1D and 2D FFT Function in MATLAB

In more than two dimensions, it is often advantageous for cache y=x(k+1,:);


locality to group the dimensions recursively. For example, a x(k+1,:)=FFT_func(y);
three-dimensional FFT might first perform two-dimensional end
FFTs of each planar "slice" for each fixed n1, and then perform for m=0:C-1 %%%%%Column wise FFT%%%%%
the one-dimensional FFTs along the n1 direction. More y(1,:)=x(:,m+1);
generally, an asymptotically optimal cache-oblivious algorithm x(:,m+1)=FFT_func(y);
consists of recursively dividing the dimensions into two groups end
%%%%%%Display the output%%%%%
and that are disp(x);
transformed recursively (rounding if d is not even). Still, this
remains a straightforward variation of the row-column Output:
algorithm that ultimately requires only a one-dimensional FFT
algorithm as the base case, and still has O(NlogN) complexity. Let us consider an input sequence of dimension 4x4. Then the
Yet another variation is to perform matrix transpositions in 2D-FFT of the sequence can be calculated by:-
between transforming subsequent dimensions, so that the
transforms operate on contiguous data; this is especially x=[1 2 3 4;1 2 3 4;1 2 3 4;1 2 3 4] %%%%%Generation of a
important for out-of-core and distributed memory situations 4x4 matrix%%%%%
where accessing non-contiguous data is extremely time-
consuming. y=FFT2_func(x); %%%%%Computation of 2D FFT%%%%%
There are other multidimensional FFT algorithms that are disp(y) %%%%%Display the output%%%%%
distinct from the row-column algorithm, although all of them
have O(NlogN) complexity. Perhaps the simplest non-row- Then the output will be shown as:
column FFT is the vector-radix FFT algorithm, which is a
generalization of the ordinary Cooley-Tukey algorithm where 40.00 -8.00 + 8.00i -8.00 -8.00 - 8.00i
one divides the transform dimensions by a vector 0 0 0 0
of radices at each step. The 0 0 0 0
simplest case of vector-radix is where all of the radices are 0 0 0 0
equal (e.g. vector-radix-2 divides all of the dimensions by two),
but this is not necessary. Vector radix with only a single non- VI. Conclusion:
unit radix at a time, i.e. , Comparison between DFT and FFT Computation Speeds:
is essentially a row-column algorithm.
Algorithm for 2D-FFT:
N Time taken in DFT Time taken in FFT
The steps involved in the computation of a 2 dimensional 128 0.2187s 0.0312s
sequence of dimension
N x N (where N=2) are as follows:- 256 0.5781s 0.0625s
512 2.1094s 0.09375s
(a) First the row wise FFT is computed and the output is
arranged row wise. Table 1
(b) Then the column wise FFT is computed and the elements
are arranged column wise. Here N is the number of input samples.

This gives us the output. Hence we can see that for the From table 1 we can see that for a couple of samples of various
computation of 2D- FFT we take the 1D-FFT twice. lengths the time taken by normal DFT is higher than the time
taken by FFT.
Code For 2D-FFT Function: Thus the speed of FFT is beneficial when large calculations are
required.
The code for a 2D FFT function is generated in MATLAB is
shown below. REFERENCES
[1] M.S. Alam, O. Perez and M. A. Karim ―Preprocessed
function x =FFT2_func(x) multiobject joint transform correlators,‖ Appl. Opt. 32(17),
a=size(x); 3102-3107(1993).
R=a(1); [2] S. Zhong, j. Jiang, S. Liu, and C. Li. ―Binary joint transform
C=a(2); correlators based in differential processing of the joint
for k=0:R-1 %%%%%Row wise FFT%%%%%

Copy Right © INDIACom-2011 ISSN 0973-7529 ISBN 978-93-80544-00-7


Proceedings of the 5th National Conference; INDIACom-2011

transform power spectrum,‖ Appl. Opt. 36(8), 1776-


1780(1997).
[3] S. Pati and K. Singh, ―Illumination sensitivity of joint
transform correlators using differential processing: computer
simulation and experimental studies,‖ Opt. Comm. 147, 26-
32(1998).
[4] G. Unnikrishnan, J. Joseph, and K. Singh, ―A non zero
order joint transform correlators for space variant pattern
recognition‖ Opt. Comm. 171,149-158(1999)
[5] F. T .S. Yu, and S. Jutamulia, Eds., Optical Pattern
Recognition, Cambridge University Press(1998).
[6] R. K. Wang, L. Shang, and C. R. Chatwin, ―Modified
Fringe Adjusted Joint Transform Correlation to accommodate
noise in the input scene,‖ Appl. Opt. 35(2). 286-296(1996)
[7] C. S. Weaver and J. W. Goodman, ―Technique for optically
convolving two functions,‖ Appl. Opt. 5, 1248(1966)
[8] M. S. Alam, A. A. S. Awwal, and M. A. Karim, ―Improved
correlation discrimination using joint transform optical
correlators,‖Micro. Opt. Tech. Lett. 4, 103-106(1991)
[9] F. T. S. Yu, F. Cheng, T. Nagata, and D. A. Gregory,
―Effect of fringe binarization of multi-object joint transform
correlation,‖ Appl. Opt. 28, 2988-2990(1989)
[10] www.wikipedia.com
[11] ―The Fast Fourier Transform‖ IEEE Trans. On Education
(March 1969), Vol. 12
[12] ―A guided tour of the Fast Fourier Transform‖ IEEE
Trans. on Education, Vol. 6.

Copy Right © INDIACom-2011 ISSN 0973-7529 ISBN 978-93-80544-00-7

S-ar putea să vă placă și