Documente Academic
Documente Profesional
Documente Cultură
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