MATLAB SignalProcessing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

A Training Session on

Signal Processing Using MATLAB


in connection with the FDP on Digital Signal Processing for Engineering Applications @ CET

12th November 2014

Resource Person : Dr. A. Ranjith Ram


Assistant Professor, ECE Dept.
Govt. College of Engineering Kannur
Cell : 94476 37667
e-mail
il : [email protected]
ji h
@
il

Objectives : Part I Study of Signals, Systems and Transforms


Part II Design of FIR and IIR Digital Filters

PART I
Study of Signals, Systems and Transforms
60 Minutes (2:00 pm 3:00 pm)
pm)

12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Outline
Related Toolboxes & Built-in Functions
Signals in MATLAB Environment
Correlation & Convolution
Laplace Transform
Z-Transform
Circular Convolution and Parsevals Theorem
Systems Impulse Response & Frequency Response
Transfer Function and Pole-Zero Plots
12 November 2014

Signal Processing Using MATLAB

ECE Toolboxes of Interest

Signal Processing Toolbox

Fuzzy Logic Toolbox

Communications System Toolbox

Symbolic Math Toolbox

Computer Vision System Toolbox

Statistics Toolbox

DSP System Toolbox

Phased Array System Toolbox

Wavelet Toolbox

Neural Network Toolbox

Image Acquisition Toolbox

Optimization Toolbox

Image Processing Toolbox

RF Toolbox

Data Acquisition Toolbox

Control System Toolbox

12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Useful BuiltBuilt-in Functions

exp()
dirac()
sinc()
erf()
erfc()
gamma()
xcorr()
corr()
laplace()
ztrans()
fft()
dct()
hilbert()

12 November 2014

filter()
conv()
cconv()
impz
step()
freqz()
fir1()
firpm()
butter()
buttord()
cheby1()
cheby2()
sptool()

Signal Processing Using MATLAB

Signal Input to MALAB


Three methods of inputting signals to MATLAB :
generated by using MATLAB functions itself
from memory
read an audio file
read a speech file
from I/O devices
input from a sensor
input from an instrument
Input from a microphone
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Signal Input Examples


x = rand(1, 200);
[x fs] = wavread(aud.wav);
S = load(filemname);
import menu import a file

12 November 2014

Signal Processing Using MATLAB

Signal Output from MATLAB


Three methods of outputting signals from MATLAB :
directly showing in the command window
to memory
create and write an audio file from a MATLAB variable
write an array of samples from a MATLAB variable
to I/O devices
output to a DAC
output to a loudspeaker
output to a display
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Signal Output Examples


