Signal Processing in Matlab
Signal Processing in Matlab
WITH MATLAB
Presented by:
Dr. Ramesh Babu. N
SELECT
VIT University, Vellore
Filtering signals
% step
% impulse
% ramp
% exponential
% generates a square wave every 0.25secs.
Waveform generation
y = sin(2*pi*50*t) + 2*sin(2*pi*120*t); %two sinusoids,
%one at 50 Hz
%and one at
%120Hz with
%twice the amplitude
plot(t,y)
plot(t(1:50),y(1:50))
Cont.
x = [1 2 1];
h = [1 1 1];
y = conv(h,x);
stem(y)
H(z)
where H(z) is the filter's transfer function. Here, the constants
b(i) and a(i) are the filter coefficients and the order of the filter is
the maximum of n and m.
Cont.
For example let;
1
H ( z)
1
1 0.9 z
step = ones(50);
%input data : step function
b = 1;
% Numerator
a = [1 -0.9]; % Denominator
where the vectors b and a represent the coefficients of a filter in transfer
function form. To apply this filter to your data, use
y = filter(b,a,step);
stem(y)
Filter Visualization Tool
fvtool(b,a) %GUI.Dont have to define input (if input is
%step/impulse function)%
Many standard names for filters reflect the number of a and b coefficients
present:
When n = 0 (that is, b is a scalar), the filter is an Infinite Impulse
Response (IIR), all-pole, recursive, or autoregressive (AR) filter.
When m = 0 (that is, a is a scalar), the filter is a Finite Impulse
Response (FIR), all-zero, nonrecursive, or moving-average (MA)
filter.
If both n and m are greater than zero, the filter is an IIR, pole-zero,
recursive, or autoregressive moving-average (ARMA) filter.
Design Function
[b,a] = besself(n,Wn,options)
[z,p,k] = besself(n,Wn,options)
[A,B,C,D] = besself(n,Wn,options)
Butterworth
[b,a] = butter(n,Wn,options)
[z,p,k] = butter(n,Wn,options)
[A,B,C,D] = butter(n,Wn,options)
Chebyshev Type I
[b,a] = cheby1(n,Rp,Wn,options)
[z,p,k] = cheby1(n,Rp,Wn,options)
[A,B,C,D] = cheby1(n,Rp,Wn,options)
Chebyshev Type II
[b,a] = cheby2(n,Rs,Wn,options)
[z,p,k] = cheby2(n,Rs,Wn,options)
[A,B,C,D] = cheby2(n,Rs,Wn,options)
Elliptic
[b,a] = ellip(n,Rp,Rs,Wn,options)
[z,p,k] = ellip(n,Rp,Rs,Wn,options)
[A,B,C,D] = ellip(n,Rp,Rs,Wn,options)
Cont
Example 1:
For data sampled at 1000 Hz, design a 9th-order highpass
Butterworth IIR filter with cutoff frequency of 300 Hz,
Solution:
9th order IIR filter
[b,a] = butter(9,300/500,'high');
freqz(b,a,128,1000)
Highpass
filter
Cont.
Example 2:
For data sampled at 1000 Hz, design a 9th-order lowpass
Chebyshev Type I filter with 0.5 dB of ripple in the
passband and a cutoff frequency of 300 Hz, which
corresponds to a normalized value of 0.6:
Solution
[b,a] = cheby1(9,0.5,300/500);
freqz(b,a,512,1000)%The frequency response of the filter
Method
Description
Function
s
Windowing
fir1, fir2,
kaiserord
Multiband with
Transition
Bands
firls,
remez,
remezord
Constrained
Least Squares
fircls,
fircls1
Arbitrary
Response
cremez
Raised Cosine
firrcos
Cont.
Example 1
Design a 48th-order FIR bandpass filter with passband 0.35
0.65:
Solution
b = fir1(48,[0.35 0.65]);
freqz(b,1,512)
Cont.
Example 2:
Design a lowpass filter with the following specifications using
the optimal design method :
rp = 0.01;
rs = 0.1;
fs = 8000;
f = [1500 2000];
a = [1 0];
% Passband ripple
% Stopband ripple
% Sampling frequency
% Cutoff frequencies
% Desired amplitudes
Cont.
Solution
a vector of maximum
deviations or
ripples allowable
for each band.
%[n,fo,ao,w] = remezord(f,a,dev,fs); %
dev=[0.01 0.1]
[n,fo,ao,w]=remezord([1500 2000],[1 0],dev,8000); %
approximate order, normalized frequency band edges, frequency band amplitudes, and
weights that meet input specifications f, a, and dev.%
b=remez(n,fo,ao,w);
freqz(b,1,1024,8000);
title('Lowpass Filter Designed to Specifications');
Filter Implementation
After the filter design process has generated the filter coefficient
vectors, b and a, two functions are available in the Signal Processing
Toolbox for implementing the filter:
filter-:for b and a coefficient input, implements a direct-form II
transposed structure and filters the data. For dfilt input, filter
uses the structure specified with dfilt and filters the data.
Dfilt-:let us specify the filter structure and creates a digital filter
object.
Cont.
Example using filter
t = (0:0.001:1);
%fs=1/0.001Hz
x = sin(2*pi*50*t) + 2*sin(2*pi*120*t); %define input
plot(t,x)
%plot input
[b,a] = butter(9,100/500,'high');
%9th order,high pass filter
%with cutoff freq. 100Hz
c=filter(b,a,x);
figure(2)
plot(t,c)
Cont.
Cont.
Example using dfilt
t = (0:0.001:1);
x = sin(2*pi*50*t) + 2*sin(2*pi*120*t); %define input
plot(t,x)
[b,a] = butter(9,100/500,'high');
%design filter
Hd = dfilt.df2t(b,a);
%Implement direct%form II transposed
c=filter(Hd,x);
returns a discrete-time
figure(2)
filter object, Hd, of type
plot(t,c)
df2t(direct-form II
transposed
Cont.
or, in terms of the cross-correlation,
Cont.
Example on xcorr
x = [1 1 1 1 1]';
y = x;
xyc = xcorr(x,y)
stem(xyc)
Example on xcov
ww = randn(1000,1); % Generate uniform noise with mean = 1/2.%
[cov_ww,lags] = xcov(ww,10,'coeff');
stem(lags,cov_ww)
Cont.
t = (0:0.001:1);
%0.001 is sampling
x = sin(2*pi*50*t) + 2*sin(2*pi*120*t);
y = fft(x);
%Compute DFT of x
m = abs(y);
%magnitude
f = (0:length(y)/2-1)*1000/length(y); %Frequency vector
plot(f,m(1:1:(length(m)-1)/2))
%before filter
grid
[b,a] = butter(9,100/500,'high');
%design filter
c=filter(b,a,x);
%implement filter
figure(2)
y = fft(c);
%Compute DFT of c(filtered x)
m = abs(y);
% Magnitude
f = (0:length(y)/2-1)*1000/length(y); %Frequency vector
plot(f,m(1:1:(length(m)-1)/2))
%after filter
Grid
Opening FDATool
To open the Filter Design and Analysis Tool, type
fdatool
Filter order
Options
Bandpass Filter Frequency Specifications
Bandpass Filter Magnitude Specifications
Now that you've specified the filter design, click the Design Filter button to
compute the filter coefficients .
References
1. Tood K. Moon, Wynn C. Stirling
Mathematical Methods and Algorithms
for Signal Processing,Prentice
Hall,2000.
2. Samuel D. Stearns, Ruth A. David
Signal Processing Algorithms In
Matlab, Prentice Hall,1996.
3. http://www.mathworks.com.
THANK YOU