normfeat.html 37.2 KB
Newer Older
Anthony Larcher's avatar
Anthony Larcher committed
1

Anthony Larcher's avatar
Anthony Larcher committed
2
3
<!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
4

Anthony Larcher's avatar
Anthony Larcher committed
5
6
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
Anthony Larcher's avatar
Anthony Larcher committed
7
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
Anthony Larcher's avatar
Anthony Larcher committed
8
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
Anthony Larcher's avatar
doc    
Anthony Larcher committed
9
    <title>frontend.normfeat &#8212; SIDEKIT  documentation</title>
Anthony Larcher's avatar
Anthony Larcher committed
10
    <link rel="stylesheet" href="../../_static/sphinxdoc.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
11
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
Anthony Larcher's avatar
Anthony Larcher committed
12
    <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
Anthony Larcher's avatar
Anthony Larcher committed
13
14
15
    <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
doc    
Anthony Larcher committed
16
    <link rel="index" title="Index" href="../../genindex.html" />
Anthony Larcher's avatar
Anthony Larcher committed
17
    <link rel="search" title="Search" href="../../search.html" /> 
Anthony Larcher's avatar
Anthony Larcher committed
18
  </head><body>
Anthony Larcher's avatar
Anthony Larcher committed
19
20
21
22
23
24
25
26
27
    <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
doc    
Anthony Larcher committed
28
29
        <li class="nav-item nav-item-0"><a href="../../index.html">SIDEKIT  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" accesskey="U">Module code</a> &#187;</li> 
Anthony Larcher's avatar
Anthony Larcher committed
30
      </ul>
Anthony Larcher's avatar
Anthony Larcher committed
31
32
33
34
35
    </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>
Anthony Larcher's avatar
Anthony Larcher committed
36
    <div class="searchformwrapper">
Anthony Larcher's avatar
Anthony Larcher committed
37
    <form class="search" action="../../search.html" method="get">
Anthony Larcher's avatar
Anthony Larcher committed
38
39
      <input type="text" name="q" />
      <input type="submit" value="Go" />
Anthony Larcher's avatar
Anthony Larcher committed
40
41
42
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
Anthony Larcher's avatar
Anthony Larcher committed
43
    </div>
Anthony Larcher's avatar
Anthony Larcher committed
44
45
46
47
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
Anthony Larcher's avatar
Anthony Larcher committed
48
49
50
51
52

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
Anthony Larcher's avatar
Anthony Larcher committed
53
54
            
  <h1>Source code for frontend.normfeat</h1><div class="highlight"><pre>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<span></span><span class="c1"># -*- coding: utf-8 -*-</span>
<span class="c1">#</span>
<span class="c1"># This file is part of SIDEKIT.</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is a python package for speaker verification.</span>
<span class="c1"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is a python package for speaker verification.</span>
<span class="c1"># Home page: http://www-lium.univ-lemans.fr/sidekit/</span>
<span class="c1">#    </span>
<span class="c1"># SIDEKIT is free software: you can redistribute it and/or modify</span>
<span class="c1"># it under the terms of the GNU LLesser General Public License as </span>
<span class="c1"># published by the Free Software Foundation, either version 3 of the License, </span>
<span class="c1"># or (at your option) any later version.</span>
<span class="c1">#</span>
<span class="c1"># SIDEKIT is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="c1"># GNU Lesser General Public License for more details.</span>
<span class="c1">#</span>
<span class="c1"># You should have received a copy of the GNU Lesser General Public License</span>
<span class="c1"># along with SIDEKIT.  If not, see &lt;http://www.gnu.org/licenses/&gt;.</span>
Anthony Larcher's avatar
Anthony Larcher committed
77
78

<span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
79
<span class="sd">Copyright 2014-2018 Anthony Larcher and Sylvain Meignier</span>
Anthony Larcher's avatar
Anthony Larcher committed
80
81
82
83

