Top Banner
Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52] United States Contact Us Store igor g My Account Log Out Products & Services Solutions Academia Support User Community Events Company Trial Software Product Updates Share Documentation Center Filtering Data With Signal Processing Toolbox Software On this page… Lowpass FIR Filter — Window Method Lowpass FIR Filter with FDATool Bandpass Filters — Minimum-Order FIR and IIR Systems Zero-Phase Filtering Lowpass FIR Filter — Window Method This example shows how to design and implement an FIR filter using the command line functions: fir1 and fdesign.lowpass , and the interactive tool fdatool. Create a signal to use in the examples. The signal is a 100-Hz sinewave in additive N(0,1/4) white Gaussian noise. Set the random number generator to the default state for reproducible results. rng default; Fs = 1000; t = linspace(0,1,Fs); x = cos(2*pi*100*t)+0.5*randn(size(t)); The filter design is an FIR lowpass filter with order equal to 20 and a cutoff frequency of 150 Hz. Use a Kasier window with length one sample greater than the filter order and β=3. See kaiser for details on the Kaiser window. Use fir1 to design the filter. fir1 requires normalized frequencies in the interval [0,1], where 1 is (1)π radians/sample. To use fir1, you must convert all frequency specifications to normalized frequencies. Signal Processing Toolbox Getting Started with Signal Processing Toolbox Accelerating the pace of engineering and science
12

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Nov 29, 2015

Download

Documents

Igor Gjorgjiev

Filtering Data
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

United States Contact Us Store

igor g My Account Log Out

Products & Services Solutions Academia Support User Community Events Company

Trial Software Product Updates Share

Documentation Center

Filtering Data With Signal Processing Toolbox Software

On this page…

Lowpass FIR Filter — Window Method

Lowpass FIR Filter with FDATool

Bandpass Filters — Minimum-Order FIR and IIR Systems

Zero-Phase Filtering

Lowpass FIR Filter — Window MethodThis example shows how to design and implement an FIR filter using the command line functions: fir1 and fdesign.lowpass, and theinteractive tool fdatool.

Create a signal to use in the examples. The signal is a 100-Hz sinewave in additive N(0,1/4) white Gaussian noise. Set the random numbergenerator to the default state for reproducible results.

rng default;Fs = 1000;t = linspace(0,1,Fs);x = cos(2*pi*100*t)+0.5*randn(size(t));

The filter design is an FIR lowpass filter with order equal to 20 and a cutoff frequency of 150 Hz. Use a Kasier window with length onesample greater than the filter order and β=3. See kaiser for details on the Kaiser window.

Use fir1 to design the filter. fir1 requires normalized frequencies in the interval [0,1], where 1 is (1)π radians/sample. To use fir1, youmust convert all frequency specifications to normalized frequencies.

Signal Processing Toolbox Getting Started with Signal Processing Toolbox

Accelerating the pace of engineering and science

Page 2: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

Design the filter and view the filter's magnitude response.

fc = 150;Wn = (2/Fs)*fc;b = fir1(20,Wn,'low',kaiser(21,3));fvtool(b,1,'Fs',Fs);

Apply the filter to the signal and plot the result for the first ten periods of the 100-Hz sinusoid.

y = filter(b,1,x);plot(t(1:100),x(1:100),'k');hold on;plot(t(1:100),y(1:100),'r','linewidth',2);legend('Original Signal','Filtered Data','Location','SouthEast');xlabel('Seconds'); ylabel('Amplitude');

Design the same filter using fdesign.lowpass. Set the filter specifications using the 'N,Fc' specification string. Withfdesign.lowpass, you can specify your filter design in Hz.

Fs = 1000;d = fdesign.lowpass('N,Fc',20,150,Fs);Hd = design(d,'window','Window',kaiser(21,3));

Filter the data and plot the result.

y1 = filter(Hd,x);

Page 3: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

plot(t(1:100),x(1:100),'k');hold on;plot(t(1:100),y1(1:100),'r','linewidth',2);legend('Original Signal','Filtered Data','Location','SouthEast');xlabel('Seconds'); ylabel('Amplitude');

Lowpass FIR Filter with FDAToolThis example shows how to design and implement a lowpass FIR filter using the window method with the interactive tool fdatool.

Start FDATool by entering

fdatool

Set the Response Type to Lowpass. Set the Design Method to FIR and select the Window method.

Under Filter Order, select Specify order. Set the order to 20.

Under Frequency Specifications. Set Units to Hz, Fs: to 1000, and Fc: to 150.

Click Design Filter.

