<!DOCTYPE html>

<html>
  <head>
    <meta charset="utf-8">
    
    <title>scipy.signal.hilbert &mdash; SciPy v1.1.0.dev0+4e64658 Reference Guide</title>
    
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../_static/css/spc-extend.css">
    <link rel="stylesheet" href="../_static/scipy.css" type="text/css" >
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" >
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.1.0.dev0+4e64658',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  false
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/scipy-mathjax/MathJax.js?config=scipy-mathjax"></script>
    <script type="text/javascript" src="../_static/js/copybutton.js"></script>
    <link rel="index" title="Index" href="../genindex.html" >
    <link rel="search" title="Search" href="../search.html" >
    <link rel="top" title="SciPy v1.1.0.dev0+4e64658 Reference Guide" href="../index.html" >
    <link rel="up" title="Signal processing (scipy.signal)" href="../signal.html" >
    <link rel="next" title="scipy.signal.hilbert2" href="scipy.signal.hilbert2.html" >
    <link rel="prev" title="scipy.signal.sosfiltfilt" href="scipy.signal.sosfiltfilt.html" > 
  </head>
  <body>

  <div class="container">
    <div class="header">
    </div>
  </div>


    <div class="container">
      <div class="main">
        
	<div class="row-fluid">
	  <div class="span12">
	    <div class="spc-navbar">
              
    <ul class="nav nav-pills pull-left">
	
        <li class="active"><a href="../index.html">SciPy v1.1.0.dev0+4e64658 Reference Guide</a></li>
	
          <li class="active"><a href="../signal.html" accesskey="U">Signal processing (<code class="docutils literal"><span class="pre">scipy.signal</span></code>)</a></li> 
    </ul>
              
              
    <ul class="nav nav-pills pull-right">
      <li class="active">
        <a href="../genindex.html" title="General Index"
           accesskey="I">index</a>
      </li>
      <li class="active">
        <a href="../py-modindex.html" title="Python Module Index"
           >modules</a>
      </li>
      <li class="active">
        <a href="scipy.signal.hilbert2.html" title="scipy.signal.hilbert2"
           accesskey="N">next</a>
      </li>
      <li class="active">
        <a href="scipy.signal.sosfiltfilt.html" title="scipy.signal.sosfiltfilt"
           accesskey="P">previous</a>
      </li>
    </ul>
              
	    </div>
	  </div>
	</div>
        

	<div class="row-fluid">
      <div class="spc-rightsidebar span3">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../index.html">
              <img class="logo" src="../_static/scipyshiny_small.png" alt="Logo">
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="scipy.signal.sosfiltfilt.html"
                        title="previous chapter">scipy.signal.sosfiltfilt</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="scipy.signal.hilbert2.html"
                        title="next chapter">scipy.signal.hilbert2</a></p>


        </div>
      </div>
          <div class="span9">
            
        <div class="bodywrapper">
          <div class="body" id="spc-section-body">
            
  <div class="section" id="scipy-signal-hilbert">
