[Math] Creating a System Impulse Response in Matlab

convolutionMATLAB

Preface: I'm extremely new to Matlab.

Ok, so I have a sound file that I loaded in Matlab.

Two variables are loaded:

xx - the speech waveform - 16001 samples
fs - the sampling frequency of the speech waveform = 22050 Hz           

Since fs = 1/T, T = 0.00004535147 seconds

Since there are 16001 samples, the total time for the file is 16001 * T = 0.72566893424 seconds

I plotted the waveform with the x-axis representing time, by using the following command:

plot(0:1/fs:0.72566893424,xx);

At this point, I'm trying to create a discrete-time system impulse response that creates an "echo" of the original waveform. (The echo should be delayed by 1-second and 1/4 the amplitude of the original waveform)

My question is, how do I generate an impulse response in Matlab?

At this time, I've tried to create another array, h, by adding 22050 zeros to the beginning (a 1 second delay), multiplying the whole array by 1/4, then convolving it with the original waveform… all I've managed to achieve is a sound that is roughly equivalent to the Dark Lord himself speaking.

Am I on the right track? This is homework, so I don't want an "answer", but rather steps to understand what I'm doing a little better, I need to learn the material. Sorry if this rambles a bit, Matlab is fairly foreign to me.

Best Answer

An impulse response should be independent of the stimulus. For example, given input signal $f(x)$ and impulse response $h(x)$, the output signal $y(x)$ is generated as follows:

$$y(x) = f(x) {\Large *} h(x)$$

This relationship holds true for any input signal we choose to apply. Let's say you want an impulse response that makes $y(x) = f(x)$. The the solution is $h(x) = \delta(x)$ where $\delta$ signifies the Dirac Delta Function.

$$y(x) = f(x) {\Large *} \delta(x) = f(x)$$

Now let's say that I want my output to be a scaled version of my output.

$$y(x) = f(x) {\Large *} \left(\tfrac{1}{2}\delta(x)\right) = \tfrac{1}{2}f(x)$$

What if I want my output to be a delayed version of my output?

$$y(x) = f(x) {\Large *} \delta(x-\tau) = f(x-\tau)$$

For linear time-invariant systems, you are free to make any linear combination of impulse responses that you would like. That means that

$$y(x) = f(x) {\Large *} \left(\tfrac{1}{2}\delta(x) + \delta(x-\tau)\right) = \tfrac{1}{2}f(x) + f(x-\tau) \qquad (1)$$

So the output in this situation is A scaled copy with no delay added to a full-scale copy of the signal with a delay of $\tau$ seconds.

Finally, how do we use dirac delta function in Matlab? Let's say that I want to implement equation (1) in Matlab. This corresponds the following impulse response, which I break into two separate impulse responses:

$$h(x) = h_1(x) + h_2(x) = \tfrac{1}{2}\delta(x) + \delta(x-\tau)$$

Since we are working in discrete time, I need my $\tau$ to be in terms of samples. Let's say that I am looking for a delay of 1000 samples.

tau = 1000;
h1 = [0.5, zeros(1, tau)];
h2 = [zeros(1, tau) 1];
h = h1 + h2;
y = conv(f,h);

Notice that in the impulse response h2 I placed the 1 at the index 1001 rather than 1000. This is because putting a 1 at the first index corresponds to no delay, so I needed to add 1000 to that in order to delay my signal. Also, I made h1 longer than it needs to be. In fact, h1 could have just been h1 = 0.5, since trailing zeros will have no effect in the convolution. However, I wanted to make it the same length as h2 so that I could add the two of them together.

Hopefully this is enough to get you started.