sprintf(The signal is %d', x) or display(x)
fid = fopen('exp.txt','w');
f
('
' ' ')
fprintf(fid,'%6.2f

%12.8f\n',y);

fclose(fid);
wavwrite(s,fs,au.wav) / sound(s,fs)
save(filename,
(
, var)
)
save menu

12 November 2014

Signal Processing Using MATLAB

Generating Basic Analog Signals


>>
>>
>>
>>

t = 1:20;
;
a = 0.2;
x = exp(a*t);
plot(x);
Define the time span
Define other parameters
Generate the signal and plot it

Generate & plot an analog signum function


Generate & plot an analog sinc function
Generate & plot an analog gaussian function
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

10

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Generating Basic Discrete Signals


>>
>>
>>
>>

n = 1:20;
;
a = 0.2;
x = exp(a*n);
stem(x);
Define sampling instants
Define other parameters
Generate the signal and plot it

Generate & plot a discrete signum function


Generate & plot a discrete sinc function
Generate & plot a discrete gaussian function
12 November 2014

Signal Processing Using MATLAB

11

Correlation of two Signals


Cross Correlation Function : c = xcorr(x,y)
where x and y are having a length M (M > 1) and c is a 2*M 1 sequence
If x and y are of different length, the shortest one is zero-padded
c will be a row vector if x is a row vector, and c will be a column vector if
x is a column vector
>> x = 1:5
>> y = 9:-1:5
>> c = xcorr(x,y)
yields c = [5.0 16.0 34.0 60.0 95.0 100.0 94.0 76.0 45.0]
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

12

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Autocorrelation function
ACF is given by c = xcorr(x), where x is a vector
when x is an M x N matrix, is a large matrix with 2*M 1 rows whose
N^2 columns contain the cross-correlation sequences for all combinations
of the columns of x
The zeroth lag of the output correlation is in the middle of the sequence, at
element or row M
>> x = 1:5
>> c = xcorr(x)
yields c = [5.0 14.0 26.0 40.0 55.0 40.0 26.0 14.0 5.0 ]

12 November 2014

Signal Processing Using MATLAB

13

Linear Correlation
Linear or Rank Correlation : corr(x)
Returns a P x P matrix containing the pairwise linear correlation
coefficient between each pair of columns in the N x P matrix x
rho = corr(x,y) returns a P1 x P2 matrix containing the pairwise
correlation coefficient between each pair of columns in the N x P1 and
N x P2 matrices x and y
[rho,
[rho pval] = corr(x,y)
corr(x y) also returns a pval,
pval a matrix of pp
values for testing the hypothesis of no correlation against the alternative
that there is a non-zero correlation
Each element of pval is the p-value for the corresponding element of rho
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

14

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Linear Correlation (Contd)


Contd)
>> x = [1 -1 0 2;2 1 -2 0;1 2 0 1;2 0 1 0];
>> y=[1 0 2 1;0 -1 -2 1;-2 0 -1 1;-1 0 -2 -1];
>> [rho pval]

= corr(x,y)

gives
>> rho =
0

>> pval =

-0.5774 -0.7625 -0.5774

1.0000 0.4226 0.2375 0.4226

-0.8000 -0.2582 -0.6138 0.2582

0.2000 0.7418 0.3862 0.7418

-0.3078 0.9272 0.1749 -0.6623

0.6922 0.0728 0.8251 0.3377

0.4045 0.5222 0.9656 0.5222

0.5955 0.4778 0.0344 0.4778

12 November 2014

Signal Processing Using MATLAB

15

Convolution of two Signals


Convolution and polynomial multiplication : conv(x,y)
c = conv(x, y) convolves vectors x and y
c is a vector of length length(x) + length(y) 1
If x and y are vectors of polynomial coefficients, convolving them is
equivalent to multiplying the two polynomials
>> x = 1:5
>> y = 9:-1:5
>> c = conv(x,y)
yields c = [9 26 50 80 115 96 74 50 25]
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

16

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Transforms Laplace & Z


The Laplace Transform gives the representation of a continuous time
signal/system in s domain
The Z Transform gives the representation of a discrete signal/system in
z domain
One can say that t is transformed to s and n is transformed to z. Both s
& z are complex variables
Both
B h needs
d no numerical
i l computation,
i but
b an integration/summation
i
i /
i off a
signal/sequence
To cope with such a situation, MATLAB supports another type of
variable the symbolic variable
12 November 2014

Signal Processing Using MATLAB

17

The Symbolic Math Toolbox


In MATLAB, symbolic variables are also possible, which do not possess
any values,
l
but
b only
l symbolic
b li in
i nature
Symbolic Math Toolbox provides functions for solving and manipulating
symbolic math expressions and performing variable-precision arithmetic
One can analytically perform differentiation, integration, simplification,
transforms, and equation solving. Also, one can generate code for
MATLAB and Simulink from symbolic math expressions.
expressions
>> syms x
>> diff(sin(x^2))
>> ans = 2*x*cos(x^2)
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

18

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Laplace Transform
Laplace Transform : laplace(); Inverse LT : ilaplace()
>> syms t n
>> f = t^2;
>> disp(f);
>> F = laplace(f);
>> disp(F);
>> disp('Inverse Laplace transform is')
>> f1 = ilaplace(F);
>> disp(f1);
12 November 2014

Signal Processing Using MATLAB

19

Z-Transform
Z-Transform : ztrans(); Inverse ZT : iztrans()
>> x = 0.5^n;
>> disp(x);
>> X = ztrans(x);
>> disp(X);
>> disp('Inverse Z-Transform is');
>> x1 = iztrans(X);
>> disp(x1);

12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

20

10

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Discrete Fourier Transform


Discrete (Fast) Fourier Transform : fft(); Inverse DFT : ifft()
fft(x) is the discrete Fourier transform (DFT) of vector x
For matrices, the fft operation is applied to each column
fft(x, N) is the N-point FFT, padded with zeros if x has less than N
points and truncated if it has more.
>> n = 1:50;
>> x = cos((pi/8)*n) + cos((pi/20)*n); plot(x)
>> X = fft(x); stem(abs(X))
>> xcap = ifft(X); plot(xcap)
12 November 2014

Signal Processing Using MATLAB

21

Circular Convolution
Modulo-N circular convolution : cconv()
c = cconv(x, y, N) circularly convolves vectors x and y.
c is of length N. If omitted, N defaults to length(x) + length(y) 1
When N = length(x) + length(y) 1, the circular convolution is
equivalent to the linear convolution computed by conv()
>> a = [1

2 -1

1];

>> b = [1

1];

>> c = cconv(a,b,11)
>> cref = conv(a,b)
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

22

11

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Parsevals Theorem
The energy of a signal/sequence is a constant, irrespective of the domain
Steps:
Find out the energy in time domain
Transform the signal to frequency domain
Find out the energy of the spectra
Both values should be the same
>>
>>
>>
>>

x
e
X
E

=
=
=
=

[2 1 -2
2 3 4 -3
3 1 2];
2]
sum(x.^2);
% Energy from samples
fft(x);
sum(abs(X).^2)/8; % Energy from spectra

12 November 2014

Signal Processing Using MATLAB

23

Systems Impulse Response


Discrete Impulse Response : impz(b,a)
b : numerator vector; a : denominator vector
>>
>>
>>
>>
>>
>>
>>
>>

b=[0.0013 0.0064 0.0128 0.0128 0.0064 0.0013];


a=[1 -2.9754 3.8060 -2.5453 0.8811 -0.1254];
h = impz(b,a); % Impulse Response, h(n)
plot(h);
grid on;
title('Impulse Response');
ylabel('Response');
xlabel('n');

12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

24

12

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Systems Transfer Function


>> sys = tf(b,a) creates a continuous-time transfer function SYS
with
ith numerator
t b andd ddenominator
i t a.
>> sys = tf(b,a,TS) creates a discrete-time transfer function with
sampling time TS (set TS = -1 if the sampling time is undetermined).
>> b = [0.0013 0.0064 0.0128 0.0128 0.0064 0.0013];
>> a = [1 -2.9754 3.8060 -2.5453 0.8811 -0.1254];
>> sys = tf(b,a)
tf(b a)
sys =
0.0013 s^5 + 0.0064 s^4 + 0.0128 s^3 + 0.0128 s^2 + 0.0064 s + 0.0013
s^5 - 2.975 s^4 + 3.806 s^3 - 2.545 s^2 + 0.8811 s - 0.1254
12 November 2014

Signal Processing Using MATLAB

25

Systems Frequency Response


[H,W] = freqz(b,a,N) returns the N-point complex frequency
response vector
t H andd th
the N-point
N i t frequency
f
vector
t W in
i radians/sample
di /
l
of the digital system
>> b = [0.0013 0.0064 0.0128 0.0128 0.0064 0.0013];
>> a = [1 -2.9754 3.8060 -2.5453 0.8811 -0.1254];
>> freqz(b,a)

12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

26

13

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Alternate Method
>> b = [0.0013 0.0064 0.0128 0.0128 0.0064 0.0013];
>> a = [1 -2.9754 3.8060 -2.5453 0.8811 -0.1254];
>> h = impz(b,a); N = 1024;
>> sh = fft(h,N);
>> f = [0:N/2-1]*2/N; % Frequency Normalization
>> subplot(2 1 1)
>> plot(f,20*log10(abs(sh(1:N/2))));
plot(f 20*log10(abs(sh(1:N/2))));
>> p=unwrap(angle(sh))*180/pi;
>> subplot(2 1 2)
>> plot(f,p(1:N/2));
12 November 2014

Signal Processing Using MATLAB

27

TF & Frequency Response


Transfer Function H(z), in 3-D plot
Amplitude Response |H()|, in 1-D plot

12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

28

14

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Pole--Zero Plot
Pole
Pole-zero plot of a digital system : zplane()
>> b = [0.0013 0.0064 0.0128 0.0128 0.0064 0.0013];
>> a = [1 -2.9754 3.8060 -2.5453 0.8811 -0.1254];
>> zplane(b,a);
>> title('Pole-Zero Plot');
>> grid on;

12 November 2014

Signal Processing Using MATLAB

29

Part I : Summary
Familiarized with signals in MATLAB environment
Computed correlation & convolution
Learnt how to find the Laplace transform using the Symbolic toolbox
Studied about Z-Transform
Studied circular convolution and verified Parsevals theorem
Studied
St di d about
b t systems
t
and
d plotted
l tt d the
th impulse
i
l response
Obtained the frequency response of discrete time systems
Studied about transfer function and pole-zero plots
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

30

15

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

PART II
Design of FIR and IIR digital Filters
60 Minutes (3:15 pm 4:15 pm)
pm)

12 November 2014

Signal Processing Using MATLAB

31

Outline
FIR transfer function
Design of FIR filters using windows
Optimal design of FIR filters
IIR transfer function
Design of Butterworth IIR filters
Design of Chebyshev Type-I and Type-II filters
1-D Filtering Process
Introduction to audio and image processing
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

32

16

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

FIR Filters
FIR filters are linear phase discrete time systems
Si
Since th
the FIR systems
t
are hhaving
i bns
only
l (a
( ns
are zeros exceptt a0),
) th
the
task is in finding bns the numerator coefficients
FIR filters can be designed classically by windowing technique
Here the impulse response of the ideal filter is windowed to get a finite
duration sequence
If hd(n) is the impulse response of the desired (ideal) filter, the designed
(actual) impulse response is given by h(n) = hd(n) . w(n) where w(n) is
the window function
The transition band of the filter directly determines its order
The selection of the window is based on the minimum stop band
attenuation required
12 November 2014

Signal Processing Using MATLAB

33

FIR Filter Design


b = fir1(N,wn) designs an N'th order lowpass FIR digital filter
It returns the filter coefficients in length N+1 vector b
The cut-off frequency wn must be between 0 < Wn < 1.0, with 1.0
corresponding to half the sample rate
The filter b is real and has linear phase.

b = fir1(N,wn,'high') designs an N'th order high-pass filter

If wn is a two-element vector, wn = [w1 w2], fir1 returns an order N


band-pass filter with pass-band w1 < w < w2
One can also specify b = fir1(N,wn,'bandpass')
If wn = [w1 w2], b = fir1(N,wn,'stop') will design a bandstop filter
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

34

17

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

FIR Filter Design (Contd)


Contd)
b = fir1(N,wn,win) designs an N-th order FIR filter using the
N+1 length window vector of 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
Kaiser and Chebwin can be specified with an optional trailing argument
F
For example,
l b = fir1(N,Wn,kaiser(N+1,4))
fir1(N Wn kaiser(N+1 4)) uses a Kaiser
K i
window with beta = 4
b = fir1(N,wn,'high',chebwin(N+1,r)) uses a Chebyshev
window with r decibels of relative side-lobe attenuation
12 November 2014

