Skip to main content
Library homepage
 
Engineering LibreTexts

9.4: Random Number Applications

  • Page ID
    87911
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

      In engineering applications, such as radar or satellite signals, the data has random noise. A process called filtering can be used to reduce the noise. There are multiple types of filters. The following is one example.

      Example \(\PageIndex{1}\) Low-Pass Filter with a Step Input

      A first-order, low-pass filter smooths noisy data by using a for-loop with the following equation, which sets the output at each time step to be a weighted average of the previous output and the current input:

      yk = gainy*yk-1 + gainx*xk

      where gainy + gainx = 1.

      The bandwidth chosen for the filter sets these gain parameters.

      The algorithm steps are:

      1. Define a time vector
      2. Create a pure step function input
      3. Define the parameters for a 1st Order Low-Pass Filter.
      4. Apply the 1st Order Low-Pass Filter to the pure step input using a for-loop.
      5. Create a 2nd input = step function + noise
      6. Apply the 1st Order Low-Pass Filter to the noisy input using a for-loop.

      Write a for loop to implement the filter equation.

      %% Low_Pass_Filter_Step.m
      clear all; format compact; format short; clc; close all;
      %% a. Define a time vector, create a pure step function input and plot it.
      % Create the time vector
      dt = 0.002; % s
      t = 0 : dt : 0.2; % s
      s0 = ones(size(t)); % Initialize the step function
      s0(1:5) = 0; % set data to zero before the step occurs.
      % Plot the step function
      figure;
      plot(t,s0,'k');
      title('Pure Step');
      grid on;

      %% b. Define the parameters for a 1st Order Low-Pass Filter
      % A 1st Order Low-Pass Filter smooths the data
      % Each iteration's output is a combination of the current measurement
      % plus the previous filter output.
      bw = 10 %(Hz) Bandwidth
      tau = 1/(2*pi*bw) % (s) 0.0318 Time constant
      % The output rises to a factor of (1-(1/e)) of the step in tau seconds
      gain_y = exp(-2*pi*bw*dt) % 0.8819
      gain_s = 1-gain_y % 0.1181
      y0 = zeros(size(s0)); % initial the output to zeros
      t_len = length(t)

      %% c. Apply the 1st Order Low-Pass Filter to the pure step input
      % using a for loop, to show the response without noise.
      % Plot the result.
      for k = 2:t_len
      y0(k) = gain_y*y0(k-1) + gain_s*s0(k);
      end
      %figure
      hold on;
      plot(t,y0,'r','LineWidth',2);
      % { } allow a 2-line title
      title({'First-Order Filter Step Response',...
      ['Bandwidth= ',num2str(bw),', Time Constant= ',num2str(tau)]});
      xlabel('Time');
      % y raises to 1- exp(-1) = 0.6321 in tau seconds

      %% d. Create a 2nd input = step function + noise.
      % Plot the noisy signal.
      noise_std = 0.2; % Standard deviation of the noise
      s1 = s0 + noise_std*randn(size(s0));
      hold on;
      plot(t,s1,'b','LineWidth',1.5);
      title('Step + Noisy Measurements');

      %% e. Apply the 1st Order Low-Pass Filter to the noisy input using a for-loop.
      % Plot the result. Add a legend.
      y1 = zeros(size(s1)); % initial the output to zeros
      t_len = length(t)
      % --- Note: Apply the fiter to the noisy s1 ---
      for k = 2:t_len
      y1(k) = gain_y*y1(k-1) + gain_s*s1(k);
      end
      hold on;
      plot(t,y1,'g','LineWidth',3);
      % { } allow a 2-line title
      title({'First-Order Filter Step Response',...
      ['Bandwidth= ',num2str(bw),', Time Constant= ',num2str(tau)]});
      xlabel('Time');
      legend('Pure Step', 'Filtered Pure Step', 'Noisy Step', 'Filtered Noisy Step')

      Low_Pass_Filter_Step.m

      Solution

      An example of the result for one realization of random noise is shown in this figure.

      Low_pass_filter_step.png

      .

      Exercise \(\PageIndex{1}\) Low-Pass Filter with a Sine Wave Input

      Modify Low_Pass_Filter_Step.m as follows:

      a. Change the input to a sine wave with this code:

      dt = 0.002; % s

      t = 0 : dt : 0.4; % s

      f1 = 10 % Hz

      s0 = sin(2*pi*f1*t);

      % Don't set any data to zero

      b. Because the sine wave has both positive values, delete this line: ylim([0,1.5]);

      c. Change the filter bandwidth from 10 to 20 Hz:

      bw = 20 %(Hz) Bandwidth

      d. Change the legend to be this:

      legend('Pure Since', 'Filtered Pure Sine', 'Noisy Sine','Filtered Noisy Sine')

      Answer

      One noise realization looks like this:

      Low_pass_filer_sine_wave.png

      .


      This page titled 9.4: Random Number Applications is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Carey Smith.

      • Was this article helpful?