# Connexions

You are here: Home » Content » Lab 3: Convolution and Its Applications

### Recently Viewed

This feature requires Javascript to be enabled.

# Lab 3: Convolution and Its Applications

Note: You are viewing an old version of this document. The latest version is available here.

This lab involves experimenting with the convolution of two continuous-time signals. The main mathematical part is written as a .m file, which is then used as a LabVIEW MathScript node within the LabVIEW programming environment to gain user interactivity. Due to the discrete-time nature of programming, an approximation of the convolution integral is needed. As an application of the convolution concept, echoes are removed from speech recordings using this concept.

## Numerical Approximation of Convolution

In this section, let us apply the LabVIEW MathScript function conv to compute the convolution of two signals. One can choose various values of the time interval ΔΔ size 12{Δ} {} to compute numerical approximations to the convolution integral.

### Convolution Example 1

In this example, use the function conv to compute the convolution of the signals x(t)=exp(at)u(t)x(t)=exp(at)u(t) size 12{x $$t$$ ="exp" $$- ital "at"$$ u $$t$$ } {} and h(t)=exp(bt)u(t)h(t)=exp(bt)u(t) size 12{h $$t$$ ="exp" $$- ital "bt"$$ u $$t$$ } {}with u(t)u(t) size 12{u $$t$$ } {}representing a step function starting at 0 for 0t80t8 size 12{0 <= t <= 8} {}. Consider the following values of the approximation pulse width or delta: Δ=0.5,0.1,0.05,0.01,0.005,0.001Δ=0.5,0.1,0.05,0.01,0.005,0.001 size 12{Δ=0 "." 5,0 "." 1,0 "." "05",0 "." "01",0 "." "005",0 "." "001"} {}. Mathematically, the convolution of h(t)h(t) size 12{h $$t$$ } {}and x(t)x(t) size 12{x $$t$$ } {}is given by

y(t)=1ab(ebteat)u(t)y(t)=1ab(ebteat)u(t) size 12{y $$t$$ = { {1} over {a - b} } $$e rSup { size 8{ - ital "bt"} } - e rSup { size 8{ - ital "at"} }$$ u $$t$$ } {}
(1)

Compare the approximation yˆ()yˆ() size 12{ { hat {y}} $$nΔ$$ } {}obtained via the function conv with the theoretical value y(t)y(t) size 12{y $$t$$ } {}given by Equation (1). To better see the difference between the approximated yˆ()yˆ() size 12{ { hat {y}} $$nΔ$$ } {}and the true yˆ()yˆ() size 12{ { hat {y}} $$nΔ$$ } {}values, display yˆ(t)yˆ(t) size 12{ { hat {y}} $$t$$ } {}and y(t)y(t) size 12{y $$t$$ } {} in the same graph.

Compute the mean squared error (MSE) between the true and approximated values using the following equation:

MSE=1Nn=1N(y()yˆ())2MSE=1Nn=1N(y()yˆ())2 size 12{ ital "MSE"= { {1} over {N} } Sum cSub { size 8{n=1} } cSup { size 8{N} } { $$y \( nΔ$$ - { hat {y}} $$nΔ$$ \) rSup { size 8{2} } } } {}
(2)

where N=TΔN=TΔ size 12{N= left lfloor { {T} over {Δ} } right rfloor } {}, T is an adjustable time duration expressed in seconds and the symbol .. size 12{ left lfloor "." right rfloor } {} denotes the nearest integer. To begin with, set T=8T=8 size 12{T=8} {}.

As you can see here, the main program is written as a .m file and placed inside LabVIEW as a LabVIEW MathScript node by invoking Functions Programming Structures MathScript. The .m file can be typed in or copied and pasted into the LabVIEW MathScript node. The inputs to this program consist of an approximation pulse width ΔΔ size 12{Δ} {}, input exponent powers aa size 12{a} {}and bb size 12{b} {} and a desired time duration TT size 12{T} {}. To add these inputs, right-click on the border of the LabVIEW MathScript node and click on the Add Input option as shown in Figure 1.

After adding these inputs, create controls to allow one to alter the inputs interactively via the front panel. By right-clicking on the border, add the outputs in a similar manner. An important consideration is the selection of the output data type. Set the outputs to consist of MSE, actual or true convolution output y_ac and approximated convolution output y. The first output is a scalar quantity while the other two are one-dimensional vectors. The output data types should be specified by right-clicking on the outputs and selecting the Choose Data Type option (see Figure 2).

