MATLAB:Plotting Discrete Responses
Jump to navigation
Jump to search
Introduction
The following is an HTML export of a MATLAB Live Editor file that looks at generating functions to calculate the impulse and step responses for:
The impulse response $$h[n]$$ and step resonse $$s_r[n]$$ are:
If you have MATLAB, you can copy and paste the code below into a Live Editor window.
Live Editor Results
Calculate and Plot Impulse and Step Responses
This code will look at solving and plotting the impulse and step responses for:
which can be re-written as:
Initialize
clear
format short e
Plot impulse and step function
n = -2:5;
% Impulse responses
figure(1)
clf
stem(n, arrayfun(@(n) hr(n), n), 'bs')
hold on
stem(n, hf(n), 'r+')
hold off
title('Impulse Responses')
xlabel('n')
% Step responses
figure(2)
clf
stem(n, arrayfun(@(n) srr(n), n), 'bs')
hold on
stem(n, srf(n), 'r+')
hold off
title('Step Responses')
xlabel('n')
Function definitions
Define unit impulse and step functions
function out=delta(n)
out = (n==0).*1;
end
function out=u(n)
out = (n>=0).*1;
end
Define impulse and step responses using recursion
function out=hr(n)
% Note: only works for one value of n at a time
% Use arrayfun(@(n) hr(n), ARRAY) for multiple responses
if n < 0
out = 0;
else
out = 1/3*hr(n-1) + 2/3*delta(n);
end
end
function out=srr(n)
% Note: only works for one value of n at a time
% Use arrayfun(@(n) srr(n), ARRAY) for multiple responses
if n < 0
out = 0;
else
out = 1/3*srr(n-1) + 2/3*u(n);
end
end
Define impulse and step responses using formula
function out=hf(n)
out = 2/3*(1/3).^n.*u(n);
end
function out=srf(n)
out = (1-(1/3).^(n+1)).*u(n);
end