Select File —>Export... to export your FIR filter to the MATLAB® workspace as coefficients or a filter object. In this example, export the filteras an object. Specify the variable name as Hd1.

Page 4: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

Click Export.

Filter the input signal in the command window with the exported filter object. Plot the result for the first ten periods of the 100-Hz sinusoid.

y2 = filter(Hd1,x);plot(t(1:100),x(1:100),'k');hold on;plot(t(1:100),y1(1:100),'r','linewidth',2);legend('Original Signal','Filtered Data','Location','SouthEast');xlabel('Seconds'); ylabel('Amplitude');

Select File —> Generate MATLAB Code to generate a MATLAB function to create a filter object using your specifications.

You can also use the interactive tool filterbuilder to design your filter.

Bandpass Filters — Minimum-Order FIR and IIR SystemsThis example shows how to design a bandpass filter and filter data with minimum-order FIR equiripple and IIR Butterworth filters. Theexample uses fdesign.bandpass . You can model many real-world signals as a superposition of oscillating components, a low-frequencytrend, and additive noise. For example, economic data often contain oscillations, which represent cycles superimposed on a slowly varyingupward or downward trend. In addition, there is an additive noise component, which is a combination of measurement error and the inherentrandom fluctuations in the process.

In these examples, assume you sample some process every day for 1 year. Assume the process has oscillations on approximately one-week and one-month scales. In addition, there is a low-frequency upward trend in the data and additive N(0,1/4) white Gaussian noise.

Create the signal as a superposition of two sine waves with frequencies of 1/7 and 1/30 cycles/day. Add a low-frequency increasing trendterm and N(0,1/4) white Gaussian noise. Set the random number generator to the default state for reproducible results. The data is sampledat 1 sample/day. Plot the resulting signal and the power spectral density (PSD) estimate.

rng default;

Page 5: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

Fs =1;n = 1:365;x = cos(2*pi*(1/7)*n)+cos(2*pi*(1/30)*n-pi/4);trend = 3*sin(2*pi*(1/1480)*n);y = x+trend+0.5*randn(size(n));subplot(211);plot(n,y); xlabel('Days'); set(gca,'xlim',[1 365]);grid on;subplot(212);[pxx,f] = periodogram(y,[],length(y),Fs);plot(f,10*log10(pxx));xlabel('Cycles/day'); ylabel('dB'); grid on;

The low-frequency trend appears in the power spectral density estimate as increased low-frequency power. The low-frequency powerappears approximately 10 dB above the oscillation at 1/30 cycles/day. Use this information in the specifications for the filter stopbands.

Design minimum-order FIR equiripple and IIR Butterworth filters with the following specifications: passband from [1/40,1/4] cycles/day andstopbands from [0,1/60] and [1/4,1/2] cycles/day. Set both stopband attenuations to 10 dB and the passband ripple tolerance to 1 dB.

d = fdesign.bandpass('Fst1,Fp1,Fp2,Fst2,Ast1,Ap,Ast2',... 1/60,1/40,1/4,1/2,10,1,10,1);Hd1 = design(d,'equiripple');Hd2 = design(d,'butter');

Page 6: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

Compare the order of the FIR and IIR filters and the unwrapped phase responses.

fprintf('The order of the FIR filter is %d\n',length(Hd1.Numerator)-1);[b,a] = sos2tf(Hd2.sosMatrix,Hd2.ScaleValues);fprintf('The order of the IIR filter is %d\n',length(max(b,a))-1);[phifir,w] = phasez(Hd1,[],1);[phiiir,w] = phasez(Hd2,[],1);plot(w,unwrap(phifir),'b'); hold on;plot(w,unwrap(phiiir),'r'); grid on;xlabel('Cycles/Day'); ylabel('Radians');legend('FIR Equiripple Filter','IIR Butterworth Filter');

The IIR filter has a much lower order that the FIR filter. However, the FIR filter has a linear phase response over the passband, while the IIRfilter does not. The FIR filter delays all frequencies in the filter passband equally, while the IIR filter does not.

Additionally, the rate of change of the phase per unit of frequency is greater in the FIR filter than in the IIR filter.

Design a lowpass FIR equiripple filter for comparison. The lowpass filter specifications are: passband [0,1/4] cycles/day, stopbandattenuation equal to 10 dB, and the passband ripple tolerance set to 1 dB.

dlow = fdesign.lowpass('Fp,Fst,Ap,Ast',1/4,1/2,1,10,1);Hdlow = design(dlow,'equiripple');

Page 7: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

Filter the data with the bandpass and lowpass filters.

