You are on page 1of 7

# 20/08/2015 Convolution

Back

Convolution
Convolutionisthemostimportantandfundamentalconceptinsignalprocessingandanalysis.Byusingconvolution,wecan
constructtheoutputofsystemforanyarbitraryinputsignal,ifweknowtheimpulseresponseofsystem.

Howisitpossiblethatknowingonlyimpulseresponseofsystemcandeterminetheoutputforanygiveninputsignal?Wewillfindout
themeaningofconvolution.

RelatedTopics:WindowFilters

Definition
ImpulseFunctionDecomposition
ImpulseResponse
BacktotheDefinition
Convolutionin1D
C++ImplementationforConvolution1D
Convolutionin2D
SeparableConvolution2D
C++AlgorithmforConvolution2D

Definition
First,let'sseethemathematicaldefinitionofconvolutionindiscretetimedomain.Laterwewillwalkthroughwhatthisequationtells
us.
(Wewilldiscussindiscretetimedomainonly.)

wherex[n]isinputsignal,h[n]isimpulseresponse,andy[n]isoutput.*denotesconvolution.Noticethatwemultiplythetermsofx[k]

Thekeystoneofunderstandingconvolutionislaidbehindimpulseresponseandimpulsedecomposition.

ImpulseFunctionDecomposition
Inordertounderstandthemeaningofconvolution,wearegoingtostartfromtheconceptofsignaldecomposition.Theinputsignal
componentspassedthroughthesystem.

Ingeneral,asignalcanbedecomposedasaweightedsumofbasissignals.Forexample,inFourierSeries,anyperiodicsignal
(evenrectangularpulsesignal)canberepresentedbyasumofsineandcosinefunctions.Buthere,weuseimpulse(delta)functions

Examinethefollowingexamplehowasignalisdecomposedintoasetofimpulse(delta)functions.Sincetheimpulsefunction,[n]
is1atn=0,andzerosatn0.x[0]canbewrittento2[n].And,x[1]willbe3[n1],because[n1]is1atn=1andzerosatothers.In
andscaledimpulsefunctions.

Ingeneral,asignalcanbewrittenassumofscaledandshifteddeltafunctions

ImpulseResponse
Impulseresponseistheoutputofasystemresultingfromanimpulsefunctionasinput.
Anditisdenotedash[n].

http://www.songho.ca/dsp/convolution/convolution.html 1/7
20/08/2015 Convolution

Ifthesystemistimeinvariant,theresponseofatimeshiftedimpulsefunctionisalsoshiftedassameamountoftime.

Forexample,theimpulseresponseof[n1]ish[n1].Ifweknowtheimpulseresponseh[n],thenwecanimmediatelygettheimpulse
responseh[n1]byshiftingh[n]by+1.Consequently,h[n2]resultsfromshiftingh[n]by+2.

Ifthesystemislinear(especiallyscalarrule),ascaledininputsignalcausesanidenticalscalingintheoutputsignal.

Forinstance,theimpulseresponseof3[n]isjustmultiplyingby3toh[n].

Iftheinputhas3components,forexample,a[n]+b[n1]+c[n2],thentheoutputissimplyah[n]+bh[n1]+ch[n2].Thisiscalled

BacktotheDefinition
Bycombiningthepropertiesofimpulseresponseandimpulsedecomposition,wecanfinallyconstructtheequationofconvolution.In
linearandtimeinvariantsystem,theresponseresultingfromseveralinputscanbecomputedasthesumoftheresponseseachinput
actingalone.

Forexample,ifinputsignalisx[n]=2[n]+3[n1]+1[n2],thentheoutputissimplyy[n]=2h[n]+3h[n1]+1h[n2].

Therefore,wenowclearlyseethatiftheinputsignalis ,thentheoutputwillbe

.Noteoneconditionconvolutionworksonthelinearandtimeinvariantsystem.

shiftedimpulseresponses.

impulseresponseofthesystem,h[n].Ifweknowasystem'simpulseresponse,thenwecaneasilyfindouthowthesystemreactsfor
anyinputsignal.

Convolutionin1D
Let'sstartwithanexampleofconvolutionof1dimensionalsignal,thenfindouthowtoimplementintocomputerprogramming
algorithm.

