vad.html 68.6 KB
Newer Older
Anthony Larcher's avatar
Anthony Larcher committed
1
2
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Anthony Larcher's avatar
Anthony Larcher committed
3
4


Anthony Larcher's avatar
Anthony Larcher committed
5
6
7
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Anthony Larcher's avatar
Anthony Larcher committed
8
    
Anthony Larcher's avatar
Anthony Larcher committed
9
    <title>frontend.vad &mdash; SIDEKIT 1.0.0 documentation</title>
Anthony Larcher's avatar
Anthony Larcher committed
10
    
Anthony Larcher's avatar
Anthony Larcher committed
11
    <link rel="stylesheet" href="../../_static/sphinxdoc.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
12
13
14
15
16
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
Anthony Larcher's avatar
Anthony Larcher committed
17
        VERSION:     '1.0.0',
Anthony Larcher's avatar
Anthony Larcher committed
18
19
20
21
22
23
24
25
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </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>
Anthony Larcher's avatar
Anthony Larcher committed
26
    <link rel="top" title="SIDEKIT 1.0.0 documentation" href="../../index.html" />
Anthony Larcher's avatar
Anthony Larcher committed
27
28
29
30
31
32
33
34
35
36
37
38
    <link rel="up" title="Module code" href="../index.html" /> 
  </head>
  <body role="document">
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
Anthony Larcher's avatar
Anthony Larcher committed
39
        <li class="nav-item nav-item-0"><a href="../../index.html">SIDEKIT 1.0.0 documentation</a> &raquo;</li>
Anthony Larcher's avatar
Anthony Larcher committed
40
41
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> &raquo;</li> 
      </ul>
Anthony Larcher's avatar
Anthony Larcher committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
    </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
Anthony Larcher's avatar
Anthony Larcher committed
60
61
62
63
64

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
Anthony Larcher's avatar
Anthony Larcher committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
            
  <h1>Source code for frontend.vad</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>
<span class="c">#</span>
<span class="c"># This file is part of SIDEKIT.</span>
<span class="c">#</span>
<span class="c"># SIDEKIT is a python package for speaker verification.</span>
<span class="c"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c">#</span>
<span class="c"># SIDEKIT is a python package for speaker verification.</span>
<span class="c"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c">#    </span>
<span class="c"># SIDEKIT is free software: you can redistribute it and/or modify</span>
<span class="c"># it under the terms of the GNU LLesser General Public License as </span>
<span class="c"># published by the Free Software Foundation, either version 3 of the License, </span>
<span class="c"># or (at your option) any later version.</span>
<span class="c">#</span>
<span class="c"># SIDEKIT is distributed in the hope that it will be useful,</span>
<span class="c"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c"># GNU Lesser General Public License for more details.</span>
<span class="c">#</span>
<span class="c"># You should have received a copy of the GNU Lesser General Public License</span>
<span class="c"># along with SIDEKIT.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>

<span class="sd">&quot;&quot;&quot;</span>
<span class="sd">Copyright 2014-2015 Anthony Larcher and Sylvain Meignier</span>

<span class="sd">:mod:`frontend` provides methods to process an audio signal in order to extract</span>
<span class="sd">useful parameters for speaker verification.</span>
<span class="sd">&quot;&quot;&quot;</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">from</span> <span class="nn">scipy.signal</span> <span class="kn">import</span> <span class="n">lfilter</span><span class="p">,</span> <span class="n">hamming</span>
<span class="kn">from</span> <span class="nn">scipy.fftpack</span> <span class="kn">import</span> <span class="n">fft</span>
<span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">scipy</span> <span class="kn">import</span> <span class="n">ndimage</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">sidekit.mixture</span> <span class="kn">import</span> <span class="n">Mixture</span>

Anthony Larcher's avatar
Anthony Larcher committed
105
106
107
108
109
110
111
112
113
114
115

<span class="n">__author__</span> <span class="o">=</span> <span class="s">&quot;Anthony Larcher and Sylvain Meignier&quot;</span>
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s">&quot;Copyright 2014-2016 Anthony Larcher and Sylvain Meignier&quot;</span>
<span class="n">__license__</span> <span class="o">=</span> <span class="s">&quot;LGPL&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s">&quot;Anthony Larcher&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s">&quot;anthony.larcher@univ-lemans.fr&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s">&quot;Production&quot;</span>
<span class="n">__docformat__</span> <span class="o">=</span> <span class="s">&#39;reStructuredText&#39;</span>