<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>
Anthony Larcher's avatar
Anthony Larcher committed
84
85
<span class="kn">import</span> <span class="nn">numpy</span>
<span class="kn">import</span> <span class="nn">pandas</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
86
87
<span class="kn">import</span> <span class="nn">scipy.stats</span> <span class="k">as</span> <span class="nn">stats</span>
<span class="kn">from</span> <span class="nn">scipy.signal</span> <span class="k">import</span> <span class="n">lfilter</span>
Anthony Larcher's avatar
Anthony Larcher committed
88
89


Anthony Larcher's avatar
doc    
Anthony Larcher committed
90
<span class="n">__author__</span> <span class="o">=</span> <span class="s2">&quot;Anthony Larcher and Sylvain Meignier&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
91
<span class="n">__copyright__</span> <span class="o">=</span> <span class="s2">&quot;Copyright 2014-2018 Anthony Larcher and Sylvain Meignier&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
92
93
94
95
96
<span class="n">__license__</span> <span class="o">=</span> <span class="s2">&quot;LGPL&quot;</span>
<span class="n">__maintainer__</span> <span class="o">=</span> <span class="s2">&quot;Anthony Larcher&quot;</span>
<span class="n">__email__</span> <span class="o">=</span> <span class="s2">&quot;anthony.larcher@univ-lemans.fr&quot;</span>
<span class="n">__status__</span> <span class="o">=</span> <span class="s2">&quot;Production&quot;</span>
<span class="n">__docformat__</span> <span class="o">=</span> <span class="s1">&#39;reStructuredText&#39;</span>
Anthony Larcher's avatar
Anthony Larcher committed
97
98


Anthony Larcher's avatar
Anthony Larcher committed
99
<div class="viewcode-block" id="rasta_filt"><a class="viewcode-back" href="../../api/frontend/normfeat.html#frontend.normfeat.rasta_filt">[docs]</a><span class="k">def</span> <span class="nf">rasta_filt</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
100
101
102
103
104
105
106
107
    <span class="sd">&quot;&quot;&quot;Apply RASTA filtering to the input signal.</span>
<span class="sd">    </span>
<span class="sd">    :param x: the input audio signal to filter.</span>
<span class="sd">        cols of x = critical bands, rows of x = frame</span>
<span class="sd">        same for y but after filtering</span>
<span class="sd">        default filter is single pole at 0.94</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">x</span><span class="o">.</span><span class="n">T</span>
Anthony Larcher's avatar
Anthony Larcher committed
108
    <span class="n">numerator</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">arange</span><span class="p">(</span><span class="o">.</span><span class="mi">2</span><span class="p">,</span> <span class="o">-.</span><span class="mi">3</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
109
    <span class="n">denominator</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mf">0.94</span><span class="p">])</span>
Anthony Larcher's avatar
Anthony Larcher committed
110

Anthony Larcher's avatar
doc    
Anthony Larcher committed
111
112
113
114
115
    <span class="c1"># Initialize the state.  This avoids a big spike at the beginning</span>
    <span class="c1"># resulting from the dc offset level in each band.</span>
    <span class="c1"># (this is effectively what rasta/rasta_filt.c does).</span>
    <span class="c1"># Because Matlab uses a DF2Trans implementation, we have to</span>
    <span class="c1"># specify the FIR part to get the state right (but not the IIR part)</span>
Anthony Larcher's avatar
Anthony Larcher committed
116
117
    <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</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">zf</span> <span class="o">=</span> <span class="n">numpy</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="mi">0</span><span class="p">],</span> <span class="mi">4</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
118
    <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="n">y</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
119
        <span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:</span><span class="mi">4</span><span class="p">],</span> <span class="n">zf</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:</span><span class="mi">4</span><span class="p">]</span> <span class="o">=</span> <span class="n">lfilter</span><span class="p">(</span><span class="n">numerator</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:</span><span class="mi">4</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">zi</span><span class="o">=</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">])</span>
Anthony Larcher's avatar
Anthony Larcher committed
120

