Вы находитесь на странице: 1из 31

University of Manchester School of Computer Science 6 Dec 2009

Comp30291 Digital Media Processing


Section 7: The Discrete Fourier Transorm
7!1! "ntroduction:
The discrete time Fourier transform (DTFT of a se!uence "#$n%& is'
((e
)
*
[ ] mple radians+sa T + * ,here e n #
s
)n -

f
n

(.
/f "#$n%& is o0tained 0y samplin1 a suita0ly 0andlimited si1nal #
a
(t2 ,hose analo1ue Fourier
transform is ( ) ( )
a
2 then
((e
)
* (.+T ( ) ( )
a

for -

3 * T 3

The DTFT provides a convenient means of computin1


( ) ) (
a
0y a summation rather than an
inte1ral4 5o,ever2 there are t,o practical difficulties'
(i the ran1e of summation is infinite2
(ii ((e
)
is a continuous function of 4
The first difficulty enforces 6,indo,in17 or restrictin1 the se!uence "#$n%& to a finite 0loc8 of non-
9ero samples: say for n in the ran1e 0 to ;-.4 The resultin1 ,indo,ed se!uence'
"444202 #$0%2 #$.%2 4442 #$;-.%2 02 444 &
may 0e represented 0y the finite len1th se!uence'
"#$0%2 #$.%2 44442 #$;-.%& denoted 0y "#$n%& 0 . 2 N 4
The second difficulty means that 6fre!uency-domain samplin17 must 0e used4
/t is normal2 0ecause of the inverse DTFT formula2 to consider ((e
)
for in the ran1e - to 4
For real si1nals2 it ,ould 0e sufficient to consider values of ((e
)
only in the ran1e
0
4

There are applications ,here ,e need to apply the DTFT to comple# si1nals2 and in such cases ,e
then need to 8no, ((e
)
for in the ran1e - to 4 5o,ever2 0ecause the spectrum ((e
)

repeats at fre!uency intervals of 2 as illustrated 0elo,2 e!uivalent information is o0tained if ,e
evaluate ((e
)
for in the ran1e 0 to 2 instead of - to 4 This is simply 0ecause
((e
)
* ((e
) $ <2 %
so the spectrum from - to 0 is repeated for in the ran1e to 24

#$%e&p%' (#

2

Comp=029. DM> ?42 @MAC Dec709


Ta8in1 M e!ually spaced fre!uency-domain samples in the ran1e 0 2 produces the finite
se!uence of comple# num0ers'

{ } ((e
)
8

O M 2 .
* " ((e ((e ((e
) )
0 . .
2 2 4444444444444 2 &
j
M
(2
,here

k
2 8 + M for 8 * 02 .2 444 2 M- .4

|X(exp(j )|
|X[1]|
|X[3]|
|X[0]|
|X[M-1]|
0 1 2 3

M)1
2

The imposition of ,indo,in1 and fre!uency-domain samplin1 on the DTFT produces the
follo,in1 e!uation'
[ ]
((e ,here
)
8
8

+

x n e k M
n
N
j
k
n
0
.
2
(=
,hich is usefully evaluated for 8 * 02 .2 22 4442 M-.2 so that
8
1oes from 0 to 24 The lar1er M2
the easier it is to dra, smooth and accurate spectral 1raphs4 5o,ever2 it is often important to
evaluate )ust sufficient samples to o0tain an unam0i1uous spectral representation of a si1nal
!uic8ly4 /t may 0e sho,n that )ust ; fre!uency-domain samples are sufficient for this purpose
since2 in this case2 an inverse transform e#ists to recover the ori1inal time-domain si1nal from the ;
samples of its DTFT spectrum4 Bith fe,er than ; fre!uency-domain samples ,e cannot 0e
1uaranteed a ,ay of 1ettin1 0ac8 the ori1inal ; time-domain samples4 Computin1 more than ;
fre!uency-domain samples has advanta1es for dra,in1 1raphs2 as mentioned earlier2 0ut this ,ould
0e at the cost of slo,in1 do,n the computation2 ,hich for some applications is undesira0le4 Bhen
M * ;2 the comple# se!uence defined 0y C!uation = 0ecomes the Discrete Fourier Transform
(DFT of the se!uence "#$n%&
02;-.
4
7!2! Deinition o the Discrete Fourier Transorm %DFT( and its in*erse
/ntroducin1 the notation' [ ] ((e
)
8

X k 2 the DFT may 0e defined as the transformation'



[ ] { } [ ] { } x n X k
O N O N 2 2

. .

,ith [ ] [ ]
X k x n e k N
j n
n
N
k

0
.
2 ,here for 8 * 02 .2 22 44444 2 ; - .
8
+
(D
/t is normal to consider
[ ] { } x n
O N 2 .
as a comple# se!uence2 thou1h in practice the ima1inary parts
of the sample values are often set to 9ero4
The inverse DFT'
[ ] { } [ ] { } X k x n
O N O N 2 2

. .
2
may 0e performed usin1 the formula'

[ ] [ ] ,ith . - 444442 22 .2 02 f + . (
.
0
N n or e k X N n x
N
k
jn
k

8
2k N +
(E
Comp=029. DM> ?4= @MAC Dec709
7!3! Computation o the DFT directl+ and *ia the FFT
The similarity 0et,een e!uations (D and (E is e#ploited 0y computer pro1rams a0le to perform the
DFT or its inverse usin1 essentially the same code4 Such a pro1ram2 ,hich implements these
e!uations in a direct manner2 is 1iven as a FCF pro1ram in Ta0le .4 This pro1ram is very slo,2
ho,ever2 and it is possi0le to speed it up 0y Fast Fourier Transform (FFT techni!ues4 Such a
techni!ue2 pro1rammed in 6C72 is presented in Ta0le 24 /t is !uite interestin1 to study ho, the
improvement of speed is achieved 0y usin1 FFFTF techni!ues to implement the DFT4 This may 0e
found in most DS> te#t0oo8s2 0ut is outside the sylla0us of this section4 /n this section ,e are more
interested in ho, to use the DFT and interpret its results than in ho, it is pro1rammed efficiently4
To further illustrate the direct DFT pro1ram2 a MGTHG@ version is presented in Ta0le =4 This
uses the a0ility of MGTHG@ to define and manipulate comple# num0ers2 0ut is complicated
sli1htly 0y the fact that all MGTHG@ arrays start ,ith inde# .4 Therefore instead of storin1 sample
#$0% in array element #(0 as ,e may ,ish2 ,e must store it in #(.4 This direct DFT MGTHG@
pro1ram is of academic interest only as MGTHG@ has an efficient fft procedure in its Fsi1nal
processin1 tool-0o#F4 To illustrate its use2 Ta0le D is a MGTHG@ pro1ram ,hich reads music from
a F,avF file2 splits it up into E.2 sample sections and performs a DFT analysis on each section4
7!,! DFT %or FFT( or spectral anal+sis
Aiven "#$n%&
02;-.
the DFT (or FFT 1ives us "($8%&
02;-.
,ith ; comple# spectral samples from
0 to fs4 (,here f
S
is the samplin1 fre!uency4 Bhen "#$n%& is real2 ,e need only to plot ma1nitudes
of ($8% for 8*0 to ;+24 Bhen ;*E.22 8*;+2 * 2E6 corresponds to fs+24
/t is usual to define si1nal 0loc8s startin1 from n*02 0ut2 unfortunately2 MGTHG@ arrays cannot
start from 9ero4 /f the si1nal is "#$n%&
02;-.
,e may ,ish to store this in a MGTHG@ array called #4
Ir ,e may prefer to adopt a different name2 such as 6arrayJforJ#72 for the array4 G shorter name
may 0e 0etter4 These notes often use the same name for the si1nal and array4 To circumvent the
lac8 of a MGTHG@ array element ,ith inde# 9ero2 ,e store #$0% in #(.2 #$.% in #(22 and so on4 /n
1eneral2 #$n% is stored as #(.<n4 Similarly "($8%&
02;-.
are stored as ((. 444 ((;4
Be no, demonstrate the use of the DFT (or FFT to spectrally analyse se1ments of si1nals
containin1 and+or cosine ,aves4
?4D4. FFT of the sum of t,o sine-,aves'
Start 0y analy9in1 -&.n/0
0123
3 -100cos%4,(n( 5 100cos%%42(n( 0
0123
,ith an FFT of order ;*6D4
for n=0:63
x(1+n)=100*cos(0.25*pi*n)+100*cos(0.5*pi*n);
end;
X=fft(x);
plot(abs(X(1:32))); rid on;
Comp=029. DM> ?4D @MAC Dec709
0 5 10 15 20 25 30 35
0
500
1000
1500
2000
2500
3000
3500
5ere is the ma1nitude spectrum o0tained4
There are 6D pts 0ut ,e only plot =24 The hori9
a#is 1oes from . to =2 for fre!uencies 0 to fs+2
( 4
The vertical a#is can tell us the amplitudes of
the sine-,aves4
Divide 0y ;+2 i4e4 =2 here4
/t can7t tell ,hether they are sines or cos as ,e
plot only ma1nitude4
Comp=029. DM> ?4E @MAC Dec709
/f ,e modify this pro1ram very sli1htly to plot the fft spectrum divided 0y ;+22 i4e4 0y =22 ,e can
then read the sine-,ave amplitudes (0oth .00 directly from the 1raph4
0 5 10 15 20 25 30 35
0
20
40
60
80
100
120
/ncreasin1 the num0er of time-domain samples from 6D to .2K 1ives us more points in the
fre!uency-domain and then ,e can read the fre!uencies and amplitudes more accurately4 @ut it is
not al,ays possi0le to ta8e more time-domain samples4 BhyL The si1nal may not 0e 6stationary7
i4e4 its spectrum may 0e 1radually chan1in1 and the chan1e may 0e too much over .2K samples4
Stic8in1 ,ith ;*6D2 consider ,hat happens if the fre!uency of one of the sine-,aves2 the hi1her
one say2 is chan1ed sli1htly4 First ,e o0serve that it ,as +22 correspondin1 to fs+D2 ,hich
coincides e#actly ,ith .6+=22 one of the fre!uency domain samplin1 points2 i4e4 the one ,ith
8*.64 Bhat happens if ,e decrease the fre!uency of this sine ,ave from .6+=2 to 4.E4E+=2
.4E224 This lies 0et,een the samplin1 points ,ith 8*.E and 8*.64 There is no fre!uency
samplin1 point at .4E224 The ma1nitude spectral 1raph ,e o0tain is'
0 5 10 15 20 25 30 35
0
20
40
60
80
100
120
This is trou0leM ;o, ,e don7t 1et the correct amplitude for the sinusoid at .4E22 ,hich does not
correspond to one of the fre!uency samplin1 points4 There is a =EN error in amplitude readin14
5o, can ,e overcome this pro0lemL /f ,e could increase ; from 6D2 this ,ould improve matters
and in this case ,ould solve the pro0lem entirely as .4E22 ,ould coincide ,ith one of the ne, fre!
samplin1 points created 0y dou0lin1 ;4 5o,ever2 this solution may not 0e possi0le as ,e may
only have 6D samples availa0le4 The si1nal may 0e rapidly chan1in1 (non-stationary and it may 0e
)ust a0out possi0le to consider it !uasi-stationary for ;*6D 0ut not for ;*.2K4
?4D42 Oero-paddin1'
Gnother possi0le solution is to apply 69ero-paddin17 ,hich means appendin1 9ero samples 0efore
or after the si1nal to artificially increase the num0er of samples4 This increases the num0er of
Comp=029. DM> ?46 @MAC Dec709
fre!uency samples produced 0y the FFT4 >lacin1 6D samples in front of or after the 6D samples ,e
analysed previously and ta8in1 the FFT 1ives us a spectrum ,hose ma1nitude is sho,n 0elo,4
0 10 20 30 40 50 60 70
0
20
40
60
80
100
120
/n this case the techni!ue has 0een reasona0ly effective in interpolatin1 0et,een the fre!uency
domain samples previously produced 0y the 6D point FFT and it has 1iven us a more accurate
indication of the amplitude of the hi1hest fre! sine-,ave4 The ripples are interestin1 and ,ill 0e
discussed later4
?4D4= C#tension 0y even symmetry'
Gn alternative to 69ero paddin17 as a means of dou0lin1 the num0er of fre!uency-domain samples
is to e#tend the 0loc8 of ; samples 0y even symmetry4 To illustrate for a se1ment of )ust D
samples2 "6 . 2 D&
02=
0ecomes either "6 . 2 D , 2 1 2& or ", 2 1 2 6 . 2 D&4 @oth of these even
order e#tension are accepta0le2 0ut the second2 pre-e#tension is preferred4
/f the 6D samples ,e analy9ed earlier are even order pre-e#tended to produce .2K samples and
these are FFT analy9ed2 the spectrum o0tained has ma1nitude sho,n 0elo,'
0 10 20 30 40 50 60 70
0
10
20
30
40
50
60
70
80
90
100
The improvement is mar1inal in this case and not as successful as 9ero-paddin14 @ut there is a
special advanta1e to this method of dou0lin1 the num0er of fre!uency-domain samples4 /t leads to
the discrete cosine transform (DCT4
Consider a1ain the simple e#ample ,here "6 . 2 D&
02=
is pre-e#tended to 0ecome
" , 2 1 2 6 . 2 D &
The K-point DFT of the e#tended se!uence is2 for 8*02 .2 444 ?2
k k k k k k k
j j j j j j j
e e e e e e e k X

+ + + + + + +
? 6 E D = 2
D 2 6 6 2 D % $
X[k] * D < 2e
-)
8
< e
-2)
8
< 6e
-=)
8
< 6e
-D)
8
<e
-E)
8
< 2e
-6)
8
< De
-?)
8
Comp=029. DM> ?4? @MAC Dec709
* e
-=4E)
8
"De
=4E)
8
<2e
24E)
8
< e
.4E)
8
< 6e
04E)
8
< 6e
-04E)
8

< e
-.4E)
8
< 2e
-24E)
8

< De
-=4E)
8
&
* 2e
-=4E)
8
"Dcos(=4E
8
< 2cos(24E
8
< cos(.4E
8
< 6cos(04E
8
&
(,here
8
* 28+K * 8+D4
* 2e
-=4E)
8
DCT[k] ,here

DCT[k] * 6cos(04E
8
< cos(.4E
8
< 2cos(24E
8
< Dcos(=4E
8

Cvaluatin1 the DFT for 8* 0 . 2 = D E 6 ? 1ives us fre!uency-samples from 0 to fs and if ,e are


analysin1 a real si1nal ,e only need e#amine the first D of these4 So "DCT[k]&
02=
2 i4e4 ta8in1 )ust
the first D out of the K DFT samples2 is the 6discrete cosine transform of "#$8%&
02=
44 /t is so called
0ecause the pre-e#tension 0y even symmetry has produced a DFT ,hich may 0e represented as the
sum of a cosine series4 The 1reat advanta1e is that the e#pression is purely real4 There is no need
for comple# num0ers as there is for the DFT4
7!6 The discrete cosine transorm %DCT(
?4E4. Ine dimensional DCT' The .-dimensional DCT has many forms4 Be have considered )ust
one2 0ut it is the most commonly encountered form4 /n this form2 1iven "#$n%&
02;-.
its DCT is
;ote that
8
* 28+(2N ,hich means that fre!uency ran1e 0 to 2 is sampled at 2; points4 /f
"#$n%&
02;-.
is real2 ,e only need DCT[k] for 8 * 0 to N-.4 This ta8es us from 0 to ( f
S
+2 in N
steps4 (FFT of same order 1ives only N+2 points in ran1e 0 to f
S
+2
Aiven "#$n%&
02;-.
the DFT (FFT of this si1nal pre-e#tended symmetrically is'
For a real si1nal2 ,e mi1ht only 1o from 8*0 to 8*;-.2 and ,e may evaluate only the modulus or
modulus s!uared4 Then2
;o, there are no comple# num0ers4
Cvaluatin1 the DCT 0y directly summin1 cosines is strai1htfor,ard to pro1ram 0ut
computationally inefficient and slo,4 >re-e#tendin1 and performin1 an FFT ,ill 0e faster4
/n MGTHG@2 the DCT is performed 0y (*dct(#4
The .-dimensional DCT has an inverse transform and it is also availa0le in MGTHG@ as
6#*/DCT((74 /t should 0e pointed out that the MGTHG@ DCT function introduces a scalin1 factor
,hich affects the amplitudes o0tained4 ($8% is scaled 0y $2+;% ,hen 8P. and 0y $.+;% ,hen
8*.4 /t is a simple matter to reverse this scalin1 ,hen plottin1 ma1nitude spectral 1raphs' i4e4
instead of dividin1 0y ;+2 to 1et the amplitudes ri1ht as ,e did ,ith the DFT2 ,e divide 0y $;+2%
,hen 8P. and 0y 2$;% ,hen 8*04 Somethin1 li8e that4 /f you are not plottin1 a 1raph 0ut usin1
the DCT for si1nal processin12 i4e4 ta8in1 the DCT2 modifyin1 it and then ta8in1 the inverse DCT2
you can for1et a0out this scalin1 entirely4

,
_


,
_

.
0
+ ,here
2
. 2
cos % $ % $
N
n
k k
N k
n
n x k DCT
. 2 24442 . 2 0 for % $ % $
2 + . 2 (


N k k DCT e k X
N j
k

% $ % $ k DCT k X

Comp=029. DM> ?4K @MAC Dec709


?4E42 The 6t,o dimensional7 DCT' The DCT is ,idely used in sound Q ima1e processin14 For the
latter2 a 2D version is needed
This transformation is performed 0y 6$3dct2%&(7 in MGTHG@ ,here # is an array if dimension ;
0y M4
The inverse 2-dimensional transform is availa0le 6& 3 idct2%$(78
More a0out the 2-dimensional DCT later4
7!2 9pplications o the DFT %FFT( and DCT
The FFT is the 6S,iss army 8nife7 of si1nal processin14 Most 6spectrum analysers7 use an FFT
al1orithm4 Some applications of spectral estimation are'
determinin1 fre!uency Q loudness of a musical note2
decidin1 ,hether a si1nal is periodic or non-periodic2
analysin1 speech to reco1nise spo8en ,ords2
loo8in1 for sine-,aves 0uried in noise2
measurin1 fre!uency distri0ution of po,er or ener1y
These transforms have many other uses in si1nal processin12 e414 filterin14 To filter out a ran1e of
spectral components2
perform DFT or DCT2
set the appropriate spectral samples to 9ero
perform inverse DFT or DCT4
The DCT is used in M>= compression4 Spectral components ,e can7t hear are 9eroed to save 0its4
The FFT al1orithm can 0e very efficiently implemented in hard,are to apply the DFT in real time
to si1nals4 This means that the analysis of one 0loc8 or a si1nal is completed ,hile the ne#t 0loc8
is still 0ein1 collected4 The use of this techni!ue in real time communications is the 0asis of M>=
decodin1 and a modulation techni!ue 8no,n as Fortho1onal fre!uency division multiple#in1F
(IFDM4 This techni!ue is used in di1ital radio and TR transmitters and receivers2 GSDH modems
and /CCC K024..1 and K024..a ,ireless HG; transmitters and receivers4
;o, illustrate filterin1 0y usin1 the DCT Q /DCT to remove the hi1her fre!uency sine-,ave from
the si1nal ,e have 0een analysin1 in these notes4
clear all7 :32,7
or n30::)1 &%15n(3100;cos%%pi4,(;n(5100;sin%1!622;n(7 end7
$ 3 dct%&(7 or <326:,0 $%<( 3 07 end7
+3idct%$(7 plot%+(7 grid on7
. ' 0 2 . ' 0 for +
2
. 2
cos +
2
. 2
cos % 2 $ % 2 $
.
0
.
0

,
_

,
_

,
_

,
_

M N k M
m
N k
n
m n x k X
N
n
M
m

Comp=029. DM> ?49 @MAC Dec709
0 10 20 30 40 50 60 70
-150
-100
-50
0
50
100
150
?4642 Use of the FFT to analyse music
Ta0le D at the end of these notes presents a MGTHG@ pro1ram ,hich reads a passa1e of music
from a 6,av7 file2 splits it into E.2 sample sections and performs a DFT (0y FFT analysis on each
section4 G 1raph of a section of violin music and its ma1nitude spectrum is sho,n 0elo,4
0 100 200 300 400 500 600
-0.5
-0.4
-0.3
-0.2
-0.1
0
0.1
0.2
0.3
0.4
0 50 100 150 200 250 300
0
5
10
15
20
25
30
35
?464= Spectral analysis of speech
File' =P>?9T=?!pcm contains sampled speech and S:?)12d@!pcm contains a sine-,ave
corrupted ,ith noise4 The si1nals are sampled at K 859 usin1 .2-0it G+D converter4 They may 0e
read into the SMGTHG@S pro1ram in Ta0le E Q spectrally analysed usin1 the FFT4 /t ,ould 0e
meanin1less to analyse a lar1e speech file at once4 So ,e divide it into 0loc8s of samples and
analyse each 0loc8 separately4 @loc8s of ; (* E.2 samples may 0e read in and displayed4 The
speech analysis pro1rams in ta0le E at the end of these is also availa0le on'
ABarr+4m+docs4Comp30291
G E.2 sample 0loc8 of male voiced speech is sho,n 0elo, (left ,ith its ma1nitude spectrum2 in
d@2 (ri1ht4
This is the time-domain si1nal after idct4
There7s no si1n of the hi1her fre!uency sine-
,ave2 0ut the 6ed1e7 effects at the 0e1innin1
Q end need to 0e improved4 Gs ,e are
usin1 6dct7 Q correspondin1 6idct7 ,e don7t
have to ,orry a0out the funny scalin14
Comp=029. DM> ?4.0 @MAC Dec709
0 100 200 300 400 500 600
-1500
-1000
-500
0
500
1000
1500
0 50 100 150 200 250 300
40
50
60
70
80
90
100
G second e#ample of male voiced speech 0loc8 produced the follo,in1 1raphs
0 100 200 300 400 500 600
-1200
-1000
-800
-600
-400
-200
0
200
400
600
800
0 50 100 150 200 250 300
40
50
60
70
80
90
100
G se1ment of voiced female speech and its ma1nitude spectrum is sho,n 0elo, as a third e#ample
0 100 200 300 400 500 600
-800
-600
-400
-200
0
200
400
600
0 50 100 150 200 250 300
40
50
60
70
80
90
100
/n these e#amples2 6pseudo-periodicity7 is seen in the time-domain for voiced speech (vo,els4 The
term 6pseudo-periodicity7 means that the si1nal is not precisely periodic li8e a fi#ed fre!uency sine
or s!uare ,ave4 The shape of the si1nal is slo,ly chan1in1 as the spo8en ,ord pro1resses4 @ut
there is stron1 similarity 0et,een successive short se1ments2 of ,hich there are a0out ..2 K and 2E
respectively in the three time domain speech 1raphs sho,n a0ove4
Cach of the ma1nitude-spectra has a fundamental Q many harmonics4 Be can measure the
fundamental to determine pitch of the voice4 Male voiced speech has 1enerally has a lo,er
fundamental fre!uency than female speech4 Unvoiced speech se1ments (consonants have no or
much less pseudo-periodicity4 6Formants7 are seen as pea8s in the spectral envelope and they are
caused 0y vocal tract resonance4 Be can determine vo,el sound (a e i o u etc from 0y
Comp=029. DM> ?4.. @MAC Dec709
determinin1 the fre!uencies of the formants4 /n principle2 ,e can do speech reco1nition this ,ay4
@it-rate compression is 0ased on the same understandin14
?464D4 Gpply 2-d DCT to a picture
Consider the processin1 of an ori1inal picture 6autumn4tif7 availa0le ,ith MGTHG@4
clear all:
r10pic * imread(Fautumn4tifF: imvie,(r10pic: N input ima1e Q display
0,pic * r1021ray(r10pic: N Convert to 1ray scale
imvie,(0,pic: im,rite(0,pic2F0,aut4tifF2FtifF: N Display Q store ima1e
@Bspectrum * dct2(0,pic: N Gpply dct
fi1ure(.: imsho,(lo1(a0s(@Bspectrum2$%2 colormap()et2 color0ar:
@Bspectrum(a0s(@Bspectrum3.0 * 0400.: N Ma8e 9ero if 3.0
fi1ure(2: imsho,(lo1(a0s(@Bspectrum2$%2 colormap()et2 color0ar:
reconpic * idct2(@Bspectrum: N Gpply inverse dct
imvie,(reconpic2$0 2EE%: im,rite(0,pic2F0,reconaut4tifF2FtifF: N Display Q store
This pro1ram ta8es a coloured picture Q converts it to 1rey scale4 /t then ta8es a 2-d DCT Q plots
a 2-D ma1nitude-spectrum ,ith a colour scale indicatin1 the spectral amplitudes4 ;otice a
concentration of ener1y in the top corner4 Be then set to 9ero any ener1y values 3.04 This is
useful for ima1e compression as ,e don7t have to code these4 Then 1o 0ac8 to an ima1e via an
inverse 2-d DCT4 Be can see the reconstructed ima1e and its modified spectrum (,ith lots of
0lue4 /s there any perceiva0le loss of !ualityL

=riginal picture Cautumn!ti8
Comp=029. DM> ?4.2 @MAC Dec709
=riginal D reconstructed images Eith DCT spectra
7!7! FindoEing and the DFT
Understandin1 the results o0tained from the DFT (or FFT re!uires an appreciation of the
com0ined effects of ,indo,in1 and fre!uency-domain samplin14 To 1ain this appreciation2 it is
useful to concentrate on the DFT analysis of sinusoids as ,e have done already4 Such analysis has
revealed some fundamental pro0lems ,hich must 0e addressed4 Be must first consider the effect
on its DTFT of applyin1 a rectan1ular ,indo, to a si1nal4 To 0e a0le to do this2 ,e must derive
the DTFT of a rectan1ular ,indo, and understand somethin1 a0out 6fre!uency-domain7 or
Fcomple#F convolution4
The DFT of "#$0%2 #$.%2 4442 #$;-.% & is a fre!uency sampled version of the DTFT of the infinite
se!uence "#$n%& ,ith all samples outside the ran1e 0 < n N set to 9ero4 "#$n%& is effectively
multiplied 0y the 6rectan1ular ,indo,7 se!uence "r$n%&
,here [ ] r n
n N

<

'

. 0 '
0 ' other,ise
/t may 0e sho,n that ,hen ; is even2 the DTFT2 [ ] R e
j
( 2 &

of "r n is a 6sinc-li8e7 function as


plotted a1ainst 0elo, for a 20
th
order rectan1ular ,indo,'
-5
0
5
10
-5
0
5
10
=riginal
?econstructed
Comp=029. DM> ?4.= @MAC Dec709

sincs10(W/(2pi))
-12
-8
-4
0
4
8
12
-6 -4 -2 0 2 4 6 8
W rads/sample
R
(
e
x
p
(
j
W
)
)
/ts modulus is as sho,n 0elo,'-

M odulus of
Dirichlet Kernel of order 10
0
2
4
6
8
10
-5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
W rads/sec
|
s
i
n
c
s
1
0
(
W
/
(
2
p
i
)
)
|
This 6sinc-li8e7 function is also encountered ,hen desi1nin1 F/T filters as discussed in Section D2
and causes the stop-0and ripples to appear ,hen F/T lo,-pass filters are desi1ned ,ith rectan1ular
,indo,s4 The ma1nitude of T(e
)
is sho,n a0ove for a 20
th
order rectan1ular ,indo,4 ;ote the
relatively narro, main lo0e and the side-lo0es4 /ts 9ero-crossin1s occur at
t t 2 D + 2 + 2 M M etc4 /t is li8e a 6sinc7 function in many ,ays4 /n fact2 it is simply an
Faliased sincF4
?4?424 Fre!uency-domain (comple# convolution'
The product of "#$n%& and "r$n%& is "#$n% r$n%&4 /t may 0e sho,n (see Section D that the DTFT of
this product is a form of convolution 0et,een the correspondin1 DTFT spectra4
Multiplication in the time!omain "#in!o#in$% is e&ui'alent to complex con'olution in the
fre&uenc( !omain)*
?4?4=4 DTFT of a sampled sine-,ave
Consider the DTFT of "cos(
0
n& ,hich e#ists for all time4 Gll its ener1y is concentrated at
0
Q
it has infinite ener1y4 The ma1nitude spectrum has impulses at * t
0
as sho,n 0elo,'
Comp=029. DM> ?4.D @MAC Dec709




$%e&p%' ((
The value in the dia1ram a0ove is an Fimpulse stren1thF not an amplitude4
?4?4D4 DTFT of a rectan1ularly ,indo,ed sampled sine-,ave
The DTFT of "cos(
0
n&
02;-.
is e!ual to the DTFT of "cos(
0
n4r$n%&4 @y fre!uency-domain
(comple# convolution2 it may 0e sho,n that this is' >(e
)
as plotted a1ainst 0elo,'
The ma1nitude of this DTFT spectrum is as sho,n 0elo,
-2
0
2
4
6
8
10
-4 -3 -2 -1 0 1 2 3 4
radians/sample

0
)
0
P%e
'
(
-4 -3 -2 -1
0
1
2
3
4
5
6
7
8
9
10
0 1 2 3 4
rad/si
|
P

(


)
|
Comp=029. DM> ?4.E @MAC Dec709
Gs a result of the convolution2 the t,o fre!uency-domain impulses of value have no, 0ecome
t,o sinc-li8e Fmain lo0esF2 each of amplitude a0out ten2 surrounded 0y FripplesF or Fside-lo0esF ,ith
lo,er amplitude4 I0viously2 ,e are hopin1 that the pea8s tell us somethin1 a0out the amplitude
and fre!uency of the cosine ,ave and that ,e can i1nore the side-lo0e ripples4 The amplitude of
main pea8 (i4e4 ten divided 0y ;+2 1ives the amplitude of the sine-,ave4
/ncreasin1 ; 1ives a hi1her and sharper pea8 Q more ripples4
The 1raph of the modulus of >(e
)
a1ainst for
< < 2
is a pair of sinc - li8e functions
(modulus ta8en centred at

0
and

0
2 ,ith 9ero crossin1s at * t
0
t 2+;2
t t
0
D + N 2 etc4 5ence2 the effect of the rectan1ular ,indo, is to cause fre!uency-domain
spreadin1 or Slea8a1eS (i4e4 the introduction of side-lo0es due to the comple#-convolution of
((e
)
,ith T(e
)
4
7!G! DFT o a rectangularl+ EindoEed sinusoid
The DFT produces a fre!uency-sampled version of >(e
)
4 The perceived effect of fre!uency
spreadin1 com0ined ,ith fre!uency-domain samplin1 can 0e confusin14
Consider the analysis of cos(
0
n t,o cases '
(. ,here
0
lies 0et,een t,o fre! samplin1 pts
(2 ,here
0
lies e#actly on a fre! samplin1 pt
(The amplitude of the sinusoid analy9ed is .2 0ut the DTFT has not 0een divided 0y ;+24 So the
DTFT pea8 value is ;+2 * .0 rather than . in these 1raphs4
Sampled DTFT o EindoEed sine)Ea*e %case 1(
0
1
2
3
4
5
6
7
8
9
10
-4 -3 -2 -1 0 1 2 3 4
rad/si
|
P

(


)
|
# P%e
'
( U

0
Comp=029. DM> ?4.6 @MAC Dec709
Sampled DTFT o EindoEed sine)Ea*e %case 2(
The effect of rectan1ular ,indo,in1 on the DFT can no, 0e summarised4 Aiven a sine-,ave of
ar0itrary fre!uency2 ,e don7t 8no, ,hether or not it ,ill line up ,ith one of the fre!uency
samplin1 points4 /f it does2 as in case 22 the pea8 of the main lo0e ,ill 0e sampled and all other
fre!uency-domain samples ,ill 0e at the 9ero-crossin1s of the sinc-li8e function4 /f it does not2 as
in case .2 t,o samples ,ill 0e ta8en of the main lo0e at either side of the pea8 value4 Be miss the
pea82 and samples of the 6ripples7 are seen in the DFT 1raph4 /f ,e ta8e one of the samples at
either side of the pea8 as an indication of the amplitude of the sine ,ave2 the measurement ,ill 0e
in error4 The ,orst case is the one ,e have illustrated ,ith
0
occurrin1 e#actly 0et,een t,o
fre!uency samplin1 points4 /n this case2 the measurement ,ill 0e reduced 0y =EN compared ,ith
,hat ,ould have 0een o0tained 0y samplin1 the pea84 /f
0
4 is not !uite in the centre 0et,een t,o
samplin1 points2 the reduction ,ill not 0e as much4 So ,ith a rectan1ular ,indo,2 there can 0e an
inaccuracy of up to a0out =EN in our estimation of the amplitude of a sine-,ave4 The DFT 1raphs
o0tained are illustrated in fi1 . (for Case. and fi1 2 (Case 2 later in these notes4
Fi1ure . sho,s
X k
for a 6D-point DFT of a sinusoid of amplitude . and fre!uency 04?=6=
radians+sample4 Inly the first =2 points need 0e plotted and sample =2 corresponds to

or
f
2
+2 594 Therefore2 the fre!uency-domain samplin1 points are


0 . 2 = =2
0 =2 .6 = =2 2 + 2 + 2 + 2 4444 2 4

;ote that 04?=6= lies 0et,een
?
and
K
4 Samples of the rectan1ular ,indo,Fs fre!uency-
response2 ,ith its side-lo0es2 are seen2 and fre!uency samples ? and K are stron1ly affected as they
are closest to the centre of the main lo0e4 Gs there is no fre!uency samplin1 point at the pea8 of
the main lo0e2 this is not seen4
The DFT ma1nitude spectrum
X k
in Fi1ure 2 ,as o0tained 0y analysin1 a sampled sinusoid of
relative fre!uency 04?KED (*

+D instead of 04?=6=4 The effect of the rectan1ular ,indo, is no


lon1er seen 0ecause the samplin1 points happen to coincide e#actly ,ith the 9ero-crossin1s of the
( ) 2 +( sincs
N
function4 This al,ays occurs ,hen the fre!uency of the cosine ,ave coincides
e#actly ,ith a fre!uency samplin1 point2 in this case

K
4
0
1
2
3
4
5
6
7
8
9
10
-4 -3 -2 -1 0 1 2 3
rad/s
i
|
P

(


)
|
# P%e
'
( U

0
Comp=029. DM> ?4.? @MAC Dec709
The difference 0et,een Fi1ures . and 2 is undesira0le2 especially as the same amplitude sinusoid
1ives a lo,er spectral pea8 in Fi1 22 i4e4 a0out 2.2 than in Fi1ure .4 This is a difference of
20lo1
.0
(=2+2. D d@2 compared ,ith ho, it appears in Fi1 .4 This situation is improved 0y the use
non-rectan1ular ,indo, functions4
7!9! Hse o the DFT Eith non)rectangular EindoEs
?494. 5ann ,indo,' G simple non-rectan1ular ,indo, is the 5ann ,indo, defined for 0 < n N
as follo,s'
,$n% *
04E - 04E cos((2n <. + ; '
0 ' other,ise
0 <

'

n N
Hann window
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
0 2 4 6 8 10 12 14 16 18 20
n
w
[
n
]
The 5ann ,indo, ,as encountered in Section D2 althou1h ,e have had to re-define it no, for the
ran1e 0 < n N rather than -;+2 n ;+24 The 5ann ,indo, has a raised cosine shape in the
discrete time-domain4 Aiven "#$n%&
02;-.
the 5ann ,indo, is applied 0y multiplyin1 to o0tain
"#$n%,$n%&
02;-.
4 The effect of the ,indo,in1 on a sine-,ave (left as seen in the time-domain is
sho,n 0elo, (ri1ht4 G rectan1ular ,indo, ,ould a0ruptly truncate the sine-,ave at the ,indo,
ed1es ,hereas the 5ann ,indo, 1radually tapers the amplitudes of the sin-,ave to,ards 9ero at
the ,indo, ed1es4
0 20 40 60 80 100 120 140
-100
-80
-60
-40
-20
0
20
40
60
80
100
0 20 40 60 80 100 120 140
-100
-80
-60
-40
-20
0
20
40
60
80
100
Cffect of rectan1ular ,indo, (left and 5ann ,indo,(ri1ht in time-domain4
/n the fre!uency-domain2 the effect of fre!uency samplin1 the sinc-li8e DTFT spectrum created 0y
the rectan1ular ,indo, has 0een discussed and is further illustrated 0elo, for )ust the main-lo0e4
The main lo0e is of hei1ht .0;+2 centred on
0
2 and drops to 9ero on either side ,ithin 2+;2 i4e
at
0
4t 2+;4 /ts ,idth is D+;4 The DFT samples this main lo0e at intervals of 2+;4 Bith luc8
Comp=029. DM> ?4.K @MAC Dec709
there is a sample e#actly in the centre (case 22 ,ith t,o others samplin1 the 69ero-crossin1s7 at
either side4 Ither,ise ,e miss the centre Q 1et t,o lo,er amplitudes (case .4
The DTFT of a 20th order 5ann ,indo, compared ,ith that of a 20th order rectan1ular ,indo,2
a1ain ne1lectin1 the e
-);+2
term2 is as follo,s4
Hann window order 20 compared with rectan!lar"
-5
0
5
10
15
20
25
-3.14 -2.355 -1.57 -0.785 0 0.785 1.57 2.355 3.14
Radians/sample
W
(
e
x
p
(
j
w
)
)
Rect
R2
R3
Hann
/n the fre!uency-domain2 ,e can ma8e the follo,in1 o0servations'
(i There is a 0roader main-lo0e for 5ann ,indo, and its ,idth is appro#imately dou0led as
compared ,ith the main lo0e o0tained ,ith a rectan1ular ,indo, of the same order4
(ii /ts ma#imum hei1ht is appro#imately halved4
(iii The side-lo0e levels have 0een reduced4

10:42

10:4,
10:4,
Comp=029. DM> ?4.9 @MAC Dec709
The main lo0e for the 5ann ,indo, is plotted a0ove for case 2 and case . as defined in the
previous section4 /t may 0e seen that at least three fre!uency domain samples (sometimes called dft
or fft spectral F0insF of the main lo0e ,ill 0e ta8en even ,hen the sine-,ave fre!uency
0
coincides e#actly ,ith a fre!uency samplin1 point(case 24 Be often ta8e the hi1hest of these as the
measured amplitude of the sine-,ave4 Three samples are ta8en in case 22 and in case . there are D
fre!uency domain samples4 /t may 0e sho,n that ,ith a 5ann ,indo,2 there ,ill 0e an amplitude
variation of only a0out .EN ,hich is still undesira0le2 0ut not as 0ad as the =EN ,e 1ot ,ith a
rectan1ular ,indo,4 This reduction in amplitude estimation error for sine-,aves is at the e#pense
of some loss of spectral resolution since2 ,ith a 5ann ,indo,2 three or four ad)acent fre!uency
0ins are al,ays stron1ly affected 0y a sin1le sine-,ave and ,e ,ill only 8no, that the fre!uency
of the sine-,ave 0ein1 analysed lies ,ithin the ran1e of these three or four fre!uencies4
?4942 Ither non-rectan1ular ,indo,s
Iamming ' very similar to 5ann 0ut a little 0etter
Jaiser: offers a ran1e of options from rectan1ular2 throu1h 5ammin1 to,ards ,indo,s ,ith even
lo,er ripples ,ith 0roader main lo0es4 MGTHG@ command' VB * 8aiser(;20eta produces a
Vaiser ,indo, array of len1th ; for any value of 0eta ( P 04 Bhen * 02 this is a rectan1ular
,indo,4 Bhen * E4DD.D ,e 1et a 5ammin1 ,indo,4 /ncreasin1 further 1ives further
reduced ripples Q 0roader main-lo0e4
@lac8man2 flat-top2 @artlett2 ,indo,s and many more e#ist44
?494=4 Gnalysis of the sum of 2 sine-,aves ,ith+,ithout 5ann ,indo,'
The effect of time-domain ,indo,in1 as seen in the fre!uency-domain2 causin1 difficulties in
estimatin1 the true amplitudes of sine-,aves2 is often referred to as the Fpic8et fence effectF4 This
0 5 10 15 20 25 30 35
0
20
40
60
80
100
120
0 10 20 30 40 50 60 70
-200
-150
-100
-50
0
50
100
150
200
0 10 20 30 40 50 60 70
-100
-50
0
50
100
150
200
0 5 10 15 20 25 30 35
0
5
10
15
20
25
30
35
40
45
50
?ectangular
Iann EindoE
Comp=029. DM> ?420 @MAC Dec709
effect is present ,ith other si1nals2 0ut is clearly seen ,ith sinusoids4 Be have seen that this effect
is reduced2 at the e#pense of spectral resolution2 0y non-rectan1ular ,indo,in14
7!10! >nerg+ and Parse*al8s Theorem
The Wener1yX of "#$n%&
02;-.
is'

.
0
2
% $ (
N
n
n x +
and the mean s!uare value (MSR of this se1ment is'

( + ( $ % .
2
0
.
N x n
n
N

* (.+; C
The MSR 0e ta8en as the Wpo,erX of a periodic discrete time si1nal2 of period ; samples2 for
,hich a sin1le cycle is "#$n%&
02;-.
4 /t may 0e used as an estimate of the po,er of "#$n%&4
?4.04.4 >arsevalFs Theorem for the DFT'
/t may 0e sho,n that for a real si1nal se1ment "#$n%&
02;-.
'

( [ ]) [ ] x n
N
Xk
n
N
k
N
2
0
1
2
0
1
1

>arseval7s Theorem allo,s ener1y and hence po,er estimates to 0e calculated in the fre!uency-
domain instead of in the time-domain4 This is e#tremely useful as it allo,s us to see ho, the
po,er of a si1nal "#$n%& is li8ely to distri0uted in fre!uency4 /s there more po,er at hi1h
fre!uencies than at lo, fre!uencies or vice-versaL
@y >arsevalFs theorem an estimate of the po,er of "#$n%&2 o0tained 0y analysin1 a se1ment
"#$n%&
02;-.
2 is'

.
0
2 2
.
0
2
% $ ( + . ( % $ ( + . (
N
k
N
n
k X N n x N
The usefulness of this estimate is illustrated 0y the follo,in1 e#ample4
?4.042 C#ample' G real periodic si1nal "#$n%& is rectan1ularly ,indo,ed to 1ive "#$n%&
02=9
,hose
D0-point DFT ma1nitude spectrum is as follo,s for samples 0 to 20'-
Comp=029. DM> ?42. @MAC Dec709

10
20
30
,0
0
0 2 3 1 , 6 2 7 G 10 12 1, 12 1G 20
<
# $.</ #
Cstimate the po,er of "#$n%& and comment on ho, relia0le this estimate is li8ely to 0e4
/f "#$n%& is passed throu1h an ideal di1ital lo,-pass filter ,ith cut-off fre!uency +2 radians+sample
ho, is the po,er li8ely to 0e affectedL
Solution' MSR of "#$n%&
02=9
po,er of "#$n%&
* (.+.600$2YD0
2
<2Y=0
2
<2Y20
2
<2Y.0
2
% * =4?E s!uared-volts4
Teduces to =4.2E i4e4 0y 04K d@
Some care must 0e ta8en in interpretin1 this result and ,hat is meant 0y the po,er estimate4 For
periodic or deterministic (non-random si1nal estimates from se1ments e#tracted from different
parts of "#$n%& may 0e similar2 and the estimates could 0e fairly relia0le4 5o,ever2 for random
si1nals2 there ,ill 0e considera0le variation from estimate to estimate4 Some form of avera1in1 may
0e necessary4
?4.04=4 >o,er spectral density estimate
For an ;-point DFT2
|($8% |
2
+ ;
2

is an estimate of Fpo,er spectral densityF in units of Batts per W0inX4
G W0inX is a 0and-,idth 2+; rad+sample centred on
8
/nstead of U($8%U a1ainst 82 ,e often plot .0 lo1
.0
(U($8%U
2
+;
2
d@4 a1ainst 8 as Wpo,er spectral
density estimateX 1raph4
Must ta8e care ,ith random si1nals4 Cach spectral estimate ,ill 0e different4 Some form of
avera1in1 is often employed4
?4.04D4 Spectral analysis of si1nals usin1 MGTHG@'
Much can 0e learnt a0out the nature of speech and other si1nals 0y e#aminin1 their DFT spectra4
The follo,in1 files are availa0le for do,nloadin1 from
,,,4cs4man4ac4u8+Z0arry+mydocs+Comp=029.+MGTHG@

operator4pcm containin1 sampled speech
noisycos4pcm containin1 a sine-,ave corrupted ,ith additive noise4

Comp=029. DM> ?422 @MAC Dec709
These files ,ere o0tained 0y samplin1 short se1ments of speech or other si1nals at K 859 usin1 a
.2-0it G+D converter4 The si1nal from any of these files may 0e read into the SMGTHG@S pro1ram
1iven in Ta0le E and spectrally analysed usin1 the FFT4
The effectiveness of DFT 1raphs in locatin1 the presence of a sine-,ave in noise2 even ,hen the
sine-,ave cannot 0e seen in a time-domain 1raph2 may 0e demonstrated 0y runnin1 the pro1rams in
Gppendi# G2 availa0le also on the ,e0 site4 The effect of avera1in1 to improve spectral estimates
may also 0e demonstrated4 For details2 see the notes in Gppendi# G4
7!9! ProBlems
.4 Do,nload and run the MGTHG@ pro1rams presented in Gppendi# G4 Bhy is avera1in1 effective
in locatin1 the sinusoids hidden in noiseL
24 Try the assi1nment presented in appendi# @4
For further pro0lems on this section see past e#am papers on ,e0-site4
Comp=029. DM> ?42= @MAC Dec709

0 5 10 16 20 30 32
k
5
10
15
|X(k)|
() (/2)
Fi1ure . ' Ma1nitude of 6D point DFT spectrum of cos(04?=6=n
0 8 16 32
k
32
|X(k)|
() (/2)
Fi1ure 2 ' Ma1nitude of 6D point DFT of cos(n+D4
Comp=029. DM> ?42D @MAC Dec709
TaBles reerred to in Section 7
++ >TIATGM D/TCCTJDFT: Direct DFT test pro1ram4
[include 3stdio4hP
[include 3stdli04hP
[include 3math4hP
[include 31raphics4hP
[define >/ =4.D.E926ED
float #r$E.2%2 #i$E.2%2 #$E.2%2 y$E.2%: int ;2 /nvers:
void directdft(void ++ DFT or /nverse DFT 0y direct method4
" ++ Irder*;2 Teal Q ima1 parts of input in arrays #r Q #i
++ Iutput'- Teal part in array (2 /ma1 part in \
++ /nvers is 0 for DFT2 . for /DFT
int 82 l: float c2e2s2,8:
if(/nvers**. e* -240Y>/+(float;: else e* 240Y>/+(float;:
for(8*0:83;:8<<
" #$8%*040: y$8%*040 : ,8*(float8Ye :
for(l*0: l3;: l<< " c*cos(lY,8: s*sin(lY,8:
#$8% * #$8% < #r$l%Yc < #i$l%Ys:
y$8% * y$8% < #i$l%Yc - #r$l%Ys:&
if(/nvers**. " #$8%*#$8%+(float;: y$8%*y$8%+(float;:&
&
&
void Dra,1raph(void ++;4@4 For TUT@I C2 needs CAGRAG4@A/ in directory4
" int 82i#2iy2dr2mode: float md:
detect1raph(Qdr2Qmode: init1raph(Qdr2Qmode2SS: ++/nitialise 1raph mode4
clearvie,port(: setcolor(.E: rectan1le(0202E.22D=0:
for(8*0: 83;+2<.: 8<<
" iy * D=0- (int((float8YK0040+(float;: line(02iy2=2iy:&
for(8*0: 83;+2: 8<<
" md*s!rt(#$8%Y#$8%<y$8%Yy$8%: iy * D=0- (int(mdYK0040+(float;:
i#* (int ((float8 Y.02D40+(float;: line(i#2D=22i#2iy:
& 1etch(: close1raph(:
&
main(
" int i: float ,:
;*6D: ++ DFT order4 /nvers * 0: ++Te!uest DFT rather than inverse DFT4
printf(SCnter relative fre!uency in radians+samplin1 interval' S:
scanf(SNfS2 Q,:
for(i*0:i3;: i<< ++Store ; samples of cos(n as test si1nal'-
" #r$i%*cos(,Y(floati: ++ Teal part of input
#i$i%* 040 : ++ /ma1 part of input
&
directdft(: ++Call DFT procedure'-
for(i*0:i3;: i<< "printf(SNDd N?4=f N?4=f]nS2 i2#$i%2y$i%:&
Dra,1raph(:
&
T9@K> 1: DFT B+ Direct Method programmed in LCL
Comp=029. DM> ?42E @MAC Dec709
void fft(void
++Irder*;2 must 0e po,er of 24 In entry2 real pt of input in (2
++ ima1 pt in \2 /nvers*0 for DFT2 . for /nverse DFT4
++ In e#it2 real Q ima1 parts of FFT or /FFT in ( Q \
" int ;.2;22;M2;2M2i2)282l: float e2c2s2a2#t2yt:
;2*; : ;M*;-.:
if(/nvers**. e* ->/+(float;: else e* >/+(float;:
for ( 8*2: 83;<.: 8*8<8
" ;.*;2: ;2*;2+2: ;2M*;2-. : e*eY240: a*040:
for()*0:)3 ;2M<.: )<<
" c*cos(a: s*sin(a: a*a<e:
for( i*): i3;M<.: i*i<;.
" l*i<;2: #t*#$i%-#$l%: #$i%*#$i%<#$l%:
yt*y$i%-y$l%: y$i%*y$i%<y$l%:
#$l%*#tYc<ytYs: y$l%*ytYc-#tYs:
&
& ++end of ^ loop
&
if(/nvers**.
for(8*0: 83(;M<.: 8<< "#$8%*#$8%+(float;: y$8%*y$8%+(float; :&
)*0:
for(i*0: i3;-.: i<<
" if(i3) "#t*#$)%: #$)%*#$i%: #$i%*#t: yt*y$)%: y$)%*y$i%: y$i%*yt:&
8*;+2: ,hile((8-.3) " )*)-8: 8*8+2:& )*)<8:
& ++ end of i loop
& ++ end of procedure FFT
T9@K> 2: FFT Procedure in LCL to replace D"?>CTDFT procedure in TaBle 1
Comp=029. DM> ?426 @MAC Dec709
Programs in M9TK9@
N Ta0le =' DFT 0y Direct Method (dmp?t=4m
disp(F>erforms DFT on ,indo,ed cosine ,aveF:
disp(FSet relative fre!uency of cosine ,ave' F:
B*pi+D:
; * 6D : N DFT order:
/nvers * 0: N Specifies DFT rather than inverse DFT
N Store ; samples of cos(BYn as test si1nal'- &
for n *0 ' ;-.
#(n<. * cos(nYB < )Y 0 :
end:
fi1ure (.: plot($0';-.%2#: 1rid on:

N Start DFT procedure'-
N ******************
if /nvers **.
C * -2Ypi+;
else
C * 2Ypi+;
end:
for 8*0 ' ;-.
((.<8 * 0 < )Y0 : B8 *8YC :
for H * 0 ' ;-.
C * cos(HYB8 < ) Ysin(HYB8:
((.<8 * ((.<8 < #(.<H Y C:
end:
if (/nvers ** .
((.<8*((.<8+;:
end:
end:
N **********************
fi1ure(2: plot($0';-.% 2 a0s((2FrY F: 1rid on:
N *************************
TaBle 3 : DFT B+ direct method using comple& arithmetic in M9TK9@
%>as+ to understand But *er+ sloE(
:ote' To increase speed usin1 the fft in MGTHG@2 replace the direct DFT statements 0y'
if invers**0
(*fft(#2;:
else
#*ifft((2;:
end:
,,,4cs4man4ac4u8+Z0arry+mydocs+Comp=029.+MGTHG@
Comp=029. DM> ?42? @MAC Dec709
N Ta0le D' MGTHG@ pro1ram (dmp?tD4m to DFT analyse a music file
clear all:
disp(FIpen the ,av file containin1 music' F:
$music2 fs2 n0its% * ,avread(FcapDth4,avF:
H*len1th(music:
disp(F>erforms DFT on ,indo,ed music si1nalF:
; * E.2 : N DFT order:
N*******************************
for frame * . ' fi#(H+;
for n *0 ' ;-.
#(.<n * music( .< (frame-.Y; < n < )Y 0 :
end:
fi1ure (.: plot($0';-.% 2 #: 1rid on:

N Start DFT procedure'-
N ******************
(*fft(#2;:
N **********************
fi1ure(2: plot( $0';+2-.%2a0s(((.';+2 : 1rid on:
SIU;D($# # #%2fs2n0its:
pause:
disp(F>ress return for ne#t frameF:
end:
TaBle ,: M9TK9@ program %dmp7t,!m( to spectrall+ anal+se a music ile Lcap,th!Ea*L
:ote' this pro1ram Q the music file capDth4,av is availa0le on '
,,,4cs4man4ac4u8+Z0arry+mydocs+Comp=029.+MGTHG@
Comp=029. DM> ?42K @MAC Dec709
NMGTHG@ pro1ram (dmp?tE4mfor spectrally analysin1 speech in operator4pcm'-
clear all:
disp(FIpen pcm file of speech sampled at K859 ,ith .6 0its+sampleF:
/Fid*fopen(Foperator4pcmF2Fr0F:
N Tead all samples from operator4pcm'-4
speech * fread(/Fid2 Fint.6F:
H*len1th(speech:

; * E.2 : N DFT order:
5*hann(;: N samples of a 5ann ,indo, of order ;
N*******************************
disp(F>erforms DFT on frames of 5ann-,indo,ed speechF:
for frame * . ' fi#(H+;

for n *0 ' ;-.
#(.<n * speech( .< (frame-.Y; < n < )Y 0 :
end:

fi1ure (.: plot($0';-.%2#: G(/S($0 ;-. -2000 2000%: 1rid on:

N ******************
N Start DFT procedure'-
,indo,ed#*#4Y5F:
(*fft(,indo,ed#2;:
N **********************
fi1ure(2: plot( $0';+2-.%220Ylo1.0(a0s(((.';+2 : 1rid on:
SIU;D(#+D0002K0002.6: N listen to the frame
pause:
disp(F>ress return for ne#t frameF:
end:

fclose(FallF:
TaBle 6: M9TK9@ script %dmp7t6!m( to spectrall+ anal+se speech in operator!pcm
:ote' this pro1ram Q the speech file operator4pcm is availa0le on'
,,,4cs4man4ac4u8+Z0arry+mydocs+Comp=029.+MGTHG@
The file contains monophonic speech sampled at K 859 ,ith .6 0its+sample4
Comp=029. DM> ?429 @MAC Dec709
9ppendi& 9
Further M9TK9@ programs and e&periments
N play,av4m' 5ear the ,av file FcapDth4,avF'-
$#2 fs2 n0its% * ,avread(FcapDth4,avF:
SIU;D(#2fs2n0its:
N playpcm4m' hear a pcm file sampled at K8592 .6 0its+sample
/Fid*fopen(Foperator4pcmF2Fr0F:
speech * fread(/Fid2 Fint.6F:
SIU;D(speech+D0002K0002.6: fclose(FallF:
N noisycos1en4m' 1enerates a cosine adds noise to it Q stores it in noisycos4pcm
B*pi+.0 : N one fifth of samplin1 fre!uency
for n*0 ' 6000
#(.<n * D000Ycos(BYn < 20000Y(rand - 04E:
end:
SIU;D(#+D0002K0002.6:
IFid*fopen(Fnoisycos4pcmF2F,0F: f,rite(IFid2 #2 Fint.6F:
fclose(FallF:
Nanoisycos4m' Spectrally analyse noisycos4pcm'-
clear all:
disp(FIpen pcm file sampled at K859 ,ith .6 0its+sampleF:
/Fid*fopen(Fnoisycos4pcmF2Fr0F:
s * fread(/Fid2 Fint.6F: NTead from noisycos4pcm
H*len1th(s:
disp(F>erforms DFT on ,indo,ed speechF:
; * E.2 : N DFT order:
5*hann(;: N samples of a 5ann ,indo, of order ;
for frame * . ' fi#(H+;
for n *0 ' ;-.
#(.<n * s( .< (frame-.Y; < n < )Y 0 :
end:
fi1ure (.: plot($0';-.%2#: 1rid on:
,indo,ed# * #4Y5F: N apply 5ann ,indo,
(*fft(,indo,ed#2;: N perform DFT of order ;
fi1ure(2: plot( $0';+2-.%220Ylo1.0(a0s(((.';+2 : 1rid on:
SIU;D(#+D0002K0002.6:
pause: disp(F>ress return for ne#t frameF:
end: fclose(FallF:
:ote' Gmplitude of cosine ,ave is D000 so its po,er is (D000
2
+2 * KY.0
6
;oise is uniform 0et,een t.02000 so its po,er is (202000
2
+.2 ==4=Y.0
6
;oise po,er a0out D times the si1nal po,er4
The Fsi1nal-to-noise ratioF S;T .0 lo1
.0
(K+==4= -642 d@4
Cosine ,ave is difficult to see in time-domain2 0ut is easily seen in fft ma1nitude plot4
Comp=029. DM> ?4=0 @MAC Dec709
Navnoicos4m' Spectrally analyse noisycos4pcm ,ith avera1in1'-
clear all:
disp(FIpen pcm file sampled at K859 ,ith .6 0its+sampleF:
/Fid*fopen(Fnoisycos4pcmF2Fr0F:
s * fread(/Fid2 Fint.6F: NTead from noisycos4pcm
H*len1th(s:
disp(F>erforms DFT on ,indo,ed speechF:
; * E.2 : N DFT order:
5*hann(;: N samples of a 5ann ,indo, of order ;
for frame * . ' fi#(H+;
for n *0 ' ;-.
#(.<n * s( .< (frame-.Y; < n < )Y 0 :
end:
fi1ure (.: plot($0';-.%2#: 1rid on:

,indo,ed# * #4Y5F: N apply 5ann ,indo,
(*fft(,indo,ed#2;: N perform DFT of order ;
if frame**.
\*a0s(((.';+2:
else
\*\<a0s(((.';+2:
end
fi1ure(2: plot( $0';+2-.%220Ylo1.0(\+frame : 1rid on:
SIU;D(#+D0002K0002.6:
pause: disp(F>ress return for ne#t frameF:
end: fclose(FallF:
;ote' This pro1ram is the same as the previous e#cept that an avera1e of up to .0 ma1nitude
spectra is plotted4 ;otice ho, the cosine ,ave 0ecomes clearer and clearer as more and more
ma1nitude spectra are avera1ed4 ;otice also that the noise is appearin1 more and more spectrally
FflatF or ,hite across the fre!uency ran1e 0 to half the samplin1 fre!uency4
Teplacin1 Fnoisycos4pcmF 0y Fnoisyspe4pcmF in the pro1ram Fanoisycos4mF on the previous pa1e
allo,s you to spectrally analyse a speech file corrupted 0y noise4 Unfortunately avera1in1 ,ill not
,or8 no, as the speech is constantly chan1in14
Comp=029. DM> ?4=. @MAC Dec709
Gppendi# @
DMP e&periment
The file W5/MUS/C4pcmX contains a mono recordin1 of telephone 0and,idth speech or music
sampled at K 8594 /t is a 0inary file ,ith each sample occupyin1 .6 0its2 i4e4 t,o 0ytes4 Gs is
common2 the order of these 0ytes is 6least si1nificant 0yte7 follo,ed 0y 6most si1nificant 0yte7 and
ne1ative num0ers are represented in .6-0it t,o7s complement form
/t is availa0le from' ,,,4cs4man4ac4u8+Z0arry+mydocs+Comp=029.+MGTHG@
;ote that you cannot vie, the contents of this file directly usin1 a te#t editor2 0ut you can see and
hear them usin1 the MGTHG@ pro1rams listed a0ove or2 more conveniently2 usin1 a ,ave-editor
such as CIIHCD/T (no, mar8eted 0y GDI@C4
This sound file W5/MUS/C4pcmX has 0een corrupted 0y in t,o ,ays'
(a 0y the addition of hi1h-pass noise a0ove = 859
(0 0y the addition of a sinusoidal WtoneX ,hich starts a0out half ,ay throu1h the file4
This assi1nment is to develop a MGTHG@ DS> pro1ram ,hich is capa0le of processin1 this
recordin1 to improve its !uality4 /nitially you should spectrally analyse the music to discover the
fre!uency of the tone4 Then develop a 2-sta1e filterin1 pro1ram'
(a To apply a tenth order F/T lo,-pass di1ital filter ,ith cut-off fre!uency 2 859 to reduce the
hi1h fre!uency noise4
(0 To apply a second order //T WnotchX filter to remove the sinusoidal tone ,ithout affectin1 other
fre!uencies too severely4
G filtered sound file WMUS/C4pcmX should 0e produced4

Вам также может понравиться