You are on page 1of 156

D.S.

ALDAR
K.B. P. College of Engineering & Poly, Satara
What Is Signal Processing Toolbox
• Signal Processing Toolbox is a collection of tools
based on the MATLAB® numeric computing
environment.
• The toolbox supports a wide range of signal
processing operations, from waveform
generation to filter design and implementation,
parametric modeling, and spectral analysis.
• The toolbox provides two categories of tools,
command-line functions and graphical user
interfaces:
What Is Signal Processing Toolbox
Command-line functions are available in the
following categories:
• Discrete-time filter design, analysis, and implementation
• Analog filter design, analysis, and implementation
• Linear system transformations
• Windows
• Spectral analysis and cepstral analysis
• Transforms
• Statistical signal processing
• Parametric modeling
• Linear prediction
• Multirate signal processing
• Waveform generation
What Is Signal Processing Toolbox
A suite of interactive graphical user interfaces
are available for
• Filter design and analysis
• Window design and analysis
• Signal plotting and analysis, spectral analysis, and
filtering
Interactive Tools
The power of Signal Processing Toolbox is greatly
enhanced by its easy-to-use interactive tools.
• The Filter Design and Analysis Tool (fdatool) provides a more
comprehensive collection of features for addressing filter design.
• The Filter Visualization Tool (fvtool) provides a graphical environment
for viewing, annotating, and printing filter response plots.
• The Window Design and Analysis Tool (wintool) provides an
environment for designing and comparing spectral windows.
• The Window Visualization Tool (wvtool) provides a graphical
environment for viewing, annotating, and printing window plots.
• The Signal Processing Tool (sptool) provides a rich graphical
environment for signal viewing, filter design, and spectral analysis.
Waveform Generation: Time Vectors
and Sinusoids
Basic Sequences:
1

0.9

• UNIT IMPULSE 0.8

0.7

n=-5:5; 0.6

0.5

x=[ 0 0 0 0 0 1 0 0 0 0 0]; 0.4

0.3

plot(n,x); 0.2

0.1

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
1

stem(n,x); 0.9

0.8

0.7

x=(n==0);
0.6

0.5

0.4

stem(n,x); 0.3

0.2

0.1

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
Waveform Generation: Time Vectors
and Sinusoids
• UNIT IMPULSE
n=-5:5;
n=-15:15; 1

0.9

x=[zeros(1,15) 1 zeros(1,15)]; 0.8

stem(n,x) 0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
-15 -10 -5 0 5 10 15
Waveform Generation: Time Vectors
and Sinusoids 1

• UNIT STEP 0.9

0.8
n=-15:15; 0.7

x=[zeros(1,15) ones(1,16)]; 0.6

stem(n,x) 0.5

0.4

0.3

% entering the starting point and the limits 0.2


n0 = -1; n1 = -3; n2 = 3; 0.1

n = [n1: n2]; % generating the index n 0


-15 -10 -5 0 5 10 15
% the following will generate the step signal desired
x=[(n- n0)>=0]; n=-15:15;
stem(n,x);
x=(n>=0);
stem(n,x)
Waveform Generation: Time Vectors
and Sinusoids 15

• RAMP 10

n=-15:15; 5

x=n; 0

stem(n,x) -5

-10

-15
-15 -10 -5 0 5 10 15
15

• UNIT RAMP 10

n=-15:15;
x=n*u(n); 5

stem(n,x)
0
-15 -10 -5 0 5 10 15
Waveform Generation: Time Vectors
and Sinusoids 35

EXPONENTIAL DECAYING
• EXPONENTIAL
30

25

n=-5:5; 20

x=(0.5).^n; 15

10

stem(n,x) 5

0
-5 -4 -3 -2 -1 0 1 2 3 4 5

35

30
EXPONENTIAL GROWING
25

n=-5:5; 20

x=(2).^n; 15

stem(n,x)
10

0
-5 -4 -3 -2 -1 0 1 2 3 4 5
1

0.8

• SINUSOIDAL
0.6

0.4

0.2

n=-5:5; 0

-0.2

x=sin(0.4*pi*n); -0.4

-0.6

stem(n,x) -0.8

-1
-5 -4 -3 -2 -1 0 1 2 3 4 5
1

0.8

0.6

0.4

0.2

x=sin(0.4*pi*n); -0.2

-0.4

stem(n,x) -0.6

-0.8

-1
-5 -4 -3 -2 -1 0 1 2 3 4 5
Common Sequences: Unit Impulse, Unit Step, and Unit
Ramp
2

t = (0:0.001:1)'; 1.8

y = [1; zeros(99,1)]; % impulse 1.6


y = ones(100,1); % step (filter 1.4
assumes 0 initial cond.)
1.2
y = t; % ramp
1
y = t.^2;
0.8
y = square(4*t);
0.6
plot(t,y);
0.4
axis([0 1 0 2])
0.2

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Common Sequences
t = (0:0.001:1)';
y = sin(2*pi*50*t) + 2*sin(2*pi*120*t);
randn('state',0); 4

yn = y + 0.5*randn(size(t));
3

plot(t(1:50),yn(1:50)) 2

-1

-2

-3
0 0.005 0.01 0.015 0.02 0.025 0.03 0.035 0.04 0.045 0.05
Common Sequences
Multichannel Signals
z = [t t.^2 square(4*t)];

>> whos
Name Size Bytes Class Attributes

t 1001x1 8008 double


y 1001x1 8008 double
yn 1001x1 8008 double
z 1001x3 24024 double
Common Periodic Waveforms
• sawtooth
1

• square 0.8

0.6
To generate 1.5 s of a 50 Hz sawtooth wave with a 0.4

sample rate of 10 kHz and plot 0.2 s of the generated 0.2

0
waveform, use -0.2

fs = 10000; -0.4

-0.6
t = 0:1/fs:1.5; -0.8

x = sawtooth(2*pi*50*t); -1
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2

plot(t,x), axis([0 0.2 -1 1])


1.5

0.5

x = square(2*pi*50*t); 0

plot(t,x), axis([0 0.2 -1.5 1.5]) -0.5

-1

-1.5
0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 0.2
Common Aperiodic Waveforms
1

0.8

• gauspuls 0.6

0.4

• chirp
0.2

-0.2

t = 0:1/1000:2; -0.4

y = chirp(t,0,1,150); -0.6

-0.8
plot(t,y)
-1
% To plot the spectrogram, use 0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

figure(1) 500

spectrogram(y,256,250,256,1000,'yaxis') 450

400

350
Frequency (Hz)

300

250

200

150

100

50

0
0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8
Time
The pulstran Function
T = 0:1/50E3:10E-3;
D = [0:1/1E3:10E-3;0.8.^(0:10)]';
1
Y = pulstran(T,D,'gauspuls',10E3,0.5);
0.8
plot(T,Y);
0.6
Sound(Y); % soundsc(Y);
0.4

0.2

-0.2

-0.4

-0.6

-0.8
0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01
Multiple transformations
20 20

Example: 10 10

g(t)= 0 t<-2 0 0
g(t)= -4-2t -2<t<0 -10 -10
g(t)= -4+3t 0<t<4 0 5 10 15 20 0 5 10 15 20
g(t)= 16-2t 0<t<8
g(t)= 0 t>0 20 20
then plot transformed function 10 10
3g(t+1), 0.5g(3t), & -2g((t-1)/2).
0 0

-10 -10

function y=g(t) 0 5 10 15 20 0 5 10 15 20

y1=-4-2*t;y2=-4+3*t; y3=16-2*t;
y = y1.*(-2<t & t<=0) + y2.*(0<t & t<=4) + y3.*(4<t & t <=8);

Contn..
Multiple transformations
clc; 20 20

clear; 10 10

0 0
tmin=-4;tmax=20; -10 -10

dt=0.1; 0 5 10 15 20 0 5 10 15 20

t=tmin:dt:tmax; 20 20

g0=g(t); 10 10

0 0
g1=3*g(t+1);
-10 -10

g2=g(3*t)/2; 0 5 10 15 20 0 5 10 15 20

