1、ConvolutionConvolutionConvolution is the most important and fundamental concept in signal processing and analysis. By using convolution, we can construct the output of system for any arbitrary input signal, if we know the impulse response of system.How is it possible that knowing only impulse respon
2、se of system can determine the output for any given input signal? We will find out the meaning of convolution.Related Topics:Window FiltersDownload:conv1d.zip,conv2d.zip Definition Impulse Function Decomposition Impulse Response Back to the Definition Convolution in 1D C+ Implementation for Convolut
3、ion 1D Convolution in 2D Separable Convolution 2D C+ Algorithm for Convolution 2DDefinitionFirst, lets see the mathematical definition of convolution in discrete time domain. Later we will walk through what this equation tells us.(We will discuss in discrete time domain only.)wherexnis input signal,
4、hnis impulse response, andynis output. * denotes convolution. Notice that we multiply the terms ofxkby the terms of a time-shiftedhnand add them up.The keystone of understanding convolution is laid behind impulse response and impulse decomposition.Impulse Function DecompositionIn order to understand
5、 the meaning of convolution, we are going to start from the concept of signal decomposition. The input signal is decomposed into simple additive components, and the system response of the input signal results in by adding the output of these components passed through the system.In general, a signal
6、can be decomposed as a weighted sum of basis signals. For example, in Fourier Series, any periodic signal (even rectangular pulse signal) can be represented by a sum of sine and cosine functions. But here, we use impulse (delta) functions for the basis signals, instead of sine and cosine.Examine the
7、 following example how a signal is decomposed into a set of impulse (delta) functions. Since the impulse function,nis 1 atn=0, and zeros atn 0.x0can be written to2n. And,x1will be3n-1, becausen-1is 1 atn=1and zeros at others. In same way, we can writex2by shiftingnby 2,x2=1n-2. Therefore, the signal
8、,xncan be represented by adding 3 shifted and scaled impulse functions.In general, a signal can be written as sum of scaled and shifted delta functions;Impulse ResponseImpulse response is the output of a system resulting from an impulse function as input.And it is denoted ashn.If the system istime-i
9、nvariant, the response of a time-shifted impulse function is also shifted as same amount of time.For example, the impulse response ofn-1ishn-1. If we know the impulse responsehn, then we can immediately get the impulse responsehn-1by shiftinghnby +1. Consequently,hn-2results from shiftinghnby +2.If
10、the system islinear(especially scalar rule), a scaled in input signal causes an identical scaling in the output signal.For instance, the impulse response of3nis just multiplying by 3 tohn.If the input has 3 components, for example,an +bn-1 +cn-2, then the output is simplyahn +bhn-1 +chn-2. This is c
11、alled additive property oflinearsystem, thus, it is valid only on thelinearsystem.Back to the DefinitionBy combining the properties of impulse response and impulse decomposition, we can finally construct the equation of convolution. Inlinearandtime-invariant system, the response resulting from sever
12、al inputs can be computed as the sum of the responses each input acting alone.For example, if input signal isxn = 2n + 3n-1 + 1n-2, then the output is simplyyn = 2hn + 3hn-1 + 1hn-2.Therefore, we now clearly see that if the input signal is, then the output will be. Note one condition; convolution wo
13、rks on thelinearandtime invariant system.To summarize, a signal is decomposed into a set of impulses and the output signal can be computed by adding the scaled and shifted impulse responses.Furthermore, there is an important fact under convolution; the only thing we need to know about the systems ch
14、aracteristics is the impulse response of the system,hn. If we know a systems impulse response, then we can easily find out how the system reacts for any input signal.Convolution in 1DLets start with an example of convolution of 1 dimensional signal, then find out how to implement into computer progr
15、amming algorithm.xn = 3, 4, 5 hn = 2, 1 xnhas only non-zero values atn=0,1,2,and impulse response,hnis not zero atn=0,1. Others which are not listed are all zeros.Input:xnImpulse Response:hnOne thing to note before we move on: Try to figure out yourself how this system behaves, by only looking at th
16、e impulse response of the system. When the impulse signal is entered the system, the output of the system looks like amplifier and echoing. At the time is 0, the intensity was increased (amplified) by double and gradually decreased while the time is passed.From the equation of convolution, the outpu
17、t signalynwill be.Lets compute manually each value ofy0,y1,y2,y3,.Output:ynNotice thaty0has only one component,x0h0, and others are omitted, because others are all zeros atk 0. In other words,x1h-1 =x2h-2 = 0. The above equations also omit the terms if they are obviously zeros.Ifnis larger than and
18、equal to 4,ynwill be zeros. So we stop here to computeynand adding these 4 signals (y0,y1,y2,y3) produces the output signalyn = 6, 11, 14, 5.Lets look more closely the each output. In order to see a pattern clearly, the order of addition is swapped. The last term comes first and the first term goes
19、to the last. And, all zero terms are ignored.y0 =x0h0y1 =x1h0 +x0h1y2 =x2h0 +x1h1y3 =x3h0 +x2h1Can you see the pattern? For example,y2is calculated from 2 input samples;x2andx1, and 2 impulse response samples;h0andh1. The input sample starts from 2, which is same as the sample point of output, and d
20、ecreased. The impulse response sample starts from 0 and increased.Based on the pattern that we found, we can write an equation for any sample of the output;whereiis any sample point and k is the number of samples in impulse response.For instance, if an impulse response has 4 samples, the sample of o
21、utput signal at 9 is;y9 =x9h0 +x8h1 +x7h2 +x6h3Notice the first sample,y0has only one term. Based on the pattern that we found,y0is calculated as:y0 =x0h0 +x-1h1. Becausex-1is not defined, we simply pad it to zero.C+ Implementation for Convolution 1DImplementing the convolution algorithm is quite si
22、mple. The code snippet is following;for ( i = 0; i sampleCount; i+ ) yi = 0; / set to zero before sum for ( j = 0; j kernelCount; j+ ) yi += xi - j * hj; / convolve: multiply and accumulate However, you should concern several things in the implementation.Watch out the range of input signal. You may
23、be out of bound of input signal, for example, x-1, x-2, and so on. You can pad zeros for those undefined samples, or simply skip the convolution at the boundary. The results at the both the beginning and end edges cannot be accurate anyway.Second, you need rounding the output values, if the output d
24、ata type is integer and impulse response is floating point number. And, the output value may be exceeded the maximum or minimum value.If you have unsigned 8-bit integer data type for output signal, the range of output should be between 0 and 255. You must check the value is greater than the minimum
25、and less than the maximum value.Download the 1D convolution routine and test program.conv1d.zipConvolution in 2D2D convolution is just extension of previous 1D convolution by convolving both horizontal and vertical directions in 2 dimensional spatial domain. Convolution is frequently used for image
26、processing, such as smoothing, sharpening, and edge detection of images.The impulse (delta) function is also in 2D space, som, nhas 1 wheremandnis zero and zeros atm,n 0. The impulse response in 2D is usually called kernel or filter in image processing.The second image is 2D matrix representation of
27、 impulse function. The shaded center point is the origin wherem=n=0.Once again, a signal can be decomposed into a sum of scaled and shifted impulse (delta) functions;For example,x0, 0isx0, 0m, n,x1, 2isx1, 2m-1, n-2, and so on. Note that the matrices are referenced here as column, row, not row, colu
28、mn. M is horizontal (column) direction and N is vertical (row) direction.And, the output oflinearandtime invariant systemcan be written by convolution of input signalxm, n, and impulse response,hm, n;Notice that the kernel (impulse response) in 2D is center originated in most cases, which means the
29、center point of a kernel ish0, 0. For example, if the kernel size is 5, then the array index of 5 elements will be -2, -1, 0, 1, and 2. The origin is located at the middle of kernel.Examine an example to clarify how to convolve in 2D space.Lets say that the size of impulse response (kernel) is 3x3,
30、and its values are a, b, c, d,.Notice the origin (0,0) is located in the center of kernel.Lets pick a simplest sample and compute convolution, for instance, the output at (1, 1) will be;It results in sum of 9 elements of scaled and shifted impulse responses. The following image shows the graphical representation of 2D convolution.2D ConvolutionNotice that the kernel matrix is flipped both horizontal and vertical direction be
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1