dsp using matlab® - 7
Post on 11-Apr-2015
1.974 Views
Preview:
TRANSCRIPT
Lecture 7: The Discrete Fourier Transform
Mr. Iskandar Yahya
Prof. Dr. Salina A. Samad
IntroductionDiscrete-time Fourier Transform
provides the frequency-domain (w) representation for absolutely summable sequences Hence, useful signals in practice for which the discrete-time
Fourier transform does not exist [ex) u(n), nu(n)] The transient response of a system due to initial conditions
or due to changing inputs cannot be computed using the discrete-time Fourier transform approach
z-Transformprovides a generalized frequency-domain (z)
representation for arbitrary sequences Extension of the discrete-time Fourier transform
IntroductionTwo features of these transforms
The transforms are defined for infinite-length sequencesThey are functions of continuous variables (w or z)
-> From the numerical computation viewpoint (or from MATLAB’s viewpoint), these two features are troublesome because one has to evaluate infinite sums at uncountably infinite frequencies
-> To use MATLAB, we have to truncate sequences and then evaluate the expressions at finitely many points
-> The evaluations were obviously approximations to the exact calculations
-> In other words, the discrete-time Fourier transform and the z-transform are not numerically computable transforms
IntroductionNumerical computable transform
DFS (Discrete Fourier Series)DFT (Discrete Fourier Transform)FFT (Fast discrete Fourier Transform)
Because the numerical computation of the DFT for long sequences is prohibitively time consuming, several algorithms have been developed to efficiently compute the DFT
Discrete Fourier SeriesPeriodic sequence
Discrete Fourier series representation of periodic sequence
synthesis or
an inverse DFS equation
The discrete Fourier series coefficients analysis or an DFS
sequencetheofperiodlfundamentaN
knkNnxnx
:
,),(~)(~
,...,n,W)k(X~
N
...,,n,e)k(X~
N)]k(X
~[IDFS)n(x~
N
k
nkN
N
k
knN
j
101
101
1
0
1
0
2
,...,k,W)n(x~
...,,k,e)n(x~)]n(x~[DFS)k(X~
N
n
nkN
N
n
nkN
j
10
10
1
0
1
0
2
Nj
eWwhere
2
Discrete Fourier SeriesExample 1: Find DFS representation of the
priodic sequence given below:
Solution:
,...},,,,,,,,,,,{...,)n(x~ 321032103210
,...)(x~)(x~)(x~)(x~)j)(n(x~W)n(x~)(X~
j)j)(n(x~W)n(x~)(X~
)j)(n(x~W)n(x~)(X~
j)(x~j)(x~)(x~j)(x~)j)(n(x~W)n(x~)(X~
)(x~)(x~)(x~)(x~W)n(x~)(X~
jsinjcoseW
Nissequenceabovetheofperiodalfundanment
,...,k,W)n(x~)k(X~
nn
n
n
nn
n
n
nn
n
n
nn
n
n
n
n
j
N
n
nkN
632104
223
22
2232101
632100
22
4
10
3
043
0
44
3
033
0
34
3
023
0
24
3
0
3
0
14
3
0
04
4
2
4
1
0
Discrete Fourier SeriesSolution in MATLAB:
We can use for…end loop to implement X(k) summation and compute all DFS coefficients. But this nested two for…end loop is inefficient in MATLAB.
An efficient way is to use a matrix-vector multiplication for each of the relations in the manual solution:
n
k
WW
WW
}W{W
X~
WN
x~x~WX~
)N()N(N
)N(N
)N(NN
Nn,kkn
NN
*NN
1111
1111
10
1
1
111
1
Discrete Fourier SeriesMatlab implementation:
Create a function called dfs:
function [Xk] = dfs(xn,N)
% Computes Discrete Fourier Series Coefficients
% ----------------------------------------
% [Xk] = dfs(xn,N)
% Xk = DFS coeff. array over 0 <= k <= N-1
% xn = One period of periodic signal over 0 <= n <= N-1
% N = Fundamental period of xn
%
n = [0:1:N-1]; % row vector for n
k = [0:1:N-1]; % row vector for k
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFS matrix
Xk = xn * WNnk; % row vector for DFS codfficients
Discrete Fourier Series Implementation for example 1 in the main window:
Check with the manual approach done earlier, should be the same.
xn = [0,1,2,3]; N = 4;
Xk = dfs(xn,N)
Xk =
6.0000 -2.0000 + 2.0000i -2.0000 - 0.0000i -2.0000 - 2.0000i
Discrete Fourier SeriesTo implement the synthesis equation, we can write a
function called “idfs”:
function [xn] = idfs(Xk,N)
% Computes Inverse Discrete Fourier Series
% ------------------------------------
% [xn] = idfs(Xk,N)
% xn = One period of periodic signal over 0 <= n <= N-1
% Xk = DFS coeff. array over 0 <= k <= N-1
% N = Fundamental period of Xk
%
n = [0:1:N-1]; % row vector for n
k = [0:1:N-1]; % row vector for k
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ (-nk); % IDFS matrix
xn = (Xk * WNnk)/N; % row vector for IDFS values
Discrete Fourier SeriesExample 2: A periodic “square wave” sequence is given by:
N : fundamental period, L/N : duty cycle
A) determine the expression for magnitude:
,...,m;N)m(nLmN
LmNnmN)n(x~ 10
11
1
0
1
)kN
sin(
)LkN
sin(e
ee
ee
e
e
e
e
otherwise
,...N,k
e
e
L
)e(ee)n(x~)n(X~
k)L(Nj
kNjk
Nj
LkNjLk
Nj
kNj
LkNj
kN
j
LkN
j
kN
j
LkN
j
L
nn
kN
jL
n
nkN
jN
n
nkN
j
1
2
2
2
2
1
0
21
0
21
0
2
1
1
0
1
1
otherwise
,...N,k
)N/ksin(
)N/kLsin(L
)n(X~ 0
Discrete Fourier SeriesB) Plot magnitude:
% Part (b)1
L = 5; N = 20;
xn = [ones(1,L), zeros(1,N-L)];
Xk = dfs(xn,N);
magXk = abs([Xk(N/2+1:N) Xk(1:N/2+1)]);
k = [-N/2:N/2];
subplot(2,2,1); stem(k,magXk); axis([-N/2,N/2,-0.5,5.5])
xlabel('k'); ylabel('Xtilde(k)')
title('DFS of SQ. wave: L=5, N=20')
% Part (b)2
L = 5; N = 40;
xn = [ones(1,L), zeros(1,N-L)];
Xk = dfs(xn,N);
magXk = abs([Xk(N/2+1:N) Xk(1:N/2+1)]);
k = [-N/2:N/2];
subplot(2,2,2); stem(k,magXk); axis([-N/2,N/2,-0.5,5.5])
xlabel('k'); ylabel('Xtilde(k)')
title('DFS of SQ. wave: L=5, N=40')
% Part (b)3
L = 5; N = 60;
xn = [ones(1,L), zeros(1,N-L)];
Xk = dfs(xn,N);
magXk = abs([Xk(N/2+1:N) Xk(1:N/2+1)]);
k = [-N/2:N/2];
subplot(2,2,3); stem(k,magXk); axis([-N/2,N/2,-0.5,5.5])
xlabel('k'); ylabel('Xtilde(k)')
title('DFS of SQ. wave: L=5, N=60')
% Part (b)4
L = 7; N = 60;
xn = [ones(1,L), zeros(1,N-L)];
Xk = dfs(xn,N);
magXk = abs([Xk(N/2+1:N) Xk(1:N/2+1)]);
k = [-N/2:N/2];
subplot(2,2,4); stem(k,magXk); axis([-N/2,N/2,-0.5,7.5])
xlabel('k'); ylabel('Xtilde(k)')
title('DFS of SQ. wave: L=7, N=60')
Discrete Fourier SeriesB) Plot magnitude:
-10 -5 0 5 10
0
1
2
3
4
5
k
Xtild
e(k)
DFS of SQ. wave: L=5, N=20
-20 -10 0 10 20
0
1
2
3
4
5
k
Xtild
e(k)
DFS of SQ. wave: L=5, N=40
-20 0 20
0
1
2
3
4
5
k
Xtild
e(k)
DFS of SQ. wave: L=5, N=60
-20 0 20
0
2
4
6
k
Xtild
e(k)
DFS of SQ. wave: L=7, N=60
Discrete Fourier SeriesRelations to the z-transform
x(n) is a finite-duration sequence of duration N
We construct a periodic sequence by periodically repeating x(n) with period N
1
0
10
0N
nnz)n(x)z(X
elsewhere
Nnnonzero)n(x
)n(x~
nN
n
kN
jN
n
nkN
j]e)[n(xe)n(x~)z(X
)n(x~ofDFS
elsewhere
Nn)n(x~)n(x
1
0
21
0
2
10
0
kN
jez
)z(X)k(X~
2
Discrete Fourier SeriesRelations to the DTFT
DFS is obtained by evenly sampling the DTFT at w1=2/N intervals
Example 3: Let: X(n) = {0,1,2,3} A) Compute DTFT:
B) Sampling at kw1=2/4 k, k=0,1,2,3
kN
w
jw )e(X)k(X~
2
wjwjjwjwnjw eeee)n(x)e(X 32 32
)(X~
jeee)e(X
)(X~
eee)e(X
)(X~
jeee)e(X
eee)e(X
/j/j/j/j
/j/j/j/j
/j/j/j/j
jjjj
32232
2232
12232
632132
4634624646
4434424444
4234224242
030200
Discrete Fourier SeriesDFT
The sampling of the DTFT result in a periodic sequence But most of all signals in practice are not periodic They are likely to be of finite duration How can we develop a numerically computable Fourier
representation for such signals? Theoretically, we can take care of this problem by
defining a periodic signal whose primary shape is that of the finite-duration signal,
and then using the DFS on this periodic signal x(n) : finite-duration sequence that has N samples over
0<=n<=N-1 as an N-point sequence : periodic signal of period N, created using the N-point
sequence x(n))n(x~
)Nmodn(x)n(x~)rNn(x)n(x~
r
Discrete Fourier SeriesWe use the following convenient notation to denote the
modulo-N operation:
Nlengthofwindowgulartanrecacalledis)n(R
else
Nn)n(x~)n(R)n(x~)n(xwhere
operationWindow)n(R)n(x~)n(x
extentionperiodic))n((x)n(x~)Nmodn(x))n((x
N
N
N
N
N
10
0
Discrete Fourier SeriesDiscrete Fourier Transform of an N-point sequence
Inverse Discrete Fourier Transform of an N-point sequence
xWX
Nk,W)n(x)k(X
or
)k(R)k(X~
else
Nn)k(X~
)]n(x[DFT)k(X
N
N
n
nkN
N
10
10
0
1
0
XWN
x
Nn,W)k(XN
)k(X
or
)n(R)n(x~)]k(X[IDFT)n(x
*N
N
k
knN
N
1
101 1
0
Discrete Fourier Series MATLAB implementation
dfs and idfs MATLAB functions can be renamed as the dft and idft functions to implement the discrete Fourier transform computations
function [Xk] = dft(xn,N)
% Computes Discrete Fourier Transform
% --------------------------------
% [Xk] = dft(xn,N)
% Xk = DFT coeff. array over 0 <= k <= N-1
% xn = N-point finite-duration sequence
% N = Length of DFT
%
n = [0:1:N-1]; % row vector for n
k = [0:1:N-1]; % row vector for k
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ nk; % DFT matrix
Xk = xn * WNnk; % row vector for DFT codfficients
function [xn] = idft(Xk,N)
% Computes Inverse Discrete Transform
% --------------------------------
% [xn] = idft(Xk,N)
% xn = N-point sequence over 0 <= n <= N-1
% Xk = DFT coeff. array over 0 <= k <= N-1
% N = Length of DFT
%
n = [0:1:N-1]; % row vector for n
k = [0:1:N-1]; % row vector for k
WN = exp(-j*2*pi/N); % Wn factor
nk = n'*k; % creates a N by N matrix of nk values
WNnk = WN .^ (-nk); % IDFT matrix
xn = (Xk * WNnk)/N; % row vector for IDFT values
Discrete Fourier Series Example 4: Let x(n) be a 4-point sequence:
A) Then, the DTFT:
B) 4-point DFT of x(n)
this are similar to those in example 1
otherwise
n)n(x
30
0
1
02
2
02
2
2
32
3
2
2
2
2
1
11 23
4323
0
)/wsin(
)wsin(when
)/wsin(
)wsin(when
w
w
)e(X
)/wsin(
)wsin()e(X
e)/wsin(
)wsin(
e
eeeee)n(x)e(X
jw
jw
/wjjw
wjwjwjjwjwnjw
j)/sin(j)/cos(eW
k,W)n(xW)n(x)k(X
/j
n
nkN
n
nkN
4242
30
424
3
0 41
04
% Example 4: Simple DFT Example
x = [1,1,1,1]; subplot(1,1,1);
% a) DTFT
w = [0:1:500]*2*pi/500; [H] = freqz(x,1,w);
magH = abs(H); phaH = angle(H); phaH(126)=-47.5841*pi/180;
subplot(2,2,1); plot(w/pi,magH); grid
xlabel('frequency in pi units'); ylabel('|X|'); title('Magnitude of the DTFT')
subplot(2,2,2); plot(w/pi,phaH/pi*180); grid
xlabel('frequency in pi units'); ylabel('Degrees'); title('Angle of the DTFT')
%print -deps2 me0506a.eps
%
% b) 4-point DFT
N = 4; w1 = 2*pi/N; k = 0:N-1; X = dft(x,N);
magX = abs(X), phaX = angle(X)*180/pi
subplot(2,2,3);plot(w*N/(2*pi),magH,'--'); grid
axis([0,4,0,4]); hold on
stem(k,magX); xlabel('k'); ylabel('|X(k)|'); title('Magnitude of the DFT: N=4')
hold off
subplot(2,2,4);plot(w*N/(2*pi),phaH*180/pi,'--'); grid
axis([0,4,-200,200]); hold on
stem(k,phaX); xlabel('k'); ylabel('Degrees'); title('Angle of the DFT: N=4')
%print -deps2 me0506b.eps
0 0.5 1 1.5 20
1
2
3
4
frequency in pi units
|X|
Magnitude of the DTFT
0 0.5 1 1.5 2-200
-100
0
100
200
frequency in pi units
Degr
ees
Angle of the DTFT
0 1 2 3 40
1
2
3
4
k
|X(k
)|
Magnitude of the DFT: N=4
0 1 2 3 4-200
-100
0
100
200
k
Degr
ees
Angle of the DFT: N=4
Discrete Fourier Series Matlab implementation:
Discrete Fourier Series Example 5: DTFT other sample X(ejw) ? For N = 8;
4821
70
00001111
48
7
0 81
08
//w:resolutionFrequency
eW;k,W)n(xW)n(x)k(X
},,,,,,,{)n(x
/jn
nkN
n
nkN
% Example 5: Zero-padding Example
subplot(1,1,1); x = [1,1,1,1];
% a) DTFT
w = [0:1:500]*2*pi/500; [H] = freqz(x,1,w);
magH = abs(H); phaH = angle(H); phaH(126)=-47.5841*pi/180;
% b) 8-point DFT
N = 8; w1 = 2*pi/N; k = 0:N-1;
x = [x, zeros(1,4)]; X = dft(x,N);
magX = abs(X), phaX = angle(X)*180/pi
subplot(2,1,1);plot(w*N/(2*pi),magH,'--');
axis([-0.1,8.1,-1,5]); hold on
stem(k,magX); xlabel('k'); ylabel('|X(k)|'); title('Magnitude of the DFT: N=8')
hold off
subplot(2,1,2);plot(w*N/(2*pi),phaH*180/pi,'--');
axis([-0.1,8.1,-200,200]); hold on
stem(k,phaX); xlabel('k'); ylabel('Degrees'); title('Angle of the DFT: N=8');
0 1 2 3 4 5 6 7 8-1
0
1
2
3
4
5
k
|X(k
)|
Magnitude of the DFT: N=8
0 1 2 3 4 5 6 7 8-200
-100
0
100
200
k
Degr
ees
Angle of the DFT: N=8
Discrete Fourier Series For N = 16;
16216
15
0 151
016 150
0000000000001111
/jn
nkN
n
nkN eW;k,W)n(xW)n(x)k(X
},,,,,,,,,,,,,,,{)n(x
% c) 16-point DFT
subplot(1,1,1)
N = 16; w1 = 2*pi/N; k = 0:N-1;
x = [x, zeros(1,8)];
X = dft(x,N);
magX = abs(X), phaX = angle(X)*180/pi
subplot(2,1,1);plot(w*N/(2*pi),magH,'--');
axis([-0.1,16.1,-1,5]); hold on
stem(k,magX);
xlabel('k');
ylabel('|X(k)|'); title('Magnitude of the DFT: N=16')
hold off
subplot(2,1,2);plot(w*N/(2*pi),phaH*180/pi,'--');
axis([-0.1,16.1,-200,200]); hold on
stem(k,phaX);
xlabel('k');
ylabel('Degrees'); title('Angle of the DFT: N=16')
%print -deps2 me0507b.eps
0 2 4 6 8 10 12 14 16-1
0
1
2
3
4
5
k
|X(k
)|
Magnitude of the DFT: N=16
0 2 4 6 8 10 12 14 16-200
-100
0
100
200
k
Degr
ees
Angle of the DFT: N=16
Discrete Fourier Series Example 6: To illustrate the difference between the high-density
spectrum and the high-resolution spectrum:
A) Determine the
)n.cos()n.cos()n(x 520480
100 n),n(xofDFT
% Chapter 05: Example 6: HiRes Spectrum
%
% a.
% Spectrum based on the first 10 samples of x(n)
n1=[0:1:9];y1=x(1:1:10);
subplot(2,1,1);stem(n1,y1);title('signal x(n), 0 <= n <= 9');xlabel('n')
axis([0,10,-2.5,2.5])
Y1=fft(y1);magY1=abs(Y1(1:1:6));
k1=0:1:5;w1=2*pi/10*k1;
subplot(2,1,2);stem(w1/pi,magY1);title('Samples of DTFT Magnitude');
xlabel('frequency in pi units')
axis([0,1,0,10])
disp('Press RETURN to continue');pause;
%print -deps2 me0508a.eps; subplot
%
0 1 2 3 4 5 6 7 8 9 10
-2
-1
0
1
2
signal x(n), 0 <= n <= 9
n
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
2
4
6
8
10Samples of DTFT Magnitude
frequency in pi units
Discrete Fourier Series% High density spectrum (100 samples) based on the first 10 samples of x(n)
n3=[0:1:99];y3=[x(1:1:10) zeros(1,90)];
subplot(2,1,1);stem(n3,y3);title('signal x(n), 0 <= n <= 9 + 90 zeros');xlabel('n')
axis([0,100,-2.5,2.5])
Y3=fft(y3);magY3=abs(Y3(1:1:51));
k3=0:1:50;w3=2*pi/100*k3;
subplot(2,1,2);plot(w3/pi,magY3);title('DTFT Magnitude');xlabel('frequency in pi units')
axis([0,1,0,10])
disp('Press RETURN to continue');pause;
%print -deps2 me0508b.eps; subplot
% b.
% High resolution spectrum based on 100 samples of the signal x(n)
subplot(1,1,1)
n=[0:1:99];
x=cos(0.48*pi*n)+cos(0.52*pi*n);
subplot(2,1,1);stem(n,x);title('signal x(n), 0 <= n <= 99');xlabel('n')
axis([0,100,-2.5,2.5])
X=fft(x);magX=abs(X(1:1:51));
k=0:1:50;w=2*pi/100*k;
subplot(2,1,2);plot(w/pi,magX);title('DTFT Magnitude');xlabel('frequency in pi units')
axis([0,1,0,60])
disp('Press RETURN to continue');pause;
%print -deps2 me0508c.eps; subplot
Discrete Fourier Series
0 10 20 30 40 50 60 70 80 90 100
-2
-1
0
1
2
signal x(n), 0 <= n <= 9 + 90 zeros
n
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
2
4
6
8
10DTFT Magnitude
frequency in pi units
0 10 20 30 40 50 60 70 80 90 100
-2
-1
0
1
2
signal x(n), 0 <= n <= 99
n
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 10
20
40
60DTFT Magnitude
frequency in pi units
Fast Fourier Transform (FFT)DFT
DFT is the only transform that is discrete in both the time and the frequency domains,
and is defined for finite-duration sequences Although it is a computable transform, the straightforward
implementation is very inefficient, especially when the sequence length N is large
It also led to the development of other efficient algorithms All these algorithms are collectively known as fast Fourier
Transform (FFT) algorithms N-point DFT
For large N, o(N2) is unacceptable in practice
)N(oC
eWwhere
1Nk0,W)n(x)k(X
2N
N
2j
1N
0n
nkN
Fast Fourier Transform (FFT)Goal of an efficient computation
One algorithm that considers only the periodicity of is the Goertzel algorithm. This algorithm still requires multiplications, but it has certain advantages. This algorithm is described in Chapter 10
DIT-FFT (decimation-in-time) and DIF-FFT (decimation-in-frequency) Symmetry and periodicity properties in reducing the number
of computations)NlogN(oCN
Fast Fourier Transform (FFT)Example: Let us discuss the computations of a 4-point DFT
and develop an efficient algorithm for its computation:
Can be done in matrix form:
which requires 16 complex multiplicationsEfficient Approach : Using periodicity
And substituting in the above matrix form, we get
jeW;3k0,W)n(x)k(X 4/2j4
3
0n
nkN
)3(x
)2(x
)1(x
)0(x
WWWW
WWWW
WWWW
WWWW
)3(X
)2(X
)1(X
)0(X
94
64
34
04
64
44
24
04
34
24
14
04
04
04
04
04
jW;1WW
jWW;1WW3
46
42
4
94
14
44
04
)3(x
)2(x
)1(x
)0(x
j1j1
1111
j1j1
1111
)3(X
)2(X
)1(X
)0(X
Fast Fourier Transform (FFT)Using symmetry, we obtain
Which requires only 2 complex multiplications, which is a considerably smaller number, even for this simple example.
21
21
21
21
2
1
2
1
21
21
21
21
jhh)3(X
gg)2(X
jhh)1(X
gg)0(X
)3(x)1(xh
)2(x)0(xh
)3(x)1(xg
)2(x)0(xg
2step1step
hh
)]3(x)1(x[j)]2(x)0(x[)3(jx)2(x)1(jx)0(x)0(X
gg
)]3(x)1(x[)]2(x)0(x[)3(x)2(x)1(x)0(x)0(X
hh
)]3(x)1(x[j)]2(x)0(x[)3(jx)2(x)1(jx)0(x)0(X
gg
)]3(x)1(x[)]2(x)0(x[)3(x)2(x)1(x)0(x)0(X
top related