<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version='2.0' xmlns:lj='http://www.livejournal.org/rss/lj/1.0/'>
	<channel>
		<title>The Corelatus Blog - Entries from October 2009</title>
		<description>Entries from October 2009</description>
                <link>../../../</link>

	
	<item>
		<title>Audio power levels on E1/T1 timeslots: the digital milliwatt</title>
		<link>../../../Audio_power_levels_on_E1_T1_timeslots__the_digital_milliwatt.html</link>        
		<guid isPermaLink="true">../../../Audio_power_levels_on_E1_T1_timeslots__the_digital_milliwatt.html</guid>
                <pubDate>Sat, 10 Oct 2009 21:33:52 +0200</pubDate>
		<description>&lt;p&gt;
Sometimes, you want to know when the audio on an E1/T1 timeslot has
gotten louder than some limit. In a voice mail application, that&#39;s
useful for catching mistakes such as a subscriber leaving a message
but then not hanging up the phone properly---you don&#39;t want to record
hours and hours of silence. In an IVR application, you might want to
keep an eye on the audio level so that a frustrated (shouting!)
subscriber can be forwarded to a human operator.
&lt;/p&gt;

&lt;p&gt;
GTH provides a &quot;level detector&quot; to do that sort of thing. You start a
level detector on a timeslot, give it a loudness threshold, and it&#39;ll
notify you whenever the audio on the timeslot goes over that
threshold. Here&#39;s an example command which notifies you if the power
on timeslot 13 of an E1/T1 is louder than -20dBm0:
&lt;/p&gt;

&lt;pre&gt;
&lt;code&gt;
  &lt;span class=&quot;synIdentifier&quot;&gt;&amp;lt;new&amp;gt;&amp;lt;level_detector &lt;/span&gt;&lt;span class=&quot;synType&quot;&gt;threshold&lt;/span&gt;=&lt;span class=&quot;synConstant&quot;&gt;&#39;-20&#39;&lt;/span&gt;&lt;span class=&quot;synIdentifier&quot;&gt;&amp;gt;&lt;/span&gt;
     &lt;span class=&quot;synIdentifier&quot;&gt;&amp;lt;pcm_source &lt;/span&gt;&lt;span class=&quot;synType&quot;&gt;span&lt;/span&gt;=&lt;span class=&quot;synConstant&quot;&gt;&#39;2A&#39;&lt;/span&gt;&lt;span class=&quot;synIdentifier&quot;&gt; &lt;/span&gt;&lt;span class=&quot;synType&quot;&gt;timeslot&lt;/span&gt;=&lt;span class=&quot;synConstant&quot;&gt;&#39;13&#39;&lt;/span&gt;&lt;span class=&quot;synIdentifier&quot;&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;synIdentifier&quot;&gt;&amp;lt;/level_detector&amp;gt;&amp;lt;/new&amp;gt;&lt;/span&gt;
&lt;/code&gt;
&lt;/pre&gt;

The algorithm is:

&lt;ol&gt;
  &lt;li&gt;Take a 100ms block of audio (800 samples)&lt;/li&gt;
  &lt;li&gt;Square all the samples&lt;/li&gt;
  &lt;li&gt;Sum the squares&lt;/li&gt;
  &lt;li&gt;If the sum exceeds the loudness threshold, send an XML event&lt;/li&gt;
&lt;/ol&gt;

There are some details to worry about.

&lt;h3&gt;The digital milliwatt&lt;/h3&gt;

&lt;p&gt; The threshold, e.g. -20 in the example above, has to be relative
to something. The standard reference power level in telecommunications
is the milliwatt.  ITU-T G.711, table 5 and 6 defines the sequences
which represent a milliwatt:
&lt;/p&gt;

&lt;p&gt;
A-law: 34 21 21 34 b4 a1 a1 b4&lt;br/&gt;
&amp;#956;-law: 1e 0b 0b 1e 9e 8b 8b 9e&lt;br/&gt;
&lt;/p&gt;

&lt;p&gt;
Here&#39;s what a few periods of the digital milliwatt look like:
&lt;/p&gt;

&lt;img
src=&quot;static/small_linear_milliwatt.png&quot;
alt=&quot;small_linear_milliwatt&quot; title=&quot;small_linear_milliwatt&quot;/&gt;

&lt;p&gt;
In this post, the unit &#39;dBm0&#39; means power, in dB relative to the digital milliwatt, as defined by the sequences above. If you have no idea what dB means, wikipedia has a &lt;a href=&quot;http://en.wikipedia.org/wiki/Decibel&quot;&gt;decent article&lt;/a&gt;.
&lt;/p&gt;

&lt;h3&gt;What&#39;s the loudest possible sound on a timeslot?&lt;/h3&gt;

&lt;p&gt; 170 is the highest value possible in A-law encoding. It
corresponds to linear 4032. That&#39;s about 6dB louder than the digital
milliwatt.
&lt;/p&gt;