Signal Processing Using MATLAB

35

FIR Filter Design (Contd)


Contd)
For filters with a gain other than zero at fs/2, e.g., high-pass and bandstop filters, N must be even. Otherwise, N will be incremented by one
In this case the window length should be specified as N+2
By default, the filter is scaled so the center of the first pass band has
magnitude exactly one after windowing
Use a trailing 'noscale' argument to prevent this scaling, e.g.,
b = fir1(N,Wn,
fir1(N Wn 'noscale')
noscale )
b = fir1(N,Wn,'high','noscale')
b = fir1(N,Wn,wind,'noscale')
We can also specify the scaling explicitly, e.g. fir1(N,wn,'scale')
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

36

18

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

FIR Filter Design example


% Design a 48th-order FIR band-pass filter with
% pass-band 0.35 <= w <= 0.65
b = fir1(48,[0.35 0.65]);
freqz(b,1,512)

12 November 2014

Signal Processing Using MATLAB

37

Optimal Design of FIR Filters


Parks-McClellan optimal equi-ripple FIR filter design : firpm()
b = firpm(N,F,A) returns a length N+1 linear phase (real,
symmetric coefficients) FIR filter which has the best approximation to the
desired frequency response described by F and A in the mini-max sense
F is a vector of frequency band edges in pairs, in ascending order between
0 and 1
1 corresponds
p
to the Nyquist
yq
frequency
q
y or half the sampling
p g frequency
q
y
At least one frequency band must have a non-zero width
A is a real vector, the same size as F which specifies the desired
amplitude of the frequency response of the resultant filter B
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

