spectral-analysis/index.html

1121 lines
63 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<!-- 2019-12-02 lun. 11:22 -->
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Compute Spectral Densities of signals with Matlab</title>
<meta name="generator" content="Org mode" />
<meta name="author" content="Dehaeze Thomas" />
<style type="text/css">
<!--/*--><![CDATA[/*><!--*/
.title { text-align: center;
margin-bottom: .2em; }
.subtitle { text-align: center;
font-size: medium;
font-weight: bold;
margin-top:0; }
.todo { font-family: monospace; color: red; }
.done { font-family: monospace; color: green; }
.priority { font-family: monospace; color: orange; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.org-right { margin-left: auto; margin-right: 0px; text-align: right; }
.org-left { margin-left: 0px; margin-right: auto; text-align: left; }
.org-center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
/* Languages per Org manual */
pre.src-asymptote:before { content: 'Asymptote'; }
pre.src-awk:before { content: 'Awk'; }
pre.src-C:before { content: 'C'; }
/* pre.src-C++ doesn't work in CSS */
pre.src-clojure:before { content: 'Clojure'; }
pre.src-css:before { content: 'CSS'; }
pre.src-D:before { content: 'D'; }
pre.src-ditaa:before { content: 'ditaa'; }
pre.src-dot:before { content: 'Graphviz'; }
pre.src-calc:before { content: 'Emacs Calc'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-fortran:before { content: 'Fortran'; }
pre.src-gnuplot:before { content: 'gnuplot'; }
pre.src-haskell:before { content: 'Haskell'; }
pre.src-hledger:before { content: 'hledger'; }
pre.src-java:before { content: 'Java'; }
pre.src-js:before { content: 'Javascript'; }
pre.src-latex:before { content: 'LaTeX'; }
pre.src-ledger:before { content: 'Ledger'; }
pre.src-lisp:before { content: 'Lisp'; }
pre.src-lilypond:before { content: 'Lilypond'; }
pre.src-lua:before { content: 'Lua'; }
pre.src-matlab:before { content: 'MATLAB'; }
pre.src-mscgen:before { content: 'Mscgen'; }
pre.src-ocaml:before { content: 'Objective Caml'; }
pre.src-octave:before { content: 'Octave'; }
pre.src-org:before { content: 'Org mode'; }
pre.src-oz:before { content: 'OZ'; }
pre.src-plantuml:before { content: 'Plantuml'; }
pre.src-processing:before { content: 'Processing.js'; }
pre.src-python:before { content: 'Python'; }
pre.src-R:before { content: 'R'; }
pre.src-ruby:before { content: 'Ruby'; }
pre.src-sass:before { content: 'Sass'; }
pre.src-scheme:before { content: 'Scheme'; }
pre.src-screen:before { content: 'Gnu Screen'; }
pre.src-sed:before { content: 'Sed'; }
pre.src-sh:before { content: 'shell'; }
pre.src-sql:before { content: 'SQL'; }
pre.src-sqlite:before { content: 'SQLite'; }
/* additional languages in org.el's org-babel-load-languages alist */
pre.src-forth:before { content: 'Forth'; }
pre.src-io:before { content: 'IO'; }
pre.src-J:before { content: 'J'; }
pre.src-makefile:before { content: 'Makefile'; }
pre.src-maxima:before { content: 'Maxima'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-picolisp:before { content: 'Pico Lisp'; }
pre.src-scala:before { content: 'Scala'; }
pre.src-shell:before { content: 'Shell Script'; }
pre.src-ebnf2ps:before { content: 'ebfn2ps'; }
/* additional language identifiers per "defun org-babel-execute"
in ob-*.el */
pre.src-cpp:before { content: 'C++'; }
pre.src-abc:before { content: 'ABC'; }
pre.src-coq:before { content: 'Coq'; }
pre.src-groovy:before { content: 'Groovy'; }
/* additional language identifiers from org-babel-shell-names in
ob-shell.el: ob-shell is the only babel language using a lambda to put
the execution function name together. */
pre.src-bash:before { content: 'bash'; }
pre.src-csh:before { content: 'csh'; }
pre.src-ash:before { content: 'ash'; }
pre.src-dash:before { content: 'dash'; }
pre.src-ksh:before { content: 'ksh'; }
pre.src-mksh:before { content: 'mksh'; }
pre.src-posh:before { content: 'posh'; }
/* Additional Emacs modes also supported by the LaTeX listings package */
pre.src-ada:before { content: 'Ada'; }
pre.src-asm:before { content: 'Assembler'; }
pre.src-caml:before { content: 'Caml'; }
pre.src-delphi:before { content: 'Delphi'; }
pre.src-html:before { content: 'HTML'; }
pre.src-idl:before { content: 'IDL'; }
pre.src-mercury:before { content: 'Mercury'; }
pre.src-metapost:before { content: 'MetaPost'; }
pre.src-modula-2:before { content: 'Modula-2'; }
pre.src-pascal:before { content: 'Pascal'; }
pre.src-ps:before { content: 'PostScript'; }
pre.src-prolog:before { content: 'Prolog'; }
pre.src-simula:before { content: 'Simula'; }
pre.src-tcl:before { content: 'tcl'; }
pre.src-tex:before { content: 'TeX'; }
pre.src-plain-tex:before { content: 'Plain TeX'; }
pre.src-verilog:before { content: 'Verilog'; }
pre.src-vhdl:before { content: 'VHDL'; }
pre.src-xml:before { content: 'XML'; }
pre.src-nxml:before { content: 'XML'; }
/* add a generic configuration mode; LaTeX export needs an additional
(add-to-list 'org-latex-listings-langs '(conf " ")) in .emacs */
pre.src-conf:before { content: 'Configuration File'; }
table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.org-right { text-align: center; }
th.org-left { text-align: center; }
th.org-center { text-align: center; }
td.org-right { text-align: right; }
td.org-left { text-align: left; }
td.org-center { text-align: center; }
dt { font-weight: bold; }
.footpara { display: inline; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.equation-container {
display: table;
text-align: center;
width: 100%;
}
.equation {
vertical-align: middle;
}
.equation-label {
display: table-cell;
text-align: right;
vertical-align: middle;
}
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
.org-svg { width: 90%; }
/*]]>*/-->
</style>
<link rel="stylesheet" type="text/css" href="./css/htmlize.css"/>
<link rel="stylesheet" type="text/css" href="./css/readtheorg.css"/>
<link rel="stylesheet" type="text/css" href="./css/zenburn.css"/>
<script type="text/javascript" src="./js/jquery.min.js"></script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script type="text/javascript" src="./js/jquery.stickytableheaders.min.js"></script>
<script type="text/javascript" src="./js/readtheorg.js"></script>
<script type="text/javascript">
/*
@licstart The following is the entire license notice for the
JavaScript code in this tag.
Copyright (C) 2012-2019 Free Software Foundation, Inc.
The JavaScript code in this tag is free software: you can
redistribute it and/or modify it under the terms of the GNU
General Public License (GNU GPL) as published by the Free Software
Foundation, either version 3 of the License, or (at your option)
any later version. The code is distributed WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU GPL for more details.
As additional permission under GNU GPL version 3 section 7, you
may distribute non-source (e.g., minimized or compacted) forms of
that code without the copy of the GNU GPL normally required by
section 4, provided you include this license notice and a URL
through which recipients can access the Corresponding Source.
@licend The above is the entire license notice
for the JavaScript code in this tag.
*/
<!--/*--><![CDATA[/*><!--*/
function CodeHighlightOn(elem, id)
{
var target = document.getElementById(id);
if(null != target) {
elem.cacheClassElem = elem.className;
elem.cacheClassTarget = target.className;
target.className = "code-highlighted";
elem.className = "code-highlighted";
}
}
function CodeHighlightOff(elem, id)
{
var target = document.getElementById(id);
if(elem.cacheClassElem)
elem.className = elem.cacheClassElem;
if(elem.cacheClassTarget)
target.className = elem.cacheClassTarget;
}
/*]]>*///-->
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
displayAlign: "center",
displayIndent: "0em",
"HTML-CSS": { scale: 100,
linebreaks: { automatic: "false" },
webFont: "TeX"
},
SVG: {scale: 100,
linebreaks: { automatic: "false" },
font: "TeX"},
NativeMML: {scale: 100},
TeX: { equationNumbers: {autoNumber: "AMS"},
MultLineWidth: "85%",
TagSide: "right",
TagIndent: ".8em"
}
});
</script>
<script type="text/javascript"
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS_HTML"></script>
</head>
<body>
<div id="content">
<h1 class="title">Compute Spectral Densities of signals with Matlab</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#org9310776">1. Spectral Analysis - Basics</a>
<ul>
<li><a href="#org2041123">1.1. PSD of ADC quantization noise</a></li>
<li><a href="#orgb854582">1.2. Sensitivity of the instrumentation</a></li>
<li><a href="#org78ed1de">1.3. Convert the time domain from volts to velocity</a></li>
<li><a href="#org8775e6e">1.4. Power Spectral Density and Amplitude Spectral Density</a></li>
<li><a href="#orgcba62e6">1.5. Modification of a signal's Power Spectral Density when going through an LTI system</a></li>
<li><a href="#org3adf2bb">1.6. From PSD of the velocity to the PSD of the displacement</a></li>
<li><a href="#org83b26dc">1.7. Cumulative Power/Amplitude Spectrum</a></li>
<li><a href="#org5ace856">1.8. <span class="todo TODO">TODO</span> Add best practices from the Article and simple snippet that works</a></li>
</ul>
</li>
<li><a href="#orga6e1da0">2. Technique 1 : Approximation with a transfer function</a>
<ul>
<li><a href="#orge44fa26">2.1. Signal's PSD</a></li>
<li><a href="#org35e87c7">2.2. Transfer Function that approximate the ASD</a></li>
<li><a href="#org5b0597b">2.3. Generated Time domain signal</a></li>
<li><a href="#org2bb9116">2.4. Comparison of the Power Spectral Densities</a></li>
<li><a href="#org6d66594">2.5. Simulink</a></li>
</ul>
</li>
<li><a href="#orgd4fbbd9">3. Technique 2 : IFFT</a>
<ul>
<li><a href="#org98e4f2a">3.1. Signal's PSD</a></li>
<li><a href="#orgcb607b3">3.2. Algorithm</a></li>
<li><a href="#org4ff718c">3.3. Obtained Time Domain Signal</a></li>
<li><a href="#orgc9527e8">3.4. PSD Comparison</a></li>
</ul>
</li>
<li><a href="#org7fe2f42">4. <span class="todo TODO">TODO</span> Compute the Noise level and Signal level from PSD</a>
<ul>
<li><a href="#org640e940">4.1. Computation</a></li>
</ul>
</li>
</ul>
</div>
</div>
<p>
This document presents the mathematics as well as the matlab scripts to do various spectral analysis on a measured signal.
</p>
<p>
Some matlab documentation about Spectral Analysis can be found <a href="https://fr.mathworks.com/help/signal/ug/spectral-analysis.html">here</a>.
</p>
<p>
First, in section <a href="#orgd149260">1</a>, some basics of spectral analysis are presented.
</p>
<p>
In some cases, we want to generate a time domain signal with defined Power Spectral Density.
Two methods are presented in sections <a href="#org2ea1c7b">2</a> and <a href="#org84b5cd1">3</a>.
</p>
<p>
Finally, some notes are done on how to compute the noise level and signal level from a given Power Spectral Density in section <a href="#org7bce553">4</a>.
</p>
<div id="outline-container-org9310776" class="outline-2">
<h2 id="org9310776"><span class="section-number-2">1</span> Spectral Analysis - Basics</h2>
<div class="outline-text-2" id="text-1">
<p>
<a id="orgd149260"></a>
</p>
<p>
Typically this signal is coming from an inertial sensor, a force sensor or any other sensor.
</p>
<p>
We here take the example of a signal coming from a Geophone measurement the vertical velocity of the floor at the ESRF.
</p>
</div>
<div id="outline-container-org2041123" class="outline-3">
<h3 id="org2041123"><span class="section-number-3">1.1</span> PSD of ADC quantization noise</h3>
<div class="outline-text-3" id="text-1-1">
<p>
This is taken from <a href="https://www.allaboutcircuits.com/technical-articles/quantization-nois-amplitude-quantization-error-analog-to-digital-converters/">here</a>.
</p>
<p>
Let's note:
</p>
<ul class="org-ul">
<li>\(q\) is the corresponding value in [V] of the least significant bit (LSB)</li>
<li>\(\Delta V\) is the full range of the ADC in [V]</li>
<li>\(n\) is the number of ADC's bits</li>
<li>\(f_s\) is the sample frequency in [Hz]</li>
</ul>
<p>
Let's suppose that the ADC is ideal.
The only noise comes from the quantization error.
Interestingly, the noise amplitude is uniformly distributed.
</p>
<p>
The quantization noise can take a value between \(\pm q/2\), and the probability density function is constant in this range (i.e., its a uniform distribution).
Since the integral of the probability density function is equal to one, its value will be \(1/q\) for \(-q/2 < e < q/2\) (Fig. <a href="#orgee629ae">1</a>).
</p>
<div id="orgee629ae" class="figure">
<p><img src="figs/probability_density_function_adc.png" alt="probability_density_function_adc.png" />
</p>
<p><span class="figure-number">Figure 1: </span>Probability density function \(p(e)\) of the ADC error \(e\)</p>
</div>
<p>
Now, we can calculate the time average power of the quantization noise as
</p>
\begin{equation}
P_q = \int_{-q/2}^{q/2} e^2 p(e) de = \frac{q^2}{12}
\end{equation}
<p>
The other important parameter of a noise source is the power spectral density (PSD), which indicates how the noise power spreads in different frequency bands.
To find the power spectral density, we need to calculate the Fourier transform of the autocorrelation function of the noise.
</p>
<p>
Assuming that the noise samples are not correlated with one another, we can approximate the autocorrelation function with a delta function in the time domain.
Since the Fourier transform of a delta function is equal to one, the <b>power spectral density will be frequency independent</b>.
Therefore, the quantization noise is white noise with total power equal to \(P_q = \frac{q^2}{12}\).
</p>
<p>
Thus, the two-sided PSD (from \(\frac{-f_s}{2}\) to \(\frac{f_s}{2}\)), we should divide the noise power \(P_q\) by \(f_s\):
</p>
\begin{equation}
\int_{-f_s/2}^{f_s/2} \Gamma(f) d f = f_s \Gamma = \frac{q^2}{12}
\end{equation}
<p>
Finally:
</p>
\begin{equation}
\begin{align}
\Gamma &= \frac{q^2}{12 f_s} \\
&= \frac{\left(\frac{\Delta V}{2^n}\right)^2}{12 f_s} \text{ in } \left[ \frac{V^2}{Hz} \right]
\end{align}
\end{equation}
</div>
</div>
<div id="outline-container-orgb854582" class="outline-3">
<h3 id="orgb854582"><span class="section-number-3">1.2</span> Sensitivity of the instrumentation</h3>
<div class="outline-text-3" id="text-1-2">
<p>
The measured signal \(x\) by the ADC is in Volts.
The corresponding real velocity \(v\) in m/s.
</p>
<p>
To obtain the real quantity as measured by the sensor, one have to know the sensitivity of the sensors and electronics used.
</p>
<div id="orgad69fc7" class="figure">
<p><img src="figs/velocity_to_voltage.png" alt="velocity_to_voltage.png" />
</p>
<p><span class="figure-number">Figure 2: </span>Schematic of the instrumentation used for the measurement</p>
</div>
</div>
</div>
<div id="outline-container-org78ed1de" class="outline-3">
<h3 id="org78ed1de"><span class="section-number-3">1.3</span> Convert the time domain from volts to velocity</h3>
<div class="outline-text-3" id="text-1-3">
<p>
Let's say, we know that the sensitivity of the geophone used is
\[ G_g(s) = G_0 \frac{\frac{s}{2\pi f_0}}{1 + \frac{s}{2\pi f_0}} \quad \left[\frac{V}{m/s}\right] \]
</p>
<div class="org-src-container">
<pre class="src src-matlab">G0 = <span class="org-highlight-numbers-number">88</span>; <span class="org-comment">% Sensitivity [V/(m/s)]</span>
f0 = <span class="org-highlight-numbers-number">2</span>; <span class="org-comment">% Cut-off frequency [Hz]</span>
Gg = G0<span class="org-type">*</span><span class="org-rainbow-delimiters-depth-1">(</span>s<span class="org-type">/</span><span class="org-highlight-numbers-number">2</span><span class="org-type">/</span><span class="org-constant">pi</span><span class="org-type">/</span>f0<span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">/</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">1</span><span class="org-type">+</span>s<span class="org-type">/</span><span class="org-highlight-numbers-number">2</span><span class="org-type">/</span><span class="org-constant">pi</span><span class="org-type">/</span>f0<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
And the gain of the amplifier is 1000: \(G_m(s) = 1000\).
</p>
<div class="org-src-container">
<pre class="src src-matlab">Gm = <span class="org-highlight-numbers-number">1000</span>;
</pre>
</div>
<p>
If \({G_m(s)}^{-1} {G_g(s)}^{-1}\) is proper, we can simulate this dynamical system to go from the voltage to the velocity units (figure <a href="#orgf9120d4">3</a>).
</p>
<div class="org-src-container">
<pre class="src src-matlab">data = load<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'mat/data_028.mat'</span>, <span class="org-string">'data'</span><span class="org-rainbow-delimiters-depth-1">)</span>; data = data.data;
t = data<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-type">:</span>, <span class="org-highlight-numbers-number">3</span><span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-comment">% [s]</span>
x = data<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-type">:</span>, <span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">-</span>mean<span class="org-rainbow-delimiters-depth-1">(</span>data<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-type">:</span>, <span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-comment">% The offset if removed (coming from the voltage amplifier) [v]</span>
dt = t<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">-</span>t<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span>; Fs = <span class="org-highlight-numbers-number">1</span><span class="org-type">/</span>dt;
</pre>
</div>
<div id="orgf9120d4" class="figure">
<p><img src="figs/voltage_to_velocity.png" alt="voltage_to_velocity.png" />
</p>
<p><span class="figure-number">Figure 3: </span>Schematic of the instrumentation used for the measurement</p>
</div>
<p>
We simulate this system with matlab:
</p>
<div class="org-src-container">
<pre class="src src-matlab">v = lsim<span class="org-rainbow-delimiters-depth-1">(</span>inv<span class="org-rainbow-delimiters-depth-2">(</span>Gg<span class="org-type">*</span>Gm<span class="org-rainbow-delimiters-depth-2">)</span>, x, t<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
And we plot the obtained velocity
</p>
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-type">figure</span>;
plot<span class="org-rainbow-delimiters-depth-1">(</span>t, v<span class="org-rainbow-delimiters-depth-1">)</span>;
xlabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">"Time </span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">[</span></span><span class="org-string">s</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">]</span></span><span class="org-string">"</span><span class="org-rainbow-delimiters-depth-1">)</span>; ylabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">"Velocity </span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">[</span></span><span class="org-string">m/s</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">]</span></span><span class="org-string">"</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<div id="org8b13b82" class="figure">
<p><img src="figs/velocity_time.png" alt="velocity_time.png" />
</p>
<p><span class="figure-number">Figure 4: </span>Measured Velocity</p>
</div>
</div>
</div>
<div id="outline-container-org8775e6e" class="outline-3">
<h3 id="org8775e6e"><span class="section-number-3">1.4</span> Power Spectral Density and Amplitude Spectral Density</h3>
<div class="outline-text-3" id="text-1-4">
<p>
We now have the velocity in the time domain:
\[ v(t)\ [m/s] \]
</p>
<p>
To compute the Power Spectral Density (PSD):
\[ S_v(f)\ \left[\frac{(m/s)^2}{Hz}\right] \]
</p>
<p>
To compute that with matlab, we use the <code>pwelch</code> function.
</p>
<p>
We first have to defined a window:
</p>
<div class="org-src-container">
<pre class="src src-matlab">win = hanning<span class="org-rainbow-delimiters-depth-1">(</span>ceil<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-highlight-numbers-number">10</span><span class="org-type">*</span>Fs<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>; % <span class="org-highlight-numbers-number">10s</span> window
</pre>
</div>
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-rainbow-delimiters-depth-1">[</span>Sv, f<span class="org-rainbow-delimiters-depth-1">]</span> = pwelch<span class="org-rainbow-delimiters-depth-1">(</span>v, win, <span class="org-rainbow-delimiters-depth-2">[]</span>, <span class="org-rainbow-delimiters-depth-2">[]</span>, Fs<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-type">figure</span>;
loglog<span class="org-rainbow-delimiters-depth-1">(</span>f, Sv<span class="org-rainbow-delimiters-depth-1">)</span>;
xlabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Frequency </span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">[</span></span><span class="org-string">Hz</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">]</span></span><span class="org-string">'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
ylabel<span class="org-rainbow-delimiters-depth-1">(</span>'Power Spectral Density $<span class="org-type">\</span>left<span class="org-rainbow-delimiters-depth-2">[</span><span class="org-type">\</span>frac<span class="org-rainbow-delimiters-depth-3">{</span><span class="org-rainbow-delimiters-depth-4">(</span>m<span class="org-type">/</span>s<span class="org-rainbow-delimiters-depth-4">)</span><span class="org-type">^</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-3">}{</span>Hz<span class="org-rainbow-delimiters-depth-3">}</span><span class="org-type">\</span>right<span class="org-rainbow-delimiters-depth-2">]</span>$'<span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
The Amplitude Spectral Density (ASD) is the square root of the Power Spectral Density:
</p>
\begin{equation}
\Gamma_{vv}(f) = \sqrt{S_{vv}(f)} \quad \left[ \frac{m/s}{\sqrt{Hz}} \right]
\end{equation}
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-type">figure</span>;
loglog<span class="org-rainbow-delimiters-depth-1">(</span>f, sqrt<span class="org-rainbow-delimiters-depth-2">(</span>Sv<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>;
xlabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Frequency </span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">[</span></span><span class="org-string">Hz</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">]</span></span><span class="org-string">'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
ylabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Amplitude Spectral Density $\left</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">[</span></span><span class="org-string">\frac</span><span class="org-string"><span class="org-rainbow-delimiters-depth-3">{</span></span><span class="org-string">m/s</span><span class="org-string"><span class="org-rainbow-delimiters-depth-3">}{</span></span><span class="org-string">\sqrt</span><span class="org-string"><span class="org-rainbow-delimiters-depth-4">{</span></span><span class="org-string">Hz</span><span class="org-string"><span class="org-rainbow-delimiters-depth-4">}</span></span><span class="org-string"><span class="org-rainbow-delimiters-depth-3">}</span></span><span class="org-string">\right</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">]</span></span><span class="org-string">$'</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgcba62e6" class="outline-3">
<h3 id="orgcba62e6"><span class="section-number-3">1.5</span> Modification of a signal's Power Spectral Density when going through an LTI system</h3>
<div class="outline-text-3" id="text-1-5">
<div id="org5a3eaea" class="figure">
<p><img src="figs/velocity_to_voltage_psd.png" alt="velocity_to_voltage_psd.png" />
</p>
<p><span class="figure-number">Figure 5: </span>Schematic of the instrumentation used for the measurement</p>
</div>
<p>
We can show that:
</p>
\begin{equation}
S_{yy}(\omega) = \left|G(j\omega)\right|^2 S_{xx}(\omega)
\end{equation}
<p>
And we also have:
</p>
\begin{equation}
\Gamma_{yy}(\omega) = \left|G(j\omega)\right| \Gamma_{xx}(\omega)
\end{equation}
</div>
</div>
<div id="outline-container-org3adf2bb" class="outline-3">
<h3 id="org3adf2bb"><span class="section-number-3">1.6</span> From PSD of the velocity to the PSD of the displacement</h3>
<div class="outline-text-3" id="text-1-6">
<div id="org14e4432" class="figure">
<p><img src="figs/velocity_to_displacement_psd.png" alt="velocity_to_displacement_psd.png" />
</p>
<p><span class="figure-number">Figure 6: </span>Schematic of the instrumentation used for the measurement</p>
</div>
<p>
The displacement is the integral of the velocity.
</p>
<p>
We then have that
</p>
\begin{equation}
S_{xx}(\omega) = \left|\frac{1}{j \omega}\right|^2 S_{vv}(\omega)
\end{equation}
<p>
Using a frequency variable in Hz:
</p>
\begin{equation}
S_{xx}(f) = \left| \frac{1}{j 2\pi f} \right|^2 S_{vv}(f)
\end{equation}
<p>
For the Amplitude Spectral Density:
</p>
\begin{equation}
\Gamma_{xx}(f) = \frac{1}{2\pi f} \Gamma_{vv}(f)
\end{equation}
<div class="note">
\begin{equation}
S_{xx}(\omega = 1) = S_{vv}(\omega = 1)
\end{equation}
</div>
<p>
Now if we want to obtain the Power Spectral Density of the Position or Acceleration:
For each frequency:
\[ \left| \frac{d sin(2 \pi f t)}{dt} \right| = | 2 \pi f | \times | \cos(2\pi f t) | \]
</p>
<p>
\[ \left| \int_0^t sin(2 \pi f \tau) d\tau \right| = \left| \frac{1}{2 \pi f} \right| \times | \cos(2\pi f t) | \]
</p>
<p>
\[ ASD_x(f) = \frac{1}{2\pi f} ASD_v(f) \ \left[\frac{m}{\sqrt{Hz}}\right] \]
</p>
<p>
\[ ASD_a(f) = 2\pi f ASD_v(f) \ \left[\frac{m/s^2}{\sqrt{Hz}}\right] \]
And we have
\[ PSD_x(f) = {ASD_x(f)}^2 = \frac{1}{(2 \pi f)^2} {ASD_v(f)}^2 = \frac{1}{(2 \pi f)^2} PSD_v(f) \]
</p>
<p>
Note here that we always have
\[ PSD_x \left(f = \frac{1}{2\pi}\right) = PSD_v \left(f = \frac{1}{2\pi}\right) = PSD_a \left(f = \frac{1}{2\pi}\right), \quad \frac{1}{2\pi} \approx 0.16 [Hz] \]
</p>
</div>
</div>
<div id="outline-container-org83b26dc" class="outline-3">
<h3 id="org83b26dc"><span class="section-number-3">1.7</span> Cumulative Power/Amplitude Spectrum</h3>
<div class="outline-text-3" id="text-1-7">
<p>
If we want to compute the Cumulative Power Spectrum:
\[ CPS_v(f) = \int_0^f PSD_v(\nu) d\nu \quad [(m/s)^2] \]
</p>
<p>
We can also want to integrate from high frequency to low frequency:
\[ CPS_v(f) = \int_f^\infty PSD_v(\nu) d\nu \quad [(m/s)^2] \]
</p>
<p>
The Cumulative Amplitude Spectrum is then the square root of the Cumulative Power Spectrum:
\[ CAS_v(f) = \sqrt{CPS_v(f)} = \sqrt{\int_f^\infty PSD_v(\nu) d\nu} \quad [m/s] \]
</p>
<p>
Then, we can obtain the Root Mean Square value of the velocity:
\[ v_{\text{rms}} = CAS_v(0) \quad [m/s \ \text{rms}] \]
</p>
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-type">figure</span>;
hold on;
plot<span class="org-rainbow-delimiters-depth-1">(</span>f, cumtrapz<span class="org-rainbow-delimiters-depth-2">(</span>f, Sv<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>;
hold off;
<span class="org-type">set</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-variable-name">gca</span>, <span class="org-string">'xscale'</span>, <span class="org-string">'log'</span><span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-type">set</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-variable-name">gca</span>, <span class="org-string">'yscale'</span>, <span class="org-string">'log'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
xlabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Frequency </span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">[</span></span><span class="org-string">Hz</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">]</span></span><span class="org-string">'</span><span class="org-rainbow-delimiters-depth-1">)</span>; ylabel<span class="org-rainbow-delimiters-depth-1">(</span>'Cumulative Power Spectrum <span class="org-rainbow-delimiters-depth-2">[</span>$<span class="org-rainbow-delimiters-depth-3">(</span>m<span class="org-type">/</span>s<span class="org-rainbow-delimiters-depth-3">)</span><span class="org-type">^</span><span class="org-highlight-numbers-number">2$</span><span class="org-rainbow-delimiters-depth-2">]</span><span class="org-type">'</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
In order to integrate from high frequency to low frequency:
</p>
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-type">figure</span>;
hold on;
plot<span class="org-rainbow-delimiters-depth-1">(</span>f, flip<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-type">-</span>cumtrapz<span class="org-rainbow-delimiters-depth-3">(</span>flip<span class="org-rainbow-delimiters-depth-4">(</span>f<span class="org-rainbow-delimiters-depth-4">)</span>, flip<span class="org-rainbow-delimiters-depth-4">(</span>Sv<span class="org-rainbow-delimiters-depth-4">)</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>;
hold off;
<span class="org-type">set</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-variable-name">gca</span>, <span class="org-string">'xscale'</span>, <span class="org-string">'log'</span><span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-type">set</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-variable-name">gca</span>, <span class="org-string">'yscale'</span>, <span class="org-string">'log'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
xlabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Frequency </span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">[</span></span><span class="org-string">Hz</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">]</span></span><span class="org-string">'</span><span class="org-rainbow-delimiters-depth-1">)</span>; ylabel<span class="org-rainbow-delimiters-depth-1">(</span>'Cumulative Power Spectrum <span class="org-rainbow-delimiters-depth-2">[</span>$<span class="org-rainbow-delimiters-depth-3">(</span>m<span class="org-type">/</span>s<span class="org-rainbow-delimiters-depth-3">)</span><span class="org-type">^</span><span class="org-highlight-numbers-number">2$</span><span class="org-rainbow-delimiters-depth-2">]</span><span class="org-type">'</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-org5ace856" class="outline-3">
<h3 id="org5ace856"><span class="section-number-3">1.8</span> <span class="todo TODO">TODO</span> Add best practices from the Article and simple snippet that works</h3>
</div>
</div>
<div id="outline-container-orga6e1da0" class="outline-2">
<h2 id="orga6e1da0"><span class="section-number-2">2</span> Technique 1 : Approximation with a transfer function</h2>
<div class="outline-text-2" id="text-2">
<p>
<a id="org2ea1c7b"></a>
</p>
</div>
<div id="outline-container-orge44fa26" class="outline-3">
<h3 id="orge44fa26"><span class="section-number-3">2.1</span> Signal's PSD</h3>
<div class="outline-text-3" id="text-2-1">
<p>
We load the PSD of the signal we wish to replicate.
</p>
<div class="org-src-container">
<pre class="src src-matlab">load<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'./mat/dist_psd.mat'</span>, <span class="org-string">'dist_f'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
We remove the first value with very high PSD.
</p>
<div class="org-src-container">
<pre class="src src-matlab">dist_f.f = dist_f.f<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">3</span><span class="org-type">:</span>end<span class="org-rainbow-delimiters-depth-1">)</span>;
dist_f.psd_gm = dist_f.psd_gm<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">3</span><span class="org-type">:</span>end<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
The PSD of the signal is shown on figure <a href="#fig:psd_ground_motion">fig:psd_ground_motion</a>.
</p>
<div id="org809f585" class="figure">
<p><img src="figs/psd_ground_motion.png" alt="psd_ground_motion.png" />
</p>
<p><span class="figure-number">Figure 7: </span>PSD of the signal (<a href="./figs/psd_ground_motion.png">png</a>, <a href="./figs/psd_ground_motion.pdf">pdf</a>)</p>
</div>
</div>
</div>
<div id="outline-container-org35e87c7" class="outline-3">
<h3 id="org35e87c7"><span class="section-number-3">2.2</span> Transfer Function that approximate the ASD</h3>
<div class="outline-text-3" id="text-2-2">
<div class="org-src-container">
<pre class="src src-matlab">G_gm = <span class="org-highlight-numbers-number">0</span>.<span class="org-highlight-numbers-number">002</span><span class="org-type">*</span><span class="org-rainbow-delimiters-depth-1">(</span>s<span class="org-type">^</span><span class="org-highlight-numbers-number">2</span> <span class="org-type">+</span> <span class="org-highlight-numbers-number">3</span>.<span class="org-highlight-numbers-number">169</span><span class="org-type">*</span>s <span class="org-type">+</span> <span class="org-highlight-numbers-number">27</span>.<span class="org-highlight-numbers-number">74</span><span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">/</span><span class="org-rainbow-delimiters-depth-1">(</span>s<span class="org-type">*</span><span class="org-rainbow-delimiters-depth-2">(</span>s<span class="org-type">+</span><span class="org-highlight-numbers-number">32</span>.<span class="org-highlight-numbers-number">73</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-type">*</span><span class="org-rainbow-delimiters-depth-2">(</span>s<span class="org-type">+</span><span class="org-highlight-numbers-number">8</span>.<span class="org-highlight-numbers-number">829</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-type">*</span><span class="org-rainbow-delimiters-depth-2">(</span>s<span class="org-type">+</span><span class="org-highlight-numbers-number">7</span>.<span class="org-highlight-numbers-number">983</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-type">^</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
</div>
</div>
<div id="outline-container-org5b0597b" class="outline-3">
<h3 id="org5b0597b"><span class="section-number-3">2.3</span> Generated Time domain signal</h3>
<div class="outline-text-3" id="text-2-3">
<div class="org-src-container">
<pre class="src src-matlab">Fs = <span class="org-highlight-numbers-number">2</span><span class="org-type">*</span>dist_f.f<span class="org-rainbow-delimiters-depth-1">(</span>end<span class="org-rainbow-delimiters-depth-1">)</span>;
Ts = <span class="org-highlight-numbers-number">1</span><span class="org-type">/</span>Fs;
t = <span class="org-highlight-numbers-number">0</span><span class="org-type">:</span>Ts<span class="org-type">:</span><span class="org-highlight-numbers-number">500</span>;
u = sqrt<span class="org-rainbow-delimiters-depth-1">(</span>Fs<span class="org-type">/</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">*</span>randn<span class="org-rainbow-delimiters-depth-1">(</span>length<span class="org-rainbow-delimiters-depth-2">(</span>t<span class="org-rainbow-delimiters-depth-2">)</span>, <span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<div class="org-src-container">
<pre class="src src-matlab">u_o = lsim<span class="org-rainbow-delimiters-depth-1">(</span>G_gm, u, t<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-type">figure</span>;
plot<span class="org-rainbow-delimiters-depth-1">(</span>t, u_o<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
</div>
</div>
<div id="outline-container-org2bb9116" class="outline-3">
<h3 id="org2bb9116"><span class="section-number-3">2.4</span> Comparison of the Power Spectral Densities</h3>
<div class="outline-text-3" id="text-2-4">
<div class="org-src-container">
<pre class="src src-matlab">nx = length<span class="org-rainbow-delimiters-depth-1">(</span>u_o<span class="org-rainbow-delimiters-depth-1">)</span>;
na = <span class="org-highlight-numbers-number">16</span>;
win = hanning<span class="org-rainbow-delimiters-depth-1">(</span>floor<span class="org-rainbow-delimiters-depth-2">(</span>nx<span class="org-type">/</span>na<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>;
<span class="org-rainbow-delimiters-depth-1">[</span>pxx, f<span class="org-rainbow-delimiters-depth-1">]</span> = pwelch<span class="org-rainbow-delimiters-depth-1">(</span>u_o, win, <span class="org-highlight-numbers-number">0</span>, <span class="org-rainbow-delimiters-depth-2">[]</span>, Fs<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
Finally, we compare the PSD of the original signal and the obtained signal on figure <a href="#fig:psd_comparison">fig:psd_comparison</a>.
</p>
</div>
</div>
<div id="outline-container-org6d66594" class="outline-3">
<h3 id="org6d66594"><span class="section-number-3">2.5</span> Simulink</h3>
<div class="outline-text-3" id="text-2-5">
<div id="org3a810b6" class="figure">
<p><img src="figs/simulink_psd_generate.png" alt="simulink_psd_generate.png" />
</p>
<p><span class="figure-number">Figure 8: </span>Simulink Schematic</p>
</div>
<p>
The parameters for the <code>Band-Limited White Noise</code> are:
</p>
<ul class="org-ul">
<li>Noise Power: 1</li>
</ul>
<div class="org-src-container">
<pre class="src src-matlab">nx = length<span class="org-rainbow-delimiters-depth-1">(</span>out.u_gm.Data<span class="org-rainbow-delimiters-depth-1">)</span>;
na = <span class="org-highlight-numbers-number">8</span>;
win = hanning<span class="org-rainbow-delimiters-depth-1">(</span>floor<span class="org-rainbow-delimiters-depth-2">(</span>nx<span class="org-type">/</span>na<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>;
<span class="org-rainbow-delimiters-depth-1">[</span>pxx, f<span class="org-rainbow-delimiters-depth-1">]</span> = pwelch<span class="org-rainbow-delimiters-depth-1">(</span>out.u_gm.Data, win, <span class="org-highlight-numbers-number">0</span>, <span class="org-rainbow-delimiters-depth-2">[]</span>, <span class="org-highlight-numbers-number">1e3</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
</div>
</div>
</div>
<div id="outline-container-orgd4fbbd9" class="outline-2">
<h2 id="orgd4fbbd9"><span class="section-number-2">3</span> Technique 2 : IFFT</h2>
<div class="outline-text-2" id="text-3">
<p>
<a id="org84b5cd1"></a>
</p>
<p>
The technique comes from <a class='org-ref-reference' href="#preumont94_random_vibrat_spect_analy">preumont94_random_vibrat_spect_analy</a> (section 12.11).
</p>
</div>
<div id="outline-container-org98e4f2a" class="outline-3">
<h3 id="org98e4f2a"><span class="section-number-3">3.1</span> Signal's PSD</h3>
<div class="outline-text-3" id="text-3-1">
<p>
We load the PSD of the signal we wish to replicate.
</p>
<div class="org-src-container">
<pre class="src src-matlab">load<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'./mat/dist_psd.mat'</span>, <span class="org-string">'dist_f'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
We remove the first value with very high PSD.
</p>
<div class="org-src-container">
<pre class="src src-matlab">dist_f.f = dist_f.f<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">3</span><span class="org-type">:</span>end<span class="org-rainbow-delimiters-depth-1">)</span>;
dist_f.psd_gm = dist_f.psd_gm<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">3</span><span class="org-type">:</span>end<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
The PSD of the signal is shown on figure <a href="#fig:psd_original">fig:psd_original</a>.
</p>
<div id="org7969683" class="figure">
<p><img src="figs/psd_original.png" alt="psd_original.png" />
</p>
<p><span class="figure-number">Figure 9: </span>PSD of the original signal (<a href="./figs/psd_original.png">png</a>, <a href="./figs/psd_original.pdf">pdf</a>)</p>
</div>
</div>
</div>
<div id="outline-container-orgcb607b3" class="outline-3">
<h3 id="orgcb607b3"><span class="section-number-3">3.2</span> Algorithm</h3>
<div class="outline-text-3" id="text-3-2">
<p>
We define some parameters.
</p>
<div class="org-src-container">
<pre class="src src-matlab">Fs = <span class="org-highlight-numbers-number">2</span><span class="org-type">*</span>dist_f.f<span class="org-rainbow-delimiters-depth-1">(</span>end<span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-comment">% Sampling Frequency of data is twice the maximum frequency of the PSD vector [Hz]</span>
N = <span class="org-highlight-numbers-number">2</span><span class="org-type">*</span>length<span class="org-rainbow-delimiters-depth-1">(</span>dist_f.f<span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-comment">% Number of Samples match the one of the wanted PSD</span>
T0 = N<span class="org-type">/</span>Fs; <span class="org-comment">% Signal Duration [s]</span>
df = <span class="org-highlight-numbers-number">1</span><span class="org-type">/</span>T0; <span class="org-comment">% Frequency resolution of the DFT [Hz]</span>
% Also equal to (dist_f.f(<span class="org-highlight-numbers-number">2</span>)<span class="org-type">-</span>dist_f.f(<span class="org-highlight-numbers-number">1</span>))
</pre>
</div>
<p>
We then specify the wanted PSD.
</p>
<div class="org-src-container">
<pre class="src src-matlab">phi = dist_f.psd_gm;
</pre>
</div>
<p>
Create amplitudes corresponding to wanted PSD.
</p>
<div class="org-src-container">
<pre class="src src-matlab">C = zeros<span class="org-rainbow-delimiters-depth-1">(</span>N<span class="org-type">/</span><span class="org-highlight-numbers-number">2</span>,<span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span>;
<span class="org-keyword">for</span> <span class="org-variable-name"><span class="org-constant">i</span></span> = <span class="org-constant"><span class="org-highlight-numbers-number">1</span></span><span class="org-constant">:N/</span><span class="org-constant"><span class="org-highlight-numbers-number">2</span></span>
C<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-constant">i</span><span class="org-rainbow-delimiters-depth-1">)</span> = sqrt<span class="org-rainbow-delimiters-depth-1">(</span>phi<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-constant">i</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-type">*</span>df<span class="org-rainbow-delimiters-depth-1">)</span>;
<span class="org-keyword">end</span>
</pre>
</div>
<p>
Add random phase to <code>C</code>.
</p>
<div class="org-src-container">
<pre class="src src-matlab">theta = <span class="org-highlight-numbers-number">2</span><span class="org-type">*</span><span class="org-constant">pi</span><span class="org-type">*</span>rand<span class="org-rainbow-delimiters-depth-1">(</span>N<span class="org-type">/</span><span class="org-highlight-numbers-number">2</span>,<span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-comment">% Generate random phase [rad]</span>
Cx = <span class="org-rainbow-delimiters-depth-1">[</span><span class="org-highlight-numbers-number">0</span> ; C<span class="org-type">.*</span>complex<span class="org-rainbow-delimiters-depth-2">(</span>cos<span class="org-rainbow-delimiters-depth-3">(</span>theta<span class="org-rainbow-delimiters-depth-3">)</span>,sin<span class="org-rainbow-delimiters-depth-3">(</span>theta<span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">]</span>;
Cx = <span class="org-rainbow-delimiters-depth-1">[</span>Cx; flipud<span class="org-rainbow-delimiters-depth-2">(</span>conj<span class="org-rainbow-delimiters-depth-3">(</span>Cx<span class="org-rainbow-delimiters-depth-4">(</span><span class="org-highlight-numbers-number">2</span><span class="org-type">:</span>end<span class="org-rainbow-delimiters-depth-4">)</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">]</span>;;
</pre>
</div>
</div>
</div>
<div id="outline-container-org4ff718c" class="outline-3">
<h3 id="org4ff718c"><span class="section-number-3">3.3</span> Obtained Time Domain Signal</h3>
<div class="outline-text-3" id="text-3-3">
<p>
The time domain data is generated by an inverse FFT.
We normalize the <code>ifft</code> Matlab command.
</p>
<div class="org-src-container">
<pre class="src src-matlab">u = <span class="org-highlight-numbers-number">1</span><span class="org-type">/</span><span class="org-rainbow-delimiters-depth-1">(</span>sqrt<span class="org-rainbow-delimiters-depth-2">(</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-type">*</span>df<span class="org-type">*</span><span class="org-highlight-numbers-number">1</span><span class="org-type">/</span>Fs<span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">*</span>ifft<span class="org-rainbow-delimiters-depth-1">(</span>Cx<span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-comment">% Normalisation of the IFFT</span>
t = linspace<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">0</span>, T0, N<span class="org-type">+</span><span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span>; <span class="org-comment">% Time Vector [s]</span>
</pre>
</div>
<div id="org69c5e9c" class="figure">
<p><img src="figs/signal_time_domain.png" alt="signal_time_domain.png" />
</p>
<p><span class="figure-number">Figure 10: </span>Obtained signal in the time domain (<a href="./figs/signal_time_domain.png">png</a>, <a href="./figs/signal_time_domain.pdf">pdf</a>)</p>
</div>
</div>
</div>
<div id="outline-container-orgc9527e8" class="outline-3">
<h3 id="orgc9527e8"><span class="section-number-3">3.4</span> PSD Comparison</h3>
<div class="outline-text-3" id="text-3-4">
<p>
We duplicate the time domain signal to have a longer signal and thus a more precise PSD result.
</p>
<div class="org-src-container">
<pre class="src src-matlab">u_rep = repmat<span class="org-rainbow-delimiters-depth-1">(</span>u, <span class="org-highlight-numbers-number">10</span>, <span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
We compute the PSD of the obtained signal with the following commands.
</p>
<div class="org-src-container">
<pre class="src src-matlab">nx = length<span class="org-rainbow-delimiters-depth-1">(</span>u_rep<span class="org-rainbow-delimiters-depth-1">)</span>;
na = <span class="org-highlight-numbers-number">16</span>;
win = hanning<span class="org-rainbow-delimiters-depth-1">(</span>floor<span class="org-rainbow-delimiters-depth-2">(</span>nx<span class="org-type">/</span>na<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>;
<span class="org-rainbow-delimiters-depth-1">[</span>pxx, f<span class="org-rainbow-delimiters-depth-1">]</span> = pwelch<span class="org-rainbow-delimiters-depth-1">(</span>u_rep, win, <span class="org-highlight-numbers-number">0</span>, <span class="org-rainbow-delimiters-depth-2">[]</span>, Fs<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
Finally, we compare the PSD of the original signal and the obtained signal on figure <a href="#fig:psd_comparison">fig:psd_comparison</a>.
</p>
<div id="orgce3ea03" class="figure">
<p><img src="figs/psd_comparison.png" alt="psd_comparison.png" />
</p>
<p><span class="figure-number">Figure 11: </span>Comparison of the PSD of the original signal and the PSD of the obtained signal (<a href="./figs/psd_comparison.png">png</a>, <a href="./figs/psd_comparison.pdf">pdf</a>)</p>
</div>
</div>
</div>
</div>
<div id="outline-container-org7fe2f42" class="outline-2">
<h2 id="org7fe2f42"><span class="section-number-2">4</span> <span class="todo TODO">TODO</span> Compute the Noise level and Signal level from PSD</h2>
<div class="outline-text-2" id="text-4">
<p>
<a id="org7bce553"></a>
</p>
</div>
<div id="outline-container-org640e940" class="outline-3">
<h3 id="org640e940"><span class="section-number-3">4.1</span> Computation</h3>
<div class="outline-text-3" id="text-4-1">
<ul class="org-ul">
<li class="off"><code>[&#xa0;]</code> Add table to compare the methods</li>
<li class="off"><code>[&#xa0;]</code> Add some explanations</li>
</ul>
<div class="org-src-container">
<pre class="src src-matlab">N = <span class="org-highlight-numbers-number">10000</span>;
dt = <span class="org-highlight-numbers-number">0</span>.<span class="org-highlight-numbers-number">001</span>;
t = dt<span class="org-type">*</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">0</span><span class="org-type">:</span><span class="org-highlight-numbers-number">1</span><span class="org-type">:</span>N<span class="org-type">-</span><span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">'</span>;
</pre>
</div>
<p>
Parameters of the signal
</p>
<div class="org-src-container">
<pre class="src src-matlab">asig = <span class="org-highlight-numbers-number">0</span>.<span class="org-highlight-numbers-number">8</span>; <span class="org-comment">% Amplitude of the signal [V]</span>
fsig = <span class="org-highlight-numbers-number">100</span>; <span class="org-comment">% Frequency of the signal [Hz]</span>
anoi = <span class="org-highlight-numbers-number">1e</span><span class="org-type">-</span><span class="org-highlight-numbers-number">3</span>; <span class="org-comment">% RMS value of the noise</span>
x = anoi<span class="org-type">*</span>randn<span class="org-rainbow-delimiters-depth-1">(</span>N, <span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-type">+</span> asig<span class="org-type">*</span>sin<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-rainbow-delimiters-depth-2">(</span><span class="org-highlight-numbers-number">2</span><span class="org-type">*</span><span class="org-constant">pi</span><span class="org-type">*</span>fsig<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-type">*</span>t<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-type">figure</span>;
plot<span class="org-rainbow-delimiters-depth-1">(</span>t, x<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
Compute the PSD of the signal.
</p>
<div class="org-src-container">
<pre class="src src-matlab">nx = length<span class="org-rainbow-delimiters-depth-1">(</span>x<span class="org-rainbow-delimiters-depth-1">)</span>;
na = <span class="org-highlight-numbers-number">8</span>;
win = blackmanharris<span class="org-rainbow-delimiters-depth-1">(</span>floor<span class="org-rainbow-delimiters-depth-2">(</span>nx<span class="org-type">/</span>na<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>;
<span class="org-rainbow-delimiters-depth-1">[</span>pxx, f<span class="org-rainbow-delimiters-depth-1">]</span> = pwelch<span class="org-rainbow-delimiters-depth-1">(</span>x, win, <span class="org-highlight-numbers-number">0</span>, <span class="org-rainbow-delimiters-depth-2">[]</span>, <span class="org-highlight-numbers-number">1</span><span class="org-type">/</span>dt<span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<p>
Normalization of the PSD.
</p>
<div class="org-src-container">
<pre class="src src-matlab">CG = sum<span class="org-rainbow-delimiters-depth-1">(</span>win<span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">/</span><span class="org-rainbow-delimiters-depth-1">(</span>nx<span class="org-type">/</span>na<span class="org-rainbow-delimiters-depth-1">)</span>;
NG = sum<span class="org-rainbow-delimiters-depth-1">(</span>win<span class="org-type">.^</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">/</span><span class="org-rainbow-delimiters-depth-1">(</span>nx<span class="org-type">/</span>na<span class="org-rainbow-delimiters-depth-1">)</span>;
fbin = f<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-type">-</span> f<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">1</span><span class="org-rainbow-delimiters-depth-1">)</span>;
pxx_norm = pxx<span class="org-type">*</span><span class="org-rainbow-delimiters-depth-1">(</span>NG<span class="org-type">*</span>fbin<span class="org-type">/</span><span class="org-rainbow-delimiters-depth-2">(</span>CG<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-type">^</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
<div class="org-src-container">
<pre class="src src-matlab">isig = round<span class="org-rainbow-delimiters-depth-1">(</span>fsig<span class="org-type">/</span>fbin<span class="org-rainbow-delimiters-depth-1">)</span><span class="org-type">+</span><span class="org-highlight-numbers-number">1</span>;
</pre>
</div>
<p>
Estimate the Signal magnitude.
</p>
<div class="org-src-container">
<pre class="src src-matlab">srmt = asig<span class="org-type">/</span>sqrt<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-comment">% Theoretical value of signal magnitude</span>
srms = sqrt<span class="org-rainbow-delimiters-depth-1">(</span>sum<span class="org-rainbow-delimiters-depth-2">(</span>pxx<span class="org-rainbow-delimiters-depth-3">(</span>isig<span class="org-type">-</span><span class="org-highlight-numbers-number">5</span><span class="org-type">:</span>isig<span class="org-type">+</span><span class="org-highlight-numbers-number">5</span><span class="org-rainbow-delimiters-depth-3">)</span><span class="org-type">*</span>fbin<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-comment">% Signal spectrum integrated</span>
srmsp = sqrt<span class="org-rainbow-delimiters-depth-1">(</span>pxx<span class="org-rainbow-delimiters-depth-2">(</span>isig<span class="org-rainbow-delimiters-depth-2">)</span> <span class="org-type">*</span> NG<span class="org-type">*</span>fbin<span class="org-type">/</span>CG<span class="org-type">^</span><span class="org-highlight-numbers-number">2</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-comment">% Maximum read off spectrum</span>
</pre>
</div>
<p>
Estimate the noise floor.
</p>
<div class="org-src-container">
<pre class="src src-matlab">nth = anoi<span class="org-type">/</span>sqrt<span class="org-rainbow-delimiters-depth-1">(</span>max<span class="org-rainbow-delimiters-depth-2">(</span>f<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-comment">% Theoretical value [V/sqrt(Hz)]</span>
inmax = isig<span class="org-type">-</span><span class="org-highlight-numbers-number">20</span>;
nsum = sqrt<span class="org-rainbow-delimiters-depth-1">(</span>sum<span class="org-rainbow-delimiters-depth-2">(</span>pxx<span class="org-rainbow-delimiters-depth-3">(</span><span class="org-highlight-numbers-number">1</span><span class="org-type">:</span>inmax<span class="org-rainbow-delimiters-depth-3">)</span><span class="org-type">*</span>fbin<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-type">/</span> sqrt<span class="org-rainbow-delimiters-depth-1">(</span>f<span class="org-rainbow-delimiters-depth-2">(</span>inmax<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-comment">% Signal spectrum integrated</span>
navg = sqrt<span class="org-rainbow-delimiters-depth-1">(</span>mean<span class="org-rainbow-delimiters-depth-2">(</span>pxx<span class="org-rainbow-delimiters-depth-3">(</span><span class="org-highlight-numbers-number">1</span><span class="org-type">:</span>inmax<span class="org-rainbow-delimiters-depth-3">)</span><span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span> <span class="org-comment">% pwelch output averaged</span>
</pre>
</div>
<div class="org-src-container">
<pre class="src src-matlab"><span class="org-type">figure</span>;
hold on;
plot<span class="org-rainbow-delimiters-depth-1">(</span>f, pxx<span class="org-rainbow-delimiters-depth-1">)</span>
plot<span class="org-rainbow-delimiters-depth-1">(</span>f, pxx_norm<span class="org-rainbow-delimiters-depth-1">)</span>
hold off;
xlabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Frequency </span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">[</span></span><span class="org-string">Hz</span><span class="org-string"><span class="org-rainbow-delimiters-depth-2">]</span></span><span class="org-string">'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
ylabel<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Power Spectral Density'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
<span class="org-type">set</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-variable-name">gca</span>, <span class="org-string">'xscale'</span>, <span class="org-string">'log'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
<span class="org-type">set</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-variable-name">gca</span>, <span class="org-string">'yscale'</span>, <span class="org-string">'log'</span><span class="org-rainbow-delimiters-depth-1">)</span>;
</pre>
</div>
</div>
</div>
</div>
<p>
<h1 class='org-ref-bib-h1'>Bibliography</h1>
<ul class='org-ref-bib'><li><a id="preumont94_random_vibrat_spect_analy">[preumont94_random_vibrat_spect_analy]</a> <a name="preumont94_random_vibrat_spect_analy"></a>Andr\'e Preumont, Random Vibration and Spectral Analysis, Springer Netherlands (1994).</li>
</ul>
</p>
</div>
<div id="postamble" class="status">
<p class="author">Author: Dehaeze Thomas</p>
<p class="date">Created: 2019-12-02 lun. 11:22</p>
<p class="validation"><a href="http://validator.w3.org/check?uri=referer">Validate</a></p>
</div>
</body>
</html>