&lt;p&gt; 85 is the smallest value possible in A-law encoding. It
corresponds to linear -1. That&#39;s about 66dB softer than the digital
milliwatt.
&lt;/p&gt;

&lt;p&gt; The range -66dBm0...+6dBm0 sets an upper bound on the range of
power on a timeslot. Then there are other things which further limit
the practical range, so you&#39;re unlikely to actually use a +6dBm0
threshold in practice, but it&#39;s there if you want it.
&lt;/p&gt;

&lt;h3&gt;Is the G.711 definition the best one?&lt;/h3&gt;

&lt;p&gt;
The sequence given in G.711 is a 1kHz sine wave. The sampling rate on
E1/T1 is 8kHz, so the reference sequence can be expressed in just
eight values. That&#39;s nice, but that also leads to small errors, about
0.13dB, because of quantisation.
&lt;/p&gt;

&lt;p&gt;
ITU-T O.133 discusses that problem in detail and proposes a test
signal which specfically is &lt;b&gt;not&lt;/b&gt; 1kHz (i.e. not a submultiple of
the sampling rate). For most practical purposes, 0.13dB doesn&#39;t matter
and so the simple and robust thing to do is to use the well-defined
and well-known G.711 sequence as a reference.
&lt;/p&gt;

&lt;p&gt;
Here&#39;s what a few periods of a 1020Hz signal look like. Notice that
the samples, i.e. the red crosses, don&#39;t appear in the same spot one
period later---that way we don&#39;t get the same errors over and over
again.
&lt;/p&gt;

&lt;div class=&quot;image&quot;&gt;
&lt;img src=&quot;static/small_1020hz1.png&quot;
alt=&quot;1020Hz signal sampled at 125us intervals&quot;/&gt;
&lt;div&gt;1020Hz signal sampled at 125us intervals&lt;/div&gt;
&lt;/div&gt;

&lt;h3&gt;Testing pitfall: the .wav header&lt;/h3&gt;

&lt;p&gt; GTH &lt;em&gt;player&lt;/em&gt;s plays raw A-law or &amp;#956;-law data. If you feed
a player a .wav file in 8kHz A-law, or &amp;#956;-law if your network uses
&amp;#956;-law, there will be a very short bit of noise at the start of the
playback because the .wav header gets treated as though it were audio.
&lt;/p&gt;

&lt;p&gt; When testing level detection, especially at quiet levels, that
header noise is enough to trigger a detector. Here&#39;s a .wav of a
1000Hz sine wave at about -30dBm0: &lt;/p&gt;

&lt;pre&gt;
00000000  52 49 46 46 42 27 00 00  57 41 56 45 66 6d 74 20  |RIFFB&#39;..WAVEfmt |
00000010  12 00 00 00 06 00 01 00  40 1f 00 00 40 1f 00 00  |........@...@...|
00000020  01 00 08 00 00 00 66 61  63 74 04 00 00 00 10 27  |......fact.....&#39;|
00000030  00 00 64 61 74 61 10 27  00 00 d5 c4 f5 f1 f3 f1  |..data.&#39;........|
00000040  f5 c4 d5 44 75 71 73 71  75 44 d5 c4 f5 f1 f3 f1  |...DuqsquD......|
*
00002740  f5 c4 d5 44 75 71 73 71  75 44                    |...DuqsquD|
&lt;/pre&gt;

&lt;p&gt; The first 58 octets (bytes) are the header. If we turn that header
into a periodic signal, it&#39;s at about -8dBm0, which is fairly
loud. With the default &lt;em&gt;period&lt;/em&gt; parameter of 100ms in the level
detector, that&#39;ll cause a false level of about -11dBm0.
&lt;/p&gt;

&lt;h3&gt;The period parameter&lt;/h3&gt;

&lt;p&gt; The &lt;em&gt;level_detector&lt;/em&gt; has an optional parameter, the
period. The period sets the size of the audio block the GTH considers
when measuring the power. A short &lt;em&gt;period&lt;/em&gt; makes the GTH
responsive to sudden changes in power on the timeslot, which would be
useful in an application such as figuring out which of the people in a
conference call are currently talking. A long &lt;em&gt;period&lt;/em&gt; averages
out the power over a longer time, which is useful in deciding whether
a voicemail recording has finished.  &lt;/p&gt;

&lt;p&gt;
The default is 100ms.
&lt;/p&gt;

&lt;h3&gt;Sample files&lt;/h3&gt;

&lt;p&gt;
&lt;a href=&quot;http://blog.corelatus.com/static/milliwatt_reference_audio.zip&quot;&gt;This
.zip file&lt;/a&gt; contains sample recordings with 1kHz sine waves at 0,
-10, -20, -30, -40 and -50 dBm0. The .wav versions are useful for
listening to or importing into an audio editing program to calibrate
the level meter. The .raw versions are just plain A-law samples---you
can use them with a GTH player.
&lt;/p&gt;
</description>
	</item>
	
        </channel>
</rss>

