ECE 5655/4655 Real-Time DSP 9–1 Real-Time Fast Fourier Transform Introduction The Fourier transform is a standard system analysis tool for viewing the spectral content of a signal or sequence. The Fourier transform of a sequence, commonly referred to as the discrete time Fourier transform or DTFT is not suitable for real-time implementation. The DTFT takes a sequence as input, but pro- duces a continuous function of frequency as output. A close rela- tive to the DTFT is the discrete Fourier transform or DFT. The DFT takes a finite length sequence as input and produces a finite length sequence as output. When the DFT is implemented as an efficient algorithm it is called the Fast Fourier Transform (FFT). The FFT is ultimately the subject of this chapter, as the FFT lends itself to real-time implementation. The most popular FFT algorithms are the radix 2 and radix 4, in either a decimation in time or a decimation in frequency signal flow graph form (trans- poses of each other) DTFT, DFT, and FFT Theory Overview Frequency domain analysis of discrete-time signals begins with the DTFT, but for practicality reasons moves to the DFT and then to the more efficient FFT. Chapter 9
34
Embed
Real-Time Fast Chapter Fourier Transformmwickert/ece5655/lecture_notes/ARM/ece5655_chap9.pdftive to the DTFT is the discrete Fourier transform or DFT ... as the overlap and save method
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
er
Real-Time Fast Fourier TransformIntroduction
The Fourier transform is a standard system analysis tool forviewing the spectral content of a signal or sequence. The Fouriertransform of a sequence, commonly referred to as the discretetime Fourier transform or DTFT is not suitable for real-timeimplementation. The DTFT takes a sequence as input, but pro-duces a continuous function of frequency as output. A close rela-tive to the DTFT is the discrete Fourier transform or DFT. TheDFT takes a finite length sequence as input and produces a finitelength sequence as output. When the DFT is implemented as anefficient algorithm it is called the Fast Fourier Transform (FFT).The FFT is ultimately the subject of this chapter, as the FFTlends itself to real-time implementation. The most popular FFTalgorithms are the radix 2 and radix 4, in either a decimation intime or a decimation in frequency signal flow graph form (trans-poses of each other)
DTFT, DFT, and FFT Theory Overview
Frequency domain analysis of discrete-time signals begins withthe DTFT, but for practicality reasons moves to the DFT and thento the more efficient FFT.
Chapt
9
ECE 5655/4655 Real-Time DSP 9–1
Chapter 9 • Real-Time Fast Fourier Transform
The DTFT
• Recall that the DTFT of a sequence is simply the sum
(9.1)
• A characteristic of the spectrum is that it is a periodicfunction having period
• The frequency axis in (9.1) is in radians per sample, which isa normalized frequency variable
• If was acquired by sampling an analog signal every Tseconds, e.g., , then the frequency axes isrelated to the normalized digital frequency andthe analog frequency as follows:
The DFT and its Relation to the DTFT
• The DFT assumes that the sequence exists only on afinite interval, say , and zero otherwise
• This may occur as a result of data windowing, e.g.,
(9.2)
x n
X ej X x n e j– n
n –=
= =
X ej
2
x n x n xa nT =
f 2 =F f Fs=
Fs 20
0
–f
1 2F Hz
X ej
Fs 2–
1– 2
Fs
2
1
2–
Fs–
1–
y n 0 n N 1–
y n x n w n =
9–2 ECE 5655/4655 Real-Time DSP
DTFT, DFT, and FFT Theory Overview
where is a window function of duration N samples andzero otherwise
– Unless defined otherwise is a rectangular window,but it may be a shaped window of the same type as used inwindowed FIR filter design
• The DTFT of is
(9.3)
• The DFT of is defined to be
(9.4)
• The relation between (9.3) and (9.4) is simply that
(9.5)
• DFT frequency axis relationships are the following:
w n
w n
y n
Y ej y n e jn–
n 0=
N 1–
=
y n
Y k y n ej2knN
---------------–k
n 0=
N 1–
0 1 N 1– = =
Y k Y ej
2kN
---------==
k0
0
0
N 21 N
F Hz2
FsFs 2
2 N
Fs N
Yk
ECE 5655/4655 Real-Time DSP 9–3
Chapter 9 • Real-Time Fast Fourier Transform
• A feature of is that it is computable; about complexmultiplications and complex additions are requiredfor an N-point DFT, while the radix-2 FFT, discussed shortly,reduces the complex multiplication count down to about
• The inverse DFT or IDFT is defined to be
(9.6)
• In both the DFT and IDFT expressions it is customary to let
(9.7)
then the DFT and IDFT pair can be written as
(9.8)
– The complex weights are known as twiddle constantsor factors
Y k N2
N N 1–
N 2 N2log
y n 1N---- Y k e
j2knN
---------------
k 0=
N 1–
n 0 1 N 1– = =
WN ej2N------–
Y k y n WNknk 0 1 N 1– =
n 0=
N 1–
=
y n 1N---- Y k WN
kn–n 0 1 N 1– =
k 0=
N 1–
=
WNk
9–4 ECE 5655/4655 Real-Time DSP
Simple Application Examples
Simple Application Examples
To further motivate study of the FFT for real-time applications,we will briefly consider two spectral domain processing exam-ples
Spectral Analysis
• Suppose that
(9.9)
and
• The true spectrum, assuming an infinite observation time, is
(9.10)
for and and
Haa F A/DN - L pt.
Zero PadN-Point
DFT
w n
x'a t xa t xa nT
x̂ n X̂ k
Window Functionof length L
FsSimple DSPBased SpectrumAnalysis
Anti-aliasFilter
x n The DFT Pointsare TypicallyDisplayed as|X[k]| or |X[k]|2
xa t A1 2F1t A2 2F2t cos+cos=
F1 F2 Fs 2
X A1
2------ 1– 1+ + =
A2
2------ 2– 2+ + +
– 1 2F1 Fs= 2 2F2 Fs=
ECE 5655/4655 Real-Time DSP 9–5
Chapter 9 • Real-Time Fast Fourier Transform
• As a result of windowing the data
(9.11)
for , where
– A rectangular window (in MATLAB boxcar) of length Lsamples has DTFT
(9.12)
X X̂
X̂ A1
2------ W 1– W 1+ + =
A2
2------ W 2– W 2+ + +
– W DTFT w n =
W L 2 sin 2 sin
---------------------------ej L 1– 2–
=
0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.50
5
10
15
20
25
30
35
40
45
50L = 50 Rectangular Window DTFT
|W(f
)|
Normalized Frequency f
Note that the peak value is L
9–6 ECE 5655/4655 Real-Time DSP
Simple Application Examples
• The DFT points, , are simply a sampled version of(9.11), that is,
(9.13)
for
• The zero-padding operation is optional, but is often used toimprove the apparent spectral resolution
• Without zero padding we have only L samples on ,while with zero padding we effectively make the DFT createadditional samples of , raising the number from L to N
• The frequency sample spacing changes from
to radians/sample
or
to cycles/sample
Example:
» n = 0:49;» x = 3*cos(2*pi*1000*n/10000) + ... 0.5*cos(2*pi*2000*n/10000); % Fs = 10000 Hz» X = fft(x,512);» plot(0:511,abs(X))» grid
X̂ k
X̂ k A1
2------ W
2kN
--------- 1– W
2kN
--------- 1+ +=
A2
2------ W
2kN
--------- 2– W
2kN
--------- 2+ ++
0 k N
0 2
X
2L
------= 2N------=
f 1L---= f 1
N----=
ECE 5655/4655 Real-Time DSP 9–7
Chapter 9 • Real-Time Fast Fourier Transform
» title('Two Sinusoids with L = 50','fontsize',18)» ylabel('|X(k)|','fontsize',16)» xlabel('DFT Index k','fontsize',16)» axis([0 512 0 80]);» printmif('5910_2')
• With spectral analysis the goal may not be to actually displaythe spectrum, but rather to perform some additional computa-tions on the DFT points
~3 x 50/2 = 75
~0.5 x 50/2 = 12.5
F, Hz
10k5k0 2k1k
0 50 100 150 200 250 300 350 400 450 5000
10
20
30
40
50
60
70
80Two Sinusoids with L = 50, N = 512
|X(k
)|
DFT Index k
DFT Sample Values withoutzero-padding have squaresymbols (mostly at zero)
9–8 ECE 5655/4655 Real-Time DSP
Simple Application Examples
• For example we may try to decide if the frequency domainfeatures of the transformed signal record are close to sometemplate, i.e., target detection, pattern recognition/classifica-tion, etc.
Transform Domain Linear Filtering
• A considerably different application of the DFT is to performfiltering directly in the frequency domain
• The basic assumption here is that the signal sample streamwe wish to filter may have infinite duration, but the impulseresponse of the filter must be finite
• One standard approach is known as the overlap and savemethod of FIR filtering
2N Points 2N Points 2N Points
Input Sequence
n
N PointsN Points
Output
Discard Valid Pointsn
N Points N Zeros nFilterImpulseResponse
2N FFT
2N FFT
2N IFFT
FilterFrequencyResponse
AlternateConnection
Overlap and Save Filtering
Load OverlappingBlocks of 2N Points
ECE 5655/4655 Real-Time DSP 9–9
Chapter 9 • Real-Time Fast Fourier Transform
• As the impulse response length grows the transform domainapproach becomes more efficient than the time domain con-volution sum, implemented in say a direct form FIR structure
• Another way of performing a transform domain convolutionis with the overlap and add method
Radix 2 FFT
• A divide-and-conquer approach is taken in which an N-point(N a power of two, ) DFT is decomposed into a cas-cade like connection of 2-point DFTs
– With the decimation-in-time (DIT) algorithm the decom-position begins by decimating the input sequence
N/2 Points N/2 Points N/2 Points
Input Sequence
n
N Points
N Points
Output
Add Overlapping Point Setsn
N/2 Points N/2 Zeros nFilterImpulseResponse
N FFT
N FFT
N IFFT
FilterFrequencyResponse
AlternateConnection
Overlap and Add Filtering
Load Blocksof N/2 Points
N/2. . .
. . .
Zero Pad to N
N 2
=
x n
9–10 ECE 5655/4655 Real-Time DSP
Radix 2 FFT
– With the decimation-in-frequency (DIF) algorithm thedecomposition begins by decimating the output sequence
and working backwards
Decimation-in-Time
• Begin by separating into even and odd pointsequences
(9.14)
• In the first sum let and in the second let
(9.15)
or
(9.16)
• Note that , thus
(9.17)
where we now see that and are point DFTsof the even and odd points of the original sequence
X k
x n N 2
X k x n WNkn
n even x n WN
kn
n odd+=
n 2r= n 2r 1+=
X k x 2r WN2rk
r 0=
N2---- 1–
= x 2r 1+ WN2r 1+ k
r 0=
N2---- 1–
+
X k x 2r WN2 rk
r 0=
N2---- 1–
= WNk
x 2r 1+ WN2 rk
r 0=
N2---- 1–
+
WN2
e2j 2 N –
ej2 N 2 –
WN 2= = =
X k x 2r WN 2rk
r 0=
N2---- 1–
= WNk
x 2r 1+ WN 2rk
r 0=
N2---- 1–
+
G k WNkH k +=
G k H k N 2x n
ECE 5655/4655 Real-Time DSP 9–11
Chapter 9 • Real-Time Fast Fourier Transform
• A combining algebra brings the N/2-point DFTs together
• After this one stage of decimation a computational savingshas already been achieved
– The number of complex multiplies is = for
• The procedure can be repeated until there are a total of stages of decimation
x 0 x 2
x N 2–
N/2 PointDFT..
.
Even
x 1 x 3
x N 1–
...
Odd
G 0
G N 2 1–
H 0
H N 2 1–
Combining Algebra
WN0
WNN 2 1–
N/2 PointDFT
X 0
XN2---- 1–
X N 1–
XN2----
WNN 1–
WNN 2
......
......
2 N 2 2 N+N
22 N N
2+ N 2
N2log=
9–12 ECE 5655/4655 Real-Time DSP
Radix 2 FFT
• The general form of the DIT FFT signal flow graph is now
• When the decimation is complete the basic computation ineach of the two point DFTs and the combining algebras alllook the same
• We have what is known as the radix-2 butterfly
2PointDFT
2PointDFT
2PointDFT
2PointDFT
...
...
...
stage 1 stage 2 stage
X 0
X N 1–
x 0
x N 1–
Inpu
ts in
Bit
Rev
erse
d O
rder
Oup
uts
in B
it N
orm
al O
rder
Combining Algebras
WNr
1–
mth stagem 1st stage–
Simplified Radix-2 Butterfly
Xm 1– p
Xm 1– q
Xm p
Xm q
ECE 5655/4655 Real-Time DSP 9–13
Chapter 9 • Real-Time Fast Fourier Transform
• The number stages is and when using the simplifiedbutterfly one complex multiply is required per stage with N/2butterflies per stage, thus the total number of complex multi-plies per FFT is
Multiplications = (9.18)
– Note: Each complex multiply requires four real multipliesand two real additions
• The number of complex additions is
Additions = (9.19)
– Note: Each complex addition requires two real additions
• Further study of the DIT algorithm reveals that it can be com-puted in-place, that is only one complex array is required totransform into
• A drawback is that the input array must be initially reorderedinto bit reversed order, that is say for
N2log
N2---- N2log
N N2log
x n X k
N 8=
9–14 ECE 5655/4655 Real-Time DSP
Radix 2 FFT
...
Decimation-in-Frequency
• With the DIF approach the decimation starts at the output andworks towards the input
• The signal flow graph turns out to be the transposed versionof the DIT flow graph
– The in-place calculation property holds again
– The operations count is identical
– A significant difference is that the input is in normal orderand the output is now in bit reversed order
Computing the IDFT
• To compute the IDFT we do it the same way as the DFT,except we must include a scale factor and the twiddleconstants must be conjugated
x 0 x 000 X 000 X 0 = =
x 4 x 100 X 001 X 1 = =
x 2 x 010 X 010 X 2 = =
x 3 x 011 X 110 X 6 = =
x 7 x 111 X 111 X 7 = =
1 N
ECE 5655/4655 Real-Time DSP 9–15
Chapter 9 • Real-Time Fast Fourier Transform
FFT Implementation
• With the CMSIS-DSP library there is a large collection ofFFT algorithms
– Initial testing of the functions resulted in failure, as the fullversion of Keil MDK is needed due to memory require-ments
– An example will be included in this chapter when the test-ing is complete
• As an alternative to CMSIS-DSP, a custom FFT module canbe used instead
• A radix-2 C-based in-place FFT is contained in the fft.c/fft.h
– This function uses the data structure COMPLEX, defined inthe header file, to with complex float32_t variables
typedef struct{
float real; float imag;
} COMPLEX;
– The function takes as input a COMPLEX array of lengthfftlen and returns the FFT of the data in the same array
– A twiddle factor array, also of length fftlen, must begenerated in a COMPLEX array beforehand
//fft.h// definition of a complex FFT function written by Rulph Chassaing// A complex data structure, COMPLEX, is also defined.// Reworked for the Cortex-M4 by Mark Wickert, April 2015
#define ARM_MATH_CM4#include <s6e2cc.h>
9–16 ECE 5655/4655 Real-Time DSP
FFT Implementation
#include <stdint.h>#include "arm_math.h"
struct cmpx //complex data structure used by FFT{ float32_t real; float32_t imag;};typedef struct cmpx COMPLEX;
void make_twiddle_array(int16_t fftlen,COMPLEX *twiddle);void fft(COMPLEX *Y, int16_t M, COMPLEX *w);
//fft.c// Implementation of a complex fft originally written by// Rulph Chassaing.// Reworked for the Cortex-M4 by Mark Wickert, April 2015.
3-Band Audio Equalizer Based on Reay Chapter 5 Example 5.8
• Here we consider a variation the Audio equalizer found in thecourse text
• Real-time filtering is performed in the transform domainusing the overlap and add method discussed on page 9–10
• Three FIR filters, each of 128 taps, are placed in parallel toallow variable gain in bass, midrange, and treble audio bands
– For bass a lowpass filter with cutoff at 500 Hz
– For midrange a bandpass filter with passband between 970and 9000 Hz
– For treble a highpass filter with cutoff at 9000 Hz
• The filters are designed in a Jupyter notebook using remezlowpass, bandpass, and highpass functions found infir_design_helper from the package scikit-dsp-comm
• Care is taken to arrive at three filters of the same length, yetalso mesh together at the transition between the three bands
9–24 ECE 5655/4655 Real-Time DSP
FFT Implementation
• This is not as easy as it looks, hence more work is needed toavoid bumps that you can see below:
• Forming the composite response with various gain settingson each of the three filters