g3=-2*g((t-1)/2);
gmax=max([max(g0),max(g1),max(g2),max(g3)]);
gmin=min([min(g0),min(g1), min(g2),min(g3)]);
subplot(221);p=plot(t,g0);set(p,'LineWidth',2);axis([tmin,tmax,gmin,gmax]);grid;
subplot(222);p=plot(t,g1);set(p,'LineWidth',2);axis([tmin,tmax,gmin,gmax]);grid;
subplot(223);p=plot(t,g2);set(p,'LineWidth',2);axis([tmin,tmax,gmin,gmax]);grid;
subplot(224);p=plot(t,g3);set(p,'LineWidth',2);axis([tmin,tmax,gmin,gmax]);grid;
Built in functions
clc;
clear;
close all;
t=-20:1/20:20;
x1=chirp(t,1/20.20,1/3);subplot(421);p=plot(t,x1);axis([-20 20 -1.5 1.5]);
x2=diric(t,5);subplot(422);p=plot(t,x2);axis([-20 20 -1.5 1.5]);
x3=sawtooth(t);subplot(423);p=plot(t,x3);axis([-20 20 -1.5 1.5]);
x4=square(t);subplot(424);p=plot(t,x4);axis([-20 20 -1.5 1.5]);
x5=rectpuls(t/10);subplot(425);p=plot(t,x5);axis([-20 20 -1.5 1.5]);
x6=tripuls(t/10);subplot(426);p=plot(t,x6);axis([-20 20 -1.5 1.5]);
x7=sinc(t/2);subplot(427);p=plot(t,x7);axis([-20 20 -1.5 1.5]);
x8=sign(t/2);subplot(428);p=plot(t,x8);axis([-20 20 -1.5 1.5]);
Built in functions

1 1
0 0
-1 -1
-20 -10 0 10 20 -20 -10 0 10 20

1 1
0 0
-1 -1
-20 -10 0 10 20 -20 -10 0 10 20

1 1
0 0
-1 -1
-20 -10 0 10 20 -20 -10 0 10 20

1 1
0 0
-1 -1
-20 -10 0 10 20 -20 -10 0 10 20
Product of Even & Odd functions
• The products of even functions are even
• The products of odd functions are even
• Mixed products of even and odd functions are odd.
Even*Odd odd*Odd

1 1
x24=x2.*x4;
figure; subplot(221);plot(t,x24);axis([-20 20 -1.5 0 0

1.5]); -1 -1
title('Even*Odd');
-20 -10 0 10 20 -20 -10 0 10 20
x34=x3.*x4;subplot(222);plot(t,x34);axis([-20 20 -
Even*Even Odd*Even
1.5 1.5]);
title('odd*Odd'); 1 1

x26=x2.*x6;subplot(223);plot(t,x26);axis([-20 20 - 0 0
1.5 1.5]);
title('Even*Even'); -1 -1

x37=x3.*x7;subplot(224);plot(t,x37);axis([-20 20 - -20 -10 0 10 20 -20 -10 0 10 20

1.5 1.5]);
title('Odd*Even');
Even and odd
clc
clear;
t=-20:1/20:20;
x1=chirp(t,1/20,20,1/3);subplot(311);plot(t,x1);title('A chirp signal');
x1e=(x1+x1(end:-1:1))/2; x1o=(x1-x1(end:-1:1))/2;
subplot(312);plot(t,x1e);title('Even part of chirp');
subplot(313);plot(t,x1o);title('Odd part of chirp'); A chirp signal
1

-1
-20 -15 -10 -5 0 5 10 15 20
Even part of chirp
1

-1
-20 -15 -10 -5 0 5 10 15 20
Odd part of chirp
1

-1
-20 -15 -10 -5 0 5 10 15 20
Operation on sequence
Example: g[n]=10e-n/4 sin(3πn/16)u[n]
Then graph the function g[2n] and g[n/3]

function y=g(n) function y=ustep(n)


ss= find(round(n) ~=n) y=n>=0;
n(ss)=NaN;
y= 10*exp(-n/4).*sin(3*pi*n/16);
y=y.*ustep(n)
Operation on sequence
n= -5:48;
g0=g(n);
g1=g(2*n);
g2=g(n/3);
subplot(311);p=stem(n,g0,'filled');set(p,'LineWidth',2,'MarkerSize',4);
subplot(312);p=stem(n,g1,'filled');set(p,'LineWidth',2,'MarkerSize',4);
subplot(313);p=stem(n,g2,'filled');set(p,'LineWidth',2,'MarkerSize',4);
10

-5
-10 0 10 20 30 40 50
10

-5
-10 0 10 20 30 40 50
10

-5
-10 0 10 20 30 40 50
ENERGY AND POWER
Using MATLAB find the signal energy or power

3. A periodic signal of fundamental period 10, described over one


period by x(t)=-3t, -5<t<5.

function y=ramp(t)
function y=tri(t) y=t.*(t>=0);
y=ramp(t+1)-2*ramp(t) +ramp(t-1);
ENERGY AND POWER
Ex.2.
Ex.1. clc;
clc; clear;
clear; n=-100:100;
x=exp(-abs(n/10)).*sin(2*pi*n/4);
t=-7:0.1:13; xsq=x.^2;
x=tri((t-3)/10);xsq=x.^2; Ex=sum(abs(xsq));
Ex=trapz(t,xsq); disp(['Energy Ex=',num2str(Ex)]);
disp(['Energy Ex=',num2str(Ex)]);
Result:
Result: Energy Ex=4.9668
Energy Ex=6.667
ENERGY AND POWER
Ex.3.
clc; N=4;
clear; n=0:3;
T0=10; x=4*cos(2*pi*n/4);
t=-5:0.1:5; Px=sum(abs(x.^2))/N
x=-3*t; disp(['POWER Px=',num2str(Px)]);
xsq=x.^2;
Px=trapz(t,xsq)/T0;
disp(['POWER Px=',num2str(Px)]);
Result:
POWER Px=8
Result:
POWER Px=75.015
Causality
1

clc; 0.9

clear 0.8

0.7
n=-5:5; 0.6

x=[zeros(1,3) ones(1,8)]; 0.5

stem(n,x); 0.4

if x(n<0)==0 0.3

0.2
disp('causa1'); 0.1

else 0
-5 -4 -3 -2 -1 0 1 2 3 4 5

disp('noncausal');
noncausal
end;
Causality
1
clc; 0.9

clear 0.8

n=-5:5; 0.7

0.6
x=(0.5).^n.*[zeros(1,5) ones(1,6)];
0.5

stem(n,x); 0.4

if x(n<0)==0 0.3

disp('causa1'); 0.2

0.1
else 0
-5 -4 -3 -2 -1 0 1 2 3 4 5
disp('noncausal');
end; causa1
Stability
clc;
clear;
n=0:100;
h=exp(-abs(n/10)).*sin(2*pi*n/4);
st=sum(abs(h));
disp(['st =',num2str(st)]);
if h<inf
disp('system stable');
else
disp('system unstable');
end;

st =4.9914
system stable
Convolution
• CONV: Convolution and polynomial multiplication.
C = CONV(A, B) convolves vectors A and B. The
resulting vector is length LENGTH(A)+LENGTH(B)-1.
• DECONV : Deconvolution and polynomial division.
[Q,R] = DECONV(B,A) deconvolves vector A out of
vector B. The result is returned in vector Q and the
remainder in vector R such that B = conv(A,Q) + R
Convolution
>> x=[2 4 5 2] 80

x= 70

60
2 4 5 2
50

>> h=[5 7 5 4 3 7 2 8] 40

h= 30

5 7 5 4 3 7 2 8 20

10
>> y=conv(x,h)
0
1 2 3 4 5 6 7 8 9 10 11
y=
10 34 63 73 61 56 55 65 56 44 16
>> z=deconv(y,h)
z=
2.0000 4.0000 5.0000 2.0000
Convolution
function [y ny]=convolution(x,h,nx,nh)
nyl=nx(1)+nh(1);nyh=nx(length(x))+nh(length(h));
ny=[nyl:nyh];
y=conv(x,h)
>> nx=-2:3
>> x=[7 8 5 7 5 4]
>> nh=[-1:5]
>> h=[2 5 6 7 9 4 7]
>> [y ny]=convolution(x,h,nx,nh)
y=
14 51 92 136 194 210 225 198 136 105 51 28
ny =
-3 -2 -1 0 1 2 3 4 5 6 7 8
Correlation
• xcorr: Cross-correlation
c = xcorr(x,y)
c = xcorr(x) 1.4

Example: 1.2

>> n=0:15; 1

>> x=(0.5).^n; 0.8

>> c=xcorr(x); 0.6

>> [c d]=xcorr(x); 0.4

>> stem(d,c) 0.2

0
-15 -10 -5 0 5 10 15
Difference Equations

• FILTER: One-dimensional digital 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.
• In MATLAB, the function called filter is used to solve
difference equations for a particular input x(n).
Example:
y(n)+0.6y(n−1)=x(n)
use MATLAB to find the impulse responses in the range –10 ≤
n ≤ 20, the step responses and the sinusoidal responses for
x(n) = .5sin(n)u(n).
a=[1 0.6];
Difference Equations
b=[1];
n=[-10:20]; The impulse response The step response The sinusoidal response
1 1 1
x=[zeros(1,10) 1 zeros(1,20)];% generating the input
impulse=filter(b,a,x); %the output due to x(n) 0.8 0.8 0.8

