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}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)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.
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:
- Define a time vector
- Create a pure step function input
- Define the parameters for a 1st Order Low-Pass Filter.
- Apply the 1st Order Low-Pass Filter to the pure step input using a for-loop.
- Create a 2nd input = step function + noise
- 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_2023.m
clear all; format compact; format short; clc; close all;
%% a. Define the time and signal vectors
% Create the time vector
dt = 0.002;
t = 0 : dt : 0.4; %(s)
% Create the step function
s0 = ones(size(t)); % Initialize the step function
s0(1:5) = 0; % set data to zero before the step occurs.
% Plot the pure signal
figure;
plot(t,s0,'k');
grid on;
hold on; % Plot the other graphs on the same figure
%% b. Add noise to the signal
noise_std = 0.2; % Standard deviation of the noise
s1 = s0 + noise_std*randn(size(s0)); % signal + noise
plot(t,s1,'bs'); % Plot as blue squares
%% c. 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 weighted combination
% of the current measurement + the previous filter output.
bw = 10 %(Hz) Bandwidth
tau = 1/(2*pi*bw) %(s) 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)
gain_s = 1-gain_y
%% d. Apply the 1st Order Low-Pass Filter
% to the noisy signal using a for-loop.
y1 = zeros(size(s1)); % initial the output to zeros
t_len = length(t)
for k = 2:t_len
y1(k) = gain_y*y1(k-1) + gain_s*s1(k);
end
hold on;
plot(t,y1,'go'); % Plot as green circles
title({'First-Order Filter Step Function',...
['Bandwidth= ',num2str(bw),', Time Constant= ',num2str(tau)]});
% The { } allows a 2-line title
xlabel('Time');
legend('Pure Signal', 'Noisy Signal', 'Filtered Signal')
Solution
An example of the result for one realization of random noise is shown in this figure.
.
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:
.