Professional Documents
Culture Documents
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
0.7
n=-5:5; 0.6
0.5
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
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
0.8
n=-15:15; 0.7
stem(n,x) 0.5
0.4
0.3
• 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
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
• square 0.8
0.6
To generate 1.5 s of a 50 Hz sawtooth wave with a 0.4
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
0.5
x = square(2*pi*50*t); 0
-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
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]
-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
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
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
0
-15 -10 -5 0 5 10 15
Difference Equations
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.
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
subplot( 2 , 1, 2 ); -0.1
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
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
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
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
Phase plot
0.6
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
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,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
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= 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
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).
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
Let
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);
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
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.
Cont..
Touch-Tone Dialing
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.
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
[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
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:
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
-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
-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
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
-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.
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:
-10
-20
Magnitude (dB)
-30
-40
-50
-60
-70
-10
-20
Magnitude (dB)
-30
-40
-50
-60
-70
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
-10
-20
Magnitude (dB)
-30
-40
-50
-60
-70
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
-10
-20
-30
-40
Magnitude (dB)
-50
-60
-70
-80
-90
-100
-10
-20
Magnitude (dB)
-30
-40
-50
-60
-70
-10
-20
-30
Magnitude (dB)
-50
-60
-70
-80
-10
-20
Magnitude (dB)
-30
-40
-60
-70
>> f = fdesign.bandpass(.35,.45,.55,.65,60,1,60);
>> Hf = design(f, 'equiripple');
>> Hf.Arithmetic = 'single';
THANK YOU !!!