subplot(1,3,1) 0.6 0.6 0.6

stem(n,impulse);xlabel('n'); 0.4 0.4 0.4

title('The impulse response'); 0.2 0.2 0.2


axis([-10 20 -1 1]) 0 0 0
x1=[zeros(1,10) ones(1,21)];
-0.2 -0.2 -0.2
step=filter(b,a,x1); %the output due to the step i/p
-0.4 -0.4 -0.4
subplot(1,3,2)
-0.6 -0.6 -0.6
axis([-10 20 0 1])
stem(n,step);xlabel('n');title('The step response'); -0.8 -0.8 -0.8

x2=0.5*sin(n); -1
-10 0 10 20
-1
-10 0 10 20
-1
-10 0 10 20
axis([-10 20 -1 1]) n n n

sinusoidal=filter(b,a,x2);
subplot(1,3,3);axis([-10 20 0 1])
stem(n,sinusoidal);xlabel('n');
title('The sinusoidal response');
axis([-10 20 -1 1])
Frequency Response
• FREQ Z : Digital filter frequency response.

– [H,W] = FREQZ(B,A,N) returns the N-point complex frequency response


vector H and the N-point frequency vector W in radians/sample of the
filter
– [H,W] = FREQZ(B,A,N,'whole') uses N points around the whole unit circle.
– H = FREQZ(B,A,W) returns the frequency response at frequencies
designated in vector W, in radians/sample (normally between 0 and pi)
– [H,F] = FREQZ(B,A,N,Fs) and [H,F] = FREQZ(B,A,N,'whole',Fs) return
frequency vector F (in Hz), where Fs is the sampling frequency (in Hz).
– H = FREQZ(B,A,F,Fs) returns the complex frequency response at the
frequencies designated in vector F (in Hz), where Fs is the sampling
frequency (in Hz).
Frequency Response
• invfreqz: Identify discrete-time filter
parameters from frequency response data
– [B,A] = IN V FREQZ(H,W,NB,NA) gives real
numerator and denominator coefficients B and A
of orders NB and NA respectively, where H is the
desired complex frequency response of the
system at frequency points W, and W contains the
normalized frequency values within the interval
[0, P i] (W is in units of radians/sample).
Frequency Response
• freqs: Laplace-transform (s-domain) frequency
response.
– H = FREQ S (B,A,W) returns the complex frequency response
vector H of the filter B/A:

• Invfreqs: Identify continuous-time filter parameters from


frequency response data
• [B,A] = IN V FREQ S (H,W,nb,na) gives real numerator and
denominator coefficients B and A of orders nb and na
respectively, where H is the desired complex frequency
response of the system at frequency points W, and W
contains the frequency values in radians/s.
Frequency Response

----- The Fourier transform is given by

1.15
N = 0:pi/400:pi; % 401 frequency points 1.1

Magnitude plot
b = [1] 1.05

a = [1 -.1] 1

x = freqz(b, a, N) 0.95

0.9
subplot( 2, 1, 1); 0 0.5 1 1.5 2 2.5 3 3.5

plot(N, abs(x)); 0

ylabel('M agnitude plot'); -0.05


Phase plot

subplot( 2 , 1, 2 ); -0.1

plot(N, angle(x)); -0.15

ylabel('P hase plot'); xlabel('Frequency'); -0.2


0 0.5 1 1.5 2 2.5 3 3.5
Frequency
Frequency Response
b = [1 -. 8 ] 2

a = [1 -.1]

Magnitude plot
1.5

w=0:.01: 2 *pi; 1

0.5
x = freqz(b,a,w) 0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
subplot(2, 1, 1);
1
plot(w/pi, abs(x)); 0.5

ylabel('M agnitude plot'); Phase plot 0

subplot( 2, 1, 2 ); -0.5

plot(w/pi, angle(x)); -1
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
Frequency
ylabel('P hase plot');
xlabel('Frequency');
Frequency Response
16

a = [1 2 3 2 1 4]; b = [1 2 3 2 3 ];14

12

[h,w] = freqz(b,a, 64); 10

plot(w,abs(h))
8

[bb,aa] = invfreqz(h,w,4,5) 4

bb = 0
0 0.5 1 1.5 2 2.5 3 3.5

1.0000 2.0000 3.0000 2.0000 3.0000


aa =
1.0000 2.0000 3.0000 2.0000 1.0000 4.0000
Frequency Response
8
b = [1 -.8]

Magnitude plot
6

a = [1 -.1] 4

w=0:.01:2*pi; 2

x = freqs(b,a,w) 0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

subplot(2, 1, 1); 1

plot(w/pi, abs(x)); 0.8

Phase plot
0.6

ylabel('Magnitude plot'); 0.4

subplot(2, 1, 2); 0.2

0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
plot(w/pi, angle(x)); Frequency

ylabel('Phase plot'); xlabel('Frequency');


Frequency Response
• Find and graph the frequency response of the
transfer function given by:

0
10

Magnitude
a = [1 0.4 1];
b = [0.2 0.3 1]; -1 0 1
10 10 10

w = logspace(-1,1); 0
Frequency (rad/s)

freqs(b,a,w)
Phase (degrees)

-50

-100

-150
-1 0 1
10 10 10
Frequency (rad/s)
Frequency Response
b = [1] 10
0

a = [1 -.4]
w=0:.01:2*pi; -2 -1 0 1
10 10 10 10

h = freqs(b,a,w) -1.5

mag = abs(h); -2

-2.5
phase = angle(h); -3

subplot(2,1,1), loglog(w,mag);grid; -3.5


10
-2 -1
10
0
10
1
10

subplot(2,1,2), semilogx(w,phase);grid;
Note: To convert to hertz, degrees, and decibels, use
f = w/(2*pi);
mag = 20*log10(mag);
phase = phase*180/pi;
Frequency Response
a = [1 2 3 2 1 4]; b = [1 2 3 2 3];
[h,w] = freqs(b,a,64);
[bb,aa] = invfreqs(h,w,4,5)
bb =
1.0000 2.0000 3.0000 2.0000 3.0000
aa =
1.0000 2.0000 3.0000 2.0000 1.0000 4.0000
Z-Transform
• The z-transform of the signal x(n) is given by

here z is the complex variable.


• To compute Z- transform using MATLAB
• ZTRANS: Z-transform.
• F = ZTRANS(f) is the Z-transform of the scalar sym f with
default independent variable n.
• F = ZTRANS(f,w) makes F a function of the sym w instead of
the default z: ZTRANS(f,w) <=> F(w) = symsum(f(n)/w^n, n, 0, inf).
• F = ZTRANS(f,k,w) takes f to be a function of the sym variable k:
ZTRANS(f,k,w) <=> F(w) = symsum(f(k)/w^k, k, 0, inf).
Z-Transform
Examples:
syms k n w z
ztrans(2^n) returns z/(z-2)
ztrans(sin(k*n),w) returns sin(k)*w/(1-2*w*cos(k)+w^2)
ztrans(cos(n*k),k,z) returns z*(-cos(n)+z)/(-2*z*cos(n)+z^2+1)
ztrans(cos(n*k),n,w) returns w*(-cos(k)+w)/(-2*w*cos(k)+w^2+1)
ztrans(sym('f(n+1)')) returns z*ztrans(f(n),n,z)-f(0)*z
Z-Transform
• IZTRANS Inverse Z-transform.
– f = IZTRANS(F) is the inverse Z-transform of the scalar sym F with
default independent variable z. The default return is a function
of n: F = F(z) => f = f(n). If F = F(n), then IZTRANS returns a
function of k: f = f(k).
– f = IZTRANS(F,k) makes f a function of k instead of the default n.
Here m is a scalar sym.
– f = IZTRANS(F,w,k) takes F to be a function of w instead of the
default symvar(F) and returns a function of k: F = F(w) & f = f(k).

Examples:
iztrans(z/(z-2)) returns 2^n
iztrans(exp(x/z),z,k) returns x^k/k!
Z-Transform
• MATLAB can be used to determine the ROC’S
of rational Z- transform.
• [z,p,k] = tf2zp(b,a): Transfer function to zero-
pole conversion. Which gives zeros, poles, and
gain constant.
• [b,a] = zp2tf(z,p,k): Convert zero-pole-gain
filter parameters to transfer function form.
• zplane(z,p): Zero-pole plot
Z-Transform
>> a=[3 -4 1];
>> b=[1,1]; 1

0.8

