Difference between revisions of "MATLAB:Plotting Discrete Responses"

From PrattWiki
Jump to navigation Jump to search
(Code)
Line 13: Line 13:
 
</math></center>
 
</math></center>
 
== Code ==
 
== Code ==
If you have MATLAB, the codes are available in the collapsed sections below:
+
If you have MATLAB, the code is available in the collapsed sections below:
*<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
+
* <div class="toccolours mw-collapsible mw-collapsed"><div style="font-weight:bold;line-height:1.6;">MATLAB .m File</div><div class="mw-collapsible-content "><syntaxhighlight lang=matlab>
<div style="font-weight:bold;line-height:1.6;">MATLAB .m File</div>
 
<div class="mw-collapsible-content mw-collapsed">
 
<syntaxhighlight lang=matlab>
 
 
%% 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:
 +
%
 +
% $$\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</syntaxhighlight>
 +
</div></div>
  
</syntaxhighlight>
 
</div></div?
 
 
== Live Editor Results ==
 
== Live Editor Results ==
 
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,IE=9,chrome=1"><meta name="generator" content="MATLAB 2021a"><title>Calculate and Plot Impulse and Step Responses</title><style type="text/css">.rtcContent { padding: 30px; } .S0 { margin: 3px 10px 5px 4px; padding: 0px; line-height: 28.8px; min-height: 0px; white-space: pre-wrap; color: rgb(213, 80, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 24px; font-weight: 400; text-align: left;  }
 
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,IE=9,chrome=1"><meta name="generator" content="MATLAB 2021a"><title>Calculate and Plot Impulse and Step Responses</title><style type="text/css">.rtcContent { padding: 30px; } .S0 { margin: 3px 10px 5px 4px; padding: 0px; line-height: 28.8px; min-height: 0px; white-space: pre-wrap; color: rgb(213, 80, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 24px; font-weight: 400; text-align: left;  }

Revision as of 16:25, 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 code is available in the collapsed sections below:

  • 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 Editor 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