Convolution.docx
《Convolution.docx》由会员分享,可在线阅读,更多相关《Convolution.docx(15页珍藏版)》请在冰豆网上搜索。
Convolution
Convolution
Convolutionisthemostimportantandfundamentalconceptinsignalprocessingandanalysis.Byusingconvolution,wecanconstructtheoutputofsystemforanyarbitraryinputsignal,ifweknowtheimpulseresponseofsystem.
Howisitpossiblethatknowingonlyimpulseresponseofsystemcandeterminetheoutputforanygiveninputsignal?
Wewillfindoutthemeaningofconvolution.
RelatedTopics:
WindowFilters
Download:
conv1d.zip, conv2d.zip
∙Definition
∙ImpulseFunctionDecomposition
∙ImpulseResponse
∙BacktotheDefinition
∙Convolutionin1D
∙C++ImplementationforConvolution1D
∙Convolutionin2D
∙SeparableConvolution2D
∙C++AlgorithmforConvolution2D
Definition
First,let'sseethemathematicaldefinitionofconvolutionindiscretetimedomain.Laterwewillwalkthroughwhatthisequationtellsus.
(Wewilldiscussindiscretetimedomainonly.)
where x[n] isinputsignal, h[n] isimpulseresponse,and y[n] isoutput.*denotesconvolution.Noticethatwemultiplythetermsof x[k] bythetermsofatime-shifted h[n] andaddthemup.
Thekeystoneofunderstandingconvolutionislaidbehindimpulseresponseandimpulsedecomposition.
ImpulseFunctionDecomposition
Inordertounderstandthemeaningofconvolution,wearegoingtostartfromtheconceptofsignaldecomposition.Theinputsignalisdecomposedintosimpleadditivecomponents,andthesystemresponseoftheinputsignalresultsinbyaddingtheoutputofthesecomponentspassedthroughthesystem.
Ingeneral,asignalcanbedecomposedasaweightedsumofbasissignals.Forexample,inFourierSeries,anyperiodicsignal(evenrectangularpulsesignal)canberepresentedbyasumofsineandcosinefunctions.Buthere,weuseimpulse(delta)functionsforthebasissignals,insteadofsineandcosine.
Examinethefollowingexamplehowasignalisdecomposedintoasetofimpulse(delta)functions.Sincetheimpulsefunction, δ[n] is1at n=0,andzerosat n≠0. x[0] canbewrittento 2·δ[n].And, x[1] willbe 3·δ[n-1],because δ[n-1] is1at n=1 andzerosatothers.Insameway,wecanwrite x[2] byshifting δ[n] by2, x[2] =1·δ[n-2].Therefore,thesignal, x[n] canberepresentedbyadding3shiftedandscaledimpulsefunctions.
Ingeneral,asignalcanbewrittenassumofscaledandshifteddeltafunctions;
ImpulseResponse
Impulseresponseistheoutputofasystemresultingfromanimpulsefunctionasinput.
Anditisdenotedas h[n].
Ifthesystemis time-invariant,theresponseofatime-shiftedimpulsefunctionisalsoshiftedassameamountoftime.
Forexample,theimpulseresponseof δ[n-1] is h[n-1].Ifweknowtheimpulseresponse h[n],thenwecanimmediatelygettheimpulseresponse h[n-1] byshifting h[n] by+1.Consequently, h[n-2] resultsfromshifting h[n]by+2.
Ifthesystemis linear (especiallyscalarrule),ascaledininputsignalcausesanidenticalscalingintheoutputsignal.
Forinstance,theimpulseresponseof 3·δ[n] isjustmultiplyingby3to h[n].
Iftheinputhas3components,forexample, a·δ[n]+ b·δ[n-1]+ c·δ[n-2],thentheoutputissimplya·h[n]+ b·h[n-1]+ c·h[n-2].Thisiscalledadditivepropertyof linear system,thus,itisvalidonlyonthelinear system.
BacktotheDefinition
Bycombiningthepropertiesofimpulseresponseandimpulsedecomposition,wecanfinallyconstructtheequationofconvolution.In linear and time-invariantsystem,theresponseresultingfromseveralinputscanbecomputedasthesumoftheresponseseachinputactingalone.
Forexample,ifinputsignalis x[n]=2·δ[n]+3·δ[n-1]+1·δ[n-2],thentheoutputissimply y[n]=2·h[n]+3·h[n-1]+1·h[n-2].
Therefore,wenowclearlyseethatiftheinputsignalis
thentheoutputwillbe
.Noteonecondition;convolutionworksonthe linear and timeinvariantsystem.
Tosummarize,asignalisdecomposedintoasetofimpulsesandtheoutputsignalcanbecomputedbyaddingthescaledandshiftedimpulseresponses.
Furthermore,thereisanimportantfactunderconvolution;theonlythingweneedtoknowaboutthesystem'scharacteristicsistheimpulseresponseofthesystem, h[n].Ifweknowasystem'simpulseresponse,thenwecaneasilyfindouthowthesystemreactsforanyinputsignal.
Convolutionin1D
Let'sstartwithanexampleofconvolutionof1dimensionalsignal,thenfindouthowtoimplementintocomputerprogrammingalgorithm.
x[n]={3,4,5}
h[n]={2,1}
x[n] hasonlynon-zerovaluesat n=0,1,2, andimpulseresponse, h[n] isnotzeroat n=0,1.Otherswhicharenotlistedareallzeros.
Input:
x[n]
ImpulseResponse:
h[n]
Onethingtonotebeforewemoveon:
Trytofigureoutyourselfhowthissystembehaves,byonlylookingattheimpulseresponseofthesystem.Whentheimpulsesignalisenteredthesystem,theoutputofthesystemlookslikeamplifierandechoing.Atthetimeis0,theintensitywasincreased(amplified)bydoubleandgraduallydecreasedwhilethetimeispassed.
Fromtheequationofconvolution,theoutputsignal y[n] willbe
.
Let'scomputemanuallyeachvalueof y[0], y[1], y[2], y[3], ...
Output:
y[n]
Noticethat y[0] hasonlyonecomponent, x[0]h[0],andothersareomitted,becauseothersareallzerosat k≠0.Inotherwords, x[1]h[-1]= x[2]h[-2]=0.Theaboveequationsalsoomitthetermsiftheyareobviouslyzeros.
If n islargerthanandequalto4, y[n] willbezeros.Sowestopheretocompute y[n]andaddingthese4signals(y[0], y[1], y[2], y[3])producestheoutputsignaly[n]={6,11,14,5}.
Let'slookmorecloselytheeachoutput.Inordertoseeapatternclearly,theorderofadditionisswapped.Thelasttermcomesfirstandthefirsttermgoestothelast.And,allzerotermsareignored.
y[0]= x[0]·h[0]
y[1]= x[1]·h[0]+ x[0]·h[1]
y[2]= x[2]·h[0]+ x[1]·h[1]
y[3]= x[3]·h[0]+ x[2]·h[1]
Canyouseethepattern?
Forexample, y[2] iscalculatedfrom2inputsamples; x[2] and x[1],and2impulseresponsesamples; h[0] and h[1].Theinputsamplestartsfrom2,whichissameasthesamplepointofoutput,anddecreased.Theimpulseresponsesamplestartsfrom0andincreased.
Basedonthepatternthatwefound,wecanwriteanequationforanysampleoftheoutput;
where i isanysamplepointandkisthenumberofsamplesinimpulseresponse.
Forinstance,ifanimpulseresponsehas4samples,thesampleofoutputsignalat9is;
y[9]= x[9]·h[0]+ x[8]·h[1]+ x[7]·h[2]+ x[6]·h[3]
Noticethefirstsample, y[0] hasonlyoneterm.Basedonthepatternthatwefound, y[0] iscalculatedas:
y[0]= x[0]·h[0]+ x[-1]·h[1].Because x[-1] isnotdefined,wesimplypadittozero.
C++ImplementationforConvolution1D
Implementingtheconvolutionalgorithmisquitesimple.Thecodesnippetisfollowing;
for(i=0;i{
y[i]=0;//settozerobeforesum
for(j=0;j{
y[i]+=x[i-j]*h[j];//convolve:
multiplyandaccumulate
}
}
However,youshouldconcernseveralthingsintheimplementation.
Watchouttherangeofinputsignal.Youmaybeoutofboundofinputsignal,forexample,x[-1],x[-2],andsoon.Youcanpadzerosforthoseundefinedsamples,orsimplyskiptheconvolutionattheboundary.Theresultsattheboththebeginningandendedgescannotbeaccurateanyway.
Second,youneedroundingtheoutputvalues,iftheoutputdatatypeisintegerandimpulseresponseisfloatingpointnumber.And,theoutputvaluemaybeexceededthemaximumorminimumvalue.
Ifyouhaveunsigned8-bitintegerdatatypeforoutputsignal,therangeofoutputshouldbebetween0and255.Youmustcheckthevalueisgreaterthantheminimumandlessthanthemaximumvalue.
Downloadthe1Dconvolutionroutineandtestprogram.
conv1d.zip
Convolutionin2D
2Dconvolutionisjustextensionofprevious1Dconvolutionbyconvolvingbothhorizontalandverticaldirectionsin2dimensionalspatialdomain.Convolutionisfrequentlyusedforimageprocessing,suchassmoothing,sharpening,andedgedetectionofimages.
Theimpulse(delta)functionisalsoin2Dspace,so δ[m,n] has1where m and n iszeroandzerosat m,n≠0.Theimpulseresponsein2Disusuallycalled"kernel"or"filter"inimageprocessing.
Thesecondimageis2Dmatrixrepresentationofimpulsefunction.Theshadedcenterpointistheoriginwherem=n=0.
Onceagain,asignalcanbedecomposedintoasumofscaledandshiftedimpulse(delta)functions;
Forexample, x[0,0] is x[0,0]·δ[m,n], x[1,2] is x[1,2]·δ[m-1,n-2],andsoon.Notethatthematricesarereferencedhereas[column,row],not[row,column].Mishorizontal(column)directionandNisvertical(row)direction.
And,theoutputof linear and timeinvariantsystem canbewrittenbyconvolutionofinputsignal x[m,n],andimpulseresponse, h[m,n];
Noticethatthekernel(impulseresponse)in2Discenteroriginatedinmostcases,whichmeansthecenterpointofakernelis h[0,0].Forexample,ifthekernelsizeis5,thenthearrayindexof5elementswillbe-2,-1,0,1,and2.Theoriginislocatedatthemiddleofkernel.
Examineanexampletoclarifyhowtoconvolvein2Dspace.
Let'ssaythatthesizeofimpulseresponse(kernel)is3x3,andit'svaluesarea,b,c,d,...
Noticetheorigin(0,0)islocatedinthecenterofkernel.
Let'spickasimplestsampleandcomputeconvolution,forinstance,theoutputat(1,1)willbe;
Itresultsinsumof9elementsofscaledandshiftedimpulseresponses.Thefollowingimageshowsthegraphicalrepresentationof2Dconvolution.
2DConvolution
Noticethatthekernelmatrixisflippedbothhorizontalandverticaldirectionbe