>> [z,p,k]=tf2zp(b,a) 0.6

z= 0.4

Imaginary Part
0.2

-1 0

p= -0.2

-0.4

1.0000 -0.6

-0.8
0.3333 -1

k= -1 -0.5 0
Real Part
0.5 1

0.3333
>> zplane(b,a)
Z-Transform
>> b=[2 16 44 56 32];
>> a=[3 3 -15 18 -12];
>> [z,p,k]=tf2zp(b,a)
z=
-4.0000
-2.0000 >> sos=zp2sos(z,p,k)
-1.0000 + 1.0000i sos =
-1.0000 - 1.0000i 0.6667 4.0000 5.3333 1.0000 2.0000 -4.0000
p= 1.0000 2.0000 2.0000 1.0000 -1.0000 1.0000
-3.2361
1.2361 >> [z,p,k] = sos2zp(sos)
0.5000 + 0.8660i
0.5000 - 0.8660i
k=
0.6667
Inverse Z-Transform
• Long Division Method:
– To compute IZT MATLAB deconv function
[q,r]=deconv(b,a)
Example:
>> b=[1 2 1];
>> a=[1 -1 0.3561];
>> n=5;
>> b=[b,zeros(1,n-1)];
>> [x,r]=deconv(b,a);
>> disp(x)
1.0000 3.0000 3.6439 2.5756 1.2780
Inverse Z-Transform
To compute IZT MATLAB impz function
[h,t]=impz(b,a)
>> b=[1 2 1];
>> a=[1 -1 0.3561];
>> n=5;
>> b=[b,zeros(1,n-1)];
>> [h,t]=impz(b,a);
Inverse Z-Transform
• sos2tf:Convert digital filter second-order section data
to transfer function form
[b,a] = sos2tf(sos)
>> n=5;
>> N1=[1 -1.122346 1];D1=[1 -1.433509 0.85811];
>> N2=[1 1.474597 1];D2=[1 -1.293601 0.556929];
>> N3=[1 1 0];D3=[1 -0.612159 0];
>> B=[N1;N2;N3];A=[D1;D2;D3];
>> [b,a]=sos2tf([B A]);
>> b=[b zeros(1,n-1)];
>> [x,r]=deconv(b,a);
>> disp(x)
1.0000 4.6915 11.4246 19.5863 27.0284
Inverse Z-Transform
• Partial fraction expansion
– To compute PFE use residuez function
[r,p,k] = residue(b,a)
Example:
>> num=[ 0 0 1]; 1

>> den=[1 -3 2 ]; 0.8

>> [r,p,k]=residuez(num,den) 0.6

r= 0.4

Imaginary Part
0.5000 0.2

-1.0000 0

p= -0.2

2 -0.4

1 -0.6

k= -0.8

0.5000 -1

-1 -0.5 0 0.5 1
>> zplane(b,a) Real Part
Inverse Z-Transform
TF Creation of transfer functions or conversion to transfer function.
Creation:
SYS = TF(NUM,DEN) creates a continuous-time transfer function SYS with
numerator(s) NUM and denominator(s) DEN. The output SYS is a TF
object.
SYS = TF(NUM,DEN,TS) creates a discrete-time transfer function with
sample time TS (set TS=-1 if the sample time is undetermined).

>> b=[1 2 1]; >> tf(b,a,.1)


>> tf(b,a,.1,'variable','z^-1')
>> a=[1 -1 0.3561];
Transfer function:
>> tf(b,a) Transfer function:
1 + 2 z^-1 + z^-2
Transfer function: z^6 + 2 z^5 + z^4
----------------------
s^6 + 2 s^5 + s^4 -----------------
1 - z^-1 + 0.3561 z^-2
----------------- z^2 - z + 0.3561
s^2 - s + 0.3561
Sampling time: 0.1
Sampling time: 0.1
Inverse Z-Transform
filt: Specify discrete transfer functions in DSP format
sys = filt(num,den)
sys = filt(num,den,Ts)
>>num = {1 , [1 0.3]};
>>den = {[1 1 2] ,[5 2]};
>> H = filt(num,den)
Transfer function from input 1 to output:
1
-----------------
1 + z^-1 + 2 z^-2
Transfer function from input 2 to output:
1 + 0.3 z^-1
------------
5 + 2 z^-1
Sampling time: unspecified
Inverse Z-Transform

>> N1=[1 -1.122346 1];D1=[1 -1.433509 0.85811]; r=


3.5837 +14.3235i
>> N2=[1 1.474597 1];D2=[1 -1.293601 0.556929]; 3.5837 -14.3235i
>> N3=[1 1 0];D3=[1 -0.612159 0]; -42.8984 -28.2333i
-42.8984 +28.2333i
>> sos=[N1 D1;N2 D2;N3 D3]; 83.0475
>> [b,a]=sos2tf(sos); p=
0.7168 + 0.5868i
>> [r,p,k]=residuez(b,a) 0.7168 - 0.5868i
0.6468 + 0.3723i
0.6468 - 0.3723i
0.6122
k=
-3.4182
Inverse Z-Transform
• Compute inverse z-transform of
>> b=1;a=poly([0.9,0.9,-0.9])
a=
1.0000 -0.9000 -0.8100 0.7290
>> [r,p,k]=residuez(b,a) >> zplane(b,a)
r=
1
0.2500 0.8

0.5000 0.6

0.4
0.2500 Imaginary Part
0.2

p= 0
3 2

0.9000 -0.2

-0.4
0.9000 -0.6

-0.9000 -0.8

k = [] -1

-1 -0.5 0 0.5 1
Real Part
Discrete Fourier Transform
• The Discrete Fourier transform is defined

•The Inverse DFT is defined as

Let

and will get


Discrete Fourier Transform
• DFT USING DEFINITION 10
Magnitude

xn=[2 4 2 1]; N=50;


L=length(xn); 5

x1=[xn zeros(1,N-L)];
for k=0:N-1 0
for n=0:N-1 0 5 10 15 20 25 30 35 40 45 50

p=exp(-i*2*pi*n*k/N); 4
Phase

x2(k+1,n+1)=p; 2
end; 0
end;
-2
Xk=x1*x2.';
-4
k=0:N-1; 0 5 10 15 20 25 30 35 40 45 50