x[n]={3,4,5}
h[n]={2,1}

x[n]hasonlynonzerovaluesatn=0,1,2,andimpulseresponse,h[n]isnotzeroatn=0,1.Otherswhicharenotlistedareallzeros.

http://www.songho.ca/dsp/convolution/convolution.html 2/7
20/08/2015 Convolution
Input:x[n] ImpulseResponse:h[n]
Onethingtonotebeforewemoveon:Trytofigureoutyourselfhowthissystembehaves,byonlylookingattheimpulseresponseof
thesystem.Whentheimpulsesignalisenteredthesystem,theoutputofthesystemlookslikeamplifierandechoing.Atthetimeis

Fromtheequationofconvolution,theoutputsignaly[n]willbe .
Let'scomputemanuallyeachvalueofy[0],y[1],y[2],y[3],...

Noticethaty[0]hasonlyonecomponent,x[0]h[0],andothersareomitted,becauseothersareall
zerosatk0.Inotherwords,x[1]h[1]=x[2]h[2]=0.Theaboveequationsalsoomitthetermsif
theyareobviouslyzeros.

these4signals(y[0],y[1],y[2],y[3])producestheoutputsignaly[n]={6,11,14,5}.

Output:y[n]
andthefirsttermgoestothelast.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]iscalculatedfrom2inputsamplesx[2]andx[1],and2impulseresponsesamplesh[0]
andh[1].Theinputsamplestartsfrom2,whichissameasthesamplepointofoutput,anddecreased.Theimpulseresponsesample
startsfrom0andincreased.

Basedonthepatternthatwefound,wecanwriteanequationforanysampleoftheoutput

whereiisanysamplepointandkisthenumberofsamplesinimpulseresponse.

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:

C++ImplementationforConvolution1D
Implementingtheconvolutionalgorithmisquitesimple.Thecodesnippetisfollowing

http://www.songho.ca/dsp/convolution/convolution.html 3/7
20/08/2015 Convolution

for(i=0;i<sampleCount;i++)
{
y[i]=0;//settozerobeforesum
for(j=0;j<kernelCount;j++)
{
y[i]+=x[ij]*h[j];//convolve:multiplyandaccumulate
}
}

However,youshouldconcernseveralthingsintheimplementation.
forthoseundefinedsamples,orsimplyskiptheconvolutionattheboundary.Theresultsattheboththebeginningandendedges
cannotbeaccurateanyway.

Second,youneedroundingtheoutputvalues,iftheoutputdatatypeisintegerandimpulseresponseisfloatingpointnumber.And,
theoutputvaluemaybeexceededthemaximumorminimumvalue.
Ifyouhaveunsigned8bitintegerdatatypeforoutputsignal,therangeofoutputshouldbebetween0and255.Youmustcheckthe
valueisgreaterthantheminimumandlessthanthemaximumvalue.

conv1d.zip

Convolutionin2D
2Dconvolutionisjustextensionofprevious1Dconvolutionbyconvolvingbothhorizontalandverticaldirectionsin2dimensional
spatialdomain.Convolutionisfrequentlyusedforimageprocessing,suchassmoothing,sharpening,andedgedetectionofimages.

Theimpulse(delta)functionisalsoin2Dspace,so[m,n]has1wheremandniszeroandzerosatm,n0.Theimpulseresponse
in2Disusuallycalled"kernel"or"filter"inimageprocessing.

Onceagain,asignalcanbedecomposedintoasumofscaledandshiftedimpulse(delta)functions

Forexample,x[0,0]isx[0,0][m,n],x[1,2]isx[1,2][m1,n2],andsoon.Notethatthematricesarereferencedhereas[column,
row],not[row,column].Mishorizontal(column)directionandNisvertical(row)direction.

And,theoutputoflinearandtimeinvariantsystemcanbewrittenbyconvolutionofinputsignalx[m,n],andimpulseresponse,h[m,
n]

Noticethatthekernel(impulseresponse)in2Discenteroriginatedinmostcases,whichmeansthecenterpointofakernelish[0,0].
Forexample,ifthekernelsizeis5,thenthearrayindexof5elementswillbe2,1,0,1,and2.Theoriginislocatedatthemiddle
ofkernel.