<div class="viewcode-block" id="pre_emphasis"><a class="viewcode-back" href="../../frontend/vad.html#frontend.vad.pre_emphasis">[docs]</a><span class="k">def</span> <span class="nf">pre_emphasis</span><span class="p">(</span><span class="n">input_sig</span><span class="p">,</span> <span class="n">pre</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
116
    <span class="sd">&quot;&quot;&quot;Pre-emphasis of an audio signal.</span>
Anthony Larcher's avatar
Anthony Larcher committed
117
<span class="sd">    :param input_sig: the input vector of signal to pre emphasize</span>
Anthony Larcher's avatar
Anthony Larcher committed
118
119
<span class="sd">    :param pre: value that defines the pre-emphasis filter. </span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
120
    <span class="k">return</span> <span class="n">lfilter</span><span class="p">([</span><span class="mf">1.0</span><span class="p">,</span> <span class="o">-</span><span class="n">pre</span><span class="p">],</span> <span class="mi">1</span><span class="p">,</span> <span class="n">input_sig</span><span class="o">.</span><span class="n">T</span><span class="p">,</span> <span class="n">axis</span><span class="o">=-</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
Anthony Larcher's avatar
Anthony Larcher committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

</div>
<div class="viewcode-block" id="segment_axis"><a class="viewcode-back" href="../../frontend/vad.html#frontend.vad.segment_axis">[docs]</a><span class="k">def</span> <span class="nf">segment_axis</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">length</span><span class="p">,</span> <span class="n">overlap</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">&#39;cut&#39;</span><span class="p">,</span> <span class="n">endvalue</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Generate a new array that chops the given array along the given axis</span>
<span class="sd">    into overlapping frames.</span>

<span class="sd">    This method has been implemented by Anne Archibald, </span>
<span class="sd">    as part of the talk box toolkit</span>
<span class="sd">    example::</span>
<span class="sd">    </span>
<span class="sd">        segment_axis(arange(10), 4, 2)</span>
<span class="sd">        array([[0, 1, 2, 3],</span>
<span class="sd">           ( [2, 3, 4, 5],</span>
<span class="sd">             [4, 5, 6, 7],</span>
<span class="sd">             [6, 7, 8, 9]])</span>

<span class="sd">    :param a: the array to segment</span>
<span class="sd">    :param length: the length of each frame</span>
<span class="sd">    :param overlap: the number of array elements by which the frames should overlap</span>
<span class="sd">    :param axis: the axis to operate on; if None, act on the flattened array</span>
<span class="sd">    :param end: what to do with the last frame, if the array is not evenly </span>
<span class="sd">            divisible into pieces. Options are:</span>
<span class="sd">            - &#39;cut&#39;   Simply discard the extra values</span>
<span class="sd">            - &#39;wrap&#39;  Copy values from the beginning of the array</span>
<span class="sd">            - &#39;pad&#39;   Pad with a constant value</span>

<span class="sd">    :param endvalue: the value to use for end=&#39;pad&#39;</span>

<span class="sd">    :return: a ndarray</span>

<span class="sd">    The array is not copied unless necessary (either because it is unevenly</span>
<span class="sd">    strided and being flattened or because end is set to &#39;pad&#39; or &#39;wrap&#39;).</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">axis</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
156
        <span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ravel</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>  <span class="c"># may copy</span>
Anthony Larcher's avatar
Anthony Larcher committed
157
158
159
160
161
162
163
164
        <span class="n">axis</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="n">l</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>

    <span class="k">if</span> <span class="n">overlap</span> <span class="o">&gt;=</span> <span class="n">length</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;frames cannot overlap by more than 100%&quot;</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">overlap</span> <span class="o">&lt;</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">length</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;overlap must be nonnegative and length must&quot;</span> <span class="o">+</span>
Anthony Larcher's avatar
Anthony Larcher committed
165
                         <span class="s">&quot;be positive&quot;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
166

Anthony Larcher's avatar
Anthony Larcher committed
167
168
169
170
    <span class="k">if</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="n">length</span> <span class="ow">or</span> <span class="p">(</span><span class="n">l</span> <span class="o">-</span> <span class="n">length</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">l</span> <span class="o">&gt;</span> <span class="n">length</span><span class="p">:</span>
            <span class="n">roundup</span> <span class="o">=</span> <span class="n">length</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="p">(</span><span class="n">l</span> <span class="o">-</span> <span class="n">length</span><span class="p">)</span> <span class="o">//</span> <span class="p">(</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">))</span> <span class="o">*</span> <span class="p">(</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">)</span>
            <span class="n">rounddown</span> <span class="o">=</span> <span class="n">length</span> <span class="o">+</span> <span class="p">((</span><span class="n">l</span> <span class="o">-</span> <span class="n">length</span><span class="p">)</span> <span class="o">//</span> <span class="p">(</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">))</span> <span class="o">*</span> <span class="p">(</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
171
172
173
174
        <span class="k">else</span><span class="p">:</span>
            <span class="n">roundup</span> <span class="o">=</span> <span class="n">length</span>
            <span class="n">rounddown</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="k">assert</span> <span class="n">rounddown</span> <span class="o">&lt;</span> <span class="n">l</span> <span class="o">&lt;</span> <span class="n">roundup</span>
Anthony Larcher's avatar
Anthony Larcher committed
175
176
        <span class="k">assert</span> <span class="n">roundup</span> <span class="o">==</span> <span class="n">rounddown</span> <span class="o">+</span> <span class="p">(</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">)</span> <span class="ow">or</span> <span class="p">(</span><span class="n">roundup</span> <span class="o">==</span> <span class="n">length</span> <span class="ow">and</span> <span class="n">rounddown</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">axis</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
177
178
179
180

        <span class="k">if</span> <span class="n">end</span> <span class="o">==</span> <span class="s">&#39;cut&#39;</span><span class="p">:</span>
            <span class="n">a</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="p">:</span><span class="n">rounddown</span><span class="p">]</span>
            <span class="n">l</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
181
        <span class="k">elif</span> <span class="n">end</span> <span class="ow">in</span> <span class="p">[</span><span class="s">&#39;pad&#39;</span><span class="p">,</span> <span class="s">&#39;wrap&#39;</span><span class="p">]:</span>  <span class="c"># copying will be necessary</span>
Anthony Larcher's avatar
Anthony Larcher committed
182
183
            <span class="n">s</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
            <span class="n">s</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">roundup</span>
Anthony Larcher's avatar
Anthony Larcher committed
184
            <span class="n">b</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
185
186
187
188
            <span class="n">b</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="p">:</span><span class="n">l</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span>
            <span class="k">if</span> <span class="n">end</span> <span class="o">==</span> <span class="s">&#39;pad&#39;</span><span class="p">:</span>
                <span class="n">b</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">l</span><span class="p">:]</span> <span class="o">=</span> <span class="n">endvalue</span>
            <span class="k">elif</span> <span class="n">end</span> <span class="o">==</span> <span class="s">&#39;wrap&#39;</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
189
                <span class="n">b</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="n">l</span><span class="p">:]</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="o">...</span><span class="p">,</span> <span class="p">:</span><span class="n">roundup</span> <span class="o">-</span> <span class="n">l</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
190
191
            <span class="n">a</span> <span class="o">=</span> <span class="n">b</span>

Anthony Larcher's avatar
Anthony Larcher committed
192
        <span class="n">a</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">swapaxes</span><span class="p">(</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">axis</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
193
194

    <span class="k">if</span> <span class="n">l</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
195
196
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Not enough data points to segment array &quot;</span> <span class="o">+</span>
                         <span class="s">&quot;in &#39;cut&#39; mode; try &#39;pad&#39; or &#39;wrap&#39;&quot;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
197
    <span class="k">assert</span> <span class="n">l</span> <span class="o">&gt;=</span> <span class="n">length</span>
Anthony Larcher's avatar
Anthony Larcher committed
198
199
    <span class="k">assert</span> <span class="p">(</span><span class="n">l</span> <span class="o">-</span> <span class="n">length</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span>
    <span class="n">n</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="p">(</span><span class="n">l</span> <span class="o">-</span> <span class="n">length</span><span class="p">)</span> <span class="o">//</span> <span class="p">(</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
200
    <span class="n">s</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">strides</span><span class="p">[</span><span class="n">axis</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
201
202
    <span class="n">newshape</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">[:</span><span class="n">axis</span><span class="p">]</span> <span class="o">+</span> <span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">length</span><span class="p">)</span> <span class="o">+</span> <span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="n">axis</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:]</span>
    <span class="n">newstrides</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">strides</span><span class="p">[:</span><span class="n">axis</span><span class="p">]</span> <span class="o">+</span> <span class="p">((</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">)</span> <span class="o">*</span> <span class="n">s</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span> <span class="o">+</span> <span class="n">a</span><span class="o">.</span><span class="n">strides</span><span class="p">[</span><span class="n">axis</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:]</span>
Anthony Larcher's avatar
Anthony Larcher committed
203
204
205
206
207
208
209
210

    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">strides</span><span class="o">=</span><span class="n">newstrides</span><span class="p">,</span>
                                  <span class="n">shape</span><span class="o">=</span><span class="n">newshape</span><span class="p">,</span> <span class="nb">buffer</span><span class="o">=</span><span class="n">a</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
        <span class="n">logging</span><span class="o">.</span><span class="n">debug</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;Problem with ndarray creation forces copy.&quot;</span><span class="p">)</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>
        <span class="c"># Shape doesn&#39;t change but strides does</span>
Anthony Larcher's avatar
Anthony Larcher committed
211
        <span class="n">newstrides</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">strides</span><span class="p">[:</span><span class="n">axis</span><span class="p">]</span> <span class="o">+</span> <span class="p">((</span><span class="n">length</span> <span class="o">-</span> <span class="n">overlap</span><span class="p">)</span> <span class="o">*</span> <span class="n">s</span><span class="p">,</span> <span class="n">s</span><span class="p">)</span> <span class="o">+</span> <span class="n">a</span><span class="o">.</span><span class="n">strides</span><span class="p">[</span><span class="n">axis</span> <span class="o">+</span> <span class="mi">1</span><span class="p">:]</span>
Anthony Larcher's avatar
Anthony Larcher committed
212
213
214
215
216
217
218
219
220
221
222
223
224
225
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="o">.</span><span class="n">__new__</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">,</span> <span class="n">strides</span><span class="o">=</span><span class="n">newstrides</span><span class="p">,</span>
                                  <span class="n">shape</span><span class="o">=</span><span class="n">newshape</span><span class="p">,</span> <span class="nb">buffer</span><span class="o">=</span><span class="n">a</span><span class="p">,</span> <span class="n">dtype</span><span class="o">=</span><span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="p">)</span>

</div>
<div class="viewcode-block" id="speech_enhancement"><a class="viewcode-back" href="../../frontend/vad.html#frontend.vad.speech_enhancement">[docs]</a><span class="k">def</span> <span class="nf">speech_enhancement</span><span class="p">(</span><span class="n">X</span><span class="p">,</span> <span class="n">Gain</span><span class="p">,</span> <span class="n">Noise_floor</span><span class="p">,</span> <span class="n">Fs</span><span class="p">,</span> <span class="n">Ascale</span><span class="p">,</span> <span class="n">NN</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;This program is only to process the single file seperated by the silence</span>
<span class="sd">    section if the silence section is detected, then a counter to number of</span>
<span class="sd">    buffer is set and pre-processing is required.</span>

<span class="sd">    Usage: SpeechENhance(wavefilename, Gain, Noise_floor)</span>

<span class="sd">    :param X: input audio signal</span>
<span class="sd">    :param Noise_floor: default value is 0.02 : suggestion range </span>
<span class="sd">            from 0.2 to 0.001</span>
Anthony Larcher's avatar
Anthony Larcher committed
226
<span class="sd">    :param Gain: default value is 0.9, suggestion range 0.6 to 1.4,</span>
Anthony Larcher's avatar
Anthony Larcher committed
227
228
229
230
231
232
233
234
235
236
<span class="sd">            higher value means more subtraction or noise redcution</span>
<span class="sd">    :param Fs: sampling frequency of the input signal</span>
<span class="sd">    :param Ascale: 1 to add noise, 0 not to add noise</span>
<span class="sd">    :param NN:</span>
<span class="sd">    </span>
<span class="sd">    :return: a 1-dimensional array of boolean that </span>
<span class="sd">        is True for high energy frames.</span>
<span class="sd">    </span>
<span class="sd">    Copyright 2014 Sun Han Wu and Anthony Larcher</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
237
238
    <span class="c"># try{</span>
    <span class="k">if</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="mi">512</span><span class="p">:</span>  <span class="c"># creer une exception</span>
Anthony Larcher's avatar
Anthony Larcher committed
239
240
241
242
        <span class="k">return</span> <span class="n">X</span>

    <span class="n">num1</span> <span class="o">=</span> <span class="mi">40</span>  <span class="c"># dsiable buffer number</span>
    <span class="n">Alpha</span> <span class="o">=</span> <span class="mf">0.75</span>  <span class="c"># original value is 0.9</span>
Anthony Larcher's avatar
Anthony Larcher committed
243
244
    <span class="n">FrameSize</span> <span class="o">=</span> <span class="mi">32</span> <span class="o">*</span> <span class="mi">2</span>  <span class="c"># 256*2</span>
    <span class="n">FrameShift</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">FrameSize</span> <span class="o">/</span> <span class="n">NN</span><span class="p">)</span>  <span class="c"># FrameSize/2=128</span>
Anthony Larcher's avatar
Anthony Larcher committed
245
    <span class="n">nfft</span> <span class="o">=</span> <span class="n">FrameSize</span>  <span class="c"># = FrameSize</span>
Anthony Larcher's avatar
Anthony Larcher committed
246
    <span class="n">Fmax</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">floor</span><span class="p">(</span><span class="n">nfft</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>  <span class="c"># 128+1 = 129</span>
Anthony Larcher's avatar
Anthony Larcher committed
247
248
    <span class="c"># arising hamming windows</span>
    <span class="n">Hamm</span> <span class="o">=</span> <span class="mf">1.08</span> <span class="o">*</span> <span class="p">(</span><span class="mf">0.54</span> <span class="o">-</span> <span class="mf">0.46</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">cos</span><span class="p">(</span><span class="mi">2</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="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">FrameSize</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)))</span>
Anthony Larcher's avatar
Anthony Larcher committed
249
250
251
252
    <span class="n">y0</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">)</span>  <span class="c"># 128 zeros</span>

    <span class="n">Eabsn</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">Fmax</span><span class="p">)</span>
    <span class="n">Eta1</span> <span class="o">=</span> <span class="n">Eabsn</span>
Anthony Larcher's avatar
Anthony Larcher committed
253
254
255
256
257
258
259
260
261
262
263

    <span class="c">###################################################################</span>
    <span class="c"># initial parameter for noise min</span>
    <span class="n">mb</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="mi">1</span> <span class="o">+</span> <span class="n">FrameSize</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">))</span> <span class="o">*</span> <span class="n">FrameSize</span> <span class="o">/</span> <span class="mi">2</span>  <span class="c"># 129x4  set four buffer * FrameSize/2</span>
    <span class="n">im</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">Beta1</span> <span class="o">=</span> <span class="mf">0.9024</span>  <span class="c"># seems that small value is better;</span>
    <span class="n">pxn</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="mi">1</span> <span class="o">+</span> <span class="n">FrameSize</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)</span>  <span class="c"># 1+FrameSize/2=129 zeros vector</span>

    <span class="c">###################################################################</span>
    <span class="n">old_absx</span> <span class="o">=</span> <span class="n">Eabsn</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">FrameSize</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
264
265
266
267
    <span class="n">x</span><span class="p">[</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">:</span><span class="n">FrameSize</span><span class="p">]</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span>
        <span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">((</span><span class="nb">int</span><span class="p">(</span><span class="n">FrameShift</span><span class="p">),</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])))]</span>  <span class="c"># fread(ifp, FrameSize, &#39;short&#39;)% read  FrameSize samples</span>

    <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">FrameSize</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
268
269
        <span class="n">EOF</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">X</span>
Anthony Larcher's avatar
Anthony Larcher committed
270

Anthony Larcher's avatar
Anthony Larcher committed
271
272
273
274
275
276
    <span class="n">EOF</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">Frame</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="c">###################################################################</span>
    <span class="c"># add the pre-noise estimates</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">200</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
277
        <span class="n">Frame</span> <span class="o">+=</span> <span class="mi">1</span>
Anthony Larcher's avatar
Anthony Larcher committed
278
279
280
281
282
        <span class="n">fftn</span> <span class="o">=</span> <span class="n">fft</span><span class="p">(</span><span class="n">x</span> <span class="o">*</span> <span class="n">Hamm</span><span class="p">)</span>  <span class="c"># get its spectrum</span>
        <span class="n">absn</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">fftn</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">Fmax</span><span class="p">])</span>  <span class="c"># get its amplitude</span>

        <span class="c"># add the following part from noise estimation algorithm</span>
        <span class="n">pxn</span> <span class="o">=</span> <span class="n">Beta1</span> <span class="o">*</span> <span class="n">pxn</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">Beta1</span><span class="p">)</span> <span class="o">*</span> <span class="n">absn</span>  <span class="c"># Beta=0.9231 recursive pxn</span>
Anthony Larcher's avatar
Anthony Larcher committed
283
        <span class="n">im</span> <span class="o">=</span> <span class="p">(</span><span class="n">im</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="mi">40</span>  <span class="c"># noise_memory=47;  im=0 (init) for noise level estimation</span>
Anthony Larcher's avatar
Anthony Larcher committed
284
285
286
287

        <span class="k">if</span> <span class="n">im</span><span class="p">:</span>
            <span class="n">mb</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">mb</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">pxn</span><span class="p">)</span>  <span class="c"># 129 by 4 im&lt;&gt;0  update the first vector from PXN</span>
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
288
            <span class="n">mb</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">:]</span> <span class="o">=</span> <span class="n">mb</span><span class="p">[:,</span> <span class="p">:</span><span class="mi">3</span><span class="p">]</span>  <span class="c"># im==0 every 47 time shift pxn to first vector of mb</span>
Anthony Larcher's avatar
Anthony Larcher committed
289
            <span class="n">mb</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">pxn</span>
Anthony Larcher's avatar
Anthony Larcher committed
290
            <span class="c">#  0-2  vector shifted to 1 to 3</span>
Anthony Larcher's avatar
Anthony Larcher committed
291
292

        <span class="n">pn</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">mb</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>  <span class="c"># pn = 129x1po(9)=1.5 noise level estimate compensation</span>
Anthony Larcher's avatar
Anthony Larcher committed
293
        <span class="c"># over_sub_noise= oversubtraction factor</span>
Anthony Larcher's avatar
Anthony Larcher committed
294
295
296
297
298
299
300
301
302
303
304

        <span class="c"># end of noise detection algotihm</span>
        <span class="n">x</span><span class="p">[:</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">]</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="n">FrameShift</span><span class="p">:</span><span class="n">FrameSize</span><span class="p">]</span>
        <span class="n">index1</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">FrameShift</span> <span class="o">*</span> <span class="n">Frame</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">((</span><span class="n">FrameShift</span> <span class="o">*</span> <span class="p">(</span><span class="n">Frame</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])))</span>
        <span class="n">In_data</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span><span class="n">index1</span><span class="p">]</span>  <span class="c"># fread(ifp, FrameShift, &#39;short&#39;);</span>

        <span class="k">if</span> <span class="n">In_data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">FrameShift</span><span class="p">:</span>  <span class="c"># to check file is out</span>
            <span class="n">EOF</span> <span class="o">=</span> <span class="mi">1</span>
            <span class="k">break</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">x</span><span class="p">[</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">:</span><span class="n">FrameSize</span><span class="p">]</span> <span class="o">=</span> <span class="n">In_data</span>  <span class="c"># shift new 128 to position 129 to FrameSize location</span>