subplot(211);stem(k,abs(Xk));
title('Magnitude');
subplot(212);stem(k,angle(Xk));
title('Phase');
Discrete Fourier Transform
• IDFT USING DEFINITION
Xk=[9 -3j -1 3j]; N=4;
for k=0:N-1
for n=0:N-1
p=exp(-i*2*pi*n*k/N);
x2(k+1,n+1)=p;
end;
end;
xn=(Xk*x2.')/N

xn =
2.0000 1.0000 - 0.0000i 2.0000 - 0.0000i 4.0000 + 0.0000i
Discrete Fourier Transform
• DFT using TWIDDLE MATRIX
xn=[2 4 2 1]; N=4; 10
Magnitude

k=0:N-1;
n=0:N-1; 5

WN=exp(-j*2*pi/N);
nk=n'*k; 0
0 0.5 1 1.5 2 2.5 3

WNnk=WN.^nk; 2
Phase

Xk=xn*WNnk;
0
subplot(211);stem(k,abs(Xk));
title('Magnitude'); -2

subplot(212);stem(k,angle(Xk)); -4
0 0.5 1 1.5 2 2.5 3

title('Phase');
Discrete Fourier Transform
• IDFT using TWIDDLE MATRIX
Xk=[9 -3j -1 3j]; N=4;
k=0:N-1;
n=0:N-1;
WN=exp(-j*2*pi/N);
nk=n'*k;
WNnk=WN.^nk;
xn=(Xk*WNnk)/N

xn =
2.0000 1.0000 - 0.0000i 2.0000 - 0.0000i 4.0000 + 0.0000i
Discrete Fourier Transform
• fft: Discrete Fourier transform
• ifft:Inverse DFT 8
Magnitude

Y = fft(X), y = ifft(X) 6

Y = fft(X,n), y = ifft(X,n) 4
2
Example:
0
0 5 10 15 20 25 30 35 40 45 50
xn=[1 2 3 1]; Phase
4
N=50;k=0:N-1;n=0:N-1;
2

Xk=fft(xn,N) 0

subplot(211);stem(k,abs(Xk)); -2
-4
title('Magnitude'); 0 5 10 15 20 25 30 35 40 45 50

subplot(212);stem(k,angle(Xk));
title('Phase');
Discrete Fourier Transform
Example:
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
t = (0:L-1)*T; % Time vector
% Sum of a 50 Hz sinusoid and a 120 Hz sinusoid
x = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
y = x + 2*randn(size(t)); % Sinusoids plus noise
plot(Fs*t(1:50),y(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('time (milliseconds)')
Discrete Fourier Transform
Signal Corrupted with Zero-Mean Random Noise
5

-1

-2

-3

-4

-5
0 5 10 15 20 25 30 35 40 45 50
time (milliseconds)
Discrete Fourier Transform
• NFFT = 2^nextpow2(L); % Next power of 2 from length of y
• Y = fft(y,NFFT)/L;
• f = Fs/2*linspace(0,1,NFFT/2);

• % Plot single-sided amplitude spectrum.


• plot(f,2*abs(Y(1:NFFT/2)))
• title('Single-Sided Amplitude Spectrum of y(t)')
• xlabel('Frequency (Hz)')
• ylabel('|Y(f)|')
Discrete Fourier Transform
Single-Sided Amplitude Spectrum of y(t)
1

0.9

0.8

0.7

0.6
|Y(f)|

0.5

0.4

0.3

0.2

0.1

0
0 50 100 150 200 250 300 350 400 450 500
Frequency (Hz)
DFT Leakage
• Consider the waveform sampled
over a period S = 1 with N = 256 points
N=256; k = 0:N-1;
x = cos(25.5*(k-128)/N);
plot(k,x) axis([0 256 -1 1]); xlabel('sample'); ylabel('response');
DFT Leakage
• DFT of Waveform - Leakage Observed:
Construct the DFT of the waveform. We used fftshift to
correctly position the centered waveform for zero phase. The
waveform is even, so its DFT is real.

y = fft(fftshift(x));
n = -16:16;
ysamp = real(y(n+129));
stem(n,ysamp) line([-16 16],[0 0]);
axis([-16 16 -16 16]);
xlabel('harmonic'); ylabel('response');
Circular convolution
• cconv: Modulo-N circular convolution
c =cconv(a,b,n)
• Example:
a = [2 1 2 1];
b = [1 2 3 4];
a = [1 2 -1 1];
c = cconv(a,b,4) b = [1 1 2 1 2 2 1 1];
c= c = cconv(a,b) % Circular convolution
cref = conv(a,b) % Linear convolution
14 16 14 16
norm(c-cref)

ans =
9.7422e-016
Sampling & Reconstruction
• An analog signal x(t)=cos(20πt), 0≤ t ≤ 1, is
sampled at Ts=0.01, 0.05, 0.1 sec. intervals
clc;
clear;close all;
t=0:0.01:1;xa=cos(20*pi*t);
Ts=0.01;N1=round(1/Ts);n1=0:N1;x1=cos(20*pi*n1*Ts);
subplot(311);plot(t,xa,n1*Ts,x1,'o');axis([0,1,-1.1,1.1]);
title('sampling at Ts=0.01');
Ts=0.05;N2=round(1/Ts);n2=0:N2;x2=cos(20*pi*n2*Ts);
subplot(312);plot(t,xa,n2*Ts,x2,'o');axis([0,1,-1.1,1.1]);
title('sampling at Ts=0.05');
Ts=0.1;N3=round(1/Ts);n3=0:N3;x3=cos(20*pi*n3*Ts);
subplot(313);plot(t,xa,n3*Ts,x3,'o');axis([0,1,-1.1,1.1]);
title('sampling at Ts=0.1');
Sampling & Reconstruction
sampling at Ts=0.01
1

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
sampling at Ts=0.05
1

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
sampling at Ts=0.1
1

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Recostruction using sinc function
%% Recostruction using sinc function
Ts=0.01;Fs=1/Ts;
xa1=x1*sinc(Fs*(ones(length(n1),1)*t-(n1*Ts)'*ones(1,length(t))));
subplot(311);plot(t,xa1);axis([0,1,-1.1,1.1]);
title('Reconstruction when Ts=0.01');
Ts=0.05;Fs=1/Ts;
xa2=x2*sinc(Fs*(ones(length(n2),1)*t-(n2*Ts)'*ones(1,length(t))));
subplot(312);plot(t,xa2);axis([0,1,-1.1,1.1]);
title('Reconstruction when Ts=0.05');
Ts=0.1;Fs=1/Ts;
xa3=x3*sinc(Fs*(ones(length(n3),1)*t-(n3*Ts)'*ones(1,length(t))));
subplot(313);plot(t,xa3);axis([0,1,-1.1,1.2]);
title('Reconstruction when Ts=0.1');
Recostruction using sinc function
Reconstruction when Ts=0.01
1

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Reconstruction when Ts=0.05
1

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Reconstruction when Ts=0.1
1

-1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Reconstruction using spline
Reconstruction when Ts=0.01
1

Ts=0.01;Fs=1/Ts; 0

xa1=spline(Ts*n1,x1,t); -1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
subplot(311);plot(t,xa1); Reconstruction when Ts=0.05
axis([0,1,-1.1,1.1]); 1

title('Reconstruction when Ts=0.01'); 0


Ts=0.05;Fs=1/Ts; -1
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
xa2=spline(Ts*n2,x2,t); Reconstruction when Ts=0.1

subplot(312);plot(t,xa2); 1

axis([0,1,-1.1,1.1]); 0

title('Reconstruction when Ts=0.05'); -10 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Ts=0.1;Fs=1/Ts;
xa3=spline(Ts*n3,x3,t);
subplot(313);plot(t,xa3);axis([0,1,-1.1,1.2]);
title('Reconstruction when Ts=0.1');
%% Sampling
Sampling & Reconstruction
clc;
clear; continuous signal DT signal Fs<2fm
1 1
t=-10:.01:10;
T=4;fm=1/T; 0.5 0.5
x=cos(2*pi*fm*t);% continuous signal

x(t)
0 0
fs1=1.6*fm;
fs2=2*fm; -0.5 -0.5
fs3=8*fm;
-1 -1
n1=-4:4; -10 -5 0 5 10 -4 -2 0 2 4
xn1=cos(2*pi*n1*fm/fs1); time
DT signal Fs=2fm DT signal Fs>2fm
subplot(221);plot(t,x); 1 1
xlabel('time');ylabel('x(t)');
0.5 0.5
title('continuous signal');
subplot(222);stem(n1,xn1);hold on; 0 0
subplot(222);plot(n1,xn1);
-0.5 -0.5
title('DT signal Fs<2fm');
n2=-5:5; -1 -1
-5 0 5 -20 -10 0 10 20
xn2=cos(2*pi*n2*fm/fs2);
subplot(223);stem(n2,xn2);hold on;
subplot(223);plot(n2,xn2);
title('DT signal Fs=2fm');
n3=-20:20;
xn3=cos(2*pi*n3*fm/fs3);
subplot(224);stem(n3,xn3);hold on;
subplot(224);plot(n3,xn3);
title('DT signal Fs>2fm');
Touch-Tone Dialing

Telephone keypad.

The basis for touch-tone dialing


is the Dual Tone Multi-Frequency
(DTMF) system.

Cont..
Touch-Tone Dialing

% basic frequencies are


fr = [697 770 852 941];
fc = [1209 1336 1477];
% If s is a character that labels one of the buttons on the keypad, the corre-
% sponding row index k and column index j can be found with
s1=input('type the digit=','s');
switch s1
case '*', k = 4; j = 1;
case '0', k = 4; j = 2;
case '#', k = 4; j = 3;
otherwise,
d = s1-'0'; j = mod(d-1,3)+1; k = (d-j)/3+1;
end; Cont..
Touch-Tone Dialing
%A key parameter in digital sound is the sampling rate.
Fs = 32768;
% A vector of points in the time interval 0 <=t<= 0:25 at this sampling rate is
t = 0:1/Fs:0.25;
%The tone generated by the button in position (k,j) is obtained by superimposing
% the two fundamental tones with frequencies fr(k) and fc(j).
y1 = sin(2*pi*fr(k)*t);
y2 = sin(2*pi*fc(j)*t);
y = (y1 + y2)/2;
%If your computer is equipped with a sound card, the Matlab statement
sound(y,Fs); % plays the tone.

Cont..
Touch-Tone Dialing
y = double(y)/128;
% rescales the vector and converts it to double precision for later use.
% The statements
n = length(y);
t = (0:n-1)/Fs;
% reproduce the sample times of the recording.

% the magnitude of the FFT of the signal, which is the key to


% determining the individual digits.
% The plot was produced with

p = abs(fft(y));
f = (0:n-1)*(Fs/n);
plot(f,p);
%axis([500 1700 0 600]);
Analog Filter Design
Analog Filter Design
Analog Filter Design
• BUTTAP :Butterworth analog lowpass filter prototype.
[Z,P,K] = BUTTAP(N) returns the zeros, poles, and gain for an N-th order
normalized prototype Butterworth analog lowpass filter. The resulting filter
has N poles around the unit circle in the left half plane, and no zeros.
Example:
% 4 th order analog LP filter
format long
[z,p,k]=buttap(4);
[pz,pp]=zp2tf(z,p,k);
omega=[0:0.01:5];
h=freqs(pz,pp,omega);
gain=20*log10(abs(h));
plot(omega,gain);
grid;
Analog Filter Design
• BUTTER : Butterworth digital and analog filter design.
[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. Example:
% butterworth LP filter
N=4;
fp=5e3;% pb edgefreq
[b,a]=butter(N,fp,'s');
om=[0:200:12000*pi];
h=freqs(b,a,om);
gain=20*log10(abs(h));
plot(om/(2*pi),gain);
grid;
Digital Filter Design
• IIR: Infinite Impulse Response
Butterworth,chebychev,elliptical etc.
• FIR: Finite Impulse Response
Fourier series method, windowing methods

• IIR Filter Design


The primary advantage of IIR filters over FIR filters is that they
typically meet a given set of specifications with a much lower filter
order than a corresponding FIR filter. Although IIR filters have
nonlinear phase, data processing within MATLAB is commonly
performed "offline,” that is, the entire data sequence is available prior
to filtering. This allows for a noncausal, zero-phase filtering approach
(via the filtfilt function), which eliminates the nonlinear phase
distortion of an IIR filter.
Digital Filter Design
Example 1:
For data sampled at 1000 Hz, design a 9th-order highpass Butterworth filter with
cutoff frequency of 300 Hz, which corresponds to a normalized value of 0.6:

[b,a] = butter(9,300/500,'high');
freqz(b,a,128,1000)

Example 2
Design a 10th-order bandpass Butterworth filter with a passband
from 100 to 200 Hz and plot its impulse response, or unit sample
response:
n = 5; Wn = [100 200]/500;
[b,a] = butter(n,Wn);
[y,t] = impz(b,a,101);
stem(t,y)
Digital Filter Design
• buttord:Butterworth filter order and cutoff frequency
[n,Wn] = buttord(Wp,Ws,Rp,Rs)
[n,Wn] = buttord(Wp,Ws,Rp,Rs,'s')

Example 1
For data sampled at 1000 Hz, design a lowpass filter with less than 3 dB of ripple in
the 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). Plot the filter's frequency response:

Wp = 40/500; Ws = 150/500;
[n,Wn] = buttord(Wp,Ws,3,60)
[b,a] = butter(n,Wn);
freqz(b,a,512,1000);
title('n=5 Butterworth Lowpass Filter')
Digital Filter Design
• Example 2:
Next design a bandpass filter with passband of 60 Hz to 200 Hz, with
less than 3 dB of ripple in the passband, and 40 dB attenuation in the
stopbands that are 50 Hz wide on both sides of the passband:
Wp = [60 200]/500; Ws = [50 250]/500;
Rp = 3; Rs = 40;
[n,Wn] = buttord(Wp,Ws,Rp,Rs)
[b,a] = butter(n,Wn);
freqz(b,a,128,1000)
title('n=16 Butterworth Bandpass Filter')
Digital Filter Design
• FIR Filter Design:
Digital filters with finite-duration impulse response (all-zero,
or FIR filters) have both advantages and disadvantages
compared to infinite- duration impulse response (IIR) filters.
– Linear Phase Filters
– Windowing Method
– Multiband FIR Filter Design with Transition Bands
– Constrained Least Squares FIR Filter Design
– Arbitrary-Response Filter Design
Digital Filter Design
• FIR filters have the following primary advantages:
– They can have exactly linear phase.
– They are always stable.
– The design methods are generally linear.
– They can be realized efficiently in hardware.
– The filter startup transients have finite duration.
Digital Filter Design
• Common Design methods for FIR Filters

• Fourier Series Method


• Windowing Method
• Frequency sampling Method;
Digital Filter Design
• FIR Filters
Digital Filter Design
• Fourier Series Method

• A length 51 filter with a lowpass cutoff frequency :


0.4π of rad/s is
b = 0.4*sinc(0.4*(-25:25));
fvtool(b,1)
Digital Filter Design
• Windowing Method
Example1: Using b(n)=hd(n)*w(n)
b = 0.4*sinc(0.4*(-25:25));
b = b.*hamming(51)';
fvtool(b,1)
• Window-based finite impulse response filter design
Example2: Design a 50th-order FIR bandpass filter with
passband 0.35 ≤ 0.65:
b = fir1(50,[0.35 0.65]);
freqz(b,1,512)
Digital Filter Design
• Windowing Method
load chirp % Load y and fs.
b = fir1(34,0.48,'high',chebwin(35,30));
freqz(b,1,512) 50

Magnitude (dB)
0

-50

-100

-150
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample)

1000
Phase (degrees)

-1000

-2000
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
Normalized Frequency ( rad/sample)
Digital Filter Design
• Frequency sampling-based finite impulse response filter
design
• Examples:
Design a 30th-order lowpass filter and overplot the desired
frequency response with the actual frequency response:

f = [0 0.6 0.6 1]; m = [1 1 0 0];


b = fir2(30,f,m);
[h,w] = freqz(b,1,128);
plot(f,m,w/pi,abs(h))
legend('Ideal','fir2 Designed')
title('Comparison of Frequency Response Magnitudes')
grid;
Filter Design
• FDESIGN :Digital Filter Design.
D = FDESIGN.<RESPONSE> returns a set of specifications that can be used
to design filters specified by RESPONSE, such as lowpass, highpass, and
many other types of filters. Type "help fdesign/responses" to get the
list of supported responses.
Example:
d = fdesign.lowpass('Fp,Fst,Ap,Ast',0.2, 0.22, 1, 60);
• DESIGN: Design the filter from the specifications.
H = DESIGN(D) Design the filter H from the specifications in D. The
algorithm will be chosen from those available for the given specification.
H = DESIGN(D, METHOD, PARAM1, VALUE1, PARAM2, VALUE2, etc.)
specifies design method specific options.
Filter Design
H = DESIGN(D, 'FIR') forces an FIR design. This method will error if
there are no FIR designs available.

H = DESIGN(D, 'IIR') forces an IIR design. This method will error if


there are no IIR designs available.

H = DESIGN(D, 'ALL') designs multiple filters using all of the


available design methods for the specifications.

H = DESIGN(D, 'ALLFIR') designs multiple filters using all of the


available FIR design methods for the specifications.

H = DESIGN(D, 'ALLIIR') designs multiple filters using all of the


available IIR design methods for the specifications.
Filter Design
Example 1:
d = fdesign.lowpass('Fp,Fst,Ap,Ast',0.2, 0.22, 1, 60);
f = design(d, 'equiripple'); % Design an FIR equiripple filter
info(f) % View information about filter
% Other designs can be performed for the same specifications
designmethods(d,'iir'); % List the available IIR design
methods
f = design(d, 'ellip') % Design an elliptic IIR filter (SOS)
fvtool(f) % visualize various filter responses
input = randn(100,1);
output = filter(f,input); % Process data through the elliptic
filter.
Filter Design
Example #2 - Design all possible IIRs for the specifications
given.
Fs = 48e3; % Sampling frequency is 48 kHz
d = fdesign.lowpass('Fp,Fst,Ap,Ast',10000,11000,0.5,80,Fs);
designmethods(d, 'iir')
Hd = design(d, 'alliir'); % Hd is a vector of IIR filters
fvtool(Hd)

Example #3 - Design a Kaiser window highpass FIR filter.


d = fdesign.highpass('Fst,Fp,Ast,Ap',0.35, 0.4,74,1);
Hd = design(d, 'kaiserwin');
fvtool(Hd)
Filter Design
Example #4 - Design a 50th order equiripple FIR filter with a
% sloped stopband. Type help(d, 'equiripple') for more
% info.
d = fdesign.lowpass('N,Fc,Ap,Ast',50, 0.4, 0.8, 80);
Hd = design(d, 'equiripple', 'StopbandShape', 'linear',
'StopbandDecay', 40);
fvtool(Hd)

Example #5 - Design a lowpass filter using the interpolated FIR


% method.
d = fdesign.decimator(4,'lowpass','Fp,Fst,Ap,Ast',.2, .22, 1 ,60);
opts = designopts(d,'ifir');
opts.JointOptimization = true;
Hd = design(d,'ifir',opts); % Hd has two FIR filters cascaded
fvtool(Hd)
Sptool and fvtool
Sptool :Open interactive digital signal processing tool
>> sptool
fvtool: Open Filter Visualization Tool
Example 1:
Display the magnitude response of an elliptic filter, starting
FVTool from the command line:
[b,a]=ellip(6,3,50,300/500);
fvtool(b,a);
Sptool and fvtool
Example 2:
• Display and analyze multiple FIR filters,
starting FVTool from the command line. Then,
display the associated analysis parameters for
the magnitude:
b1 = firpm(20,[0 0.4 0.5 1],[1 1 0 0]);
b2 = firpm(40,[0 0.4 0.5 1],[1 1 0 0]);
fvtool(b1,1,b2,1);
fdatool
• fdatool:Open Filter Design and Analysis Tool
--fdatool opens the Filter Design and Analysis
Tool (FDATool). Use this tool to
Design filters
Analyze filters
Modify existing filter designs
wvtool
• wvtool:Open Window Visualization Tool
• Examples: Compare Hamming, Hann, and Gaussian
windows:
>>wvtool(hamming(64),hann(64),gausswin(64))
Time domain Frequency domain
50
1

0.8 0

Magnitude (dB)
Amplitude

0.6
-50

0.4

-100
0.2

0 -150
10 20 30 40 50 60 0 0.2 0.4 0.6 0.8
Samples Normalized Frequency ( rad/sample)
wvtool
• Examples:
Create a 200-point Kaiser window with a beta of 2.5
and display the result using WVTool:
w = kaiser(200,2.5);
wvtool(w) Time domain
60
Frequency domain

1
40

0.8
20

Magnitude (dB)
Amplitude

0.6
0

0.4
-20

0.2 -40

0 -60
50 100 150 200 0 0.2 0.4 0.6 0.8
Samples Normalized Frequency ( rad/sample)
Spectral Analysis

• The goal of spectral estimation is to describe the


distribution (over frequency) of the power contained
in a signal, based on a finite set of data. Estimation of
power spectra is useful in a variety of applications,
including the detection of signals buried in wide-
band noise.
– Spectral Estimation Method
– Nonparametric Methods
– Parametric Methods
Spectral Estimation Methods/Functions
Spectral Estimation Methods/Functions
Nonparametric Methods
• Periodogram
Example:
randn('state',0);
fs = 1000; % Sampling frequency
t = (0:fs)/fs; % One second worth of samples
A = [1 2]; % Sinusoid amplitudes (row vector)
f = [150;140]; % Sinusoid frequencies (column vector)
xn = A*sin(2*pi*f*t) + 0.1*randn(size(t));
% The periodogram estimate of the PSD can be computed by creating a
%periodogramobject
Hs = spectrum.periodogram('Hamming');
% and a plot of the estimate can be displayed with the psd method:
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','twosided')
Nonparametric Methods
Periodogram Power Spectral Density Estimate
0

-10

-20
Power/frequency (dB/Hz)

-30

-40

-50

-60

-70

-80
0 100 200 300 400 500 600 700 800 900
Frequency (Hz)
Nonparametric Methods
randn('state',0)
fs = 1000; % Sampling frequency
t = (0:fs/10)/fs; % One-tenth second worth of samples
A = [1 2]; % Sinusoid amplitudes
f = [150;140]; % Sinusoid frequencies Periodogram Power Spectral Density Estimate
0
xn = A*sin(2*pi*f*t) + 0.1*randn(size(t));
-10
Hs = spectrum.periodogram;
-20
psd(Hs,xn,'Fs',fs,'NFFT',1024)
Power/frequency (dB/Hz)
-30

-40

-50

-60

-70
Hhamm = spectrum.periodogram('Hamming');
-80
0 50 100 150 200 250 300 350 400 450 500
Frequency (Hz)
Parametric Methods
load mtlb
Hwelch = spectrum.welch('hamming',256,50);
psd(Hwelch,mtlb,'Fs',Fs,'NFFT',1024)
Welch Power Spectral Density Estimate
-20

-30
Power/frequency (dB/Hz)

-40

-50

-60

-70

-80
0 0.5 1 1.5 2 2.5 3 3.5
Frequency (kHz)
Parametric Methods
Hyulear = spectrum.yulear(14);
psd(Hyulear,mtlb,'Fs',Fs,'NFFT',1024)
Yule-Walker Power Spectral Density Estimate
-20

Power/frequency (dB/Hz) -30

-40

-50

-60

-70

-80
0 0.5 1 1.5 2 2.5 3 3.5
Frequency (kHz)
Multirate Filter Bank Implementation
• The function upfirdn alters the sampling rate of a signal by an
integer ratio P/Q.
– Upsampling (zero insertion) by integer factor p
– Filtering by FIR filter h
– Downsampling by integer factor q

For example, to change the sample rate of a signal from 44.1 kHz to
48 kHz, we first find the smallest integer conversion ratio p/q. Set
d = gcd(48000,44100);
p = 48000/d;
q = 44100/d;
In this example, p = 160 and q =147. Sample rate conversion is then
accomplished by typing y = upfirdn(x,h,p,q)
Multirate Filter Bank Implementation
• resample: Change sampling rate by rational factor
• y = resample(x,p,q) : resamples the sequence in vector x at
p/q times the original sampling rate, using a polyphase filter
implementation. p and q must be positive integers. The length
of y is equal to ceil(length(x)*p/q).If x is a matrix, resample
works down the columns of x.
EXAMPLE:
Resample a simple linear sequence at 3/2 the original rate:
1.5

fs1 = 10; % Original sampling frequency in Hz original


resampled

t1 = 0:1/fs1:1; % Time vector 1

x = t1; % Define a linear sequence


y = resample(x,3,2); % Now resample it 0.5

t2 = (0:(length(y)-1))*2/(3*fs1); % New time vector


plot(t1,x,'*',t2,y,'o',-0.5:0.01:1.5,-0.5:0.01:1.5,':')
0

legend('original','resampled'); xlabel('Time')
-0.5
-0.5 0 0.5 1 1.5
Time
Multirate Filter Bank Implementation
• interp : Interpolation — increase sampling rate by integer factor
• y = interp(x,r) : increases the sampling rate of x by a factor of r.
The interpolated vector y is r times longer than the original input x.
Original Signal
Examples 2

1
Interpolate a signal by a factor of four:
t = 0:0.001:1; % Time vector 0

x = sin(2*pi*30*t) + sin(2*pi*60*t); -1

y = interp(x,4); -2
0 5 10 15 20 25 30
stem(x(1:30));
title('Original Signal'); 2
Interpolated Signal

figure
1
stem(y(1:120));
0
title('Interpolated Signal');
-1

-2
0 20 40 60 80 100 120
Multirate Filter Bank Implementation
• decimate: Decimation — decrease sampling rate
• y = decimate(x,r) reduces the sample rate of x by a factor r.
The decimated vector y is r times shorter in length than the
input vector x.
Original Signal
Examples 2

Decimate a signal by a factor of four: 1

t = 0:.00025:1; % Time vector 0


x = sin(2*pi*30*t) + sin(2*pi*60*t);
-1
y = decimate(x,4);
-2
subplot(211); 0 20 40 60 80 100 120

stem(x(1:120)), axis([0 120 -2 2]) % Original signal Decimated Signal


2
title('Original Signal')
subplot(212); 1

stem(y(1:30)) % Decimated signal0


title('Decimated Signal') -1

-2
0 5 10 15 20 25 30
Multirate Filter Bank Implementation
• upsample: Increase sampling rate by integer factor
• Syntax
– y = upsample(x,n) ; y = upsample(x,n,phase)
Examples
Increase the sampling rate of a sequence by 3:
>> x = [1 2 3 4];
y = upsample(x,3);
x,y
x= 1 2 3 4
y= 1 0 0 2 0 0 3 0 0 4 0 0
Increase the sampling rate of the sequence by 3 and add a phase offset of
2:
y = upsample(x,3,2);
x,y
x= 1 2 3 4
y= 0 0 1 0 0 2 0 0 3 0 0 4
Multirate Filter Bank Implementation
• downsample: Decrease sampling rate by integer factor
• Syntax
– y = downsample(x,n); y = downsample(x,n,phase)

Examples
Decrease the sampling rate of a sequence by 3:
x = [1 2 3 4 5 6 7 8 9 10];
y = downsample(x,3)
y=
1 4 7 10
Decrease the sampling rate of the sequence by 3 and add a phase offset of
2:
y = downsample(x,3,2)
y=
3 6 9
How Filter Design Toolbox Works
There are two distinct objects involved in filter design:
• Specification Object — Captures the required design
parameters of a filter.
• Filter Object — Describes the designed filter;
includes the array of coefficients and the filter
structure
Basic Filter Design Process
• Use the following two steps to design a simple filter.
1. Create a filter specification object.
2. Design your filter.

Typically a bandpass filter is defined as shown in the following figure.


Basic Filter Design Process
Bandpass filter has the following specifications:
Basic Filter Design Process
• Example — Design a Filter in Two Steps
1. To create a filter specification object type or evaluate the following code at
the MATLAB prompt:
>> BandPassSpecObj = fdesign.bandpass

BandPassSpecObj =
Response: 'Bandpass'
Specification: 'Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2'
Description: {7x1 cell}
NormalizedFrequency: true
Fstop1: 0.35
Fpass1: 0.45
Fpass2: 0.55
Fstop2: 0.65
Astop1: 60
Apass: 1
Astop2: 60
>> set(BandPassSpecObj, 'Fpass2', 0.65, 'Fstop2', 0.75)
Basic Filter Design Process
2. Design the filter by using the design command.Evaluate or
type the following at the MATLAB prompt:

>> BandPassFilt = design(BandPassSpecObj)


BandPassFilt =
FilterStructure: 'Direct-Form FIR'
Arithmetic: 'double'
Numerator: [1x48 double]
PersistentMemory: false

>> fvtool(BandPassFilt) %plot the filter magnitude response


Basic Filter Design Process
Magnitude Response (dB)

-10

-20
Magnitude (dB)

-30

-40

-50

-60

-70

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency ( rad/sample)
Using FilterBuilder to Design a Filter
1. Type or evaluate the following at the MATLAB prompt:
>> filterbuilder
Using FilterBuilder to Design a Filter
2. Select Bandpass filter response from the list in the dialog
box, and hit the OK button. The following dialog box opens:
Using FilterBuilder to Design a Filter
3. Enter the correct frequencies for Fpass2 and Fstop2, as
shown in the preceding figure, then click OK. The following
message appears at the MATLAB prompt:
The variable 'Hbp' has been exported to the command window.
Using FilterBuilder to Design a Filter
4. To check your work, plot the filter magnitude response using
the Filter Visualization tool. Verify that all the design
parameters are met:
>> fvtool(Hbp) %plot the filter magnitude response
Magnitude Response (dB)

-10

-20
Magnitude (dB)

-30

-40

-50

-60

-70

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency ( rad/sample)
Designing Multirate and Multistage Filters
• What Is a Multirate Filter?
• A multirate filter reduces or increases the input sample rate,
resulting in an output rate different from the input rate.
• A high input frequency, while necessary in some cases, may be
rather costly.
• The higher the frequency, the more samples need to be evaluated
per unit time.
• Increased sampling causes a higher the load on the filter, and
therefore, results in the higher the cost.
•A filter that reduces the input rate is called a decimator.
• A filter that increases the input rate is called an interpolator.
Designing Multirate and Multistage Filters

Original Signal
1

0.5

-0.5

-1
0 5 10 15 20 25 30 Interpolated by 2
Interpolated Signal
1
Decimated by 2
0.5

-0.5

-1
0 20 40 60 80 100 120
Designing Multirate and Multistage Filters
• What Is a Mulstage Filter?
• A multistage filter consists of several filters connected in series.
Each filter in this series is called a stage.
• multistage filter s are used where requirement that includes a very
narrow transition width.
• Multistage filter is used for a multirate system, where there is a
decimator or an interpolator with a large factor.
• Typically a multistage filter is not easy to design by hand. It's
difficult to guess how many stages would provide an optimal design,
to optimize each stage, and then optimize all the stages together.
Designing Multirate and Multistage Filters
• Design a Lowpass Filter to Use as a Baseline
>> Fpass = 0.11;
>>Fstop = 0.12;
>>Apass = 0.02;
>>Astop = 60;
>>SOlowpass = fdesign.lowpass(Fpass, Fstop, Apass, Astop);
>>Flowpass = design (SOlowpass, 'equiripple');
>>cost (Flowpass)

ans =
Number of Multipliers : 649
Number of Adders : 648
Number of States : 648
MultPerInputSample : 649
AddPerInputSample : 648
This design requires 649 multiplications per input sample (MPIS).
Depending on the application where this filter is to be used, this may not be feasible.
Designing Multirate and Multistage Filters

• Design a multirate filter to improve cost


>> M = 8;
>> SOmultirate = fdesign.decimator(M, 'lowpass', Fpass, Fstop, Apass,
Astop);
>> Fmultirate = design (SOmultirate, 'equiripple')
>> cost (Fmultirate)
ans =

Number of Multipliers : 649


Number of Adders : 648
Number of States : 648
MultPerInputSample : 81.125
AddPerInputSample : 81

The cost has decreased from 649 MPIS to 81.125 MPIS.


Designing Multirate and Multistage Filters
Magnitude Response (dB)

-10

-20
Magnitude (dB)

-30

-40

-50

-60

-70

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency ( rad/sample)
Designing Multirate and Multistage Filters
• Design a Multistage Filter to Improve Cost and
Performance
>> Fmultistage = design(SOmultirate, 'multistage')

Fmultistage =
FilterStructure: Cascade
Stage(1): Direct-Form FIR Polyphase Decimator
Stage(2): Direct-Form FIR Polyphase Decimator
Stage(3): Direct-Form FIR Polyphase Decimator
PersistentMemory: false

>> cost (Fmultistage)


ans =
Number of Multipliers : 195
Number of Adders : 192 The cost has been reduced from 649 MPIS
Number of States : 190 to 29.5 MPIS — a significant savings!
MultPerInputSample : 29.5
AddPerInputSample : 28.625
Designing Multirate and Multistage Filters

Magnitude Response (dB)

-10

-20

-30

-40
Magnitude (dB)

-50

-60

-70

-80

-90

-100

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency ( rad/sample)
Floating-Point to Fixed-Point Conversion
• Designing the Filter
>> f = fdesign.bandpass(.35,.45,.55,.65,60,1,60);
>> Hd = design(f, 'equiripple');
>> fvtool(Hd)
Magnitude Response (dB)

-10

-20
Magnitude (dB)

-30

-40

-50

-60

-70

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency ( rad/sample)
Floating-Point to Fixed-Point Conversion
• Quantizing the Coefficients
>> Hf = Hd;
>> Hf.Arithmetic = 'fixed';
>> set(Hf, 'CoeffWordLength', 8);
Magnitude Response (dB)
>> fvtool(Hf) 0

-10

-20

-30
Magnitude (dB)

Filter #1: Quantized


-40
Filter #1: Reference

-50

-60

-70

-80

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency ( rad/sample)
Floating-Point to Fixed-Point Conversion

• Quantizing the Coefficients

>> set(Hf, 'CoeffWordLength', 12);


>> fvtool(Hf) Magnitude Response (dB)

-10

-20
Magnitude (dB)

-30

-40

Filter #1: Quantized

-50 Filter #1: Reference

-60

-70

0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9


Normalized Frequency ( rad/sample)
Floating-Point to Fixed-Point Conversion
>> get (Hf)
PersistentMemory: 0
NumSamplesProcessed: 0
FilterStructure: 'Direct-Form FIR'
States: [47x1 embedded.fi]
Numerator: [1x48 double]
Arithmetic: 'fixed'
CoeffWordLength: 12
CoeffAutoScale: 1
Signed: 1 >> set (Hf, 'InputWordLength', 12);
RoundMode: 'convergent' >> set (Hf, 'FilterInternals', 'SpecifyPrecision')
OverflowMode: 'wrap' >> set (Hf, 'ProductWordLength', 24)
InputWordLength: 16 >> set (Hf, 'AccumWordLength', 28)
InputFracLength: 15 >> set (Hf, 'OutputWordLength', 16)
NumFracLength: 14
FilterInternals: 'FullPrecision'
OutputWordLength: 31
OutputFracLength: 29
ProductWordLength: 27
ProductFracLength: 29
AccumWordLength: 31
AccumFracLength: 29
Data Type Support
There are three different data types supported in Filter
Design Toolbox:

• Fixed — Requires Fixed Point Toolbox and is supported by


packages listed in Fixed Data Type Support.
• Double — Double precision, floating point and is the default
data type for Filter Design Toolbox; accepted by all functions
• Single — Single precision, floating point and is supported by
specific packages outlined in Single Data Type Support.
Data Type Support
• Fixed Data Type Support
>> f = fdesign.bandpass(.35,.45,.55,.65,60,1,60);
>> Hf = design(f, 'equiripple');
>> Hf.Arithmetic = 'fixed';

• Single Data Type Support

>> f = fdesign.bandpass(.35,.45,.55,.65,60,1,60);
>> Hf = design(f, 'equiripple');
>> Hf.Arithmetic = 'single';
THANK YOU !!!

You might also like