lektronik
abor
Laboratory
Discrete Fourier and z
Transform Using Matlab
Martin J. W. Schubert
Electronics Laboratory
Regensburg University of Applied Sciences Regensburg
-1-
Abstract. This laboratory illustrated possibilities of time-discrete
Fourier and z Transformation Using Matlab.
1 Introduction
Preface
In the literature we frequently find acronyms like DFT, FFT and z transformation. Here FFT
stands for Fast Fourier Transformation, a particularly efficient computation of the DFT
assuming 2N data points with N being an integral number. The acronym DFT stand for
Discrete – or more precise Time-Discrete – Fourier Transformation. Discretization is a 2dimensional process: (i) value-discretization termed quantization one hand and (ii) equidistant
time-discretization termed sampling on the other.
Matlab [1], [2] is a powerful tool for computation and illustration of such mathematical
matters. Some similar freeware like GNU Octave [3] can be downloaded for free. This
laboratory assumes that you have some basic skills using Matlab, which can be obtained from
[4] or laboratory “LTI System Modeling Using Matlab” [5]. More theoretical insight can be obtained
from the authors chapter on time-discretization [6] and Fourier-Transformation and Symmerties [7] or
James W. Cooley and John W. Tukey [8].
Convention
In this document we write continuous functions with braces, e.g. x(t), and sampled function
with brackets, e.g. x[n] = x(tn).
The organization of this document is as follows
Section 1 is the introduction.
Section 2 offers some theoretical background on Fourier transformation.
Section 3 applies Matlab’s fast Fourier transform (FFT).
Section 4 introduces z Transformation for better frequency resolution and recursive functions.
Section 5 investigates window functions.
Section 6 lists code of functions used in this lab.
Section 7 offers some references.
-2-
2 Fundamentals on Fourier Transformation
2.1 Time-Continuous Case
2.1.1
The Fundamental Transformation
Fourier transformation F and its reverse F-1 are defined as
X (b) F{x(a)}
x(a ) e
j 2ab
da
(2.1)
X (b) e
x(a) F 1{ X (b)}
j 2ab
db
(2.2)
Note,
1. that theses transformations are identical except for a minus sign in the exponent which
affects phase only, not amplitude issues. The is what we call symmetry of back and forth
transformation, allowing for tremendous conclusions: What we have proved for one
direction will also hold for the other.
2. that a and b are arbitrary quantities and that the physical dimension of product ab must
not have a dimension as it is in the exponent of Euler number e. In this lab we will use a=t
for time and b=f for frequency. For a function with limited energy x(t) we get spectral
energy density
x(t ) e
X ( f ) F{x(t )}
j 2ft
(2.3)
dt
t
X( f )e
1
x(t ) F { X ( f )}
j 2tf
df
(2.4)
f
3. that delaying x(t) by T in the time domain, x(t-T), delivers a multiplication with e-j2F in
the frequency domain [6], which corresponds to a phase shift of -2F:
F{x(t T )}
x(t T ) e
j 2ft
dt F{x(t )} e j 2F
(2.5)
t
so that with z= ej2F we can write
F{x(t T )} X ( f ) z 1
(2.6)
n
4. that consequently a time delay of nT in time domain translates to X ( f ) z in frequency
domain. We write
When x(t )
X(f )
then
x(t nT )
X ( f ) z n
(2.7)
For functions of unlimited energy (such as noise or periodic functions) we compute spectral
density (e.g. in V/Hz) as
1
T T
X ' ( f ) F '{x(t )} lim
T / 2
x(t ) e
2ft
(2.8)
dt
T / 2
Exercise: Proof Eq. (2.5)!
.............................................
..............................................................
-3-
2.1.2
Amplitude, Power, Energy
Let a time-domain signal x(t) represent an amplitude. If x is a voltage and R an electrical
resistor, then physical power is x2(t)/R, if x is a current then power ist x2(t)R, but what is
power when x[n] is da digital data stream? So from signal processing point of view we say
that if x(t) represents an amplitude, then x2(t) corresponds to the signal’s power and ∫ x2(t)dt
corresponds to the signal’s energy.
Consequently, for the Fourier transformed X ( f )
x(t ) e
j 2ft
dt we say that
t
|X(f)| stands for a spectral amplitude density, and
|X(f)|2 stands for a spectral energy density, and
|X'(f)|2 stands for a spectral power density computed from
T / 2
1
X ' ( f ) lim
x(t ) e 2ft dt
T T
T / 2
which is chosen when the signal energy is infinite, e.g. for noise or periodic functions. The
word “spectral” stands for “over frequency axis”.
2.1.3
Odd and Even Functions
Any function can be seen to be composed of an odd and an even component
f(x) = fg(x) + fu(x)
An even function is symmetric around x=0, the odd part is symmetric around point
(x,y)=(0,0):
fg(x) = fg(-x)
fu(x) = -fu(-x)
example: cos(x)
example: sin(x)
Computation of that parts from any function f(x):
fg(x) = ½ [ f(x) + f(-x) ]
fu(x) = ½ [ f(x) - f(-x) ]
2.1.4
Even/Odd Symmetry of Fourier Transformation
It is a feature of Fourier transformation that a real + even functions translate into real + even
functions and that real + odd functions translate into imaginary + odd function. The
consequence of the “stereo-behavior” is seen in Fig. 2.1.3:
Fig. 2.1.4:
Transformation of complex time-domain
function v(t)=x(t)+jy(t) to complex
frequency-domain function
V(f)=X(f)+jY(f).
xg(t) + xu(t) + jyu(t) + jyg(t)
Xg(f) + Xu(f) + jYu(f) + jYg(f)
-4-
2.2 Time-Discrete Functions
2.2.1
Computing Relative Frequency F=f/fs
We model sampling as the translation of a time-continuous signal x(t) into a time-discrete
signal x[n]=x[tn] using
t n = n T = n / fs
(2.11)
with fs=1/T being the sampling frequency. For signal processing we are typically interested in
the relative frequency
F = f / fs = fT
and
Ω = 2πF
(2.12)
rather than in the absolute frequency f which is easily obtained back from
f = Ffs = F / T
2.2.2
and
= Ω fs = Ω / T
(2.13)
Spectrum Becoming Periodical by Sampling
When the time-domain taps x[n] are complex numbers (having an imaginary part), then their
amplitude spectrum will be symmetric in fs:
X(f) = X(f+Mfs)
X(F) = X(F+M)
(2.14)
When the coefficients of the time-domain signal x[n] are real numbers (having no imaginary
part) then we construct the amplitude diagram in the frequency range ½fs<f<fs ½<F<1
from symmetry around fs/2:
|X(f)| = |X(fs-f)|
|X(F)| = |X(1-F)|
(2.15)
so that the only frequency range of interest is
0 f ½fs
0 F ½ (0 Ω π)
(2.16)
Anything else can be obtained from symmetry and periodicity.
Xanalog(jf)
(a)
f
0
Xsampled(jf)
(b)
-2fS
-fS
0
fS
2fS
3fS
Fig. 2.2.2: Spectrum of (a) time continuous signal and (b) after time-domain sampling.
-5-
f
2.2.3
Symmetry of Frequencies Around f=0
Any real sinusoidal wave with frequency f contains half of its energy in the positive and the
other half in negative frequency.
cos( x)
1 jx
e e jx
2
sin( x)
und
1 jx
e e jx .
2j
(2.17)
Note that this are the formulae to compute real and imaginary part of a complex number:
z=x+jy: real part Re{z}=(z+z')/2 and imaginary part Im{z}=(z-z')/2j, where z' stands for z
with negated imaginary part. This rule holds for ejx=cos(x)+jsin(x) also.
Fig. 2.2.3:
Real frequencies have half of their
energy in the positive and the other half
in the negative frequency component.
lH(jf)l
-f0
2.2.4
-fg
0
fg
f0
f
Aliasing
With sampling frequency fs the frequency range that can be represented is f=0...fs/2 or
F=0...½. Higher frequencies are subject to aliasing: After sampling they will appear at a lower
frequency, the so-called aliasing frequency. For the two-dimensional process of A/D
conversion we get
falias = f – fs ˑround(f/fs)
Falias = F – round(f/fs)
(2.18)
Negative frequencies falias are measured positive.
Comment: A/D conversion is a two-dimensional process. The value dimension produces a quantization error eq
which is computed from the same formula with being the minimum step (LSB) of the ADC:
eq = x – ˑround(x/)
(2.19)
Exercise:
Fourier transformation is symmetric regarding forward and reverse transformation.
(a) We know that a sampling the time-domain function delivers a periodic frequency-domain
function.
(b) From symmetry reasons, a periodic time-domain function must deliver…? Give a proof!
.............................................................
.............................................................
-6-
2.2.5
Time-Discrete Fourier Transformation Using z=ej2πF
Fourier Transformation according to eq. (2.3) becomes for a time-discrete function x[n]
X ( f ) F{x(t )}
j 2ft
x(t ) e dt =
t
xn e j 2fT n T
n
xn e
j 2F n
n
1
.
fS
with dt=T and T=1/fS. Consequently, we get for z and zn
z e j 2F
and
z n e j 2F n e j 2F
.
n
(2.21)
Where we can select any F. Typically we use F=0 - 0.5, because outside of this interval
results can be obtain by periodicity. With this z(F) we get the transformation
x[n], n=0...(N-1)
1 N 1
X z ( F )
x[n] z n
f S n 0
(2.22)
The factor T=1/fS is normally omitted, so that DFT is typically written as
N 1
x[n], n=0...(N-1)
X z ( F ) x[n] z n
(2.23)
n 0
This is the z transformation. Computation within interval F=0...½ is sufficient due to
periodicity. As we can insert any F we can obtain any frequency resolution F.
A significant advantage of the z transform compared to DFT and FFT is its applicability to
transfer functions H(z) = Y(z)/X(z) having a denominator:
b0 b1 z 1 ... bN 1 z N 1
H ( z)
.
a0 a1 z 1 ... aM 1 z M 1
(2.24)
With H(z) we can compute the frequency response of a time-discrete system to input X(z) as
Y(z) = H(z) X(z) .
(2.25)
The z transformation assumes a sampled time domain function within a limited time interval.
Consequently, is must be continuous and periodic in frequency domain.
-7-
2.2.6
DFT: Discrete Fourier Transformation
The transformation according to eq2. (2.22), (2.23) is unique (German: eindeutig). But it is
not biunique (German: eineindeutig), because no unique reverse Fourier transform is possible.
To make the time-discrete Fourier transform biunique we need to translate N data points to N
data points. Typically we simply get a vector of N data points x[n], n=0…N-1. And DFT of
FFT deliver a vector of N data points X[n], n=0…N-1 (typically complex values).
Time and frequency points can then be concluded when sampling frequency fS=1/T is given.
Time points are obtained by
tn = nˑT, n=0…N-1.
The associated points Fk are then equi-distributed over the interval F = 0 – 1. are simply
obtained by dividing the frequency interval F=0...1 into N subintervals:
Fk = k / N,
k=0…N-1.
(2.31)
The real frequencies are then
fk= Fk fs = kˑfs/N,
k=0…N-1.
with frequency resolution
Due to periodicity
(2.32)
F=1/N
f=fs/N
X(mˑN+k) = X(k)
for any integral value m.
(2.33)
(2.34)
The discrete Fourier transform is then computed forth and back as
N 1
x[n], n=0...(N-1)
X [k ] x[n] e j ( 2k / N ) n , k=0...(N-1),
(2.35)
n 0
N 1
X[k], k=0...(N-1)
x[n] X [k ] e j ( 2k / N ) n , n=0...(N-1),
(2.36)
k 0
2.2.7
FFT: Fast Fourier Transformation
FFT is a numerically very efficient DFT. To speed up DFT we can combine terms as pairs,
the results again as pairs and so on. This creates the condition, that 2M time-points with
integral M have to be given. If this condition is not fulfilled, the time-point vector is extended.
The additional are filled with numbers that – hopefully – make sense, e.g. zeros.
-> For FFT it is best to feed 2M data taps with integral M. <-
2.2.8
Key Difference between z transformation and DFT/FFT
-8-
3 Matlab FFT for Non-Recursive Situations
3.1 Key Difference between z Zransformation and DFT/FFT
What does Matlab’s DFT/FFT compute? Using N=cTaps (count of taps) we can re-write DFT
X k X [ Fk ]
cTaps 1
x[n] e
j ( 2Fk ) n
, k=0...(cTaps-1)
(3.1)
n 0
As we compute N data points from N data points, both sides are sampled and consequently
both (“other”) sides are periodic. For real (i.e. non-complex) numbers x[n] we can obtain X(f)
within the complete frequency domain from symmetries when X(f) is known for 0F<½. If we
would note things accurately, then we would have to write DFT/FFT results as X'[Fk],
because it is computed for one period of the periodically repeated x[n]. The solution is defined
on the discrete frequency points
Fk=k/cTaps , k=0...cTaps-1,
only. For other frequencies we have to use the z transformation.
For z transformation we have a continuous abscissa value F=f/fS and use z=ej2F to compute
X(F) for any F.
Key difference of DFT/FFT and z transform: For DFT/FFT the frequency domain is sampled
also. Consequently, their time domain interval must assumed to be periodically repeated.
Copy the code of listing 3.1 into a Matlab file, run it and answer the following questions:
The only non-zero component is the DC component. How do you explain the DC
component’s value as function of cTaps and amplitude A?
|Xk(Fk=0)|
=
.............................................................
|XdB(Fk=0)| =
.............................................................
How do you get the missing tap Xk(20)?
.............................................................
It is said that |X(F)| is symmetric around F=½. Is it?
.............................................................
-9-
% Computing spectral of time interval
cTaps=20
% number of taps
A=1;
% amplitude
nt = 0:cTaps-1;
% time axis: nt=n*T
xn = A*ones(1,cTaps);
% DC signal
Fk = [0:cTaps-1]/cTaps;
% Freq. axis: Fk=k/cTaps
Xk=fft(xn);
% spectral energy within the time domain interval
% Graphical postprocessing
figure(31)
subplot(211)
stem(nt,xn); xlabel('time-domain tap number'); ylabel('xn');
subplot(212)
stem(Fk,real(Xk),'b'); hold on; stem(Fk,imag(Xk),'r'); hold off;
ylabel('Xk');
xlabel('relative Frequency F');
Listing 3.1: Computing spectral amplitude density from set of samples
3.2 Observing Symmetries with QAM
In chapter 2.1.3 we reported the “stereo-effect” of the Fourier transform (FT): a real + even
signal translates into a real and even signal, a real + odd signal translates into an imaginary +
odd signal. For illustration purposes we will use a quadrature - amplitude modulated QAM
signal. It is defined as
sQAM(t) = I cos(2πfct) + Q sin(2πfct)
(3.11)
with in-phase-amplitude I, quadrature-phase amplitude Q, absolute carrier frequency fc and
time t. In the time-discrete Matlab model we get
sQAM(tn) = I cos(2πFcn) + Q sin(2πFcn)
(3.12)
with carrier frequency relative carrier frequency Fc=fcT and discrete time points tn=ntˑT,
nt=0,1,2,...cTaps-1. With Fc we get a carrier wavelength of 1/Fc taps.
Copy the code of listing 3.2 into a Matlab file and answer the questions below.
sQAM(t) = Iˑcos(...)+Qˑsin(...) has an even and an odd part. Which is even, which is odd? Why?
Even part of sQAM is
..........................................
Odd part of sQAM is
..........................................
- 10 -
% Symmetries of Fourier transform
cTaps=100;
nt=0:cTaps-1;
Fc=1/10;
I = 2;
Q = 1;
xn=I*cos(2*pi*Fc*nt);
yn=Q*sin(2*pi*Fc*nt);
sqam=xn+yn;
Sqam=fft(sqam);
Xk=real(Sqam);
Yk=imag(Sqam);
Fk=[0:cTaps-1]/cTaps;
srev=fft(Sqam')/cTaps;
% Graphical postprocessing
figure(32)
subplot(511);
plot(nt,xn,'b',nt,yn,'r'); grid on;
ylabel('x_b_l_u_e, y_r_e_d');
title('Illustration of Stereo-Effect using QAM')
subplot(512);
plot(nt,sqam); grid on;
ylabel('s_q_a_m=x+y');
xlabel('tap number n_t');
subplot(513);
stem(Fk,Xk,'b'); grid on;
ylabel('real(Sqam)');
subplot(514);
stem(Fk,Yk,'r'); grid on;
ylabel('imag(Sqam)');
ylabel('imag(Sqam)');
xlabel('relative frequency F_k');
subplot(515);
plot(nt,real(srev),'b',nt,imag(srev),'r'); grid on;
hold on; stem(nt,sqam,'k'); hold off;
ylabel('s_q_a_m,
fft(fft(s_q_a_m))'); xlabel('tap number n_t');
Listing 3.2: Generation and sampling of a Quadrature-Amplitude Modulated (QAM) signal
Does the even part of sqam yield an even, real part of Sqam?
..................
Does the odd part of sqam yield an odd, imaginary part of Sqam?
..................
Replace in the Matlab code Sqam=fft(sqam) by Sqam=fft(j*sqam) and fill the gaps below with
terms like real + even or imag + odd.
The imag+...... part of jˑsQAM translates to the ........... part of SQAM=fft(j sQAM).
The imag+...... part of jˑsQAM translates to the ........... part of SQAM=fft(j sQAM).
Play with the number of taps, cTaps, (which must be an integral multiple of 1/Fc)
Change code from Sqam=fft(sqam); to Sqam=fft(sqam)/cTaps;
What is the difference in the graphics?
...................................
- 11 -
Play with amplitudes I, Q. how do you explain the amplitudes that you see as function of I, Q?
.............................................................
.............................................................
An other symmetry is given by forth and reverse FT. The reverse FT has only a phase shift
compared to the forward FT, that has no impact on amplitude diagram. In listing 3.2 the phase
shift is compensated for by the tick ' when computing back the time-domain function sreverse
from Sqam: “srev=fft(Sqam')/cTaps;”
We should find that after two Fourier transformations we should get back the original signal
sqam. This proofs that back and forth transformation are identical with except to a phase
inversion.
3.3 Aliasing
We said that Falias = F – round(f/fs). Complete the Matlab code in listing 3.3 that generates
one waveform with relative frequency F1 with |F1|<½, and an other waveform with frequency
F1=k+F1 with k=1, 2, 3,..
Does it make a difference if you generate a sin-wave with F1=0.1 or Fk=10.1? ....no...
% Testing the alias effect
cTaps=31;
% time axis length
nt = 0:cTaps-1;
% relative time axis
F1 = 1/16;
% relative Frequency < 0.5
x1=sin(2*pi*F1*nt);
% sin-wave without aliasing
k = 5;
% should be ignored due to aliasing
Fk = k+F1;
% aliasing relative Frequency
xk = sin(2*pi*Fk*nt); % sin-wave with aliasing
% Graphical postprocessing
figure(33)
plot(nt,x1,'b'); grid on;
hold on; stem(nt,xk,'r'); hold off;
ylabel('imag(Sqam)');
xlabel('relative frequency F_k');
Listing 3.3: Checking for aliasing effects
3.4 Convolution
Multiplication in time-domain corresponds to convolution in frequency domain and vice
versa.
Furthermore, convolution of any function x[n] with a Dirac pulse [n-a] delivers x[n-a]. This
is illustrated in Listing 3.4. Run it and answer: If x[n] has a length of M taps and y[n] has N
taps, then conv(x,y) has a length of
taps
................
- 12 -
% using convolution
M=40; N=60;
% No of taps of x and y
xn=zeros(1,M);
% vector with 100 zeros
xn(30) = 1;
% Dirac at n=30;
yn=zeros(1,N);
% vector with 60 zeros
yn(1:10) = [1:10]/10; % ramp function
cn = conv(xn,yn);
% convolution
length(cn)
% print length of conv(x,y)
% Graphical postprocessing
figure(34)
subplot(311), stem(xn); axis([0 length(cn)+1 -0.2 1.2]); ylabel('x');
title('Convolution of any function with a Dirac pulse');
subplot(312), stem(yn); axis([0 length(cn)+1 -0.2 1.2]); ylabel('y');
subplot(313), stem(cn); axis([0 length(cn)+1 -0.2 1.2]);
xlabel('tap number n_t'); ylabel('conv(x,y)');
Listing 3.4: Convolution of Dirac signal x[n] with ramp signal y[n]
3.5 Number of Taps in FFT Interval
It is said that the number of taps for FFT must be an integral power of 2, i.e. cTaps=2M with
M being an integral number. Let’s check it!
% check further FFT application rules
cTaps = 32;
nt=0:cTaps-1;
xoffset = 0;
kosc
= 1;
Fosc=kosc/cTaps; % Fosc = integer_number/N
xn=sin(2*pi*Fosc*nt) + xoffset;
Fk=[0:cTaps-1]/cTaps;
Xk=fft(xn);
% Graphical Postprocessing
figure(35);
subplot(311); stem(nt,xn); grid on;
axis([nt(1) nt(end) -1.75 1.75]);
hold on; plot(nt,xn,'k'); hold off;
xlabel('number of taps');
title('Fosc = 1 / cTaps'); ylabel('x[n]');
subplot(312); stem(Fk,abs(Xk)); grid on;
axis([0 1 -0 20]);
ylabel('|X[k]|');
subplot(313); stem(Fk,20*log10(abs(Xk)));
axis([0 1 -350 100]); grid on;
xlabel('relative Frequency');
ylabel('dB(|X[k]|)');
Fig. 3.5: FFT of sinusoidal wave
Listing 3.5: FFT of sinusoidal waveform
In listing 3.5 and Fig. 3.5 we see in the top a sinusoidal waveform with 32 taps. The FT of
x[n] is illustrated in the middle part of Fig. 3.5. The same in dB is shown in the bottom row.
We see errors of –300dB, this is some 10-15, the round-off noise of double precision floatingpoint numbers. Set cTaps=40 and simulate again. Do the errors become significantly worse,
as we can read in the literature?
..............................................................
- 13 -
3.6 Number of Complete Waves in FFT Interval
Copy listing 3.6 into a Matlab file and run it with kosc=2, 3, 4, ... How does the number of
waves in the simulation interval correspond to kosc?
..............................................................
Do the errors at k kosc become significantly worse if kosc is an integer? ......no.....
Next use a real number for kosc, e.g. kosc=5.1. Now the FFT time-domain interval is no more
an integral multiple of the wavelength. What happens with the errors at k kosc ?
..............................................................
The experiments above are illustrated in Fig. 3.6.
(a) Interval on that a Fourier Transformation (FT) is applied.
(b) The FFT assumes that the interval is repeated periodically to both sides.
(c) Fourier transform with kosc=5 (left) and 5.1 (right) waves in the interval of the transform.
It is seen that we get exact FT results with an integral kosc, but not with a fractional kosc.
(a) Interval for FT
(b) Interval for FT when we consider periodicity
(c) cTaps =32, Top-down: x[n], |X[F]|, dB(|X[F]|), Left: Fosc=5/cTaps, Right: Fosc=5.1/ cTaps.
Fig. 3.6: Effect for FFT when time interval is / is not an integral multiple of the wavelength.
- 14 -
% Wavelength tests.m
cTaps = 32;
tn=0:cTaps-1;
Fk =[0:cTaps-1]/cTaps;;
kosc_int = 5;
% integer: No. of waves in time interval
win = ones(1,cTaps);
% win = f_blackmanharris(cTaps);
%
% Fosc = integer_number/N
Fosc_int=kosc_int/cTaps;
xn_int=sin(2*pi*Fosc_int*tn).*win;
Xk_int=fft(xn_int);
%
% Fosc = real_number/cTaps
Fosc_real=(kosc_int+0.1)/cTaps;
xn_real=sin(2*pi*Fosc_real*tn).*win;
Xk_real=fft(xn_real);
%
% Graphical post processing
figure(36);
% left column
subplot(321); stem(tn,xn_int); grid on; axis([tn(1) tn(end) -1.75 1.75]);
hold on; plot(tn,xn_int,'r'); hold off;
title('Fosc = 5 / cTaps'); ylabel('x[n]'); xlabel('time-domain tap number');
subplot(323); stem(Fk,abs(Xk_int)); grid on; axis([0 0.5 -5 20]);
ylabel('|X[F]|');
subplot(325); stem(Fk,20*log10(abs(Xk_int))); grid on; axis([0 0.5 -350 100]);
ylabel('dB(|X[F]|)'); xlabel('relative Freqeuncy F');
% right column
subplot(322); stem(tn,xn_real); grid on; axis([tn(1) tn(end) -1.75 1.75]);
hold on; plot(tn,xn_real,'r'); hold off;
title('Fosc = 5 / cTaps'); ylabel('x[n]'); xlabel('time-domain tap number');
subplot(324); stem(Fk,abs(Xk_real)); grid on; axis([0 0.5 -5 20]);
ylabel('|X[F]|');
subplot(326); stem(Fk,20*log10(abs(Xk_real))); grid on; axis([0 0.5 -350 100]);
ylabel('dB(|X[F]|)'); xlabel('relative Frequency F');
Listing 3.6: Checking for the impact of wavelength with respect to transformation window.
Note: Fig. 3.6(c) is a sampled version of Fig. 5.1, because the FFT assumes periodic
repetition to both sides until infinity. When the transformed set of samples stands alone
then z transform and Fig. 5.1 is correct.
- 15 -
4 z Transform
4.1 Writing Discrete Fourier Transform as z Transform
When we compute cTaps frequency-domain points from cTaps time-domain points as done
with Matlab’s FFT we get a biunique transformation, i.e. we can compute the transformation
uniquely back and forth. However, we cannot improve frequency resolution or respect a
transfer function with nominator and denominator. Therefore we recall Eq. (2.21), (2.22):
x[n], n=0...(N-1)
N 1
N 1
n0
n 0
X z ( F ) x[n] e ( j 2F ) n x[n] z n
z e j 2F e j
(2.21)
(2.22)
Only the relative frequency range of F=0...½ is of interest for amplitude diagram, all the rest
can be obtained by symmetry. Therefore we write Eq. (2.22) in Matlab z(Fz) as
% part (a): compute variable z
j=sqrt(-1);
% set content of j back to default
Fz=0:1e-4:0.5;
% 5001 relative Frequency points
z=exp(-j*2*pi*Fz);
% 5001 z values
xn = [0 0 1 1 1 1 1 1 0 0]; % define any time-domain function
Listing 4.1(a): Computing 5001 z values over relative frequency Fz.
With this z we can evaluate the sum over n of x[n]z-n according to Eq. (2.21):
% part (b): Compute z Transformed
Xz = zeros(1,length(z));
% initialize Hz
for n=1:length(xn);
% compute the sum(x(n)*z^-n)
Xz = Xz + xn(n).*z.^-n;
% Matlab vectors begin with index 1
end;
Listing 4.1(b): Computing 5001 Xz values over relative frequency Fz.
To make results visible and compare z transform with FFT append code of listing 4.1(c). In
your working directory should now be file f_dB.m containing listing f_dB(x) from chapter 6.
% part(c): Graphical post processing
figure(41);
subplot(211); stem(xn);
title('Comparison: z Transform versus FFT');
xlabel('number of tap'); ylabel('taps');
subplot(212); plot(Fz,f_dB(Xz),'b');
axis([0 0.5 -80 20]); xlabel('taps');
Xk=fft(xn); Fk=[0:length(xn)-1]/length(xn);
hold on; stem(Fk,f_dB(Xk),'r'); hold off;
xlabel('relative Frequency'); ylabel('line: z-T , circles: FFT');
Listing 4.1(c): Graphical post processing with computation of fft(xn).
Exercise:
Combine listings 4.1(a-c) to compute the z transform Xz of xn. Compare resolution: 10½
visible Fk-points (red circles) obtained from FFT with 5001 Fz-points from z transform.
- 16 -
Fig.4.1 of listing 4.1.
Top: 10 time-domain
samples
Bottom: 10 red circles
(here 5 as F=0-½) from
FFT sample the blue
curve obtained from ztransformation,
the
latter with significantly
higher
frequency
resolution.
While z-transfomation
assumes
the
10
samples above being
alone, FFT assumes
periodical repletion of
the time interval.
4.2 Using an Accelerated Version of z Transform
The z transform of listing 4.1 is correct but numerically inefficient and in listing 4.1(b)
without denominator. Listing 4.2 shows the header of function f_fzt. The full code is available
in chapter 6. Copy it into a file named f_fzt.m.
function [Hz,Fz] = f_fzt(coefs,Fz)
Listing 4.2: Function header of Matlab function f_fzt. The use of Fz, is optional, its default is
Fz=0:1e-3:0.5.
You may replace function call Xk=fft(xn) by [Xz,Fz]=f_fzt(xn). Then you will get by
default a frequency axis of Fz=0:1e-3:0.5; i.e. with 501 points.
If you want to have that frequency axis available write [Xz,Fz]=f_fzt(xn).
If you want to use a self-made frequency axis write Xz=f_fzt(xn,Fz).
If you want to use a transfer function with numerator num[n] and a denominator den[n],
then set coefs(1)=num; coefs(2)=denum and call Xz=f_fzt(coefs). This assignment to coefs
requires num and denum to have the same length, so fill with training zeros if necessary.
To do it with your own frequency axis Fz write Xz=f_fzt(coefs,Fz).
Exercise:
Use code of listing 4.1 and replace the code of part (b) by calling f_fzt.
Remove all command lines in parts (a) and (b) that are no more required.
Type “clear all” (existing declarations) in the command window before testing your model.
- 17 -
4.3 Time-Discrete Differentiator
A time-discrete differentiator has the impulse response hdiff=[1, -1]. We can apply the FFT
but get 2 frequency-domain point only from 2 time-domain taps, seen as red circles in
Fig. 4.3(b). On the other hand the z transform can deliver an arbitrary number of F points,
visible as blue line in Fig. 4.3(b)
% Transfer Function of Time-Discrete Differentiator
hdiff = [1 -1];
% impulse response
[Hdiff,Fz] = f_fzt(hdiff); % Freq. domain response + F-axis
%
% Graphical post processing
figure(43);
subplot(131); stem(hdiff);
xlabel('number of tap'); ylabel('taps');
subplot(132); plot(Fz,abs(Hdiff),'b');
title('Differentiator Comparison: z Transform versus FFT');
axis([0 0.5 -0.1 2.1]); xlabel('taps');
Hk=fft(hdiff); Fk=[0:length(hdiff)-1]/length(hdiff);
hold on; stem(Fk,abs(Hk),'r'); hold off;
xlabel('relative Frequency'); ylabel('line: z-T , circles: FFT');
subplot(133); plot(Fz,angle(Hdiff)*180/pi,'b');
xlabel('relative Frequency'); ylabel('phase');
Listing 4.3: Computing frequency-domain response of time-discrete differentiator.
X
z-1
(a) Schematics
Y
(b) Impulse and frequency response computed with z
transform (-) and FFT (o), on linear-linear axis.
Fig. 4.3: Time-discrete differentiator. The phase is linear like for all FIR filters
Exercise:
Run the Matlab code of listing 4.3 and try to understand command lines 2 and 3:
hdiff = [1 -1]; is the impulse response of a time-discrete differentiator
[Hdiff,Fz] = f_fzt(hdiff); delivers the Fourier transformed of hdiff on Hdiff and
the default frequency axis vector on Fz.
- 18 -
4.4 Time-Discrete Integrator
A time-discrete integrator is recursive, consequently it has a numerator and a denominator.
Numerator is hintnum=[1], denominator is hintden=[1, -1]. The FFT is no more applicable for
recursive systems. The amplitude diagram is shown in a log-log scale.
% Transfer Function of Time-Discrete Integrator
hintcoefs(1,:) = [1 0];
% numerator of integrator
hintcoefs(2,:) = [1 -1];
% denominator of integrator
Fz=1e-3:1e-3:0.5;
Hint = f_fzt(hintcoefs,Fz); % Freq. response
%
% Graphical post processing
figure(44);
subplot(121);
semilogx(Fz,f_dB(Hint));
title('
Frequency Response of Time-Discrete Integrator');
ylabel('H_integral / dB'); xlabel('relative Frequency');
subplot(122);
semilogx(Fz,angle(Hint)*180/pi);
ylabel('Phase'); xlabel('relative Frequency');
Listing 4.4: Computing frequency response (Bode diagram) of time-discrete integrator.
(c)
(a)
X
(d)
Y
X
Y1
z-1
(b)
X
1
1-z-1
Y
Fig. 4.4: Integrator: (a) symbol, (b) z representation, (c) schematics, (d) amplitude diagram.
Fig. 4.4(d) illustrates the result of listing 4.4 which is a Bode diagram or frequency response
of the time-discrete integrator. Run the Matlab code of listing 4.4 and understand the first 4
command lines.
Exercise:
Why can’t replace the two command lines in listing 4.4
Fz=1e-3:1e-3:0.5;
Hint = f_fzt(hintcoefs,Fz); % Freq. response
by
[Hint,Fz] = f_fzt(coefs);
..........................................................
..........................................................
- 19 -
4.5 ‘Small’ and ‘Big’ Data Computed by FFT and FZT
% Comparing fft and fzt for short and long vectors
%
% read long vector from file
iselect = input('read sound file? no=0, yes=1: ');
if iselect==1;
left_track_orig = dlmread('sound_fs=44100Hz_23sec.txt');
end;
%
% loop of FFT's and FZT's with selectable count of taps
count=0;
iselect=1;
while(iselect>0)
% select count of taps
cTaps = input('input cTaps (=count of taps of sound): ');
left_track = left_track_orig(1:cTaps);
%
% start FFT
iselect=input('hit return key to continue with fft ');
Fk = [0:cTaps-1]/cTaps;
LEFT_TRACK_fft_dB = f_dB(fft(left_track));
%
% start z transform
iselect = input('hit return key to continue with fzt ');
Fz = 0:1e-3:0.5;
LEFT_TRACK_fzt_dB = f_dB(f_fzt(left_track,Fz));
%
% start graphical post processing
iselect = input('hit return key to plot graphics ');
figure(45+count);
fs = 44100;
subplot(211);
fk_KHz = Fk*fs/1000;
half_len=ceil(cTaps/2);
fk_KHz = fk_KHz(1:half_len);
if(cTaps<102);
plot(fk_KHz,LEFT_TRACK_fft_dB(1:half_len),'-o'); grid on;
else
plot(fk_KHz,LEFT_TRACK_fft_dB(1:half_len)); grid on;
end;
xmin = fk_KHz(1); xmax=fk_KHz(end);
ymin = min(LEFT_TRACK_fft_dB); ymax = max(LEFT_TRACK_fft_dB);
delt=0.1*(ymax-ymin); ymin=ymin-delt; ymax=ymax+delt;
axis([xmin xmax ymin ymax]);
title('Frequency Response of Song');
ylabel('fft / dB'); xlabel('frequency / KHz');
subplot(212);
fz_KHz = Fz*fs/1000;
if(cTaps<102);
plot(fz_KHz,LEFT_TRACK_fzt_dB,'-o'); grid on;
else
plot(fz_KHz,LEFT_TRACK_fzt_dB); grid on;
end;
axis([xmin xmax ymin ymax]);
ylabel('fzt / dB'); xlabel('frequency / KHz');
%
% one more FFT + FZT computation?
iselect=input('********* one more computation? ********* no=0, yes=1: ');
count=count+1;
end;
Listing 4.5: Frequency responses computed by FFT and FZT for ‘small’ and ‘big’ data.
- 20 -
4.5.1
Reading a Long Data Vector
Up to now we have seen several advantages of z transform, except the biuniqueness of FFT,
that we do not need in everyday engineering life. In this chapter, we will compare situations
where the different kinds of Fourier transformations are preferable.
Put the sound file sound_fs=44100Hz.txt into your Matlab working directory. You can find
it at K:\SB\Education\SoundProcessing\sounds. If you cannot get that file you can make an
own one from any *.mpeg or *.wav file using the respective Matlab program listed in
chapter 6. You can play that sound with Matlab command sound(left_track_orig,44100);
where left_track_orig is a real-vector and 44100 the sampling frequency fs in Hz.
Copy the code shown in listing 4.5 into a Matlab file (e.g.
run it. Fist of all the program will ask
FL45_BigData_FFT_vs_FZT.m)
and
read sound file? no=0, yes=1:
Press return when you have already done this read process in your actual Matlab session,
otherwise type “1” (without quotation marks) and then return. Matlab will then read the data
from file sound_fs=44100Hz.txt to the vector left_track_orig.
4.5.2
Computing FFT and FZT for a Short Data Vector
After reading the sound from file sound_fs=44100Hz.txt to the vector left_track_orig
Matlab will ask
input cTaps (=count of taps of sound):
Type “11” (without quotation marks) and then 4 times return. Then you should see the Matlab
graphics window shown in Fig. 4.5.1 with figure number 45. Any further figures will have
incremented figure numbers. Don’t continue when Matlab asks you
********* one more computation? ********* no=0, yes=1:
Top of Fig. 45: FFT Command
left_track = left_track_orig(1:cTaps);
reads the first 11 data points from vector
left_track_orig to vector left_track. Statement
LEFT_TRACK_fft_dB = f_dB(fft(left_track));
computes their Fourier transformed and translates it
in dB before storing that 11 frequency points on
vector LEFT_TRACK_fft_dB. Before plotting it we
need the appropriate frequency axis for it. Line 19
computes the cTaps=11 taps relative of frequency:
Fk = [0:cTaps-1]/cTaps;
Fig. 4.5.1: Matlab response to 11 taps
- 21 -
To obtain absolute frequencies for the graphical representation line 32 multiplies Fk with
sampling frequency fs to obtain Hz and then divides by 1000 to get a KHz representation:
fk_KHz = Fk*fs/1000;
As we know that the Fourier transformed on vector LEFT_TRACK_fft_dB is symmetric around
F=Fk=½ f=fs/2 we truncate this vectors to their half lengths (with ceil( ) = round up):
half_len=ceil(cTaps/2);
fk_KHz = fk_KHz(1:half_len);
This is the reason why we see only 6 data points in the top of Fig. 4.5.1: ceil(11/2)=6. In
amplitude diagrams taps are illustrated with circles when cTaps < 102.
Bottom row of Fig. 45: FZT. The lower diagram in Fig. 4.5.1 is z transformed computed
with f_fzt() as listed in the appendix. The frequency axis
(24) Fz = 0:1e-3:0.5;
has 501 sampling points uniform from 0...½ and the command
(24) LEFT_TRACK_fzt_dB = f_dB(f_fzt(left_track,Fz));
computes 501 frequency-domain points in dB-representation to vector LEFT_TRACK_fzt_dB. It
is seen in the lower part of Fig. 45 that we have significantly more data points here than
obtained with FFT. (The additional data points would make sense only for transfer functions.)
4.5.3
Computing FFT and FZT for a Long Data Vector
Matlab is still at prompt
********* one more computation? ********* no=0, yes=1:
Answer with “1” and you will be asked for cTaps again. Answer with “2^20” which is the
maximum length of vector in file sound_fs=44100Hz.txt given by the author.
Then the Program will prompt you
hit return key to continue with fft
Take a [stop-]watch with resolution seconds or better. Start it when you press return and stop
it when you see the next Matlab prompt on your screen. Note in table 4.5.2.
You should now see the prompt
hit return key to continue with fzt
reset your stop-watch and measure the time until you see the next Matlab prompt. How much
time does your z transformation need? Note in table 4.5.2.
- 22 -
You should now see the prompt
hit return key to plot graphics
Press return to see the graphics shown in Fig. 4.5.2.
Summarize your results in table 4.5.2.
Table 4.5.2: Measurement Results
Time-domain Taps (cTaps)
11
220
Computed freq. taps by fft( )
Computed freq. taps by f_fzt( )
Time consumed by fft( )
X
Time consumed by f_fzt( )
X
Fig. 4.5.2: Matlab response to 220 taps
Note: f_fzt, assumes that samples to stand alone, while fft assumes that they are repeated
periodically.
- 23 -
4.5.4
Conclusion of Comparison between fft( ) and f_fzt( )
What is the fundamental difference in the underlying assumption of z transform and DFT?
.............................................................
.............................................................
.............................................................
.............................................................
When is it absolutely necessary to use a z transformation rather than FFT?
.............................................................
When is it advantageous to use z transformation rather than FFT?
.............................................................
.............................................................
When is it necessary to use FFT rather than z transform?
.............................................................
When is it advantageous to use FFT command rather than z transform?
.............................................................
.............................................................
- 24 -
5 Window Functions
5.1 Preface
Fig 5.1 shows the same time-domain samples as Fig. 3.6. However, here they are assumed to
be a single interval and therefore they are z transformed and deliver the spectra shown in the
rows below, labeled |X(F)| and dB(|X(F)|). As the time-domain functions are sampled their
frequency-domain spectra are periodic. When we assume the time-domain intervals to be
repeated periodically, then their spectra become a sampled version X(z(F)). If the time interval
is an integral multiple of the wavelength (e.g. kosc=5), then there is exactly one single tap of
the discrete frequency spectrum X[F] non-zero, all the other taps are in the zeros of the
continuous spectrum X(F). When the time interval is a fractional multiple of the wavelength
(e.g. kosc=5.1) then several taps of the discrete spectrum X[F] are non-zero.
In many practical cases it is impossible to guarantee that the considered time window is an
integral multiple of the wavelength, for example for measured data and if a waveform
contains several frequencies at a time, like music.
Exercise: Modify listing 3.6 such, that FFT is replaced by z-transform to obtain Fig. 5.1. Do:
Fk = [0:cTaps-1]/cTaps
fft(xn)
Xk
stem( ) in subplots with FFT
Fz = 0:1e-5:0.5;
f_fzt(xn,Fz)
Xz
plot( )
Fig. 5.1: Same time-domain samples as shown in Fig. 3.6, but here z transformed instead of
FFT. Top-down: x[n], |X(F)|, dB(|X(F)|), Left: Fosc=5/cTaps, Right: Fosc=5.1/cTaps.
- 25 -
5.1.1
Why we Cannot Avoid Window Functions
Measuring without window would require an infinitely long measurement interval for
periodic functions such as a sinusoidal wave.
The product between the with of a time-domain function and its Fourier transformed is a
constant. (This is the fundament of Heidelberg’s uncertainty principle, German:
"Unschärferelation".) Therefore, an infinitely narrow Frequency-domain function as the Dirac
in Fig. 5.1.1(af) corresponds to an infinitely wide time-domain function, as the sinusoidal
wave in Fig. part (at).
5.1.2
Convolution
Fig. 5.1.2: Amplitude
modulation is time-domain
multiplication with sin(2f0t)
which corresponds to
frequency-domain
convolution with (f-f0)
moving |X(f)| to |X(f-f0)|.
(a)
lX(f)l
l(f-f0)l
f0
0
f
(b)
X(f)
0
l(f-f0)l
f0
f
Convolution in time-domain corresponds to multiplication in frequency-domain and vice
versa. Convolution of f(x) with Dirac (x-a) shift of f(x)to f(x-a).
f(x-a) = f(x) * (x-a)
(5.1)
Formulae (for interested students only):
Time continuous: y (t ) x(t ) * h(t ) h(t ) * x(t )
Time discrete:
5.1.3
i
i
i
i
x( ) h(t )d h( ) x(t )d
y ( n) x ( n) * h( n) h( n) * x ( n)
x(i) h(n i) h(i) x(n i)
(5.2)
(5.3)
Exercise (read subchapter 5.2.1 first!)
What do we see in Fig. 5.1?
Top subplots:
............................................................
............................................................
Spectrum plots:
............................................................
............................................................
- 26 -
5.2 Window Functions
5.2.1
Rectangular "do-nothing window"
time domain
(at)
frequency domain
(af)
s(t)=cos(2f0t)
(bt)
0
t
0
S(jf)
(bf)
w(t)
f0
f
f0
f
f0
f
W(jf)
1
-T
T
0
t
X(jf) = S(jf) * W(jf)
x(t)=s(t) w(t)
(ct)
(cf)
0
t
Fig 5.2.1: Illustration in time and frequency domain: (at) time-domain sinus, (bt) time-domain
rectangle, (ct) time-domain multiplication of sinus and rectangle, (af) frequency-domain
Dirac function, (bf) frequency-domain |sinc| function, (cf) frequency-domain convolution
of Dirac and sinc function.
As Fourier transform of a sinusoidal wave as shown in Fig. 5.2.1(at) we expect a Dirac
function as shown in Fig. part (af). We could obtain this if we could measure infinitely long.
However, the time interval of real measurements is limited. Measuring a limited sector of an
sinusoidal wave does mathematically correspond to the multiplication of an infinite sinusoidal
wave as shown in Fig. part (at) multiplied with a rectangular window (called “do-nothing
window”) illustrated in Fig. part (bt). The result of this multiplication is seen in Fig. part (ct).
In the frequency domain we see in Fig. part (af) the expected Dirac as Fourier transformed of
the sinusoidal wave that we want to measure. But the limited time window has also a Fourier
transformed, namely a sinc function, where sinc(x)=sin(x)/(x). Fig. part (bf) sketches
|2Tsinc(2Tf)| as Fourier transformed of the 2T wide time-domain window.
Multiplication in time-domain corresponds to convolution in Frequency domain and vice
versa. As Fig. parts (at) and (bt) are multiplied to obtain (ct), a convolution between Fig. parts
(af) and (bf) delivers (cf). As a convolution of any function X(f) with a Dirac function (f0)
delivers X(f-f0), we see the Fourier transformed of the window function shifted by f0 of the
sinusoidal waveform. Instead of the desired Dirac seen in Fig. part (af) we get the |sinc( )|
function around f0 seen in Fig. part (af). We see side maxima at frequencies where we don’t
want to see them. What can we do to improve the situation? Use better window functions!
- 27 -
5.2.2
Gaussian as Window Function
time domain
(at)
frequency domain
(af)
s(t)=sin(2f0t)
0
t
0
w(t)
(bt)
-t
0
S(jf)
f
f0
f
f0
f
W(jf)
(bf)
t
f0
0
t
s(t) w(t)
(ct)
S(jf) * W(jf)
(cf)
0
t
Fig. 5.2.2: Time and frequency domain representations: (at) time-domain sinus, (bt) timedomain Gaussian, (ct) time-domain multiplication of sinus and Gaussian, (af) frequencydomain Dirac, (bf) frequency-domain Gaussian, (cf) frequency-domain convolution of
Dirac and Gaussian.
Figure part (at) sketches the infinitely wide sinusoidal function s(t)=cos(2πf0t).
Figure part (af) illustrates its Fourier transformed S(f)=S0(f-f0).
Figure part (bt) sketches window function w(t ) w0 e
t
2 t
2
.
Figure part (bf) illustrates its Fourier transformed W ( f ) W0e
1 f
2 f
2
, whereas σtσf=1.
Figure part (ct) sketches function x(t)=s(t)w(t), obtained from time-domain multiplication.
Figure part (cf) illustrates the corresponding f-domain convolution X(f)=S(f)*W(f). As W(f) is
a Dirac-function the convolution yields a frequency shift of S(f).
The Gaussian as window function translates into a Gaussian thus avoiding side maxima.
Problem: It is infinitely wide, thus it re-introduces the problem that we want to eliminate. If
we limit it to a finite width, this limitation corresponds to a multiplication with a rectangular
window with the above detailed problems in the frequency domain.
- 28 -
Remark 1: tf=1 with t, f being the time and frequency domain standard deviations,
respectively. In other words: If the time-domain Gaussian window becomes wider by a factor
M, then the frequency domain Gaussian becomes narrower by the same factor M.
Remark 2: This tf=1 corresponds to the uncertainty principle (German: Unschärferelation)
found by the German physician Werner Heisenberg: xpx=h where x is the spatial width
of a wave package, p corresponds to its width in the frequency domain and h is a constant.
Rectangular and Gaussian window functions conclusion:
We now have considered two window functions which couldn’t satisfy our needs, because the
rectangular window’s Fourier transformed is too 'dirty' and the 'clean' Gaussian window is too
wide. What we are looking for is a Gaussian-like window function having a finite width in
time-domain and strongly suppressed side maxima in frequency domain.
5.2.3
Triangular Window Function
w(t)
Fig 5.2.3: Triangular window
as finite approximation the Gaussian.
A
-T
A1 t / T
wtriang (t ) A1 t / T
0
für
T t 0
für
für
0t T
| t | T
0
T
t
(12)
5.2.4 Hamming Window
0,54 0,46 cos 2t
w(t )
0
n
für 0 t 1
w(n) 0,54 0,46 cos 2 N
sonst
0
für
0n N
(13)
sonst
The Hamming-window has a maximum of 1. its outer edges, i.e. 0 and 1, or 0 und N, have
the value 0.08.
5.2.5 Hanning Window
w(t)
Fig 5.2.5:
Hanning-Window: cos function
approximates Gaussian
1
0
0,5(1 cos 2t ) für 0 t 1
w(t )
0
sonst
1
1
n
w(n) 2 1 cos 2 N
- 29 -
0
für
sonst
0n N
t
(14)
The outer edges of the Hanning window, i.e. 0 and 1, or 0 and N, are 0. The maximum is 1.
In the digital domain the first and last taps can be ignored as the cause a multiplication with 0.
5.2.6 Blackman Window
The Blackman window is made up of several cos functions.
It and was improved to Blackman-Harris window.
(15)
5.2.7 Blackman-Harris Window
The Blackman-Harris window is an improved Blackman window. With centered zero:
a a cos(T t ) a 2 cos(2T t ) a 3 cos(3T t ) für | t | T / 2
2
with T
. (16)
wbh (t ) 0 1
0
sonst
T
Practically the left window edge is at t'=0 (instead of t=0) resulting in:
a a cos(T t ) a 2 cos(2T t ) a 3 cos(3T t ) für 0 t ' T
.
wbh (t ' ) 0 1
0
sonst
(17)
The coefficients a0...a3 deliver a sum of 1 and are given by a0=0,35875, a1=0,48829,
a2=0,14128, a3=0,01168.
Self-made Matlab code of a Blackman-Harris window function is listed in chapter 6. It
delivers the vector as row. The Matlab functions return such windows as column vectors.
Exercise:
What is the difference between commands blackmanharris(5) and blackmanharris(5)'
and f_blackmanharris(5)? Write these commands into the Matlab command window!
..................................................................
..................................................................
..................................................................
5.2.8 Chebychev Window
The Russian mathematician Pafnuti Lwowitsch Tschebyschow (1821–1894) was concerned
with optimization. Based on his principles Matlab offers the Chebychev window
chebwin(N,A). It has N taps and is optimized such, that in frequency domain it is as narrow as
possible while guaranteeing A dB attenuation of side maxima. The last column of Fig 5.3
illustrates chebwin window with N=41 time-domain taps and A=70dB attenuation.
- 30 -
5.3 Comparing Windows
Fig. 5.3 shows all the windows illustrated above and was created with Listing 5.3. Note that
“DC amplification” of 0 dB was obtained by statement wn=wn/sum(wn); Matlab creates
window functions such that the maximum time-domain tap = 1.
Fig. 5.3: Different windows plotted with Matlab in time domain (left column), frequency
domain (centre column) and logarithmic frequency domain (right column).
- 31 -
% Compare Window Functions
cTaps = 41;
tn = 0:cTaps-1;
Fk = [0:cTaps-1]/cTaps;
Fz = 0:1e-4:0.5;
F = Fz;
mindB=-100; maxdB=10;
figure(53);
% Rectangular ("do-nothing window")
wn = zeros(cTaps,1); wn(round(cTaps/4):round(cTaps*3/4))=1; wn=wn/sum(wn);
Wk = abs(f_fzt(wn,F)); WkdB = f_dB(Wk);
subplot(631); stem(tn,wn); ylabel('Rectangular'); title('time domain');
subplot(632); plot(F,Wk); title('frequency domain');
subplot(633); plot(F,WkdB); title('frequency domain dB');
axis([0 0.5 mindB maxdB]); grid on;
% Triangular
wn = triang(cTaps); wn=wn/sum(wn); Wk = abs(f_fzt(wn,F)); WkdB = f_dB(Wk);
subplot(634); stem(tn,wn); ylabel('Triangular'); subplot(635); plot(F,Wk);
subplot(636); plot(F,WkdB); axis([0 0.5 mindB maxdB]); grid on;
% Hamming
wn = hamming(cTaps); wn=wn/sum(wn); Wk = abs(f_fzt(wn,F)); WkdB = f_dB(Wk);
subplot(637); stem(tn,wn); ylabel('Hamming'); subplot(638); plot(F,Wk);
subplot(639); plot(F,WkdB);
axis([0 0.5 mindB maxdB]); grid on;
% Hanning
wn = hanning(cTaps); wn=wn/sum(wn); Wk = abs(f_fzt(wn,F)); WkdB = f_dB(Wk);
subplot(6,3,10); stem(tn,wn); ylabel('Hanning'); subplot(6,3,11); plot(F,Wk);
subplot(6,3,12); plot(F,WkdB);
axis([0 0.5 mindB maxdB]); grid on;
% Blackman
wn = blackman(cTaps); wn=wn/sum(wn); Wk = abs(f_fzt(wn,F)); WkdB = f_dB(Wk);
subplot(6,3,13); stem(tn,wn); ylabel('Blackman'); subplot(6,3,14); plot(F,Wk);
subplot(6,3,15); plot(F,WkdB);
axis([0 0.5 mindB maxdB]); grid on;
% Chebychev
wn = chebwin(cTaps,70); wn=wn/sum(wn); Wk = abs(f_fzt(wn,F)); WkdB = f_dB(Wk);
subplot(6,3,16); stem(tn,wn); ylabel('Chebychev'); subplot(6,3,17); plot(F,Wk);
subplot(6,3,18); plot(F,WkdB);
axis([0 0.5 mindB maxdB]); grid on;
Listing 5.3: Frequency responses of different window functions computed with z transform.
Exercise:
Use the code of listing 5.3 and replace f_fzt(...) by fft(.). You will also need a little change in
the statement F=Fz. What change?
......................
What kind of transformation delivers the more precise information in this case, fft or fzt ?
- 32 -
5.4 Applying Windows
Create file f_blackmanharris.m as listed in chapter 6. Use listing created in chapter 5.1 from
listing 3.6 using fft(.). Listing 5.1 uses f_fzt(...) In chapters 3.6 and 5.1 we used a rectangular
(“do-noting”) window:
win = ones(1,cTaps);
% win = f_blackmanharris(cTaps);
% win = chebwin(cTaps,100)';
Modify the window function to a Blackman-Harris window by modifying the comment signs:
% win = ones(1,cTaps);
win = f_blackmanharris(cTaps);
% win = chebwin(cTaps,100)';
Running the model should deliver Fig. 5.4.
Fig. 5.4: Same time-domain samples as shown in Fig. 5.1, but here multiplied with a
Blackman-Harris window function before z transformation. Top-down: x[n], |X(F)|,
dB(|X(F)|), Left: Fosc=5/cTaps, Right: Fosc=5.1/cTaps.
Table 5.4: Comparison of measurements with rectangular and Blackman-Harris window
(a) rectangular (“do-nothing”) window
waves / time-interval:
kosc = 5
(b) Blackman-Harris window
kosc=5.1
waves / time-interval:
max(|Xz(Fz)|) / dB
max (|Xz(Fz)|) / dB
SideMax{|Xz(Fz)|}/dB
SideMax{|Xz(Fz)|}/dB
Xz,max - Xz,SideMax / dB
Xz,max - Xz,SideMax / dB
Window width / F
Window-Width / F
- 33 -
kosc = 5
kosc=5.1
Table 5.4 summarizes the comparison between the z transforms in Fig. 5.1 with rectangular
(do-nothing) window and that of Fig. 5.4 with Blackman-Harris window. Run both models
and take the following data from the graphics windows (use zoom).
Measure in both Fig. 5.1 and Fig. 5.4 for both kosc=5 and kosc=5.1 and note the results in
Table 5.4:
max(|Xz(Fz)|) / dB : The height of the maximum of Fz(F) in dB.
SideMax{|Xz(Fz)|} / dB: The height of the highest side maximum of Fz(F) in dB.
Xz,max - Xz,SideMax / dB : Difference between absolute and highest side maximum in dB.
Window width / F: The width in relative frequency, F, of the main hump of the window
function measured between the local minima left and right to the main hump.
Note that what we see Figs. 5.1 and 5.4 are the window functions in the frequency domain, as
the underlying sin function is a Dirac in frequency domain, and convolution from Dirac and
window delivers window centered around the Dirac.
The side maxima of the window functions are significantly lower than for the do-nothing
window, however on the cost of wider windows in the frequency-domain. What can we do to
get that F-domain window narrower? Try it!
.............................................................
When we use window functions such as Blackman-Harris, is it still important to have an
integral number of wavelengths in the time-domain interval?
.............................................................
- 34 -
6 Appendix: Selfmade Matlab Function Library
6.1 Amplification-to-Decibel Conversion
The function f_dB(x) computes the amplification of a signal (e.g. voltage or current) into a dB
value. Optionally a tiny offset can be added to avoid log(0). Note that this dB computation
does not hold for power-amplification, that would deliver half the value.
function dB = f_dB(x,offset)
if nargin>1;
dB=20*log10(abs(x)+offset); % tiny offset avoids log10(0)
else
dB=20*log10(abs(x));
end;
Listing 6.1: Converting an amplitude amplification into decibels.
6.2 Blackman-Harris Window
The function f_blackmanharris(cTaps) generates row-vector containing a Blackman-Harris
window with cTaps taps. The Matlab or Octave signal processing toolboxes contain
predefined function blackmanharris(N) doing the same job with the only distinction that they
generate a column-vector.
function blackmanharris = f_blackmanharris(cTaps)
a0=0.35875; a1=0.48829; a2=0.14128; a3=0.01168;
% sum_ai = a0 + a1 + a2 + a3 % should deliver 1
% dif_ai = a0 - a1 + a2 - a3 % should deliver 6e-5
if cTaps < 1;
printf('\nError in f_blackmanharris: less than 1 tap: %g',cTaps);
elseif cTaps==1;
blackmanharris=1;
else
t=0:cTaps-1;
phi=2*pi*t/(cTaps-1);
blackmanharris = a0 - a1*cos(phi) + a2*cos(2*phi) - a3*cos(3*phi);
end;
Listing 6.2: Computing a Blackman-Harris window as row-vector with cTaps taps.
6.3 (Relatively) Fast z Transform
The self-made fast z transform computes the standard formula using possibilities to speed up
the numerical computation.
- 35 -
%%%%%%%%%1%%%%%%%%%2%%%%%%%%%3%%%%%%%%%4%%%%%%%%%5%%%%%%%%%6%%%%%%%%%7%%%%%%%%%8
%
% Function: f_fzt
%
% Compute z transformation, try to speed up
%
1-dim case: Hz = a0 + a1*z^-1 + ... + ak*z^-k
%
2-dim case: Hz = (a0 + a1^-1 + ... + ak^z^-k)/(b0 + b1^-1 + ... + ab^z^-k)
%
% Inputs :
% Required: coefs: vector or 2-dim array of coefficients
%
possible sizes: (1,N), (2,N), (M,1), (M,2),
%
in case of size (2,2) coefs(1,:) is numerator
% Optional: F : relatve Frequency values, typ. range 0-0.5
%
% Outputs:
% Hz : z-transf.: Hz=sum{nom(k)*z.^-(k-1))/sum(denom(k)*z-(k-1))}
% F : relatve frequnecy values, typ. range 0-0.5
%
% Author : Martin Schubert
% Date
: 04.Oct.2015
%
%%%%%%%%%1%%%%%%%%%2%%%%%%%%%3%%%%%%%%%4%%%%%%%%%5%%%%%%%%%6%%%%%%%%%7%%%%%%%%%8
%
function [Hz,Fz] = f_fzt(coefs,Fz)
if not(exist('Fz')), % if no F is given, make it yourself
Fz=0:1e-3:0.5;
% 501 Frequency points
end;
%
% check array size and transpose if necessary
M = size(coefs,1);
% horizontal size of array
N = size(coefs,2);
% vertical size of array
if M>N;
coefs=coefs';
M = size(coefs,1);
% horizontal size of array
N = size(coefs,2);
% vertical size of array
end;
%
% compute numerator
numerator = coefs(1,:);
if M>1; denominator = coefs(2,:); end;
%
zpow1 = exp(-sqrt(-1)*2*pi*Fz);
zpowk = 1;
Hz = numerator(1)*ones(1,length(Fz));
for k=2:length(numerator);
zpowk = zpowk.*zpow1;
Hz = Hz + numerator(k)*zpowk;
end;
% compute denominator if available
if M>1; % do we have a denominator?
Hz_denom = denominator(1)*ones(1,length(Fz));
zpowk = 1;
for k=2:length(denominator)
zpowk = zpowk.*zpow1;
Hz_denom = Hz_denom + denominator(k)*zpowk;
end;
Hz = Hz./Hz_denom;
end;
Listing 6.3: Converting an amplification into decibels.
- 36 -
6.4 Convert MPEG and WAV Files to an ASCII Vector
%%%%%%%%%1%%%%%%%%%2%%%%%%%%%3%%%%%%%%%4%%%%%%%%%5%%%%%%%%%6%%%%%%%%%7%%%%%
% Convert mp3- or wav- file to ascii-file [with clipping]
% Inputs : inquired
% Outputs: ascii file soundout.txt
% Author : Martin Schubert
% Date
: 24.Sep.2015
%%%%%%%%%1%%%%%%%%%2%%%%%%%%%3%%%%%%%%%4%%%%%%%%%5%%%%%%%%%6%%%%%%%%%7%%%%%
%
% read sound data
s0='\n0: quit\n';
s1='\n1: sound input file name = sound_fs=44100Hz.txt \n';
s2= '2: sound input file name = sound.mpg \n';
s3= '3: sound input file name = sound.wav \n';
se= '<return>: end';
ssel='type an integer to take your choice: ';
smenu=strcat('\n',s1,s2,s3,se);
fprintf(smenu);
isel=input('type an integer to take your choice: ');
while(isel>0),
if isel==1,
sound_input_filename='sound_fs=44100Hz.txt';
sound_left = dlmread(sound_input_filename);
elseif isel==2,
sound_input_filename='sound.mpg';
[sound_left_right,fs] = audioread(sound_input_filename);
sound_left = sound_left_right(:,1);
sound_right = sound_left_right(:,2);
elseif isel==3,
sound_input_filename='sound.wav';
[sound_left_right,fs] = audioread(sound_input_filename);
sound_left = sound_left_right(:,1);
sound_right = sound_left_right(:,2);
end;
%
% cut sound vector
offset_sample = input('first samples to be discarded? '); % Queen: 35
saved_samples = input('number of samples to be saved? '); % 2^20
last_sample = offset_sample+saved_samples;
% cut out short version
sound_left_short=sound_left(offset_sample+1:last_sample);
%
% write sound data to file
jsel=input('write sound to soundout.txt? no|short|complete [0|1|2] ');
sound_output_filename = 'soundout.txt';
if(jsel==1);
dlmwrite(sound_output_filename,sound_left_short);
elseif(jsel==2)
dlmwrite(sound_output_filename,sound_left);
end;
%
%
% Play sound:
ksel = input('Play sound? no|short|full [0|1|2] ');
if(ksel==1);
sound(sound_left_short,fs);
elseif(ksel==2)
sound(sound_left,fs);
end;
smenu=strcat('\n',s1,s2,s3,sq,ssel);
isel=input(smenu);
end;
Listing 6.4: Convert a mpeg or wav file to an ASCII vector of numbers.
- 37 -
7 References
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
MathWorks, http://www.mathworks.de/ at 23.Sep.2015.
A. Angermann, M. Beuschel, M. Rau, U. Wohlfarth, Lehrbuch Matlab - Simulink – Stateflow,
www.matlabbuch.de, at 23.Sep.2015
GNU Octave, available: https://www.gnu.org/software/octave/ at 23.Sep.2015.
M. Schubert, LTI System Modeling Using Matlab,
URL: Homepage M. Schubert > Offered Education > Courses and Laboratories > RED+PRED
> Matlab summary, available Oct. 4, 2015: https://hps.hs-regensburg.de/scm39115/homepage
/education/courses/red/Zusammenfassung_Matlab-Buch.pdf.
M. Schubert, LTI System Modeling Using Matlab,
URL: Homepage M. Schubert > Offered Education > Courses and Laboratories > SK > PSK2:
https://hps.hs-regensburg.de/scm39115/homepage/education/courses/psk/psk.htm, 21.Sep.2015.
M. Schubert, Zeitdiskretisierung, Chapter 7 of Course Systemkonzepte,
Homepage Ostbayerische Technische Hochschule (OTH) Regensburg, Prof. M. Schubert >
Offered Education > Courses and Laboratories > SK > Ch. 8, URL: https://hps.hsregensburg.de/scm39115/homepage/education/courses/sk/sk.htm, at 21.09.2015.
M. Schubert, Fourier und Symmetrien, Chapter 8 of Course Systemkonzepte,
Homepage Ostbayerische Technische Hochschule (OTH) Regensburg, Prof. M. Schubert >
Offered Education > Courses and Laboratories > SK > Ch. 8, URL: https://hps.hsregensburg.de/scm39115/homepage/education/courses/sk/sk.htm, at 21.09.2015.
James W. Cooley, John W. Tukey: An Algorithm for the Machine Calculation of Complex
Fourier Series, Math. Comp. 19, p. 297-301
- 38 -
© Copyright 2026 Paperzz