Examineanexampletoclarifyhowtoconvolvein2Dspace.
Let'ssaythatthesizeofimpulseresponse(kernel)is3x3,andit'svaluesarea,b,c,d,...

Noticetheorigin(0,0)islocatedinthecenterofkernel.

Let'spickasimplestsampleandcomputeconvolution,forinstance,theoutputat(1,1)willbe

http://www.songho.ca/dsp/convolution/convolution.html 4/7
20/08/2015 Convolution

Itresultsinsumof9elementsofscaledandshiftedimpulseresponses.Thefollowingimageshowsthegraphicalrepresentationof
2Dconvolution.

Noticethatthekernelmatrixisflippedbothhorizontalandvertical
directionbeforemultiplyingtheoverlappedinputdata,becausex[0,0]
ismultipliedbythelastsampleofimpulseresponse,h[1,1].Andx[2,2]
ismultipliedbythefirstsample,h[1,1].

2DConvolution

Input Kernel Output
ThecompletesolutionforthisexampleishereExampleof2DConvolution

Bytheway,thekernelinthisexampleiscalledSobelfilter,whichisusedtodetectthehorizontaledgelinesinanimage.Seemore
detailsinthewindowfilters.

SeparableConvolution2D
Inconvolution2DwithMNkernel,itrequiresMNmultiplicationsforeachsample.Forexample,ifthekernelsizeis3x3,then,9
multiplicationsandaccumulationsarenecessaryforeachsample.Thus,convolution2Disveryexpensivetoperformmultiplyand
accumulateoperation.

However,ifthekernelisseparable,thenthecomputationcanbereducedtoM+Nmultiplications.

Amatrixisseparableifitcanbedecomposedinto(M1)and(1N)matrices.
Forexample

And,convolutionwiththisseparablekernelisequivalentto

(ProofofSeparableConvolution2D)

andM1kernelinverticaldirection,thenconvolveagainhorizontaldirectionwiththeresultfromthepreviousconvolutionand1N
kernel.Thefirstvertical1DconvolutionrequiresMtimesofmultiplicationsandthehorizontalconvolutionneedsNtimesof

http://www.songho.ca/dsp/convolution/convolution.html 5/7
20/08/2015 Convolution
multiplications,altogether,M+Nproducts.

vertical1Dconvolutionfirst,youmustpreservetheresultsinatemporarybufferinordertousethemforhorizontalconvolution
subsequently.

Noticethatconvolutionisassociativetheresultissame,eveniftheorderofconvolutionischanged.So,youmayconvolve
horizontaldirectionfirstthenverticaldirectionlater.

Gaussiansmoothingfilterisawellknownseparablematrix.Forexample,3x3Gaussianfilteris

3x3GaussianKernel

C++AlgorithmforConvolution2D

//findcenterpositionofkernel(halfofkernelsize)
kCenterX=kCols/2;
kCenterY=kRows/2;

for(i=0;i<rows;++i)//rows
{
for(j=0;j<cols;++j)//columns
{
for(m=0;m<kRows;++m)//kernelrows
{
mm=kRows1m;//rowindexofflippedkernel

for(n=0;n<kCols;++n)//kernelcolumns
{
nn=kCols1n;//columnindexofflippedkernel

//indexofinputsignal,usedforcheckingboundary
ii=i+(mkCenterY);
jj=j+(nkCenterX);

//ignoreinputsampleswhichareoutofbound
if(ii>=0&&ii<rows&&jj>=0&&jj<cols)
out[i][j]+=in[ii][jj]*kernel[mm][nn];
}
}
}
}

Theabovesnippetcodeissimpleandeasiestwaytounderstandhowconvolutionworksin2D.Butitmaybetheslowest
implementation.

Takealookatarealexampleconvolutionwith256x256imageand5x5Gaussianfilter.

http://www.songho.ca/dsp/convolution/convolution.html 6/7
20/08/2015 Convolution

Thesourceimageisuncompressedraw,8bit(unsignedchar)grayscaleimage.Andagain,Gaussiankernelisseparable

5x5GaussianKernel