Next write the following .m file textual code inside the LabVIEW MathScript node:

t=0:Delta:8;

Lt=length(t);

x1=exp(-a*t);

x2=exp(-b*t);

y=Delta*conv(x1,x2);

y_ac=1/(a-b)*(exp(-b*t)-exp(-a*t));

MSE=sum((y(1:Lt)-y_ac).^2)/Lt

With this code, a time vector t is generated by taking a time interval of Delta for 8 seconds. Convolve the two input signals, x1 and x2, using the function conv. Compute the actual output y_ac using Equation (1). Measure the length of the time vector and input vectors by using the command length(t). The convolution output vector y has a different size (if two input vectors m and n are convolved, the output vector size is m+n-1). Thus, to keep the size the same, use a portion of the output corresponding to y(1:Lt) during the error calculation.

Use a waveform graph to show the waveforms. With the function Build Waveform (Functions → Programming → Waveforms → Build Waveforms), one can show the waveforms across time. Connect the time interval Delta to the input dt of this function to display the waveforms along the time axis (in seconds).

Merge together and display the true and approximated outputs in the same graph using the function Merge Signal (Functions → Express → Signal Manipulation → Merge Signals). Configure the properties of the waveform graph as shown in Figure 3.

Figure 4 illustrates the completed block diagram of the numerical convolution.

Figure 5 shows the corresponding front panel, which can be used to change parameters. Adjust the input exponent powers and approximation pulse-width Delta to see the effect on the MSE.

### Convolution Example 2

Next, consider the convolution of the two signals x(t)=exp(2t)u(t)x(t)=exp(2t)u(t) size 12{x $$t$$ ="exp" $$- 2t$$ u $$t$$ } {}and h(t)=rect(t22)h(t)=rect(t22) size 12{h $$t$$ = ital "rect" $${ {t - 2} over {2} }$$ } {} for , where u(t)u(t) size 12{u $$t$$ } {}denotes a step function at time 0 and rect a rectangular function defined as