<h1>scipy.signal.hilbert<a class="headerlink" href="#scipy-signal-hilbert" title="Permalink to this headline">¶</a></h1>
<dl class="function">
<dt id="scipy.signal.hilbert">
<code class="descclassname">scipy.signal.</code><code class="descname">hilbert</code><span class="sig-paren">(</span><em>x</em>, <em>N=None</em>, <em>axis=-1</em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/scipy/scipy/blob/4e64658/scipy/signal/signaltools.py#L1475-L1587"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#scipy.signal.hilbert" title="Permalink to this definition">¶</a></dt>
<dd><p>Compute the analytic signal, using the Hilbert transform.</p>
<p>The transformation is done along the last axis by default.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><p class="first"><strong>x</strong> : array_like</p>
<blockquote>
<div><p>Signal data.  Must be real.</p>
</div></blockquote>
<p><strong>N</strong> : int, optional</p>
<blockquote>
<div><p>Number of Fourier components.  Default: <code class="docutils literal"><span class="pre">x.shape[axis]</span></code></p>
</div></blockquote>
<p><strong>axis</strong> : int, optional</p>
<blockquote>
<div><p>Axis along which to do the transformation.  Default: -1.</p>
</div></blockquote>
</td>
</tr>
<tr class="field-even field"><th class="field-name">Returns:</th><td class="field-body"><p class="first"><strong>xa</strong> : ndarray</p>
<blockquote class="last">
<div><p>Analytic signal of <em class="xref py py-obj">x</em>, of each 1-D array along <em class="xref py py-obj">axis</em></p>
</div></blockquote>
</td>
</tr>
</tbody>
</table>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference internal" href="scipy.fftpack.hilbert.html#scipy.fftpack.hilbert" title="scipy.fftpack.hilbert"><code class="xref py py-obj docutils literal"><span class="pre">scipy.fftpack.hilbert</span></code></a></dt>
<dd>Return Hilbert transform of a periodic sequence x.</dd>
</dl>
</div>
<p class="rubric">Notes</p>
<p>The analytic signal <code class="docutils literal"><span class="pre">x_a(t)</span></code> of signal <code class="docutils literal"><span class="pre">x(t)</span></code> is:</p>
<div class="math">
\[x_a = F^{-1}(F(x) 2U) = x + i y\]</div>
<p>where <em class="xref py py-obj">F</em> is the Fourier transform, <em class="xref py py-obj">U</em> the unit step function,
and <em class="xref py py-obj">y</em> the Hilbert transform of <em class="xref py py-obj">x</em>. <a class="reference internal" href="#r268" id="id1">[R268]</a></p>
<p>In other words, the negative half of the frequency spectrum is zeroed
out, turning the real-valued signal into a complex signal.  The Hilbert
transformed signal can be obtained from <code class="docutils literal"><span class="pre">np.imag(hilbert(x))</span></code>, and the
original signal from <code class="docutils literal"><span class="pre">np.real(hilbert(x))</span></code>.</p>
<p class="rubric">References</p>
<table class="docutils citation" frame="void" id="r268" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[R268]</td><td><em>(<a class="fn-backref" href="#id1">1</a>, <a class="fn-backref" href="#id2">2</a>)</em> Wikipedia, “Analytic signal”.
<a class="reference external" href="http://en.wikipedia.org/wiki/Analytic_signal">http://en.wikipedia.org/wiki/Analytic_signal</a></td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="r269" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id3">[R269]</a></td><td>Leon Cohen, “Time-Frequency Analysis”, 1995. Chapter 2.</td></tr>
</tbody>
</table>
<table class="docutils citation" frame="void" id="r270" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label"><a class="fn-backref" href="#id4">[R270]</a></td><td>Alan V. Oppenheim, Ronald W. Schafer. Discrete-Time Signal
Processing, Third Edition, 2009. Chapter 12.
ISBN 13: 978-1292-02572-8</td></tr>
</tbody>
</table>
<p class="rubric">Examples</p>
<p>In this example we use the Hilbert transform to determine the amplitude
envelope and instantaneous frequency of an amplitude-modulated signal.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="nn">plt</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">scipy.signal</span> <span class="k">import</span> <span class="n">hilbert</span><span class="p">,</span> <span class="n">chirp</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">duration</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fs</span> <span class="o">=</span> <span class="mf">400.0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">samples</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">fs</span><span class="o">*</span><span class="n">duration</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">samples</span><span class="p">)</span> <span class="o">/</span> <span class="n">fs</span>
</pre></div>
</div>
<p>We create a chirp of which the frequency increases from 20 Hz to 100 Hz and
apply an amplitude modulation.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">signal</span> <span class="o">=</span> <span class="n">chirp</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="mf">20.0</span><span class="p">,</span> <span class="n">t</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span> <span class="mf">100.0</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">signal</span> <span class="o">*=</span> <span class="p">(</span><span class="mf">1.0</span> <span class="o">+</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sin</span><span class="p">(</span><span class="mf">2.0</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="o">*</span><span class="mf">3.0</span><span class="o">*</span><span class="n">t</span><span class="p">)</span> <span class="p">)</span>
</pre></div>
</div>
<p>The amplitude envelope is given by magnitude of the analytic signal. The
instantaneous frequency can be obtained by differentiating the
instantaneous phase in respect to time. The instantaneous phase corresponds
to the phase angle of the analytic signal.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">analytic_signal</span> <span class="o">=</span> <span class="n">hilbert</span><span class="p">(</span><span class="n">signal</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">amplitude_envelope</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">abs</span><span class="p">(</span><span class="n">analytic_signal</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">instantaneous_phase</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">unwrap</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">angle</span><span class="p">(</span><span class="n">analytic_signal</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">instantaneous_frequency</span> <span class="o">=</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">instantaneous_phase</span><span class="p">)</span> <span class="o">/</span>
<span class="gp">... </span>                           <span class="p">(</span><span class="mf">2.0</span><span class="o">*</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span><span class="p">)</span> <span class="o">*</span> <span class="n">fs</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">fig</span> <span class="o">=</span> <span class="n">plt</span><span class="o">.</span><span class="n">figure</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax0</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">211</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax0</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">signal</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;signal&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax0</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">amplitude_envelope</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="s1">&#39;envelope&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax0</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s2">&quot;time in seconds&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax0</span><span class="o">.</span><span class="n">legend</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax1</span> <span class="o">=</span> <span class="n">fig</span><span class="o">.</span><span class="n">add_subplot</span><span class="p">(</span><span class="mi">212</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax1</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="mi">1</span><span class="p">:],</span> <span class="n">instantaneous_frequency</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax1</span><span class="o">.</span><span class="n">set_xlabel</span><span class="p">(</span><span class="s2">&quot;time in seconds&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ax1</span><span class="o">.</span><span class="n">set_ylim</span><span class="p">(</span><span class="mf">0.0</span><span class="p">,</span> <span class="mf">120.0</span><span class="p">)</span>
</pre></div>
</div>
<div class="figure">
<img alt="../_images/scipy-signal-hilbert-1.png" src="../_images/scipy-signal-hilbert-1.png" />
</div>
</dd></dl>

</div>


          </div>
        </div>
          </div>
        </div>
      </div>
    </div>

    <div class="container container-navbar-bottom">
      <div class="spc-navbar">
        
      </div>
    </div>
    <div class="container">
    <div class="footer">
    <div class="row-fluid">
    <ul class="inline pull-left">
      <li>
        &copy; Copyright 2008-2016, The Scipy community.
      </li>
      <li>
      Last updated on Sep 21, 2017.
      </li>
      <li>
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.3.
      </li>
    </ul>
    </div>
    </div>
    </div>
  </body>
</html>