Anthony Larcher's avatar
Anthony Larcher committed
305
            <span class="c"># end of for loop for noise estimation</span>
Anthony Larcher's avatar
Anthony Larcher committed
306
307
308

    <span class="c"># end of prenoise estimation ************************</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">FrameSize</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
309
    <span class="n">x</span><span class="p">[</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">:</span><span class="n">FrameSize</span><span class="p">]</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">((</span><span class="nb">int</span><span class="p">(</span><span class="n">FrameShift</span><span class="p">),</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])))]</span>
Anthony Larcher's avatar
Anthony Larcher committed
310

Anthony Larcher's avatar
Anthony Larcher committed
311
    <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">FrameSize</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
312
313
        <span class="n">EOF</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">return</span> <span class="n">X</span>
Anthony Larcher's avatar
Anthony Larcher committed
314

Anthony Larcher's avatar
Anthony Larcher committed
315
316
317
318
319
320
321
    <span class="n">EOF</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">Frame</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="n">X1</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">)</span>
    <span class="n">Frame</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="k">while</span> <span class="n">EOF</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
322
        <span class="n">Frame</span> <span class="o">+=</span> <span class="mi">1</span>
Anthony Larcher's avatar
Anthony Larcher committed
323
324
        <span class="n">xwin</span> <span class="o">=</span> <span class="n">x</span> <span class="o">*</span> <span class="n">Hamm</span>

