function h = uwb_imr(chan, ch_idx, plot_imr)
% The first step is to read in the data from the .mat files supplied by the
% IEEE 802.15.3a working group
error('Unrecognized UWB channel number.');
% The second step is to reinterpolate the data from the unequally spaced
% points in the .mat file to a set of points spaced at the simulation
% sampling interval.
% Define the sampling time
Ts = 3.125e-7/990;
% If the user has entered a valid channel index in the range 1 to 100,
% extract the corresponding column of data from each of the matrices
% and reinterpolate it.
if ch_idx>=1 && ch_idx<=100
% Extract the correct columns from the data
% h_ct and t_ct are the "continuous-time" impulse response sample and
% sampling times, respectively.
z = h_ct(:,ch_idx);
tau = t_ct(:,ch_idx)*1e-9;
% x1 is the time-sampled data normalized to the regular sampling interval
% x2 is a grid of points to be used for sinc interpolation
x1 = tau/Ts;
x2 = [-10:489];
% Build up the impulse response one point at a time. Each point in the
% reinterpolated impulse response contains a contribution from every
% point in the original impulse response, weighted by the sinc
h = zeros(500,1);
for h_idx = 1:500,
h(h_idx) = my_sinc(x1-x2(h_idx)).'*z;
error('Channel index must be in the range 1 to 100.');
% Normalize the result so that the total energy in the impulse response is
h = h/sqrt(h'*h);
% Take the real bandpass impulse response and convert it to a complex
% baseband equivalent.
% Set the carrier frequency to be the middle of band 2, 3960 MHz.
FcTs = 3960e6*Ts;
% Translate the data from passband to baseband.
h = h.*exp(j*2*pi*FcTs*[1:500]');
% Truncate the start of the response to remove any delay introduced in the
% reinterpolation process.
gd = min(find(h>0.05*max(abs(h))));
% Keep the length of the impulse response fixed to allow "on-the-fly"
% channel changes in Simulink.
h = [h(gd:end);zeros(gd-1,1)];
% If the simulation is stopped, plot the impulse response.
simstatus = get_param(gcs,'simulationstatus');
figure;plot(Ts/1e-9*[1:480]/6,abs(h(1:480)),[32 32]*Ts/1e-9,[0 0.5],'r--');
% A small change to the sinc function for speed.