0% found this document useful (0 votes)
394 views9 pages

Practical FIR Filter Design - Part 1 - Design With Octave or Matlab

This document describes designing a finite impulse response (FIR) filter using Octave or Matlab. It provides an equation to calculate a starting filter length based on desired specifications. The document then shows code to design a lowpass FIR filter with a 10kHz passband and 15kHz cutoff using Octave's fir1 function. The response is plotted and shows it initially has a smaller bandwidth than specified. The parameters are tweaked and the final filter matches the specifications and is tested on sample input data.

Uploaded by

NorozKhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
394 views9 pages

Practical FIR Filter Design - Part 1 - Design With Octave or Matlab

This document describes designing a finite impulse response (FIR) filter using Octave or Matlab. It provides an equation to calculate a starting filter length based on desired specifications. The document then shows code to design a lowpass FIR filter with a 10kHz passband and 15kHz cutoff using Octave's fir1 function. The response is plotted and shows it initially has a smaller bandwidth than specified. The parameters are tweaked and the final filter matches the specifications and is tested on sample input data.

Uploaded by

NorozKhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 1/9
3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab

PracticalFIRFilterDesign:Part1Designwith
OctaveorMatlab
January24,2016byShanePetcavich(/author/shanepetcavich)

Thistutorialwillfocusondesigningafiniteimpulseresponse(FIR)filter.Asthe
seriesprogresses,itwilldiscussthenecessarystepstoimplementthefilteron
realhardware.