Anthony Larcher's avatar
doc    
Anthony Larcher committed
121
    <span class="c1"># .. but don&#39;t keep any of these values, just output zero at the beginning</span>
Anthony Larcher's avatar
Anthony Larcher committed
122
    <span class="n">y</span> <span class="o">=</span> <span class="n">numpy</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>
Anthony Larcher's avatar
Anthony Larcher committed
123

Anthony Larcher's avatar
doc    
Anthony Larcher committed
124
    <span class="c1"># Apply the full filter to the rest of the signal, append it</span>
Anthony Larcher's avatar
Anthony Larcher committed
125
    <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="n">y</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
126
        <span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">4</span><span class="p">:]</span> <span class="o">=</span> <span class="n">lfilter</span><span class="p">(</span><span class="n">numerator</span><span class="p">,</span> <span class="n">denominator</span><span class="p">,</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="mi">4</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">zi</span><span class="o">=</span><span class="n">zf</span><span class="p">[</span><span class="n">i</span><span class="p">,</span> <span class="p">:])[</span><span class="mi">0</span><span class="p">]</span>
Anthony Larcher's avatar
Anthony Larcher committed
127
    
Anthony Larcher's avatar
doc    
Anthony Larcher committed
128
    <span class="k">return</span> <span class="n">y</span><span class="o">.</span><span class="n">T</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
129

Anthony Larcher's avatar
doc    
Anthony Larcher committed
130

