signal processing – summarygrezl/zre/lectures/02_sig_en.pdf · analog to digital (ad) converter...
TRANSCRIPT
Signal Processing – Summary
Jan Cernocky, Valentina Hubeika
cernocky,[email protected]
DCGM FIT BUT Brno, [email protected]
FIT BUT Brno
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 1/41
Agenda
• Introduction
• Analog signals and sampling
• Frequency analysis of sampled signals.
• Random signals.
• Filters.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 2/41
Why digital signal processing ?
• reproducibility (we don’t have to think about “component tolerance”).
• no changes due to material aging or temperature.
• easy setup (may be difficult tuning for analog signals).
• possible adaptive processing (“changing device functionality depending on the type of
the signal”).
• simulation = application.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 3/41
Usual Approach
A/Ddigitalprocessing(PC, DSP)
D/A
storingtransmisioninterpretationother processing...
- - - -
..................................................
x(t) x (n) y (n) y(t)
The input signal is continuous: function of a real-valued variable (in our case time) t, t is
defined over the interval (−∞, ∞), t → ∞.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 4/41
t
s(t)
0
To represent the signal in frequency domain (spectrum), Fourier transform is used:
X(f) =
∫ −∞
−∞
x(t)e−j2πftdt, (1)
where the function X(f) is called the spectral function, or in short spectrum. The
function is defined ∀f from −∞ to ∞ and is complex. Its magnitude is |X(f)| and its
phase is 6 X(f). Here, we talk about magnitude and phase spectra.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 5/41
In case of real signals, we will need only the right side of the spectral function (f > 0).
The left side is complex adjoint to the right side:
X(f) = X⋆(−f), (2)
or |X(f)| = |X(−f)| a arg X(f) = − arg X(−f).
f max0-f max f
|S(f)|
⇒ Good signals are frequency constrained (energy within the band of (0, fmax) ).
⇒ Spectral function cant be calculated (infinite, integral,. . . ).
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 6/41
Analog to Digital (AD) Converter
sampledsignal xs(n)
- -
££
££° -
£££°- antialias.
filtersampling quantization
quantizedsignal xq(n)
x(t)
Sampled signal is calculated by multiplying the original signal with a periodic function
over time.
sD
x (t)=x(t)s(t)
t
s(t)
0t0 1/D T
Ds(t)
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 7/41
Theoretically, sampling is multiplying the original analog signal with a series od Dirac
deltas (unit impulse function) (infinite height, zero width, unit area). The result of
multiplication is again a series of Dirac impulses, with the powers corresponding to the
values of the original signal at times nT :
1 1
s
t0 T
s(t)D
x (t)=x(t)s(t)
t
s(t)
0
T is the sampling period
Fs =1
Tis the sampling frequency
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 8/41
The spectrum of the sampled signal is periodized !!!
Xs(f) =1
T
+∞∑
n=−∞
X(
f −n
T
)
=1
T
+∞∑
n=−∞
X (f − nFs) (3)
From the point of view of the sampling theorem:
1) Fs > 2fmax: The copies of the spectrum of the original signal do not overlap so the
original signal can be perfectly reconstructed from its sampled version by filtering it with
low-pass filter with the frequency Fs/2.
2) Fs ≤ 2fmax: the copies of the spectrum of the original signal do overlap so the
resulting spectrum has a different shape than the original spectrum. The original signal
cannot be reconstructed from its sampled version. We observe so called aliasing.
Shanon–Kotelnikov–Nyquist–sampling theorem
Fs > 2fmax
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 9/41
1. Example: sampling and reconstruction – OK
Fs = 8000 Hz, fmax = 3000 Hz, so that Ωs = 16000π rad/s, ωmax = 6000π rad/s.
T = 18000 s
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
2000
4000
6000
8000
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
x 10−4
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 10/41
2. Example: sampling and reconstruction – BAD
Fs = 8000 Hz, fmax = 7000 Hz, so that Ωs = 16000π rad/s, ωmax = 14000π rad/s.
T = 18000 s
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
2000
4000
6000
8000
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
x 10−4
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 11/41
Antialiasing filter – constrain to [−Fs/2, Fs/2]
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
5000
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
x 10−4
−1.5 −1 −0.5 0 0.5 1 1.5
x 105
0
0.5
1
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 12/41
Denotation of the Sampled Signal
xs(nT ) or just xs[n] — the sampled signal is a series of values.
1. Along with the sampled signal, we always need information on the sampling frequency
(implicit or explicit)
2. When working with a sampled signal, we do not want to consider the sampling
frequency in the calculations, therefore assume that the period is T ′ = 1, thus F ′s = 1.
The normalized time is then given as:
t′ =t
T, thus n =
nT
T(4)
The normalized frequency is:
f ′ =f
Fs
(5)
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 13/41
Example
Define a function generating cosine with the frequency of 200 Hz with the sampling
frequency Fs = 8000 Hz.
In the continuous time: s(t) = cos(2πf0t) = cos(2π200t).
While sampling we will substitute the continuous time variable t with the discrete time nT ,
where T is the sampling period: x(nT ) = cos(2πf0nT ) = cos
(
2πf0
Fs
)
n.
The frequencyf0
Fs
is normalized. The desired signal is defined as: x(n) = cos
(
2π1
40n
)
.
Generate 1 second of such a signal in Matlab:
n = 0:7999;
x = cos (2 * pi * 1 / 40 * n);
wavwrite(x,8000,16,’sig.wav’);
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 14/41
Behaviour of the Sampled Signal in Frequency Domain — Spectrum
Discrete Fourier transform – DTFT – definition:
X(k) =
N−1∑
n=0
x[n]e−j2π nkN pro k ∈< 0, N − 1 > (6)
How to use it for discrete signals?:
• analyze a “window” of the length of N samples.
• result: when multiplying values X(k) with the sampling perioud T , the approximation
of the spectral function at the points k∆f , where ∆f =Fs
N(true frequency) or
∆f ′ =1
N(normalized frequency (usually not used)
X(k∆f) = T
N−1∑
n=0
x[n]e−j2π nkN (7)
in this definition, x[n] can be substitute for x(nT ).
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 15/41
Comparing to the spectral function of the corresponding analog signal, the spectrum of the
discrete signal is not the same thing!
1. we calculate spectrum of the sampled signal, so the spectrum is periodic with the
period N units (corresponds to the sampling frequency Fs). If k ∈ (−∞, +∞), then
X(k∆f) is repeated after each N values.
2. The signal is windowed. The resulting spectrum carries information on the window:
the signal is multiplied with the window function in time, the spectrum of the window
is then convoluted with the spectrum of the original signal. This often results in
blurring of the theoretically sharp lines of the spectrum (for instance during analysis of
the harmonic signal). More in the lecture on preprocessing.
3. if the spectrum is discrete (we dispose of only N values from 0 to Fs), then the signal
must be periodic !
time frequency
sampling periodization
periodization discretization
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 16/41
How can we use it in practice?
We want to make frequency analysis of one voiced frame of speech
s = wavread(’test.wav’)’;
sfr = frame (s,160,80);
x = sfr(:,13);
plot (x);
0 20 40 60 80 100 120 140 160−0.3
−0.2
−0.1
0
0.1
0.2
0.3
0.4
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 17/41
DTFT only – careful with the frequency axes:
Fs = 8000; f = (0:159) / 160 * Fs; X = fft(x);
subplot (211); plot(f,abs(X)); subplot (212); plot(f,angle(X));
0 1000 2000 3000 4000 5000 6000 7000 80000
1
2
3
4
5
6
7
0 1000 2000 3000 4000 5000 6000 7000 8000−4
−2
0
2
4
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 18/41
DTFT only – the left half is symmetric to the right half (we consider only on in analysis)
Fs = 8000; f = (0:79) / 160 * Fs; X = fft(x); X = X(1:80);
subplot (211); plot(f,abs(X)); subplot (212); plot(f,angle(X));
0 500 1000 1500 2000 2500 3000 3500 40000
1
2
3
4
5
6
7
0 500 1000 1500 2000 2500 3000 3500 4000−3
−2
−1
0
1
2
3
4
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 19/41
If we want to smoother the spectrum (add more points – shorten intervals between values),
having only one frame (without a possibility of extension), we can use ⇒ zero padding
Fs = 8000; f = (0:511) / 1024 * Fs;
X = fft([x’ zeros(1,1024-160)]); X = X(1:512);
subplot (211); plot(f,abs(X)); subplot (212); plot(f,angle(X));
0 500 1000 1500 2000 2500 3000 3500 40000
2
4
6
8
0 500 1000 1500 2000 2500 3000 3500 4000−4
−2
0
2
4
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 20/41
FREQUENCY ANALYSIS OF RANDOM SIGNALS
Speech signal is a random signal. Thus we use power spectral density (PSD) function
for its frequency analysis. PSD is real and represent power density of the signal in
frequency domain. One of the possible ways of computation uses DTFT:
GDTFT (k∆f) =1
N|X[k]|2.
Which is just the power of the magnitude.
⇒ careful with |X[k]|2. in Matlab – power of the complex number is not the same as the
power of its magnitude:
• X = fft(x); Gdft = X .^ 2; wrong !
• X = fft(x); Gdft = abs(X) .^ 2; good !
• X = fft(x); Gdft = X .* conj(X); good and fast ,
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 21/41
Example:
Fs = 8000; f = (0:511) / 1024 * Fs
X = fft([x’ zeros(1,1024-160)]); X = X(1:512); Gdft= 1/160 *abs(X) .^ 2;
plot(f,Gdft);
0 500 1000 1500 2000 2500 3000 3500 40000
0.05
0.1
0.15
0.2
0.25
0.3
0.35
0.4
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 22/41
Dynamic performance of the power spectral density is higher than of DTFT (second
power) which causes poor visualization of “weak” parts of the image. For this reason,
often decibels are used (Matlab: function log10)
GDTFT (k∆f) = 10 log10
1
N|X[k]|2.
0 500 1000 1500 2000 2500 3000 3500 4000−80
−70
−60
−50
−40
−30
−20
−10
0
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 23/41
LINEAR FILTERS
filter
x(n) y(n)
Usually, filters are
• linear — hold linear combination : if x1(n) → y1(n) a x2(n) → y2(n), then
ax1(n) + bx2(n) → ay1(n) + by2(n), where a, b ∈ ℜ.
• time invariant — behaviour does not depend on time: if x(n) → y(n), then as well
x(n − n0) → y(n − n0), where n0 is any arbitrary shift. Sometimes however we desire
filters with time variant behaviour — adaptive systems, speech frames (change
∀10 ms).
• causal — the filter “does not see into future”: y(n) ∼ y(m < n) a x(m ≤ n).
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 24/41
Impulse Response
or impulse characteristics is the response of a filter to the Kroneck or unit impulse (do not
confuse with the Dirac delta):
δ(n) =
0 pro n 6= 0
1 pro n = 0
(8)
filter
(n)δ h(n)
Knowing the impulse response, we can calculate the response to any input signal. Each
sample is perceived as a shifted and scaled unit impulse. We represent the output as the
convolution:
y(n) = x(n) ⋆ h(n) =∞∑
m=−∞
x(m)h(n − m) =∞∑
m=−∞
h(m)x(n − m) (9)
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 25/41
Analysis of the impulse characteristics:
• if h(k) = 0 for ∀k < 0, then the filter is causal (all samples after n-th sample will be
multiplied by zero).
• impulse response can be finite — FIR (finite impulse response) or infinite — IIR
(infinite impulse response).
• the Fourier image in frequency then represents the complex characteristics of the filter:
h(k) → H(f)
Convolution in time domain correspond to product in frequency domain, thus the
spectrum of the resulting signal is:
Y (f) = X(f)H(f) (10)
We should keep in mind that we work with the discrete signals (so is the impulse response
of a filter) the image in the frequency is therefore periodic with the period Fs or 1 for the
normalized frequency.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 26/41
Representation of a Filter
z -1 z -1 z -1...
bQ-1
z -1 z -1 z -1Σ
...
-a
-a
1
2
bQ
...
bQ-2
-a Pb0
-ab1 P-1
...
y(n)
x(n)
The block z−1 denotes a delay of 1 sample. The behaviour of a filter can be expressed as
a difference equation:
y(n) =
Q∑
k=0
bkx(n − k) −
P∑
k=1
aky(n − k), (11)
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 27/41
where x(n− k) are actual and delayed input values and y(n− k) are delayed output values.
Types of filters:
• FIR – non-recursive: only b0 . . . bQ nonzero. Always stable.
• IIR – solely recursive: only b0, a1 . . . aP nonzero.
• IIR – generally recursive: ai and bi nonzero.
It is difficult to estimate the behaviour of the filter and analyze it from the point of view of
stability from the difference equation.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 28/41
Z-Transform
X(z) =
∞∑
n=−∞
x(n)z−n (12)
The z-transform is a generalization of the DTFT. The z-transform becomes the DTFT
when z = expjw (when DTFT is evaluated on the unit circle). In order to determine the
frequency response of the system the z-transform must be evaluated on the circle (the
region of convergence must contain a circle). Otherwise, the DTFT of the system does not
exist.
Lets define delay as: if x(n) → X(z), then for y(n) = x(n − n0) will hold:
Y (z) = z−n0X(z) (13)
for the delay of one time unit holds: x(n − 1) → z−1X(z). Thus the delay of one time
unit will be denoted as:
z -1
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 29/41
Transfer Function of a Filter
We can rewrite the difference equation using z-transform
Y (z) =
Q∑
k=0
bkX(z)z−k −P
∑
k=1
akY (z)z−k, (14)
The transfer function can be defined as the fraction:
H(z) =Y (z)
X(z)=
Q∑
k=0
bkz−k
1 +
P∑
k=1
akz−k
=B(z)
A(z), (15)
where B(z) and A(z) are two polynomes. The a0 coefficient of the denominator must be
equal to 1. a0 does not physically present in the filter (denotes that the filter has the
output).
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 30/41
Frequency Characteristics
of a filter from 0 to Fs (or from 0 to 1 in using normalized frequency) can be acquired
from the transfer function by analyzing the unit circle. We are interested in the complex
values of the function H(z):
H(f) = H(z)|z=ej2πf (16)
for the normalized frequency. Or:
H(f) = H(z)|z=ej2πfT (17)
for the normal frequency.
For each value f we need to calculate the position of the point on the unit circle:
z = ej2πf (complex number), then for this number we need to compute the fraction of the
polynomes B(z) and A(z) (complex number). In Matlab by using function freqz.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 31/41
Re
Im
1~Fs1/2~Fs/2
"z"
πexp(j2 f)
0 1
−j
−1
+j
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 32/41
Zeros and Poles of the Transmission Function. . .
The other way how to represent the transfer function H(z) is using product:
H(z) =B(z)
A(z)=
b0 + b1z−1 + . . . + bQz−Q
1 + a1z−1 + . . . + aP z−P
=z−Q(b0z
Q + b1zQ−1 + . . . + bQ)
z−P (zP + a1zP−1 + . . . + aP )
=
= b0z−Q
z−P
Q∏
k=1
(z − nk)
P∏
k=1
(z − pk)
= b0zP−Q
Q∏
k=1
(z − nk)
P∏
k=1
(z − pk),
If ak, bk ∈ ℜ, then poles pk and zeros nk can be either real or complex conjugate in pairs.
Filter is stable when all poles lie within the unity circle:
|pk| < 1
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 33/41
Example of a Filter
We want to implement a filter that will be simulating a telephone channel to filter a signal
of the CD quality. Our filter will be a band-pass filter from 300 to 3400 Hz. There are
several functions available in Matlab to implement a filter. We will be using so called
elliptic filters:
Fs = 44100; Fs2 = Fs/2; % has to be normalized by half Fs
Wp = [300/Fs2 3400/Fs2]; % pass-band
Ws = [200/Fs2 3500/Fs2]; % stop-band - approximately
Rp = 3; % ripple in pass-bandu dB
Rs = 30; % muting stop-bandu dB (obe hodnoty od
% see the norms.
[N, Wn] = ellipord(Wp, Ws, Rp, Rs) % computing of the filter order
[B,A] = ellip(N,Rp,Rs,Wn) % computing of polynoms B a A
. . . as the result we have 2 polynomes of the 12th order.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 34/41
Frequency characteristics: freqz (B,A,512,Fs);
0 0.5 1 1.5 2
x 104
−1500
−1000
−500
0
Frequency (Hz)
Pha
se (
degr
ees)
0 0.5 1 1.5 2
x 104
−100
−50
0
50
Frequency (Hz)
Mag
nitu
de (
dB)
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 35/41
Poles and zeros: zplane (B,A);
−0.5 0 0.5 1 1.5 2−1
−0.8
−0.6
−0.4
−0.2
0
0.2
0.4
0.6
0.8
Real Part
Imag
inar
y P
art
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 36/41
Implementation in C
• the basic implementation of the direct structure is very simple – just rewrite the
difference equation into C (see the file filter.c)
• in practice, usually optimal structures are used, which have only one delay bus and are
not prone to the rounding errors.
• for more information on the filter theory see SXC/ISS – lecture “discrete systems”:
http://www.fit.vutbr.cz/~cernocky/sig
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 37/41
Passing a Random Signal through a Filter
Let the filter have a complex frequency characteristic H(f). For the input signal with the
spectral power density Gx(f) the output spectral power density is given as:
Gy(f) = |H(f)|2Gx(f)
. . . the input PSD is multiplied by the power of the magnitude of the complex frequency
characteristic.
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 38/41
Example: Given a realization of a random signal. The
signal is filtered by the filter defined as H(z) = 1−0.9z−1. The output ignal and its PSD are:
50 100 150 200 250 300
−0.2
0
0.2
0 0.5 1 1.5 2 2.5 3
0.1
0.2
0.3
0.4
0.5
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 39/41
The magnitude of the frequency characteristic and its power are:
0 0.5 1 1.5 2 2.5 3
0.5
1
1.5
|H|
0 0.5 1 1.5 2 2.5 3
0.5
1
1.5
2
2.5
3
3.5
|H|2
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 40/41
The output signal and its PSD:
50 100 150 200 250 300
−0.2
0
0.2
0 0.5 1 1.5 2 2.5 3
0.02
0.04
0.06
0.08
0.1
Signal Processing – Summary Jan Cernocky, Valentina Hubeika, CDGM FIT BUT Brno 41/41