Difference between revisions of "MATLAB:Plotting Discrete Responses"

From PrattWiki
Jump to navigation Jump to search
(Live Code Results)
 
Line 197: Line 197:
 
-->
 
-->
 
</div></body></html>
 
</div></body></html>
 +
[[Category:ECE 280]]

Latest revision as of 16:33, 11 February 2024

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:

\( \begin{align*} \frac{3}{2}y[n]-\frac{1}{2}y[n-1]&=x[n]\\ y[n]&=\frac{1}{3}y[n-1]+\frac{2}{3}x[n] \end{align*} \)

The impulse response $$h[n]$$ and step resonse $$s_r[n]$$ are:

\( \begin{align*} h[n]&=\frac{2}{3}\left(\frac{1}{3}\right)^nu[n]\\ s_r[n]&=\left(1-\left(\frac{1}{3}\right)^{n+1}\right)u[n] \end{align*} \)

Code

If you have MATLAB, the .m file is available in the collapsed section below. If you want to reconstitute it as a Live Code file:

  • Copy the text into a new .m file and save it
  • Click the down arrow on the Save button and select Save As
  • Change the Save as type to Live Code and then save
  • MATLAB .m File
    %% Calculate and Plot Impulse and Step Responses
    % This code will look at solving and plotting the impulse and step responses 
    % for:
    % 
    % $$\frac{3}{2}y[n]-\frac{1}{2}y[n-1]=x[n]$$ 
    % 
    % which can be re-written as:
    % 
    % $$y[n]=\frac{1}{3}y[n-1]+\frac{2}{3}x[n]$$
    %% 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
    

Live Code Results

Calculate and Plot Impulse and Step Responses

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