Biomedical Signal Processing
Biomedical Signal Processing
Biomedical Signal
Processing
Lecture # 4
Course webpage:
http://www.iyte.edu.tr/~zubeyirunlu/teaching/ee434
DFT analysis
equation
Even though the original time domain sequence to be transformed is not periodic!
This can also be seen easily from the complex exponential wheel. Since there are
only N vectors around a unit circle, the transform will repeat
itself every N points
The spectrum of the sampled signal was identical to that of the continuous signal,
except, with periodically replications of itself every 2π
That is, sampling the signal in time domain, caused the frequency domain to be
periodic with 2π
From the duality of the Fourier transform, this corresponds to making the time
domain periodic
X = fft(x) returns the discrete Fourier transform (DFT) of vector X, computed with a fast
Fourier transform (FFT) algorithm
If x is a matrix, fft returns the Fourier transform of each column of the matrix. In this case, the length of
X and the length of x are identical
X = fft(x,N) returns the N-point DFT. If the length of X is less than N, X is padded with trailing zeros
to length N. If the length of X is greater than N, the sequence X is truncated
The N points returned by fft corresponds to frequencies in the [0 2π] range, equally spaced with an
interval of 2π/N
Note that the Nth FFT point corresponds to 2π, which in turn corresponds to the sampling frequency
If x[n] is real, X[k] is symmetric. Using the fftshift() function shifts the center of symmetry so that the
FFT is given in the [-π to π] interval, rather then [0 2π]
n=0:31; k=0:31;
x=0.9.^n;
w=linspace(0, 2*pi, 512);
K=linspace(0, 2*pi, 32);
X1=1./(1-0.9*exp(-j*w)); % DTFT
X2=(1-(0.9*exp(-j*(2*pi/32)*k)).^32)./
(1-0.9*exp(-j*(2*pi/32)*k)); %DFT
X=fft(x); %FFT
subplot(311)
plot(w, abs(X1)); grid
subplot(312)
stem(K, abs(X2), 'r', 'filled'); grid
subplot(313)
stem(K, abs(X), 'g', 'filled'); grid
Digital filters are designed, expressed, applied and represented in terms of the
z-transform
It follows that the DTFT is indeed a special case of the z-transform, specifically,
z-transform reduces to DTFT for the special case of r=1, that is, |z|=1, provided
that the latter exists
The contour |z|=1 is a circle in the z-plane of unit radius Æ the unit circle
Hence, the DTFT is really the z-transform evaluated on the unit circle
Just like the DTFT, z-transform too has its own, albeit less restrictive, convergence
requirements, specifically, the infinite series must converge
For a given sequence, the set R of values of z for which its z-transform converges
is called the region of convergence (ROC)
March 13, 2008 Lecture # 4 12
Examples
Determine the z-transform and the corresponding ROC of the causal sequence
x[n]=αnu[n]
Both transfer functions have a pole at z=α, which make the transfer function
asymptotically approach to infinity at this value. Therefore, z=α is not included in
either of the ROCs
The circle with the radius of α is called the pole circle. A system may have many
poles, and hence many pole circles
For right sided sequences, the ROC extend outside of the outermost pole circle,
whereas for left sided sequences, the ROC is the inside of the innermost pole circle
For two-sided sequences, the ROC will be the intersection of the two ROC areas
corresponding to the left and right sides of the sequence
Since DTFT is the z-transform evaluated on the unit circle, that is for z=ejω,
DTFT of a sequence exists if and only if the ROC includes the unit circle!
March 13, 2008 Lecture # 4 15
Rational z-Transforms
The z-transforms of LTI systems can be expressed as a ratio of
two polynomials in z-1, hence they are rational transforms
At a root z=ζℓ of the numerator polynomial H(ζℓ)=0, and as a result, these values of z are
known as the zeros of H(z)
At a root z=pℓ of the denominator polynomial H(pℓ) Æ ∞, and as a result, these values of z
are known as the poles of H(z)
If N > M there are additional N-M zeros at z = 0 (the origin in the z-plane)
As we will see later, a digital filter is designed by placing appropriate number of zeros at the
frequencies (z-values) to be suppressed, and poles at the frequencies to be amplified!
The ROC of a causal system extends outside of the outermost pole circle
The ROC of an anticausal system (whose h[n] is purely left-sided) lies inside of the
innermost pole circle
The ROC of a noncausal system (whose h[n] two-sided) is bounded by two different pole
circles
For a system to be stable, its h[n] must be absolutely summable Æ An LTI system is
stable, if and only if the ROC of its transfer function H(z) includes the unit circle!
A causal system’s ROC lies outside of a pole circle. If that system is also stable, its ROC
must include unit circle Æ Then a causal system is stable, if and only if, all poles are inside
the unit circle! Similarly, an anticausal system is stable, if and only if its poles lie outside
the unit circle.
subplot(221)
plot(w/pi, abs(H1)); grid
title('Transfer Function of 5 point MAF')
subplot(222)
zplane(b1,a1);
title('Pole-zero plot of 5 point MAF')
subplot(223)
plot(w/pi, abs(H2)); grid
title('Transfer Function of 9 point MAF')
subplot(224)
zplane(b2,a2);
title('Pole-zero plot of 9 point MAF')
March 13, 2008 Lecture # 4 20
Ideal Filter
An ideal filter is a digital filter designed to pass signal components of
certain frequencies without distortion, which therefore has a
frequency response equal to 1 at these frequencies, and has a frequency
response equal to 0 at all other frequencies
The range of frequencies where the frequency response takes the value
of one is called the passband
The range of frequencies where the frequency response takes the value
of zero is called the stopband
The remaining three ideal filters are also characterized by doubly infinite,
noncausal impulse responses and also are not absolutely summable
Thus, the ideal filters with the ideal brick wall frequency
responses cannot be realized with finite dimensional LTI filter
Zero phase
Linear phase
Non-linear phase
In many applications, it is necessary that the digital filter designed does not distort the phase
of the input signal components with frequencies in the passband
March 13, 2008 Lecture # 4 26
Phase and Group Delay
A frequency selective system (filter) with frequency response
H(ω)=|H(ω)|∠H(ω) = |H(ω)|ejθ(ω) changes the amplitude of all
frequencies in the signal by a factor of |H(ω)|, and adds a phase of
θ(ω) to all frequencies
Note that both the amplitude change and the phase delay are functions of ω
The phase θ(ω) is in terms of radians, but can be expressed in terms of time,
which is called the phase delay. The phase delay at a particular frequency ω0
is given as
Process the input data (finite length) with a causal real-coefficient filter H(z).
Time reverse the output of this filter and process by the same filter.
For a causal transfer function with a nonzero phase response, the phase
distortion can be avoided by ensuring that the transfer function has (preferably)
a unity magnitude and a linear-phase characteristic in the frequency band of
interest
If we have linear phase, that is, θ(ω)=-αω, then the total delay at
any frequency ω0 is τ0 = -θ(ω0)/ ω0 = -αω0/ω0 = α
If the filter does not have linear phase, then different frequency
components are delayed by different amounts, causing significant
distortion
March 13, 2008 Lecture # 4 32
Linear Phase Filters
It is typically impossible to design a linear phase IIR filter, however,
designing FIR filters with precise linear phase is very easy:
This transfer function has linear phase, if its impulse response h[n] is either
symmetric
or anti-symmetric
lin_phase_demo2.m
March 13, 2008 Lecture # 4 35
Zeros & Poles of An FIR Filter
Example
Typically magnitude (and sometimes phase) response of the desired filter is specified.
Recall that there are four basic types of ideal digital filter, based on magnitude response
We will assume that we are dealing with filters with real coefficients, hence the
frequency response is periodic with 2π, and symmetric around 0 and π
March 13, 2008 Lecture # 4 38
Filter Specifications
Filter specifications are often given in
decibels, in terms of loss of gain:
If linear phase is desired, the filter coefficients h[n] (also the impulse response)
must satisfy symmetry constraints: h[n] = ±h[M-n]
For computational efficiency, the minimum filter order M that satisfies design
criteria must be used
If the filter is IIR, then H(z) is a real rational function of z-1
The nonlinear phase of an IIR filter can be minimized using an appropriate allpass
filter, however, by that time the computational advantage of IIR is lost
However, in most applications that does not require real-time operation, phase is
not an issue. Why…?
March 13, 2008 Lecture # 4 41
Basic Design Approaches
IIR filter design:
1. Convert the digital filter specifications into an analog prototype lowpass filter
specifications
2. Determine the analog lowpass filter transfer function |H(Ω)|
3. Transform |H(Ω)| into the desired digital transfer function H(z)
Analog approximation techniques are highly advanced
They usually yield closed-form solutions
Extensive tables are available for analog filter design
Many applications require digital simulation of analog systems
Truncate using a window, say a rectangle, so that M+1 coefficients of h[n] are
retained, and all the others are discarded
We now have a finite length (order M) filter, ht[n], however, it is still non-causal
Shift the truncated h[n] to the right (i.e., delay) by M/2 samples, so that the
first sample now occurs at n=0
The resulting impulse response, ht[n-M/2] is a causal, stable, FIR filter, which has
an almost identical magnitude response and a phase factor or e-jM/2 compared to
the original filter, due to delay introduced
How to design:
Set
Choose window type based on the specified sidelobe attenuation (Asl) or
minimum stopband attenuation (αs)
Choose M according to the transition band width (∆ω) and/or mainlobe width
(∆ML ). Note that this is the only parameter that can be adjusted for fixed
window functions. Once a window type and M is selected, so are Asl, αs, and ∆ML
Ripple amplitudes cannot be custom designed
Adjustable windows have a parameter that can be varied to trade-off between
main-lobe width and side-lobe attenuation
March 13, 2008 Lecture # 4 54
Kaiser Window
The most popular adjustable window
3. Window parameters:
b = fir1(N,Wn,'high') designs an N'th order highpass filter. You can also use b = fir1(N,Wn,'low') to
design a lowpass filter.
If Wn is a two-element vector, Wn = [W1 W2], FIR1 returns an order N bandpass filter with passband W1 <
W < W2. You can also specify b = fir1(N,Wn,'bandpass'). If Wn = [W1 W2], b = fir1(N,Wn,'stop') will
design a bandstop filter.
b = fir1(N,Wn,WIN) designs an N-th order FIR filter using the N+1 length vector WIN to window the
impulse response. If empty or omitted, FIR1 uses a Hamming window of length N+1. For a complete list of available
windows, see the help for the WINDOW function. If using a Kaiser window, use the following
b = fir1(N,Wn,kaiser(n+1,beta))
March 13, 2008 Lecture # 4 62
Here is the complete procedure:
In Matlab
Obtain the specs: Cutoff frequency, passband and stopband edge frequencies, allowed
maximum ripple, filter order
Note that you do not need filter order for Kaiser (to be determined), and you do
not need the edge frequencies and ripple amount for the others
Design the filter using the fir1() command. Default window is Hamming. For all
window types –except Kaiser – provide filter order N and normalized cutoff
frequency (between 0 and 1)
For Kaiser window, determine the beta and M manually from the given equations
before providing them to the fir1 command with kaiser as filter type
You can also use kaiserord() to estimate the filter order from
This gives you the “b” coefficients, or in other words, the impulse response h[n]
Use this filter with the filter() command as y=filter(b,a,x), where b are the
coefficients obtained from fir1(), a=1 since this is an FIR filter, x is the signal to be
filtered, and y is the filtered signal
OR, use sptool for the entire design cycle
March 13, 2008 Lecture # 4 Fir1_demo_chirp.m 63
Frequency Selection Method: fir2
freq=[0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1]
amp=[0 0.5 1 0.8 0.6 0 0.5 0.5 1 0 0]
subplot(211)
[H w]=freqz(b, 1, 1024);
plot(w/pi, abs(H)); hold on
plot(freq, amp, 'r*')
grid
xlabel('Frequency, \omega/\pi')
title(' Magnitude response of the filter and the
corresponding points')
subplot(212)
plot(w/pi, unwrap(angle(H)));
xlabel('Frequency, \omega/\pi')
title(' Phase response of the designed filter')
grid
You will need to determine the filter order by trial and error. You may need higher orders if
your specified points require a sharp transition
March 13, 2008 Lecture # 4 64
IIR Filter Design
Bilinear transformation for analog ÅÆ digital domains
Analog IIR filter design
Butterworth approximation and Matlab implementation
Chebyshev approximation and Matlab implementation
Elliptic filters
Bessel filters
Traditionally, these filters are first designed in the analog domain, and then
converted into digital domain using bilinear transformation, but today they can be
designed directly in the digital domain
Spectral transformations
Analog – to – analog transformations
Digital – to – digital transformations
Direct IIR filter design
March 13, 2008 Lecture # 4 65
In Matlab
Yes, you guessed it right, Matlab has several functions:
[N, Wn] = buttord(Wp, Ws, Rp, Rs) returns the order N of the lowest
order digital Butterworth filter that loses no more than Rp dB in the
passband and has at least Rs dB of attenuation in the stopband. Wp and
Ws are the passband and stopband edge frequencies, normalized from 0
to 1 (where 1 corresponds to pi radians/sample)
buttord() also returns Wn, the Butterworth natural frequency (or, the "3
dB frequency") to use with butter() to achieve the desired specs
[N, Wn] = buttord(Wp, Ws, Rp, Rs, 's') does the computation for an
analog filter, in which case Wp and Ws are in radians/second. Note that
for analog filter design Wn is not restricted to the [0 1] range. When Rp is
chosen as 3 dB, the Wn in butter() is equal to Wp in buttord()
March 13, 2008 Lecture # 4 66
butter() Butterworth digital and analog filter design
In Matlab
[B,A] = butter(N,Wn) designs an Nth order lowpass digital Butterworth filter and
returns the filter coefficients in length N+1 vectors B (numerator) and A (denominator).
The coefficients are listed in descending powers of z. The cutoff frequency Wn must be
0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. If Wn is a two-element
vector, Wn = [W1 W2], butter() returns an order 2N bandpass filter with passband W1 <
W < W2
When used with three left-hand arguments, as in [Z,P,K] = butter(...), the zeros and poles
are returned in length N column vectors Z and P, and the gain in scalar K
[B,A] = cheby1(N,R,Wn) designs an Nth order type I lowpass digital Chebyshev filter with R decibels
of peak-to-peak ripple in the passband. cheby1 returns the filter coefficients in length N+1 vectors B
(numerator) and A (denominator). The cutoff frequency Wn must be 0.0 < Wn < 1.0, with 1.0
corresponding to half the sample rate. Use R=0.5 as a starting point, if you are unsure about choosing R
[B,A] = cheby2(N,R,Wn) designs an Nth order type II lowpass digital Chebyshev filter with the
stopband ripple R decibels down and stopband edge frequency Wn. cheby2() returns the filter
coefficients in length N+1 vectors B (numerator) and A (denominator). The cutoff frequency Wn must be
0.0 < Wn < 1.0, with 1.0 corresponding to half the sample rate. Use R = 20 as a starting point, if you are
unsure about choosing R
If Wn is a two-element vector, Wn = [W1 W2], cheby1 returns an order 2N bandpass filter with passband
W1 < W < W2.
[B,A] = cheby1 (N,R,Wn,'high') designs a highpass filter.
[B,A] = cheby1(N,R,Wn,'stop') is a bandstop filter if Wn = [W1 W2].
When used with three left-hand arguments, as in [Z,P,K] = cheby1(…), the zeros and poles are returned
in length N column vectors Z and P, and the gain in scalar K.
[N, Wn] = cheb1ord(Wp, Ws, Rp, Rs) returns the order N of the lowest order digital
Chebyshev Type I filter that loses no more than Rp dB in the passband and has at least Rs
dB of attenuation in the stopband. Wp and Ws are the passband and stopband edge
frequencies, normalized from 0 to 1 (where 1 corresponds to π rad/sample.). For example,
Lowpass: Wp = .1, Ws = .2 Highpass: Wp = .2, Ws = .1 (note the reversal of freq.)
Bandpass: Wp = [.2 .7], Ws = [.1 .8] Bandstop: Wp = [.1 .8], Ws = [.2 .7]
cheb1ord() also returns Wn, the Chebyshev natural frequency to use with cheby1() to
achieve the specifications.
[N, Wn] = cheb1ord(Wp, Ws, Rp, Rs, 's') does the computation for an analog filter, in
which case Wp and Ws are in radians/second.
If designing an analog filter, use bilinear() to convert the analog filter coeff. to digital
filter coeff.