Guaranteed Higher Grade!

Free Quote
Convolution and Correlation for Signal Manipulation, Finding Signals, and Name that Tune

Answered

**Part 1: Convolution for Signal Manipulation**

Create a Signal: Open the MATLAB editor and create a script file. In your script, create a discrete-time signal x[n] that is 20 samples in length. Start with this signal containing all zeros (i.e., we are “initializing” the signal). The easiest way to do this is to write:

>> x = zeros(20,1);

In your script file, make your signal x[n] equal to

x[n] = u[n n 3] ] u[n n 8] ] δ[n n 17] .

There are multiple ways that you can do this. Choose the way that works best for you.

Manually plot x[n] and then plot x in MATLAB using the commands

>> n = 0:19;

>> stem(n, x);

>> xlabel(’Samples’);

>> ylabel(’Amplitude’);

Confirm that your plot and MATLAB plot match.

Note: The first index in MATLAB (i.e., x(1)) corresponds to time n = 0 (i.e., x[n]).

Therefore, keep in mind that our MATLAB index is always one value ahead of our n.

Convolve with a Delta Function: Now create a system impulse response h[n] that is also

20 samples in length. Create the impulse response h in MATLAB such that h[n] and y[n] are

defined by

h[n] = δ[n] y[n] = x[n] ∗ h[n].

Perform the convolution in MATLAB by using

>> y = conv(x, h)

Note: The output signal y[n] should now have a length of N = 39. We will see why in

the next subsection.

2

Convolve with a Shifted Delta Function: Create a new impulse response h[n] that is also

20 samples in length. Create the impulse response h in MATLAB such that

h[n] = δ[n n k1] ,

for k1 = 5. Determine and plot the convolution y[n] = x[n] ∗ h[n] in MATLAB. Repeat this for

k1 = 6, k1 = 10, and k1 = 19.

1.1. Answer: What is relationship between your input x[n], your output y[n], and k1?

1.2. Answer: Why is it important to have an output length of N = 39?

Convolve with Two Delta Functions: Create a new impulse response h[n] that is also 20 sam?ples in length. Create the impulse response h in MATLAB such that

h[n] = δ[n] + δ[n n k2] ,

for k2 = 19. Determine and plot the convolution y[n] = x[n] ∗ h[n] in MATLAB. Repeat this for

k2 = 17, k2 = 14, and k2 = 11.

1.3. Answer: What is relationship between your input x[n], your output y[n], and k2?

1.4. Answer: What does this system do?

Convolve with a Box (Running Averaging): Create a new impulse response h[n] that is also

20 samples in length. Create the impulse response h in MATLAB such that

h[n] = (1/3)(u[n] ] u[n n 3]) .

Determine and plot the convolution y[n] = x[n] ∗ h[n] in MATLAB.

1.5. Answer: What does this system do? What application might this be used in?

Convolve with a Difference (Edge Detection): Create a new impulse response h[n] that is

also 20 samples in length. Create the impulse response h in MATLAB such that

h[n] = = δ[n] + 2δ[n n 1] ] δ[n n 2] .

Determine and plot the convolution y[n] = x[n] ∗ h[n] in MATLAB.

1.6. Answer: What does this system do? What application might this be used in?

3

Part 2: Convolution for Finding Signals

Consider the following small change to the convolution equation