Anthony Larcher's avatar
Anthony Larcher committed
325
        <span class="n">fftx</span> <span class="o">=</span> <span class="n">fft</span><span class="p">(</span><span class="n">xwin</span><span class="p">,</span> <span class="n">nfft</span><span class="p">)</span>  <span class="c"># FrameSize FFT</span>
Anthony Larcher's avatar
Anthony Larcher committed
326
327
328
329
330
331
        <span class="n">absx</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">fftx</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">Fmax</span><span class="p">])</span>  <span class="c"># Fmax=129,get amplitude of x</span>
        <span class="n">argx</span> <span class="o">=</span> <span class="n">fftx</span><span class="p">[:</span><span class="n">Fmax</span><span class="p">]</span> <span class="o">/</span> <span class="p">(</span><span class="n">absx</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">spacing</span><span class="p">(</span><span class="mi">1</span><span class="p">))</span>  <span class="c"># normalize x spectrum phase</span>

        <span class="n">absn</span> <span class="o">=</span> <span class="n">absx</span>

        <span class="c"># add the following part from rainer algorithm</span>
Anthony Larcher's avatar
Anthony Larcher committed
332
        <span class="n">pxn</span> <span class="o">=</span> <span class="n">Beta1</span> <span class="o">*</span> <span class="n">pxn</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">Beta1</span><span class="p">)</span> <span class="o">*</span> <span class="n">absn</span>  <span class="c"># s Beta=0.9231   recursive pxn</span>
Anthony Larcher's avatar
Anthony Larcher committed
333
334
335
336
337
338

        <span class="n">im</span> <span class="o">=</span> <span class="nb">int</span><span class="p">((</span><span class="n">im</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">%</span> <span class="p">(</span><span class="n">num1</span> <span class="o">*</span> <span class="n">NN</span> <span class="o">/</span> <span class="mi">2</span><span class="p">))</span>  <span class="c"># original =40 noise_memory=47;  im=0 (init) for noise level estimation</span>

        <span class="k">if</span> <span class="n">im</span><span class="p">:</span>
            <span class="n">mb</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">mb</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">],</span> <span class="n">pxn</span><span class="p">)</span>  <span class="c"># 129 by 4 im&lt;&gt;0  update the first vector from PXN</span>
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
339
            <span class="n">mb</span><span class="p">[:,</span> <span class="mi">1</span><span class="p">:]</span> <span class="o">=</span> <span class="n">mb</span><span class="p">[:,</span> <span class="p">:</span><span class="mi">3</span><span class="p">]</span>  <span class="c"># im==0 every 47 time shift pxn to first vector of mb</span>
Anthony Larcher's avatar
Anthony Larcher committed
340
341
342
            <span class="n">mb</span><span class="p">[:,</span> <span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">pxn</span>

        <span class="n">pn</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">(</span><span class="n">mb</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>  <span class="c"># pn = 129x1po(9)=1.5 noise level estimate compensation</span>
Anthony Larcher's avatar
Anthony Larcher committed
343

Anthony Larcher's avatar
Anthony Larcher committed
344
345
        <span class="n">Eabsn</span> <span class="o">=</span> <span class="n">pn</span>
        <span class="n">Gaina</span> <span class="o">=</span> <span class="n">Gain</span>
Anthony Larcher's avatar
Anthony Larcher committed
346

Anthony Larcher's avatar
Anthony Larcher committed
347
348
        <span class="n">temp1</span> <span class="o">=</span> <span class="n">Eabsn</span> <span class="o">*</span> <span class="n">Gaina</span>

Anthony Larcher's avatar
Anthony Larcher committed
349
350
        <span class="n">Eta1</span> <span class="o">=</span> <span class="n">Alpha</span> <span class="o">*</span> <span class="n">old_absx</span> <span class="o">+</span> <span class="p">(</span><span class="mi">1</span> <span class="o">-</span> <span class="n">Alpha</span><span class="p">)</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">absx</span> <span class="o">-</span> <span class="n">temp1</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">new_absx</span> <span class="o">=</span> <span class="p">(</span><span class="n">absx</span> <span class="o">*</span> <span class="n">Eta1</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">Eta1</span> <span class="o">+</span> <span class="n">temp1</span><span class="p">)</span>  <span class="c"># wiener filter</span>
Anthony Larcher's avatar
Anthony Larcher committed
351
352
353
354
        <span class="n">old_absx</span> <span class="o">=</span> <span class="n">new_absx</span>

        <span class="n">ffty</span> <span class="o">=</span> <span class="n">new_absx</span> <span class="o">*</span> <span class="n">argx</span>  <span class="c"># multiply amplitude with its normalized spectrum</span>

Anthony Larcher's avatar
Anthony Larcher committed
355
        <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">real</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">fft</span><span class="o">.</span><span class="n">fftpack</span><span class="o">.</span><span class="n">ifft</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">ffty</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">conj</span><span class="p">(</span><span class="n">ffty</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">Fmax</span> <span class="o">-</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)])))))</span>
Anthony Larcher's avatar
Anthony Larcher committed
356
357
358
359
360

        <span class="n">y</span><span class="p">[:</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">]</span> <span class="o">=</span> <span class="n">y</span><span class="p">[:</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">]</span> <span class="o">+</span> <span class="n">y0</span>
        <span class="n">y0</span> <span class="o">=</span> <span class="n">y</span><span class="p">[</span><span class="n">FrameShift</span><span class="p">:</span><span class="n">FrameSize</span><span class="p">]</span>  <span class="c"># keep 129 to FrameSize point samples </span>
        <span class="n">x</span><span class="p">[:</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">]</span> <span class="o">=</span> <span class="n">x</span><span class="p">[</span><span class="n">FrameShift</span><span class="p">:</span><span class="n">FrameSize</span><span class="p">]</span>

Anthony Larcher's avatar
Anthony Larcher committed
361
        <span class="n">index1</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">FrameShift</span> <span class="o">*</span> <span class="n">Frame</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">min</span><span class="p">((</span><span class="n">FrameShift</span> <span class="o">*</span> <span class="p">(</span><span class="n">Frame</span> <span class="o">+</span> <span class="mi">1</span><span class="p">),</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])))</span>
Anthony Larcher's avatar
Anthony Larcher committed
362
363
364
        <span class="n">In_data</span> <span class="o">=</span> <span class="n">X</span><span class="p">[</span><span class="n">index1</span><span class="p">]</span>  <span class="c"># fread(ifp, FrameShift, &#39;short&#39;);</span>

        <span class="n">z</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">/</span> <span class="n">NN</span> <span class="o">*</span> <span class="n">y</span><span class="p">[:</span><span class="n">FrameShift</span><span class="p">]</span>  <span class="c"># left channel is the original signal </span>
Anthony Larcher's avatar
Anthony Larcher committed
365
        <span class="n">z</span> <span class="o">/=</span> <span class="mf">1.15</span>
Anthony Larcher's avatar
Anthony Larcher committed
366
367
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">minimum</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="mi">32767</span><span class="p">)</span>
        <span class="n">z</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">maximum</span><span class="p">(</span><span class="n">z</span><span class="p">,</span> <span class="o">-</span><span class="mi">32768</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
368
        <span class="n">index0</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">FrameShift</span> <span class="o">*</span> <span class="p">(</span><span class="n">Frame</span> <span class="o">-</span> <span class="mi">1</span><span class="p">),</span> <span class="n">FrameShift</span> <span class="o">*</span> <span class="n">Frame</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
369
370
371
372
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">(</span><span class="n">index0</span> <span class="o">&lt;</span> <span class="n">X1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
            <span class="n">idx</span> <span class="o">=</span> <span class="mi">0</span>
            <span class="k">while</span> <span class="p">(</span><span class="n">index0</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">&lt;</span> <span class="n">X1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">idx</span> <span class="o">&lt;</span> <span class="n">index0</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]):</span>
                <span class="n">X1</span><span class="p">[</span><span class="n">index0</span><span class="p">[</span><span class="n">idx</span><span class="p">]]</span> <span class="o">=</span> <span class="n">z</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
373
                <span class="n">idx</span> <span class="o">+=</span> <span class="mi">1</span>
Anthony Larcher's avatar
Anthony Larcher committed
374
375
376
377
378
379
        <span class="k">else</span><span class="p">:</span>
            <span class="n">X1</span><span class="p">[</span><span class="n">index0</span><span class="p">]</span> <span class="o">=</span> <span class="n">z</span>

        <span class="k">if</span> <span class="n">In_data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">EOF</span> <span class="o">=</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
380
            <span class="n">x</span><span class="p">[</span><span class="n">np</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="n">FrameSize</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">,</span> <span class="n">FrameSize</span> <span class="o">+</span> <span class="n">In_data</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">FrameShift</span><span class="p">)]</span> <span class="o">=</span> <span class="n">In_data</span>
Anthony Larcher's avatar
Anthony Larcher committed
381

Anthony Larcher's avatar
Anthony Larcher committed
382
383
384
    <span class="n">X1</span> <span class="o">=</span> <span class="n">X1</span><span class="p">[</span><span class="n">X1</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-</span> <span class="n">X</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">]:]</span>
    <span class="c"># }</span>
    <span class="c"># catch{</span>
Anthony Larcher's avatar
Anthony Larcher committed
385

Anthony Larcher's avatar
Anthony Larcher committed
386
    <span class="c"># }</span>
Anthony Larcher's avatar
Anthony Larcher committed
387
388
389
390
391
392
393
394
395
396
397
398
399
400
    <span class="k">return</span> <span class="n">X1</span>

</div>
<span class="k">def</span> <span class="nf">vad_energy</span><span class="p">(</span><span class="n">logEnergy</span><span class="p">,</span>
               <span class="n">distribNb</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span>
               <span class="n">nbTrainIt</span><span class="o">=</span><span class="mi">8</span><span class="p">,</span>
               <span class="n">flooring</span><span class="o">=</span><span class="mf">0.0001</span><span class="p">,</span> <span class="n">ceiling</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>
               <span class="n">alpha</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
    <span class="c"># center and normalize the energy</span>
    <span class="n">logEnergy</span> <span class="o">=</span> <span class="p">(</span><span class="n">logEnergy</span> <span class="o">-</span> <span class="n">np</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">logEnergy</span><span class="p">))</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">logEnergy</span><span class="p">)</span>

    <span class="c"># Initialize a Mixture with 2 or 3 distributions</span>
    <span class="n">world</span> <span class="o">=</span> <span class="n">Mixture</span><span class="p">()</span>
    <span class="c"># set the covariance of each component to 1.0 and the mean to mu + meanIncrement</span>
Anthony Larcher's avatar
Anthony Larcher committed
401
402
403
    <span class="n">world</span><span class="o">.</span><span class="n">cst</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">distribNb</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">pi</span> <span class="o">/</span> <span class="mf">2.0</span><span class="p">)</span>
    <span class="n">world</span><span class="o">.</span><span class="n">det</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">distribNb</span><span class="p">)</span>
    <span class="n">world</span><span class="o">.</span><span class="n">mu</span> <span class="o">=</span> <span class="o">-</span><span class="mi">2</span> <span class="o">+</span> <span class="mf">4.0</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">distribNb</span><span class="p">)</span> <span class="o">/</span> <span class="p">(</span><span class="n">distribNb</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
404
    <span class="n">world</span><span class="o">.</span><span class="n">mu</span> <span class="o">=</span> <span class="n">world</span><span class="o">.</span><span class="n">mu</span><span class="p">[:,</span> <span class="n">np</span><span class="o">.</span><span class="n">newaxis</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
405
    <span class="n">world</span><span class="o">.</span><span class="n">invcov</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">((</span><span class="n">distribNb</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
406
407
408
    <span class="c"># set equal weights for each component</span>
    <span class="n">world</span><span class="o">.</span><span class="n">w</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">distribNb</span><span class="p">)</span> <span class="o">/</span> <span class="n">distribNb</span>
    <span class="n">world</span><span class="o">.</span><span class="n">cov_var_ctl</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">world</span><span class="o">.</span><span class="n">invcov</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
409

Anthony Larcher's avatar
Anthony Larcher committed
410
411
    <span class="c"># Initialize the accumulator</span>
    <span class="n">accum</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">deepcopy</span><span class="p">(</span><span class="n">world</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
412

Anthony Larcher's avatar
Anthony Larcher committed
413
414
415
416
417
418
    <span class="c"># Perform nbTrainIt iterations of EM</span>
    <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nbTrainIt</span><span class="p">):</span>
        <span class="n">accum</span><span class="o">.</span><span class="n">_reset</span><span class="p">()</span>
        <span class="c"># E-step</span>
        <span class="n">world</span><span class="o">.</span><span class="n">_expectation</span><span class="p">(</span><span class="n">accum</span><span class="p">,</span> <span class="n">logEnergy</span><span class="p">)</span>
        <span class="c"># M-step</span>
Anthony Larcher's avatar
Anthony Larcher committed
419
        <span class="n">world</span><span class="o">.</span><span class="n">_maximization</span><span class="p">(</span><span class="n">accum</span><span class="p">,</span> <span class="n">ceiling</span><span class="p">,</span> <span class="n">flooring</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433

    <span class="c"># Compute threshold</span>
    <span class="n">threshold</span> <span class="o">=</span> <span class="n">world</span><span class="o">.</span><span class="n">mu</span><span class="o">.</span><span class="n">max</span><span class="p">()</span> <span class="o">-</span> <span class="n">alpha</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sqrt</span><span class="p">(</span><span class="mf">1.0</span> <span class="o">/</span> <span class="n">world</span><span class="o">.</span><span class="n">invcov</span><span class="p">[</span><span class="n">world</span><span class="o">.</span><span class="n">mu</span><span class="o">.</span><span class="n">argmax</span><span class="p">(),</span> <span class="mi">0</span><span class="p">])</span>

    <span class="c"># Apply frame selection with the current threshold</span>
    <span class="n">label</span> <span class="o">=</span> <span class="n">logEnergy</span> <span class="o">&gt;</span> <span class="n">threshold</span>
    <span class="k">return</span> <span class="n">label</span>


<div class="viewcode-block" id="vad_snr"><a class="viewcode-back" href="../../frontend/vad.html#frontend.vad.vad_snr">[docs]</a><span class="k">def</span> <span class="nf">vad_snr</span><span class="p">(</span><span class="n">sig</span><span class="p">,</span> <span class="n">snr</span><span class="p">,</span> <span class="n">fs</span><span class="o">=</span><span class="mi">16000</span><span class="p">,</span> <span class="n">shift</span><span class="o">=</span><span class="mf">0.01</span><span class="p">,</span> <span class="n">nwin</span><span class="o">=</span><span class="mi">256</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Select high energy frames based on the Signal to Noise Ratio</span>
<span class="sd">    of the signal.</span>
<span class="sd">    </span>
<span class="sd">    :param sig: the input audio signal</span>
Anthony Larcher's avatar
Anthony Larcher committed
434
<span class="sd">    :param snr: Signal to noise ratio to consider</span>
Anthony Larcher's avatar
Anthony Larcher committed
435
436
437
438
439
440
441
<span class="sd">    :param fs: sampling frequency of the input signal in Hz. Default is 16000.</span>
<span class="sd">    :param shift: shift between two frames in seconds. Default is 0.01</span>
<span class="sd">    :param nwin: number of samples of the sliding window. Default is 256.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">overlap</span> <span class="o">=</span> <span class="n">nwin</span> <span class="o">-</span> <span class="nb">int</span><span class="p">(</span><span class="n">shift</span> <span class="o">*</span> <span class="n">fs</span><span class="p">)</span>

    <span class="n">sig</span> <span class="o">*=</span> <span class="mi">32768</span>
Anthony Larcher's avatar
Anthony Larcher committed
442

Anthony Larcher's avatar
Anthony Larcher committed
443
    <span class="n">sig</span> <span class="o">=</span> <span class="n">speech_enhancement</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">sig</span><span class="p">),</span> <span class="mf">1.2</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">,</span> <span class="n">fs</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
444
    <span class="c"># sig = wiener(sig, mysize=32)</span>
Anthony Larcher's avatar
Anthony Larcher committed
445
446

    <span class="c"># Compute Standard deviation</span>
Anthony Larcher's avatar
Anthony Larcher committed
447
448
    <span class="n">sig</span> <span class="o">+=</span> <span class="mf">0.1</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">random</span><span class="o">.</span><span class="n">randn</span><span class="p">(</span><span class="n">sig</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
    <span class="c"># std2 = sidekit.toFrame(sig / 32768, nwin, overlap).T</span>
Anthony Larcher's avatar
Anthony Larcher committed
449
450
    <span class="c"># assume 16bit coding</span>
    <span class="n">std2</span> <span class="o">=</span> <span class="n">segment_axis</span><span class="p">(</span><span class="n">sig</span> <span class="o">/</span> <span class="mi">32768</span><span class="p">,</span> <span class="n">nwin</span><span class="p">,</span> <span class="n">overlap</span><span class="p">,</span>
Anthony Larcher's avatar
Anthony Larcher committed
451
                        <span class="n">axis</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s">&#39;cut&#39;</span><span class="p">,</span> <span class="n">endvalue</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
Anthony Larcher's avatar
Anthony Larcher committed
452
453
454
455
    <span class="n">std2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">std2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">std2</span> <span class="o">=</span> <span class="mi">20</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">log10</span><span class="p">(</span><span class="n">std2</span><span class="p">)</span>  <span class="c"># convert the dB</span>

    <span class="c"># APPLY VAD</span>
Anthony Larcher's avatar
Anthony Larcher committed
456
457
    <span class="n">label</span> <span class="o">=</span> <span class="p">(</span><span class="n">std2</span> <span class="o">&gt;</span> <span class="n">np</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">std2</span><span class="p">)</span> <span class="o">-</span> <span class="n">snr</span><span class="p">)</span> <span class="o">&amp;</span> <span class="p">(</span><span class="n">std2</span> <span class="o">&gt;</span> <span class="o">-</span><span class="mi">75</span><span class="p">)</span>

Anthony Larcher's avatar
update    
Anthony Larcher committed
458
459
    <span class="c">#####</span>
    <span class="c"># Speech Enhancement    </span>
Anthony Larcher's avatar
Anthony Larcher committed
460
    <span class="c"># sig = speech_enhancement(np.squeeze(sig), 1.2, 0.0, fs, 1.0, 2)</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
461
462

    <span class="c"># Compute Standard deviation</span>
Anthony Larcher's avatar
Anthony Larcher committed
463
464
    <span class="c"># sig = sig + 0.001 * np.random.randn(sig.shape[0])</span>
    <span class="c"># std2 = sidekit.toFrame(sig / 32768, nwin, overlap).T</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
465
    <span class="c"># assume 16bit coding</span>
Anthony Larcher's avatar
Anthony Larcher committed
466
467
    <span class="c"># if norm:</span>
    <span class="c"># std2 = segment_axis(sig / 32768, nwin, overlap,</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
468
    <span class="c">#                   axis=None, end=&#39;cut&#39;, endvalue=0).T</span>
Anthony Larcher's avatar
Anthony Larcher committed
469
470
471
472
473
    <span class="c"># else:</span>
    <span class="c"># std2 = segment_axis(sig, nwin, overlap,</span>
    <span class="c">#                   axis=None, end=&#39;cut&#39;, endvalue=0).T</span>
    <span class="c"># std2 = np.std(std2, axis=0)</span>
    <span class="c"># std2 = 20 * np.log10(std2)  # convert the dB</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
474
475

    <span class="c"># APPLY VAD</span>
Anthony Larcher's avatar
Anthony Larcher committed
476
    <span class="c"># label = (std2 &gt; np.max(std2) - snr) &amp; (std2 &gt; -75)</span>
Anthony Larcher's avatar
Anthony Larcher committed
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

    <span class="k">return</span> <span class="n">label</span>

</div>
<div class="viewcode-block" id="label_fusion"><a class="viewcode-back" href="../../frontend/vad.html#frontend.vad.label_fusion">[docs]</a><span class="k">def</span> <span class="nf">label_fusion</span><span class="p">(</span><span class="n">label</span><span class="p">,</span> <span class="n">win</span><span class="o">=</span><span class="mi">3</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Apply a morphological filtering on the label to remove isolated labels.</span>
<span class="sd">    In case the input is a two channel label (2D ndarray of boolean of same </span>
<span class="sd">    length) the labels of two channels are fused to remove</span>
<span class="sd">    overlaping segments of speech.</span>
<span class="sd">    </span>
<span class="sd">    :param label: input labels given in a 1D or 2D ndarray</span>
<span class="sd">    :param win: parameter or the morphological filters</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">channel_nb</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">label</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">channel_nb</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
        <span class="n">overlap_label</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">label</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">label</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
        <span class="n">label</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">label</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="o">~</span><span class="n">overlap_label</span><span class="p">)</span>
        <span class="n">label</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">logical_and</span><span class="p">(</span><span class="n">label</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="o">~</span><span class="n">overlap_label</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
495

Anthony Larcher's avatar
Anthony Larcher committed
496
    <span class="k">for</span> <span class="n">idx</span><span class="p">,</span> <span class="n">lbl</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">label</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
497
498
499
500
501
        <span class="n">cl</span> <span class="o">=</span> <span class="n">ndimage</span><span class="o">.</span><span class="n">grey_closing</span><span class="p">(</span><span class="n">lbl</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">win</span><span class="p">)</span>
        <span class="n">label</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span> <span class="o">=</span> <span class="n">ndimage</span><span class="o">.</span><span class="n">grey_opening</span><span class="p">(</span><span class="n">cl</span><span class="p">,</span> <span class="n">size</span><span class="o">=</span><span class="n">win</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">label</span></div>
</pre></div>
Anthony Larcher's avatar
Anthony Larcher committed
502
503

          </div>
Anthony Larcher's avatar
Anthony Larcher committed
504
505
506
507
508
509
510
511
512
513
514
515
516
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
Anthony Larcher's avatar
Anthony Larcher committed
517
        <li class="nav-item nav-item-0"><a href="../../index.html">SIDEKIT 1.0.0 documentation</a> &raquo;</li>
Anthony Larcher's avatar
Anthony Larcher committed
518
519
520
521
          <li class="nav-item nav-item-1"><a href="../index.html" >Module code</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
Anthony Larcher's avatar
Anthony Larcher committed
522
        &copy; Copyright 2014, Anthony LARCHER &amp; Sylvain MEIGNIER &amp; Kong Aik LEE.
Anthony Larcher's avatar
Anthony Larcher committed
523
524
525
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.3.1.
    </div>
  </body>
Anthony Larcher's avatar
Anthony Larcher committed
526
</html>