rect(t)={10.5t<0.50otherwiserect(t)={10.5t<0.50otherwise size 12{ ital "rect" $$t$$ = left lbrace matrix { 1 {} # - 0 "." 5 <= t<0 "." 5 {} ## 0 {} # ital "otherwise"{} } right none } {}
(3)

Let Δ=0.01Δ=0.01 size 12{Δ=0 "." "01"} {}. Figure 6 shows the block diagram for this second convolution example. Again, the .m file textual code is placed inside a LabVIEW MathScript node with the appropriate inputs and outputs.

Figure 7 illustrates the corresponding front panel where x(t)x(t) size 12{x $$t$$ } {}, h(t)h(t) size 12{h $$t$$ } {} and x(t)h(t)x(t)h(t) size 12{x $$t$$ * h $$t$$ } {} are plotted in different graphs. Convolution ()() size 12{ $$*$$ } {} and equal (=)(=) size 12{ $$=$$ } {}signs are placed between the graphs using the LabVIEW function Decorations.

### Convolution Example 3

In this third example, compute the convolution of the signals shown in Figure 8.

Figure 9 shows the block diagram for this third convolution example and Figure 10 the corresponding front panel. The signals x1(t)x1(t) size 12{x1 $$t$$ } {}, x2(t)x2(t) size 12{x2 $$t$$ } {} and x1(t)x2(t)x1(t)x2(t) size 12{x1 $$t$$ * x2 $$t$$ } {} are displayed in different graphs.

## Convolution Properties

In this part, examine the properties of convolution. Figure 11 shows the block diagram to examine the properties and Figure 12 and Figure 13 the corresponding front panel. Both sides of equations are plotted in this front panel to verify the convolution properties. To display different convolution properties within a limited screen area, use a Tab Control (Controls ModernContainersTab Control) in the front panel.

## Linear Circuit Analysis Using Convolution

In this part, let us consider an application of convolution in analyzing RLC circuits to gain a better understanding of the convolution concept. A linear circuit denotes a linear system, which can be represented with its impulse response h(t)h(t) size 12{h $$t$$ } {}, that is, its response to a unit impulse input. The input to such a system can be considered to be a voltage v(t)v(t) size 12{v $$t$$ } {}and the output to be the circuit current i(t)i(t) size 12{i $$t$$ } {}. See Figure 14.

For a simple RC series circuit shown in Figure 15, the impulse response is given by (Reference) ,

h(t)=1RCexp(1RCt)h(t)=1RCexp(1RCt) size 12{h $$t$$ = { {1} over {RC} } "exp" $$- { {1} over { ital "RC"} } t$$ } {}
(4)

which can be obtained for any specified values of R and C. When an input voltage v(t)v(t) size 12{v $$t$$ } {} (either DC or AC) is applied to the system, the circuit current i(t)i(t) size 12{i $$t$$ } {} can be obtained by simply convolving the system impulse response with the input voltage, that is

i(t)=h(t)v(t)i(t)=h(t)v(t) size 12{i $$t$$ =h $$t$$ * v $$t$$ } {}
(5)

Similarly, for the simple RL series circuit shown in Figure 16, the impulse response is given by (Reference) ,

h(t)=RLexp(RLt)h(t)=RLexp(RLt) size 12{h $$t$$ = { {1} over {L} } "exp" $$- { {R} over {L} } t$$ } {}
(6)

When an input voltage v(t)v(t) size 12{v $$t$$ } {} is applied to the system, the circuit current i(t)i(t) size 12{i $$t$$ } {} can be obtained by computing the convolution integral.

Figure 17 shows the block diagram of this linear system and Figure 18 the corresponding front panel. From the front panel, one can control the system type (RL or RC), input voltage type (DC or AC) and input voltage amplitude. One can also observe the system response by changing R, L and C values. Three graphs are used to display the input voltage v(t)v(t) size 12{v $$t$$ } {}, impulse response of the circuit h(t)h(t) size 12{h $$t$$ } {} and circuit current i(t)i(t) size 12{i $$t$$ } {}.

## Lab Exercises

### Exercise 1

Echo Cancellation

In this exercise, consider the problem of removing an echo from a recording of a speech signal. The LabVIEW MathScript function sound() or the function Play Waveform in LabVIEW can be used to play back the speech recording. To begin, load the .m file echo_1.wav provided on the book website by using the function wavread(‘filename’). This speech file was recorded at the sampling rate of 8 kHz, which can be played back through the computer speakers by typing

>> sound(y)

You should be able to hear the sound with an echo. If the LabVIEW function Play Waveform(Functions Programming Graphics & Sound Sound Output Play Waveform) is used to play the sound, you first need to build a waveform based on the loaded data and the time interval dt=1/8000dt=1/8000 size 12{ ital "dt"=1/"8000"} {} because this speech was recorded using an 8 kHz sampling rate. Connect the waveform to the function Play Waveform.

An echo is produced when the signal (speech, in this case) is reflected off a non-absorbing surface like a wall. What is heard is the original signal superimposed on the signal reflected off the wall (echo). Because the speech is partially absorbed by the wall, it decreases in amplitude. It is also delayed. The echoed signal can be modeled as ax(tτ)ax(tτ) size 12{ ital "ax" $$t - τ$$ } {}where a<1a<1 size 12{a<1} {} and ττ size 12{τ} {} denotes the echo delay. Thus, one can represent the speech signal plus the echoed signal as [7]

y(t)=x(t)+ax(tτ)y(t)=x(t)+ax(tτ) size 12{y $$t$$ =x $$t$$ + ital "ax" $$t - τ$$ } {}
(7)

What is heard is y(t)y(t) size 12{y $$t$$ } {}. In many applications, it is important to recover x(t)x(t) size 12{x $$t$$ } {} – the original, echo-free signal – from y(t)y(t) size 12{y $$t$$ } {}.

Method 1

In this method, remove the echo using deconvolution. Rewrite Equation (7) as follows [7]:

y[]=x[]+ax[(nN)Δ]=x[](δ[]+[nN]Δ)=x[]h[]y[]=x[]+ax[(nN)Δ]=x[](δ[]+[nN]Δ)=x[]h[] size 12{y $nΔ$ =x $nΔ$ + ital "ax" $$$n - N$$ Δ$ =x $nΔ$ * $$δ $nΔ$ +aδ $n - N$ Δ$$ =x $nΔ$ * h $nΔ$ } {}
(8)

The echoed signal is the convolution of the original signal x()x() size 12{x $$nΔ$$ } {} and the signal h()h() size 12{h $$nΔ$$ } {}. Use the LabVIEW MathScript function deconv(y,h) to recover the original signal.

Method 2

An alternative way of removing the echo is to run the echoed signal through the following system:

z[]=y[]az[(nN)Δ]z[]=y[]az[(nN)Δ] size 12{z $nΔ$ =y $nΔ$ - ital "az" $$$n - N$$ Δ$ } {}
(9)

Assume that z[]=0z[]=0 size 12{z $nΔ$ =0} {}for n<0n<0 size 12{n<0} {}. Implement the above system for different values of aa size 12{a} {} and NN size 12{N} {}.

Display and play back the echoed signal and the echo-free signal using both of the above methods. Specify the parameters aa size 12{a} {}and NN size 12{N} {}as controls. Try to measure the proper values of aa size 12{a} {}and NN size 12{N} {}by the autocorrelation method described below.

The autocorrelation of a signal can be described by the convolution of a signal with its mirror. That is,

Rxx[n]=x[n]x[n]Rxx[n]=x[n]x[n] size 12{R rSub { size 8{ ital "xx"} } $n$ =x $n$ * x $- n$ } {}
(10)

Use the autocorrelation of the output signal (echo-free signal) to estimate the delay time ( NN size 12{N} {}) and the amplitude of the echo ( aa size 12{a} {}). For different values of NN size 12{N} {}and aa size 12{a} {}, observe the autocorrelation output. To have an echo-free signal, the side lobes of the autocorrelation should be quite low, as shown in Figure 19.

Figure 20 shows a typical front panel for this exercise. It is not necessary to obtain the same front panel but there should be controls for aa size 12{a} {} and NN size 12{N} {} as well as graphs to observe the echoed signal, echo-free signal and autocorrelation function of the echo-free signal.

#### Solution

Insert Solution Text Here

### Exercise 2

Noise Reduction Using Mean Filtering

The idea of mean filtering is simply to replace each value in a signal with the mean (average) value of its neighbors. A mean filter is widely used for noise reduction.

Start by adding some random noise to a signal (use the file echo_1.wav or any other speech data file). Then, use mean filtering to reduce the introduced noise. More specifically, take the following steps:

1. Normalize the signal values in the range [0 1].
2. Add random noise to the signal by using the function randn. Set the noise level as a control.
3. Convolve the noise-added signal with a mean filter. This filter can be designed by taking an odd number of ones and dividing by the size. For example, a 1×31×3 size 12{1 times 3} {} size mean filter is given by [1/3 1/3 1/3] and a 1×51×5 size 12{1 times 5} {}size mean filter by [1/5 1/5 1/5 1/5 1/5]. Set the size of the mean filter as an odd number control (3, 5 or 7, for example).

#### Solution

Insert Solution Text Here

### Exercise 3

Impulse Noise Reduction Using Median Filtering

A median filter is a non-linear filter that replaces a data value with the median of the values within a neighboring window. For example, the median value for this data stream [2 5 3 11 4] is 4. This type of filter is often used to remove impulse noise. Use the file echo_1.wav or any other speech data file and take the following steps:

1. Normalize the signal values in the range [0 1].
2. Randomly add impulse noise to the signal by using the LabVIEW MathScript function randperm. Set the noise density as a control.
3. Find the median values of neighboring data using the function median` and replace the original value with the median value. Set the number of neighboring values as an odd number control (3, 5 or 7, for example).

#### Solution

Insert Solution Text Here

## Content actions

PDF | EPUB (?)

### What is an EPUB file?

EPUB is an electronic book format that can be read on a variety of mobile devices.

My Favorites (?)

'My Favorites' is a special kind of lens which you can use to bookmark modules and collections. 'My Favorites' can only be seen by you, and collections saved in 'My Favorites' can remember the last module you were on. You need an account to use 'My Favorites'.

| A lens I own (?)

#### Definition of a lens

##### Lenses

A lens is a custom view of the content in the repository. You can think of it as a fancy kind of list that will let you see content through the eyes of organizations and people you trust.

##### What is in a lens?

Lens makers point to materials (modules and collections), creating a guide that includes their own comments and descriptive tags about the content.

##### Who can create a lens?

Any individual member, a community, or a respected organization.

##### What are tags?

Tags are descriptors added by lens makers to help label content, attaching a vocabulary that is meaningful in the context of the lens.

| External bookmarks