AFIRfilterisadigitalfilterwhoseimpulseresponsesettlestozeroinfinitetime
asopposedtoaninfiniteimpulseresponsefilter(IIR),whichusesfeedbackand
mayrespondindefinitelytoaninputsignal.ThegreatthingaboutFIRfiltersis
thattheyareinherentlystableandcaneasilybedesignedtohavelinearphase.
Iwon'tgetintothedetailsmuchfurtheronFIRfiltersandtheirpro'sandcon's
asthistutorialfocusesmoreondesigningfiltersfastandefficientlywiththeaid
ofOctave(https://www.gnu.org/software/octave/).

Typically,inFIRfilterdesignthelengthofthefilterwillneedtobespecified.You
canguessandcheckuntilthefiltermatchesyourexpectedbandwidthand
cutoffrequirements,butthiscouldbealongandtediousprocess.Theequation
belowisanefficientwaytocomputeareasonablestartinglength.Aftertrying
thecalculatedN,onecanthentweakNorparameterswhichmakeupNto
meetfilterspecifications.


AdB Fs
N
22f

where

AdB isyourstopbandattenuationindB

Fs isyoursamplingfrequency

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 2/9
3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab

and

f isyourtransitionbandwidth

Letssaywewanttofilteranaudiosignalwiththefollowingcharacteristicsand
desiredfilterresponse:

Thesamplesmaycontainfrequenciesfrom020kHz

Wewishtodesignafilterthatpassesonlyfrequencieslessthan10kHz

Wewantastopbandattenuationof40dBat15kHz

OursystemhasaFs of192kHz

Withthisinformationwecanbegintheprocessofdesign.Usingtheequation
forNweestimatethefilterlengthtoapproximatelybe:


(40)(192000H z)
N
22(15kH z 10kH z)

N 69.818

roundingtothenearestoddnumber

N 69

*DesigninganFIRfilterlengthtobeoddlengthwillgivethefilteranintegral
delayof(N1)/2.

UsingtheOctave/Matlabcodebelow,wecanseehowtodesignalowpassfilter
withabandwidthof10kHzandacutoffof15kHzusingOctave'sbuiltinfir1
function,whichiswelldocumentedhere
(http://octave.sourceforge.net/signal/function/fir1.html)

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 3/9
3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab


closeall;
clearall;
clf;

f1=10000;
f2=15000;
delta_f=f2f1;
Fs=192000;
dB=40;
N=dB*Fs/(22*delta_f);

f=[f1]/(Fs/2)
hc=fir1(round(N)1,f,'low')

figure
plot((0.5:1/4096:0.51/4096)*Fs,20*log10(abs(fftshift(fft(hc,4096)))))
axis([0200006020])
title('FilterFrequencyResponse')
gridon

Octave/MatlabCode(/login/?redirect=https://www.allaboutcircuits.com/technicalarticles/d

Thecodeabovegivesusthefollowingresponse:

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 4/9
3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab

(/uploads/articles/lpfrp.png)

Figure1

Butifwezoominwewillseethattheattenuationat10kHzisgreaterthan3dB:

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 5/9
3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab

(/uploads/articles/zoomlpfrp.png)\

Figure2

Thebandwidthofthefilterisalwaysspecifiedtothe3dBpoint,sointhefirst
iterationofdesignourfilterhasasmallerbandwidththanspecified(somewhere
lessthan9kHz).Wecanseefromthefirstfigurethattheattenuationinthe
stopbandexceededourspecifications,perhapswecantweaktheattenuation
andpassbandfrequencytoenhancethedesign.

Tweakingtheparametersinthecodeto

f1=11200
f2=15000
dB=30

givesusafilterwhichcloselymatchesourspeicfications

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 6/9
3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab

(/uploads/articles/lpfrpfixed.png)

Figure3

Thefilterdesignisnowcomplete.Let'ssimulatehowitworksbyaddingthe
codebelowtothefirstbitofcodewelookedat.

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 7/9
3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab


x=sin(2*pi*[1:1000]*5000/Fs)+sin(2*pi*[1:1000]*2000/Fs)+sin(2*pi*[1:1000]*130
00/Fs)+sin(2*pi*[1:1000]*18000/Fs);

sig=20*log10(abs(fftshift(fft(x,4096))));
xf=filter(hc,1,x)

figure
subplot(211)
plot(x)
title('Sinusoidwithfrequencycomponents2000,5000,13000,and18000Hz')

subplot(212)
plot(xf)
title('FilteredSignal')
xlabel('time')
ylabel('amplitude')

x=(x/sum(x))/20
sig=20*log10(abs(fftshift(fft(x,4096))));
xf=filter(hc,1,x)

figure
subplot(211)
plot((0.5:1/4096:0.51/4096)*Fs,sig)
holdon
plot((0.5:1/4096:0.51/4096)*Fs,20*log10(abs(fftshift(fft(hc,4096)))),'color','r')
holdoff
axis([0200006010])
title('Inputtofilter4Sinusoids')
gridon
subplot(212)
plot((0.5:1/4096:0.51/4096)*Fs,20*log10(abs(fftshift(fft(xf,4096)))))
axis([0200006010])
title('Outputfromfilter')
xlabel('Hz')
ylabel('dB')
gridon

MoreMatlab/OctaveCode(/login/?redirect=https://www.allaboutcircuits.com/technicalartic

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 8/9
3/14/2017 PracticalFIRFilterDesign:Part1DesignwithOctaveorMatlab

Figure4

AswecanseeinFigure4,wehavethetimedomainsignalsontheleftandthe
frequencydomainontheright.Thefilter(inred)isoverlaidontotheplotto
showhowthefilterleavesthesinusoidsinthepassbandandattenuatesthe
signalsinthetransitionandstopband.

Thefir1functioncanalsobeusedtoproducenotchfilters,highpassfilters,and
bandpassfiltersbyreplacingtheselines:

f=[f1]/(Fs/2),mayneedtobespecifiedwithtwoargumentsforbandpassand
notchfiltersassuch:

f=[f1f2]/(Fs/2),wheref1istheleft3dBedgeandf2istheright3dBedge

hc=fir1(round(N)1,f,'low')canbemodifiedassuch:

'low'canbereplacedwith'stop'(notch),'high'(highpass),'bandpass'
(bandpass)

https://www.allaboutcircuits.com/technicalarticles/designoffirfiltersdesignoctavematlab/ 9/9

You might also like