# Creating a System Impulse Response in Matlab

Preface: I'm extremely new to Matlab.

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

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.

• In the future, you might consider posting such questions at dsp.stackexchange.com. This type of question is a perfect fit for the forum there. – nispio Oct 11 '13 at 3:36
• Thanks, I didn't even see that one! – Ramrod Oct 11 '13 at 3:53

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.

• OK, so thus far this is what I have: h1 = [zeros(1,tau-1) 1]; h2 = 0.25.*[zeros(1,tau-1) 1]; h=h1+h2; y = conv(xx,h); – Ramrod Oct 11 '13 at 7:16
• @Ramrod: How are you setting tau? Assuming you want to delay by tau samples, you want h2 = 0.25*[zeros(1,tau) 1];. Also, your current h1 is delaying the signal by tau-1 samples, but in your case you want h1 to have no delay: h1 = [1 zeros(1,tau)];. – nispio Oct 11 '13 at 14:51
• Sorry for the delay, but your help definitely put me where I needed to be. h1 = [1 zeros(1,tau-1)]; % No delay, original signal h2 = [zeros(1,tau-1) 0.25]; % 1 second delay (22050 samples) and 1/4 strength signal The end result being a signal is played, and then the attenuated echo is played one second later. I believe you are right that my tau is off by 1 sample, making the delay just under one second. – Ramrod Oct 16 '13 at 23:34