38

19

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Optimal Design of FIR Filters


The desired response is the line connecting the points (F(k), A(k)) and
(F(k 1) A(k+1))
(F(k+1),
A(k 1)) ffor odd
dd k;
k firpm
fi
treats the
h bbands
d bbetween F(k
F(k+1)
1) andd
F(k+2) for odd k as transition bands or don't care regions
Thus the desired amplitude is piecewise linear with transition bands
The maximum error is minimized.

For filters with a gain other than zero at fs/2, e.g., high-pass and bandstopp filters,, N must be even. Otherwise,, N will be incremented by
y one

Alternatively, you can use a trailing 'h' flag to design a type 4 linear phase
filter and avoid incrementing N.

b = firpm(N,F,A,W) uses the weights in W to weight the error

12 November 2014

Signal Processing Using MATLAB

39

Optimal Design of FIR Filters


>> % Example of a length 31 low-pass filter
>> h=firpm(30,[0 .1 .2 .5]*2,[1 1 0 0]);
>> freqz(h,1,512)

gives the frequency response :

12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

40

20

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

IIR Filters
IIR systems are not having any linear phase property
Occur as having recursive structures
Since the IIR systems are having both ans and bns, the task is in finding
both of the numerator and denominator coefficients
Classical analog filter design theory could be used for designing IIR
filters
Hence there are many approximations Butterworth, Chebyshev,
Elliptical, etc.
Butterworth approximation is having a smooth passband as well as a
stopband
But a Chebyshev approximation is either the passband having a ripple
(Type-I) or the stopband having a ripple (Type-II)
12 November 2014