yfir = filter(Hd1,y);yiir = filter(Hd2,y);ylow = filter(Hdlow,y);

Plot the PSD estimate of the bandpass IIR filter output. You can replace yiir with yfir in the following code to view the PSD estimate ofthe FIR bandpass filter output.

[pxx,f] = periodogram(yiir,[],length(yiir),Fs);plot(f,10*log10(pxx));xlabel('Cycles/day'); ylabel('dB'); grid on;

The PSD estimate shows the bandpass filter attenuates the low-frequency trend and high-frequency noise.

Plot the first 120 days of FIR and IIR filter output.

plot(n(1:120),yfir(1:120),'b');hold on;plot(n(1:120),yiir(1:120),'r');xlabel('Days'); axis([1 120 -2.8 2.8]);legend('FIR bandpass filter output','IIR bandpass filter output',... 'Location','SouthEast');

Page 8: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

The increased phase delay in the FIR filter is evident in the filter output.

Plot the lowpass FIR filter output superimposed on the superposition of the 7-day and 30-day cycles for comparison.

plot(n,x,'k');hold on;plot(n,ylow,'r'); set(gca,'xlim',[1 365]);legend('7-day and 30-day cycles','FIR lowpass filter output',... 'Location','NorthWest');xlabel('Days');

Page 9: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

You can see in the preceding plot that the low-frequency trend is evident in the lowpass filter output. While the lowpass filter preserves the 7-day and 30-day cycles, the bandpass filters perform better in this example because the bandpass filters also remove the low-frequencytrend.

Zero-Phase FilteringThis example shows how to perform zero-phase filtering.

Repeat the signal generation and lowpass filter design with fir1 and fdesign.lowpass. You do not have to execute the following code ifyou already have these variables in your workspace.

rng default;Fs = 1000;t = linspace(0,1,Fs);x = cos(2*pi*100*t)+0.5*randn(size(t));% Using fir1fc = 150;Wn = (2/Fs)*fc;b = fir1(20,Wn,'low',kaiser(21,3));% Using fdesign.lowpassd = fdesign.lowpass('N,Fc',20,150,Fs);Hd = design(d,'window','Window',kaiser(21,3));

Page 10: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

Filter the data using filter. Plot the first 100 points of the filter output along with a superimposed sinusoid with the same amplitude andinitial phase as the input signal.

yout = filter(Hd,x);xin = cos(2*pi*100*t);plot(t(1:100),xin(1:100),'k');hold on; grid on;plot(t(1:100),yout(1:100),'r','linewidth',2);xlabel('Seconds'); ylabel('Amplitude');legend('Input Sine Wave','Filtered Data',... 'Location','NorthEast');

Looking at the initial 0.01 seconds of the filtered data, you see that the output is delayed with respect to the input. The delay appears to beapproximately 0.01 seconds, which is almost 1/2 the length of the FIR filter in samples (10*0.001).

This delay is due to the filter's phase response. The FIR filter in these examples is a type I linear-phase filter. The group delay of the filter is10 samples.

lot the group delay using fvtool.

fvtool(Hd,'analysis','grpdelay');

In many applications, phase distortion is acceptable. This is particularly true when phase response is linear. In other applications, it isdesirable to have a filter with a zero-phase response. A zero-phase response is not technically possibly in a noncausal filter. However, youcan implement zero-phase filtering using a causal filter with filtfilt.

Page 11: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

Filter the input signal using filtfilt. Plot the responses to compare the filter outputs obtained with filter and filtfilt

yzp = filtfilt(Hd.Numerator,1,x);% or yzp = filtfilt(b,1,x);plot(t(1:100),xin(1:100),'k');hold on;plot(t(1:100),yout(1:100),'r','linewidth',2);plot(t(1:100),yzp(1:100),'b','linewidth',2);xlabel('Seconds'); ylabel('Amplitude');legend('100-Hz Sine Wave','Filtered Signal','Zero-phase Filtering',... 'Location','NorthEast');

In the preceding figure, you can see that the output of filtfilt does not exhibit the delay due to the phase response of the FIR filter.

Was this topic helpful?

Try MATLAB, Simulink, and Other ProductsGet trial now

Page 12: Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink.pdf

Filtering Data With Signal Processing Toolbox Software - MATLAB & Simulink

http://www.mathworks.com/help/signal/ug/filtering-data-with-signal-processing-toolbox.html[08/11/2013 14:37:52]

Join the conversation

Preventing PiracyPrivacy PolicyTrademarksPatentsSite Help© 1994-2013 The MathWorks, Inc.