%% Estimate Impulse Response Using Sweep Tone Excitation
% Create a sweep tone excitation signal by using the sweeptone function.

excitation = sweeptone(2,1,44100);

plot(excitation)
title('Excitation')

%
% Pass the excitation signal through an infinite impulse response (IIR) filter and add noise to model a real-world recording (system response).

[B,A] = butter(10,[.1 .7]);
rec = filter(B,A,excitation);
nrec = rec + 0.12*randn(size(rec));

plot(nrec)
title('System Response')

%
% Pass the excitation signal and the system response to the impzest function to estimate the impulse response. Truncate the estimate to 100 points. Use impz to determine the true impulse response of the system. Plot the true impulse response and the estimated impulse response for comparison.

irEstimate = impzest(excitation,nrec);
irEstimate = irEstimate(1:101);

irTrue = impz(B,A,101);
plot(0:100,irEstimate, ...
     0:100,irTrue,'ro')

legend('True impulse response','Estimated impulse response')

%
% Copyright 2018 The MathWorks, Inc.