Signal Processing Using MATLAB

41

IIR Filters Transfer Function


Zero-pole-gain to second-order sections model conversion : zp2sos()
[sos, g] = zp2sos(z,p,k) finds a matrix sos in second-order
sections form and a gain g which represent the same system H(z) as the
one with zeros in vector z, poles in vector p and gain in scalar k
The poles and zeros must be in complex conjugate pairs

sos is an L by 6 matrix with the following structure :


[b01 b11 b21 1 a11 a21
b02 b12 b22 1 a12 a22
...
b0L b1L b2L 1 a1L a2L]

12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

42

21

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

IIR Transfer Function (Contd)


Contd)
Each row of the sos matrix describes a 2nd order transfer function H(z) :
(b0k + b1k z^-1 + b2k z^-2)
(1 + a1k z^-1 + a2k z^-2) , where k is the row index.
g is a scalar which accounts for the overall gain of the system
If g is not specified, the gain is embedded in the first section
The second order structure thus describes the system H(z) as:
H( ) = g H1(z)
H(z)
H1( ) H2(
H2(z)) ... HL(
HL(z))
Embedding the gain in the first section when scaling a direct-form II
structure is not recommended and may result in erratic scaling. To avoid
embedding the gain, use zp2sos with two outputs.
12 November 2014