∞ y[n] = x[[ n] ∗ h[n] = X x[n + m]h[m] m=−∞

This is often known correlation (or cross-correlation). The correlation has several very useful prop?erties. It is the key component of many pattern recognition algorithms, such as facial recognition.

We will illustrate how this pattern recognition works in the following subsections.

Creating Signals: Create the following signals in MATLAB:

>> x1 = sin(pi/10*(0:19));

>> x2 = sin(pi/5*(0:19));

>> x3 = sin(pi/2*(0:19));

>> x4 = (-1).^(0:19);

Plot each of these signals individually with the stem command.

Now concatenate each of these four signals:

>> z = [x1 x2 x3 x4];

The Auto-Correlation: First, perform the correlation of x1 with itself. This is known as the

auto-correlation. Perform the auto-correlation with the following two sets of commands:

>> a1 = conv(fliplr(x1), x1);

>> b1 = xcorr(x1, x1);

The results from conv should be equivalent to the results from xcorr, but with one small difference:

xcorr is much faster for large signals. You may want to remember this for Part 3 of the lab.

The correlation can be thought of as a measure of similarity between x[m] with a version of

h[m+n] that is delayed by y n samples. The auto-correlation is always maximum at n = 0 because

the signals are identical at this delay. The x-axis for a correlation is referred to as the “lag” or

”delay.” The correlation can be plotted (with the correct x-axis) through the following commands.

>> n = -(20-1):(20-1); % 10 is from the maximum length between x1 and h

>> stem(n, a1)

>> xlabel(’Lag [samples]’)

>> ylabel(’Amplitude’)

Plot the result for both a1 and b1 to confirm that they are equal. Also plot the auto-correlations

for x2, x3, and x4.

2.1. Include: The four plots resulting from the auto-correlation of x1, x2, x3, x4. Include

correct axes and labels.

4

The Cross-Correlation (Template Matching): Now perform a correlation between z and x1

with the command:

>> z1 = xcorr(z, x1);

Plot the result for z1 using

>> n = -(80-1):(80-1); % 40 is from the maximum length between z and h

>> stem(n, z1)

>> xlabel(’Lag [samples]’)

>> ylabel(’Amplitude’)

Note that this process is also known as “template matching” (particularly in image processing) or

“matched filtering” (particularly in communications). Replace x1, x2, x3, x4 and repeat the xcorr

and plotting process.

2.2. Include: The four plots resulting from “template matching” x1, x2, x3, x4 with z. Include

correct axes and labels.

2.3. Answer : How do these results compare with the autocorrelations?

2.4. Answer : What is the significance of the maximum values?

Verify your conclusions with your TA before moving on.

5

Part 3: Name that Tune

Reading audio files: Included with this lab is 39 mp4 files by Peter Rudenko from the free

music archive (http://freemusicarchive.org/). To load these files into MATLAB use the audioread

command:

>> [y, Fs] = audioread([’rudenko_01.mp4’]);

If you want to use a variable to control the file number, you can use the commands:

>> ii = 1;

>> [y, Fs] = audioread([’rudenko_’ num2str(ii, ’%02i’) ’.mp4’]);

The output of the audioread function provides y (the audio signal) and Fs (the sampling rate of

the audio signal). The sampling rate is the number of discrete values (i.e., samples) per second in

the signal.

To visually see the audio signal with correct axis labels, type the commands

>> t = 1/Fs:1/Fs:length(y)/Fs;

>> plot(t, y);

>> xlabel(’Time [sec]’)

>> ylabel(’Amplitude’)

The variable t describes the time-axis of the audio.

Running P-files: Included with this lab is a p-file (an obfuscated m-file) function >> [findme, Fs] = get_tune(uid);

Note that to run this function, all 39 Rudenko files must be in the current directory. The input to this function uid will be a string with your uid. The output of the function findme is an audio segment from the one of the 39 audio files. The second output Fs is the sampling rate of the audio (this should be equal to the sampling rate of the full audio).

Replace uid with your uID (in single quotes) and run this function to extract an audio segment that is unique to you. Use the command sound(findme, Fs) to play the audio segment.

Your Task: Name that Tune: Write a MATLAB script that utilizes the capabilities of convolution that we learned in the previous exercises to determine which music file your unique audio segment findme originates. Note that even when using the xcorr function, your script may take a few minutes to fully execute. Save this script in a MATLAB m-file named “lastname firstname lab2.m”, where “lastname” is replaced with you last name and “firstname” is replaced with your first name.

3.1 Submit: Your custom “lastname firstname lab2.m” file

3.2 Answer: From which music file did your music segment originate?

3.3 Answer: Describe your system / algorithm with one-three paragraphs. Do not refer to any specific MATLAB code. Your description should be specific enough that a reader could replicate your algorithm but general enough that a reader could replicate the algorithm in any programming language (C++, Java, etc.) while not knowing MATLAB. Assume your audience has the technical knowledge of a typical electrical engineering sophomore (with the exception of MATLAB knowledge).