Difference between revisions of "MATLAB:Plotting Discrete Responses"

From PrattWiki
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
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 .m file is available in the collapsed section below.  If you want to reconstitute it as a Live Code file:
*<div class="toccolours mw-collapsible" style="width:400px; overflow:auto;">
+
* Copy the text into a new .m file and save it
<div style="font-weight:bold;line-height:1.6;">MATLAB .m File</div>
+
* Click the down arrow on the '''Save''' button and select '''Save As'''
<div class="mw-collapsible-content mw-collapsed">
+
* Change the '''Save as type''' to '''Live Code''' and then save
<syntaxhighlight lang=matlab>
+
* <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>
 
%% 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>
+
== Live Code Results ==
</div></div?
 
== 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;  }
 
.S1 { margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;  }
 
.S1 { margin: 2px 10px 9px 4px; padding: 0px; line-height: 21px; min-height: 0px; white-space: pre-wrap; color: rgb(0, 0, 0); font-family: Helvetica, Arial, sans-serif; font-style: normal; font-size: 14px; font-weight: 400; text-align: left;  }
Line 123: 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