Signal Processing Using MATLAB

43

IIR Transfer Function (Contd)


Contd)
Zero-pole to transfer function conversion : zp2tf()
[b,a] = zp2tf(z,p,k) forms the transfer function b(s)/a(s),
given a set of zero locations in vector z, a set of pole locations in vector
p, and a gain in scalar k
Vectors b and a are returned with numerator and denominator
coefficients in descending powers of s.
Zero-pole
p to state-space
p
conversion : zp2ss()
p
[A,B,C,D] = zp2ss(z,p,k) calculates a state-space model
x = Ax + Bu
y = Cx + Du, the A,B,C,D matrices are returned in block diagonal form
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

44

22

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

IIR Filter Design Butterworth


Butterworth digital and analog filter design : butter()
[b, a] = butter(N,wn) designs an Nth order lowpass Butterworth
filter and returns the filter coefficients in length N+1 vectors b and a
The coefficients are listed in descending powers of z. The cutoff
frequency Wn must be 0 < wn < 1, with 1 corresponding to half the fs
If wn is a two-element vector, wn = [w1 w2], butter returns an order
2N bandpass filter with passband w1 < w < w2
[b,a] = butter(N,Wn,'high') designs a highpass filter; [b,a]
= butter(N,Wn,'low') designs a lowpass filter and [b,a] =
butter(N,Wn,'stop') is a bandstop filter if wn = [w1 w2]
12 November 2014

Signal Processing Using MATLAB

IIR Filter Design

45

butter & buttord

When used as [z,p,k] = butter(), the zeros and poles are


returned
t
d in
i length
l th N column
l
vectors
t z andd p, andd th
the gain
i in
i scalar
l k
When used with four left-hand arguments, as in [A,B,C,D] =
butter(...), state-space matrices are returned
butter(N,wn,'high','s') designs an analog HP Butterworth
filter. In this case, wn is in [rad/s] and it can be > 1
Butterworth filter order selection : buttord()
[N, wn] = buttord(wp,ws,rp,rs) returns the order N of the
lowest order digital Butterworth filter which has a passband ripple of no
more than rp dB and a stopband attenuation of at least rs dB
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

46

23

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

IIR Filter Design Buttord


wp and ws are the passband and stopband edge frequencies, normalized
from 0 to 1 (where 1 corresponds to pi radians/sample). e.g., Lowpass: wp
= 0.1, ws = 0.2 & Bandstop: wp = [0.1 .8], ws = [0.2 0.7]
buttord also returns wn, the Butterworth natural frequency (or, the 3 dB
frequency) to use with butter to achieve the specifications
[N, wn] = buttord(wp, ws, rp, rs, 's') does the
computation for an analog filter,
filter in which case wp and ws are in
radians/second
When rp is chosen as 3 dB, the wn in butter is equal to wp in
buttord
12 November 2014

Signal Processing Using MATLAB

47

IIR Filter Design Butterworth Example


>> % For data sampled at 1000 Hz, design a lowpass
>> % filter
fil
with
i h less
l
than
h
3 dB
d of
f ripple
i l in
i the
h
>> % passband, defined from 0 to 40 Hz, and at least
>> % 60 dB of attenuation in the stopband, defined
>> % from 150 Hz to the Nyquist frequency (500 Hz)
>> Wp = 40/500;
>> Ws = 150/500;
>> [n,Wn] = buttord(Wp,Ws,3,60); % Gives the order
>> [b,a] = butter(n,Wn); % Butterworth filter design
>> freqz(b,a,512,1000);% Plots the frequency response
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