Anthony Larcher's avatar
Anthony Larcher committed
131
<div class="viewcode-block" id="cms"><a class="viewcode-back" href="../../api/frontend/normfeat.html#frontend.normfeat.cms">[docs]</a><span class="k">def</span> <span class="nf">cms</span><span class="p">(</span><span class="n">features</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">global_mean</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
132
133
134
135
136
    <span class="sd">&quot;&quot;&quot;Performs cepstral mean subtraction</span>
<span class="sd">    </span>
<span class="sd">    :param features: a feature stream of dimension dim x nframes </span>
<span class="sd">            where dim is the dimension of the acoustic features and nframes the </span>
<span class="sd">            number of frames in the stream</span>
Anthony Larcher's avatar
Anthony Larcher committed
137
<span class="sd">    :param label: a logical vector</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
138
<span class="sd">    :param global_mean: pre-computed mean to use for feature normalization if given</span>
Anthony Larcher's avatar
Anthony Larcher committed
139
140
141

<span class="sd">    :return: a feature stream</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
142
143
    <span class="c1"># If no label file as input: all speech are speech</span>
    <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
144
145
146
        <span class="n">label</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">features</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">astype</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">label</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="n">features</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
147
    <span class="k">if</span> <span class="n">global_mean</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
148
149
150
        <span class="n">mu</span> <span class="o">=</span> <span class="n">global_mean</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">features</span><span class="p">[</span><span class="n">label</span><span class="p">,</span> <span class="p">:],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
151
    <span class="n">features</span> <span class="o">-=</span> <span class="n">mu</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
152

Anthony Larcher's avatar
doc    
Anthony Larcher committed
153

Anthony Larcher's avatar
Anthony Larcher committed
154
<div class="viewcode-block" id="cmvn"><a class="viewcode-back" href="../../api/frontend/normfeat.html#frontend.normfeat.cmvn">[docs]</a><span class="k">def</span> <span class="nf">cmvn</span><span class="p">(</span><span class="n">features</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">global_mean</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">global_std</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
155
156
157
158
159
    <span class="sd">&quot;&quot;&quot;Performs mean and variance normalization</span>
<span class="sd">    </span>
<span class="sd">    :param features: a feature stream of dimension dim x nframes </span>
<span class="sd">        where dim is the dimension of the acoustic features and nframes the </span>
<span class="sd">        number of frames in the stream</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
160
161
<span class="sd">    :param global_mean: pre-computed mean to use for feature normalization if given</span>
<span class="sd">    :param global_std: pre-computed standard deviation to use for feature normalization if given</span>
Anthony Larcher's avatar
Anthony Larcher committed
162
163
164
165
<span class="sd">    :param label: a logical verctor</span>

<span class="sd">    :return: a sequence of features</span>
<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
166
167
    <span class="c1"># If no label file as input: all speech are speech</span>
    <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
168
        <span class="n">label</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">features</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">astype</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
169

Anthony Larcher's avatar
doc    
Anthony Larcher committed
170
    <span class="k">if</span> <span class="n">global_mean</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">global_std</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
171
172
        <span class="n">mu</span> <span class="o">=</span> <span class="n">global_mean</span>
        <span class="n">stdev</span> <span class="o">=</span> <span class="n">global_std</span>
Anthony Larcher's avatar
update    
Anthony Larcher committed
173
174
        <span class="n">features</span> <span class="o">-=</span> <span class="n">mu</span>
        <span class="n">features</span> <span class="o">/=</span> <span class="n">stdev</span>
Anthony Larcher's avatar
Anthony Larcher committed
175

Anthony Larcher's avatar
Anthony Larcher committed
176
177
178
179
    <span class="k">elif</span> <span class="ow">not</span> <span class="n">label</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">mu</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">mean</span><span class="p">(</span><span class="n">features</span><span class="p">[</span><span class="n">label</span><span class="p">,</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">stdev</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">std</span><span class="p">(</span><span class="n">features</span><span class="p">[</span><span class="n">label</span><span class="p">,</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">features</span> <span class="o">-=</span> <span class="n">mu</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
180
        <span class="n">features</span> <span class="o">/=</span> <span class="n">stdev</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
181

Anthony Larcher's avatar
doc    
Anthony Larcher committed
182

Anthony Larcher's avatar
Anthony Larcher committed
183
<div class="viewcode-block" id="stg"><a class="viewcode-back" href="../../api/frontend/normfeat.html#frontend.normfeat.stg">[docs]</a><span class="k">def</span> <span class="nf">stg</span><span class="p">(</span><span class="n">features</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">win</span><span class="o">=</span><span class="mi">301</span><span class="p">):</span>
Anthony Larcher's avatar
Anthony Larcher committed
184
185
186
187
188
    <span class="sd">&quot;&quot;&quot;Performs feature warping on a sliding window</span>
<span class="sd">    </span>
<span class="sd">    :param features: a feature stream of dimension dim x nframes </span>
<span class="sd">        where dim is the dimension of the acoustic features and nframes the</span>
<span class="sd">        number of frames in the stream</span>
Anthony Larcher's avatar
Anthony Larcher committed
189
190
<span class="sd">    :param label: label of selected frames to compute the Short Term Gaussianization, by default, al frames are used</span>
<span class="sd">    :param win: size of the frame window to consider, must be an odd number to get a symetric context on left and right</span>
Anthony Larcher's avatar
Anthony Larcher committed
191
192
193
<span class="sd">    :return: a sequence of features</span>
<span class="sd">    &quot;&quot;&quot;</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
194
195
    <span class="c1"># If no label file as input: all speech are speech</span>
    <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
196
197
        <span class="n">label</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">features</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">astype</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>
    <span class="n">speech_features</span> <span class="o">=</span> <span class="n">features</span><span class="p">[</span><span class="n">label</span><span class="p">,</span> <span class="p">:]</span>
Anthony Larcher's avatar
Anthony Larcher committed
198

Anthony Larcher's avatar
doc    
Anthony Larcher committed
199
    <span class="n">add_a_feature</span> <span class="o">=</span> <span class="kc">False</span>
Anthony Larcher's avatar
Anthony Larcher committed
200
    <span class="k">if</span> <span class="n">win</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
201
        <span class="c1"># one feature per line</span>
Anthony Larcher's avatar
Anthony Larcher committed
202
        <span class="n">nframes</span><span class="p">,</span> <span class="n">dim</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">speech_features</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
203

Anthony Larcher's avatar
doc    
Anthony Larcher committed
204
        <span class="c1"># If the number of frames is not enough for one window</span>
Anthony Larcher's avatar
Anthony Larcher committed
205
        <span class="k">if</span> <span class="n">nframes</span> <span class="o">&lt;</span> <span class="n">win</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
206
207
            <span class="c1"># if the number of frames is not odd, duplicate the last frame</span>
            <span class="c1"># if nframes % 2 == 1:</span>
Anthony Larcher's avatar
Anthony Larcher committed
208
209
            <span class="k">if</span> <span class="ow">not</span> <span class="n">nframes</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">nframes</span> <span class="o">+=</span> <span class="mi">1</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
210
                <span class="n">add_a_feature</span> <span class="o">=</span> <span class="kc">True</span>
Anthony Larcher's avatar
Anthony Larcher committed
211
                <span class="n">speech_features</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">concatenate</span><span class="p">((</span><span class="n">speech_features</span><span class="p">,</span> <span class="p">[</span><span class="n">speech_features</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="p">]]))</span>
Anthony Larcher's avatar
Anthony Larcher committed
212
213
            <span class="n">win</span> <span class="o">=</span> <span class="n">nframes</span>

Anthony Larcher's avatar
doc    
Anthony Larcher committed
214
        <span class="c1"># create the output feature stream</span>
Anthony Larcher's avatar
Anthony Larcher committed
215
        <span class="n">stg_features</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">zeros</span><span class="p">(</span><span class="n">numpy</span><span class="o">.</span><span class="n">shape</span><span class="p">(</span><span class="n">speech_features</span><span class="p">))</span>
Anthony Larcher's avatar
Anthony Larcher committed
216

Anthony Larcher's avatar
doc    
Anthony Larcher committed
217
        <span class="c1"># Process first window</span>
Anthony Larcher's avatar
Anthony Larcher committed
218
219
220
221
        <span class="n">r</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">speech_features</span><span class="p">[:</span><span class="n">win</span><span class="p">,</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">r</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">r</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">arg</span> <span class="o">=</span> <span class="p">(</span><span class="n">r</span><span class="p">[:</span> <span class="p">(</span><span class="n">win</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">/</span> <span class="n">win</span>
        <span class="n">stg_features</span><span class="p">[:</span> <span class="p">(</span><span class="n">win</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">stats</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
222

Anthony Larcher's avatar
doc    
Anthony Larcher committed
223
        <span class="c1"># process all following windows except the last one</span>
Anthony Larcher's avatar
Anthony Larcher committed
224
        <span class="k">for</span> <span class="n">m</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">((</span><span class="n">win</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">nframes</span> <span class="o">-</span> <span class="p">(</span><span class="n">win</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">)):</span>
Anthony Larcher's avatar
Anthony Larcher committed
225
            <span class="n">idx</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">m</span> <span class="o">-</span> <span class="p">(</span><span class="n">win</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">),</span> <span class="nb">int</span><span class="p">(</span><span class="n">m</span> <span class="o">+</span> <span class="p">(</span><span class="n">win</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)))</span>
Anthony Larcher's avatar
Anthony Larcher committed
226
227
228
229
            <span class="n">foo</span> <span class="o">=</span> <span class="n">speech_features</span><span class="p">[</span><span class="n">idx</span><span class="p">,</span> <span class="p">:]</span>
            <span class="n">r</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">foo</span> <span class="o">&lt;</span> <span class="n">foo</span><span class="p">[(</span><span class="n">win</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</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="o">+</span> <span class="mi">1</span>
            <span class="n">arg</span> <span class="o">=</span> <span class="p">(</span><span class="n">r</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">/</span> <span class="n">win</span>
            <span class="n">stg_features</span><span class="p">[</span><span class="n">m</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">stats</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
230

Anthony Larcher's avatar
doc    
Anthony Larcher committed
231
        <span class="c1"># Process the last window</span>
Anthony Larcher's avatar
Anthony Larcher committed
232
233
234
        <span class="n">r</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">speech_features</span><span class="p">[</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">nframes</span> <span class="o">-</span> <span class="n">win</span><span class="p">,</span> <span class="n">nframes</span><span class="p">)),</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">r</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">argsort</span><span class="p">(</span><span class="n">r</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">arg</span> <span class="o">=</span> <span class="p">(</span><span class="n">r</span><span class="p">[(</span><span class="n">win</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">:</span> <span class="n">win</span><span class="p">,</span> <span class="p">:]</span> <span class="o">+</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">/</span> <span class="n">win</span>
Anthony Larcher's avatar
Anthony Larcher committed
235
        
Anthony Larcher's avatar
Anthony Larcher committed
236
        <span class="n">stg_features</span><span class="p">[</span><span class="nb">list</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">nframes</span> <span class="o">-</span> <span class="p">(</span><span class="n">win</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span><span class="p">),</span> <span class="n">nframes</span><span class="p">)),</span> <span class="p">]</span> <span class="o">=</span> <span class="n">stats</span><span class="o">.</span><span class="n">norm</span><span class="o">.</span><span class="n">ppf</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
237
    <span class="k">else</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
238
239
        <span class="c1"># Raise an exception</span>
        <span class="k">raise</span> <span class="ne">Exception</span><span class="p">(</span><span class="s1">&#39;Sliding window should have an odd length&#39;</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
240

Anthony Larcher's avatar
doc    
Anthony Larcher committed
241
    <span class="c1"># wrapFeatures = np.copy(features)</span>
Anthony Larcher's avatar
Anthony Larcher committed
242
    <span class="k">if</span> <span class="n">add_a_feature</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
243
        <span class="n">stg_features</span> <span class="o">=</span> <span class="n">stg_features</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
244
    <span class="n">features</span><span class="p">[</span><span class="n">label</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">stg_features</span></div>
Anthony Larcher's avatar
Anthony Larcher committed
245

Anthony Larcher's avatar
doc    
Anthony Larcher committed
246

Anthony Larcher's avatar
Anthony Larcher committed
247
<div class="viewcode-block" id="cep_sliding_norm"><a class="viewcode-back" href="../../api/frontend/normfeat.html#frontend.normfeat.cep_sliding_norm">[docs]</a><span class="k">def</span> <span class="nf">cep_sliding_norm</span><span class="p">(</span><span class="n">features</span><span class="p">,</span> <span class="n">win</span><span class="o">=</span><span class="mi">301</span><span class="p">,</span> <span class="n">label</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">center</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">reduce</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
Sylvain Meignier's avatar
Sylvain Meignier committed
248
    <span class="sd">&quot;&quot;&quot;</span>
Anthony Larcher's avatar
Anthony Larcher committed
249
<span class="sd">    Performs a cepstal mean substitution and standard deviation normalization</span>
Sylvain Meignier's avatar
Sylvain Meignier committed
250
251
<span class="sd">    in a sliding windows. MFCC is modified.</span>

Anthony Larcher's avatar
Anthony Larcher committed
252
253
254
255
256
<span class="sd">    :param features: the MFCC, a numpy array</span>
<span class="sd">    :param win: the size of the sliding windows</span>
<span class="sd">    :param label: vad label if available</span>
<span class="sd">    :param center: performs mean subtraction</span>
<span class="sd">    :param reduce: performs standard deviation division</span>
Sylvain Meignier's avatar
Sylvain Meignier committed
257
258

<span class="sd">    &quot;&quot;&quot;</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
259
    <span class="k">if</span> <span class="n">label</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
260
261
262
        <span class="n">label</span> <span class="o">=</span> <span class="n">numpy</span><span class="o">.</span><span class="n">ones</span><span class="p">(</span><span class="n">features</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">astype</span><span class="p">(</span><span class="nb">bool</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">numpy</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">label</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="n">win</span><span class="p">:</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
263
        <span class="k">if</span> <span class="n">reduce</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
264
265
266
267
268
            <span class="n">cmvn</span><span class="p">(</span><span class="n">features</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">cms</span><span class="p">(</span><span class="n">features</span><span class="p">,</span> <span class="n">label</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">d_win</span> <span class="o">=</span> <span class="n">win</span> <span class="o">//</span> <span class="mi">2</span>
Sylvain Meignier's avatar
Sylvain Meignier committed
269

Anthony Larcher's avatar
Anthony Larcher committed
270
        <span class="n">df</span> <span class="o">=</span> <span class="n">pandas</span><span class="o">.</span><span class="n">DataFrame</span><span class="p">(</span><span class="n">features</span><span class="p">[</span><span class="n">label</span><span class="p">,</span> <span class="p">:])</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
271
        <span class="n">r</span> <span class="o">=</span> <span class="n">df</span><span class="o">.</span><span class="n">rolling</span><span class="p">(</span><span class="n">window</span><span class="o">=</span><span class="n">win</span><span class="p">,</span> <span class="n">center</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
Anthony Larcher's avatar
Anthony Larcher committed
272
273
        <span class="n">mean</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">mean</span><span class="p">()</span><span class="o">.</span><span class="n">values</span>
        <span class="n">std</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">std</span><span class="p">()</span><span class="o">.</span><span class="n">values</span>
Sylvain Meignier's avatar
Sylvain Meignier committed
274

Anthony Larcher's avatar
Anthony Larcher committed
275
276
        <span class="n">mean</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">d_win</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">mean</span><span class="p">[</span><span class="n">d_win</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">mean</span><span class="p">[</span><span class="o">-</span><span class="n">d_win</span><span class="p">:,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">mean</span><span class="p">[</span><span class="o">-</span><span class="n">d_win</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="p">:]</span>
Sylvain Meignier's avatar
Sylvain Meignier committed
277

Anthony Larcher's avatar
Anthony Larcher committed
278
279
        <span class="n">std</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="n">d_win</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">std</span><span class="p">[</span><span class="n">d_win</span><span class="p">,</span> <span class="p">:]</span>
        <span class="n">std</span><span class="p">[</span><span class="o">-</span><span class="n">d_win</span><span class="p">:,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">std</span><span class="p">[</span><span class="o">-</span><span class="n">d_win</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="p">:]</span>
Sylvain Meignier's avatar
Sylvain Meignier committed
280

Anthony Larcher's avatar
Anthony Larcher committed
281
282
        <span class="k">if</span> <span class="n">center</span><span class="p">:</span>
            <span class="n">features</span><span class="p">[</span><span class="n">label</span><span class="p">,</span> <span class="p">:]</span> <span class="o">-=</span> <span class="n">mean</span>
Anthony Larcher's avatar
doc    
Anthony Larcher committed
283
            <span class="k">if</span> <span class="n">reduce</span><span class="p">:</span>
Anthony Larcher's avatar
Anthony Larcher committed
284
285
                <span class="n">features</span><span class="p">[</span><span class="n">label</span><span class="p">,</span> <span class="p">:]</span> <span class="o">/=</span> <span class="n">std</span></div>
</pre></div>
Anthony Larcher's avatar
Anthony Larcher committed
286
287

          </div>
Anthony Larcher's avatar
Anthony Larcher committed
288
289
290
291
292
293
294
295
296
297
298
299
300
        </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
doc    
Anthony Larcher committed
301
302
        <li class="nav-item nav-item-0"><a href="../../index.html">SIDEKIT  documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >Module code</a> &#187;</li> 
Anthony Larcher's avatar
Anthony Larcher committed
303
304
305
      </ul>
    </div>
    <div class="footer" role="contentinfo">
Anthony Larcher's avatar
doc    
Anthony Larcher committed
306
        &#169; Copyright 2014-16, Anthony LARCHER &amp; Sylvain MEIGNIER &amp; Kong Aik LEE.
Anthony Larcher's avatar
Anthony Larcher committed
307
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.7.9.
Anthony Larcher's avatar
Anthony Larcher committed
308
309
    </div>
  </body>
Anthony Larcher's avatar
Anthony Larcher committed
310
</html>