48

24

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

IIR Filter Design Chebyshev


[N,wp] = cheb1ord(wp,ws,rp,rs) returns the order N of the
lowest order digital Chebyshev Type-I filter which has a passband ripple
of no more than rp dB and a stopband attenuation of at least rs dB
[b,a] = cheby1(N,r,wp) designs an Nth order lowpass digital
Chebyshev filter with r decibels of peak-to-peak ripple in the passband
[N,ws] = cheb2ord(wp,ws,rp,rs) returns the order N of the
lowest order digital Chebyshev Type-II
Type II filter which has a passband ripple
of no more than rp dB and a stopband attenuation of at least s dB
[b,a] = cheby2(N,r,wst) : Nth order LP digital Chebyshev filter
with the stopband ripple r dB down and stopband-edge frequency wst
12 November 2014

Signal Processing Using MATLAB

49

1-D Filtering Process


One-dimensional digital filtering : filter()
y = filter(b,a,x) filters the data in vector x with the filter
described by vectors a and b to create the filtered data y
The filter is a Direct Form II Transposed implementation of the standard
difference equation, a(1)y(n) = b(1)x(n) + b(2)x(n-1) + ... + (nb+1)x(nnb) a(2)y(n-1) ... a(na+1)y(n-na)
If a(1) is not equal to 11, filter normalizes the filter coefficients by a(1)
a(1).
filter() always operates along the first non-singleton dimension,
namely dimension 1 for column vectors and non-trivial matrices, and
dimension 2 for row vectors
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

50

25

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

1-D Filtering Process Example


>> wp=0.3; ws=0.4; rp=1; rs=10; % Specifications
>> [n,wn] = buttord(wp,ws,rp,rs); % Order selection
>> [b,a] = butter(n,wn); % IIR filter design
>> figure(1); freqz(b,a);
>> title('The frequency response of LP IIR filter');
>> t=1:100;
>> sig1 = sin(0
sin(0.1*pi*t);
1*pi*t); % Signal inside the PB
>> sig2 = sin(0.6*pi*t); % Signal inside the SB
>> sig = sig1 + sig2;
>> filsig = filter(b,a,sig); figure(2); plot(filsig)
12 November 2014

Signal Processing Using MATLAB

51

Introduction to Audio Processing


>> [s fs] = wavread(audio.wav)
>> [s fs] = audioread('ethrayo.mp4');
Check for stereo
Study about the sampling rate
Study about simple alterations
Output
p the sound :

>> sound(s,fs)
( , )
>> sound(s,2*fs)

Write the audio file : >> wavwrite(y, fs,output.wav)


>> audiowrite(output.mp4,y,fs)
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

52

26

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Introduction to Image Processing


>> x = imread(image.jpg)
Check for colours
Study about monochrome conversion
Study about thresholding / binarization
Display the image :
>> imshow(y)
(y)
>> image(y)
Write the image file :
>> imwrite(y,output.jpg)
12 November 2014

Signal Processing Using MATLAB

53

Part II : Summary
Studied about FIR transfer function
Designed FIR filters using windows
Performed optimal design of FIR filters
Studied about IIR transfer function
Designed Butterworth IIR filters
Designed
D i d Chebyshev
Ch b h Type-I
T
I andd Type-II
T
II fil
filters
Demonstrated the 1-D filtering process
Studied how to deal with audio files and image files using MATLAB
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

54

27

Signal Processing using MATLAB : A session on FDP @ CET Cheemeni

12/11/2014

Thanks

[email protected]
12 November 2014

Signal Processing Using MATLAB

Dr. A. Ranjith Ram [email protected]

55

28

You might also like