SignalToNoise2.c.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. SignalToNoise2.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='SignalToNoise1.c.html' title=' SignalToNoise1.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='slac_connect.c.html' title=' slac_connect.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * signed SignalToNoise2 (struct plc * plc);
  64. *
  65. * amp.h
  66. *
  67. *
  68. * Contributor(s):
  69. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  70. *
  71. *--------------------------------------------------------------------*/
  72. #ifndef SIGNALTONOISE2_SOURCE
  73. #define SIGNALTONOISE2_SOURCE
  74. #include &lt;stdio.h&gt;
  75. #include &lt;stdint.h&gt;
  76. #include &quot;../tools/types.h&quot;
  77. #include &quot;../tools/flags.h&quot;
  78. #include &quot;../tools/error.h&quot;
  79. #include &quot;../tools/endian.h&quot;
  80. #include &quot;../plc/plc.h&quot;
  81. #include &quot;../mme/mme.h&quot;
  82. #ifndef TONEMAPS2_SOURCE
  83. static uint8_t const mod2bits [AMP_BITS] =
  84. {
  85. 0,
  86. 1,
  87. 2,
  88. 3,
  89. 4,
  90. 6,
  91. 8,
  92. 10,
  93. 12
  94. };
  95. #endif
  96. #ifndef TONEMAPS2_SOURCE
  97. static uint8_t const mod2db [AMP_BITS] =
  98. {
  99. 0,
  100. 2,
  101. 4,
  102. 7,
  103. 10,
  104. 16,
  105. 22,
  106. 28,
  107. 36
  108. };
  109. #endif
  110. signed SignalToNoise2 (struct plc * plc)
  111. {
  112. struct channel * channel = (struct channel *)(plc-&gt;channel);
  113. struct message * message = (struct message *)(plc-&gt;message);
  114. byte tonemap [PLC_SLOTS + 1][AMP_CARRIERS &gt;&gt; 1];
  115. uint16_t GIL [PLC_SLOTS];
  116. uint16_t AGC [PLC_SLOTS];
  117. double SNR [PLC_SLOTS];
  118. double BPC [PLC_SLOTS];
  119. double AvgSNR;
  120. double AvgBPC;
  121. uint16_t extent = 0;
  122. uint16_t active = 0;
  123. uint16_t carriers = AMP_CARRIERS;
  124. uint16_t carrier = 0;
  125. uint8_t slots = PLC_SLOTS;
  126. uint8_t slot = 0;
  127. #ifndef __GNUC__
  128. #pragma pack (push,1)
  129. #endif
  130. struct __packed vs_rx_tone_map_char_request
  131. {
  132. struct ethernet_hdr ethernet;
  133. struct qualcomm_fmi qualcomm;
  134. uint8_t MME_SUBVER;
  135. uint8_t Reserved1 [3];
  136. uint8_t MACADDRESS [ETHER_ADDR_LEN];
  137. uint8_t TMSLOT;
  138. uint8_t COUPLING;
  139. }
  140. * request = (struct vs_rx_tone_map_char_request *) (message);
  141. struct __packed vs_rx_tonemap_char_confirm
  142. {
  143. struct ethernet_hdr ethernet;
  144. struct qualcomm_fmi qualcomm;
  145. uint8_t MSTATUS;
  146. uint8_t Reserved1;
  147. uint16_t MME_LEN;
  148. uint8_t MME_SUBVER;
  149. uint8_t Reserved2;
  150. uint8_t MACADDR [6];
  151. uint8_t TMSLOT;
  152. uint8_t COUPLING;
  153. uint8_t NUMTMS;
  154. uint8_t Reserved4;
  155. uint16_t TMNUMACTCARRIERS;
  156. uint32_t Reserved6;
  157. uint8_t GIL;
  158. uint8_t Reserved7;
  159. uint8_t AGC;
  160. uint8_t Reserved8;
  161. uint8_t MOD_CARRIER [1];
  162. }
  163. * confirm = (struct vs_rx_tonemap_char_confirm *) (message);
  164. struct __packed vs_rx_tonemap_char_fragment
  165. {
  166. struct ethernet_hdr ethernet;
  167. struct homeplug_fmi qualcomm;
  168. uint8_t MOD_CARRIER [1];
  169. }
  170. * fragment = (struct vs_rx_tonemap_char_fragment *) (message);
  171. #ifndef __GNUC__
  172. #pragma pack (pop)
  173. #endif
  174. memset (tonemap, 0, sizeof (tonemap));
  175. for (carrier = slot = 0; slot &lt; slots; carrier = 0, slot++)
  176. {
  177. memset (message, 0, sizeof (* message));
  178. EthernetHeader (&amp;request-&gt;ethernet, channel-&gt;peer, channel-&gt;host, channel-&gt;type);
  179. QualcommHeader1 (&amp;request-&gt;qualcomm, 1, (VS_RX_TONE_MAP_CHAR | MMTYPE_REQ));
  180. memcpy (request-&gt;MACADDRESS, plc-&gt;RDA, sizeof (request-&gt;MACADDRESS));
  181. request-&gt;TMSLOT = slot;
  182. request-&gt;COUPLING = plc-&gt;coupling;
  183. plc-&gt;packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  184. if (SendMME (plc) &lt;= 0)
  185. {
  186. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  187. return (-1);
  188. }
  189. if (ReadMME (plc, 1, (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF)) &lt;= 0)
  190. {
  191. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  192. return (-1);
  193. }
  194. if (confirm-&gt;MSTATUS)
  195. {
  196. error (1, 0, &quot;Device refused request for slot %d: %s&quot;, slot, MMECode (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF, confirm-&gt;MSTATUS));
  197. }
  198. GIL [slot] = confirm-&gt;GIL;
  199. AGC [slot] = confirm-&gt;AGC;
  200. carriers = LE16TOH (confirm-&gt;TMNUMACTCARRIERS);
  201. slots = confirm-&gt;NUMTMS;
  202. extent = LE16TOH (confirm-&gt;MME_LEN) - 22;
  203. if (extent &gt; (AMP_CARRIERS &gt;&gt; 1))
  204. {
  205. error (1, EOVERFLOW, &quot;Too many carriers&quot;);
  206. }
  207. plc-&gt;packetsize -= sizeof (struct vs_rx_tonemap_char_confirm);
  208. plc-&gt;packetsize += sizeof (confirm-&gt;MOD_CARRIER);
  209. if (plc-&gt;packetsize &gt; extent)
  210. {
  211. plc-&gt;packetsize = extent;
  212. }
  213. memcpy (&amp;tonemap [slot] [carrier], &amp;confirm-&gt;MOD_CARRIER, plc-&gt;packetsize);
  214. carrier += plc-&gt;packetsize;
  215. extent -= plc-&gt;packetsize;
  216. while (extent)
  217. {
  218. if (ReadMME (plc, 1, (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF)) &lt;= 0)
  219. {
  220. error (1, errno, CHANNEL_CANTREAD);
  221. }
  222. plc-&gt;packetsize -= sizeof (struct vs_rx_tonemap_char_fragment);
  223. plc-&gt;packetsize += sizeof (fragment-&gt;MOD_CARRIER);
  224. if (plc-&gt;packetsize &gt; extent)
  225. {
  226. plc-&gt;packetsize = extent;
  227. }
  228. memcpy (&amp;tonemap [slot] [carrier], fragment-&gt;MOD_CARRIER, plc-&gt;packetsize);
  229. carrier += plc-&gt;packetsize;
  230. extent -= plc-&gt;packetsize;
  231. }
  232. }
  233. carrier = 0;
  234. /*
  235. * LOW BANDS;
  236. */
  237. memset (BPC, 0, sizeof (BPC));
  238. memset (SNR, 0, sizeof (SNR));
  239. AvgBPC = 0;
  240. AvgSNR = 0;
  241. while (carrier &lt; INT_CARRIERS)
  242. {
  243. unsigned value = 0;
  244. unsigned scale = 0;
  245. unsigned index = carrier &gt;&gt; 1;
  246. printf (&quot;%04d&quot;, carrier);
  247. for (slot = 0; slot &lt; slots; slot++)
  248. {
  249. value = tonemap [slot][index];
  250. if ((carrier &amp; 1))
  251. {
  252. value &gt;&gt;= 4;
  253. }
  254. value &amp;= 0x0F;
  255. if (value &gt; (AMP_BITS-1))
  256. {
  257. error (0, EINVAL, &quot;Index %d Slot %d Value %d&quot;, carrier, slot, value);
  258. }
  259. printf (&quot;,%02d&quot;, mod2bits [value]);
  260. BPC [slot] += mod2bits [value];
  261. SNR [slot] += mod2db [value];
  262. AvgBPC += mod2bits [value];
  263. AvgSNR += mod2db [value];
  264. value *= value;
  265. scale += value;
  266. }
  267. if (_anyset (plc-&gt;flags, PLC_GRAPH))
  268. {
  269. printf (&quot; %03d &quot;, scale);
  270. if (scale)
  271. {
  272. scale /= slots;
  273. while (scale--)
  274. {
  275. printf (&quot;#&quot;);
  276. }
  277. active++;
  278. }
  279. }
  280. printf (&quot;\n&quot;);
  281. carrier++;
  282. }
  283. AvgBPC /= active;
  284. AvgBPC /= slots;
  285. AvgSNR /= active;
  286. AvgSNR /= slots;
  287. printf (&quot; SNR&quot;);
  288. for (slot = 0; slot &lt; slots; slot++)
  289. {
  290. printf (&quot;,%8.3f&quot;, (float)(SNR [slot]) / active);
  291. }
  292. printf (&quot;,%8.3f&quot;, AvgSNR);
  293. printf (&quot; \n&quot;);
  294. printf (&quot; ATN&quot;);
  295. for (slot = 0; slot &lt; slots; slot++)
  296. {
  297. printf (&quot;,%8.3f&quot;, (float)(SNR [slot]) / active - 60);
  298. }
  299. printf (&quot;,%8.3f&quot;, AvgSNR - 60);
  300. printf (&quot; \n&quot;);
  301. printf (&quot; BPC&quot;);
  302. for (slot = 0; slot &lt; slots; slot++)
  303. {
  304. printf (&quot;,%8.3f&quot;, (float)(BPC [slot]) / active);
  305. }
  306. printf (&quot;,%8.3f&quot;, AvgBPC);
  307. printf (&quot; \n&quot;);
  308. /*
  309. * HIGH BANDS;
  310. */
  311. memset (BPC, 0, sizeof (BPC));
  312. memset (SNR, 0, sizeof (SNR));
  313. AvgBPC = 0;
  314. AvgSNR = 0;
  315. while (carrier &lt; carriers)
  316. {
  317. unsigned value = 0;
  318. unsigned scale = 0;
  319. unsigned index = carrier &gt;&gt; 1;
  320. printf (&quot;%04d&quot;, carrier);
  321. for (slot = 0; slot &lt; slots; slot++)
  322. {
  323. value = tonemap [slot][index];
  324. if ((carrier &amp; 1))
  325. {
  326. value &gt;&gt;= 4;
  327. }
  328. value &amp;= 0x0F;
  329. if (value &gt; (AMP_BITS-1))
  330. {
  331. error (0, EINVAL, &quot;Index %d Slot %d Value %d&quot;, carrier, slot, value);
  332. }
  333. printf (&quot;,%02d&quot;, mod2bits [value]);
  334. BPC [slot] += mod2bits [value];
  335. SNR [slot] += mod2db [value];
  336. AvgBPC += mod2bits [value];
  337. AvgSNR += mod2db [value];
  338. value *= value;
  339. scale += value;
  340. }
  341. if (_anyset (plc-&gt;flags, PLC_GRAPH))
  342. {
  343. printf (&quot; %03d &quot;, scale);
  344. if (scale)
  345. {
  346. scale /= slots;
  347. while (scale--)
  348. {
  349. printf (&quot;#&quot;);
  350. }
  351. }
  352. }
  353. printf (&quot;\n&quot;);
  354. carrier++;
  355. active++;
  356. }
  357. AvgBPC /= active;
  358. AvgBPC /= slots;
  359. AvgSNR /= active;
  360. AvgSNR /= slots;
  361. printf (&quot; SNR&quot;);
  362. for (slot = 0; slot &lt; slots; slot++)
  363. {
  364. printf (&quot;,%8.3f&quot;, (float)(SNR [slot]) / active);
  365. }
  366. printf (&quot;,%8.3f&quot;, AvgSNR);
  367. printf (&quot; \n&quot;);
  368. printf (&quot; ATN&quot;);
  369. for (slot = 0; slot &lt; slots; slot++)
  370. {
  371. printf (&quot;,%8.3f&quot;, (float)(SNR [slot]) / active - 60);
  372. }
  373. printf (&quot;,%8.3f&quot;, AvgSNR - 60);
  374. printf (&quot; \n&quot;);
  375. printf (&quot; BPC&quot;);
  376. for (slot = 0; slot &lt; slots; slot++)
  377. {
  378. printf (&quot;,%8.3f&quot;, (float)(BPC [slot]) / active);
  379. }
  380. printf (&quot;,%8.3f&quot;, AvgBPC);
  381. printf (&quot; \n&quot;);
  382. printf (&quot; AGC&quot;);
  383. for (slot = 0; slot &lt; slots; slot++)
  384. {
  385. printf (&quot;,%02d&quot;, AGC [slot]);
  386. }
  387. printf (&quot; \n&quot;);
  388. printf (&quot; GIL&quot;);
  389. for (slot = 0; slot &lt; slots; slot++)
  390. {
  391. printf (&quot;,%02d&quot;, GIL [slot]);
  392. }
  393. printf (&quot; \n&quot;);
  394. return (0);
  395. }
  396. #endif
  397. </pre>
  398. <div class='footerlink'>
  399. [<a href='SignalToNoise1.c.html' title=' SignalToNoise1.c '>PREV</a>]
  400. [<a href='toolkit.html' title=' Index '>HOME</a>]
  401. [<a href='slac_connect.c.html' title=' slac_connect.c '>NEXT</a>]
  402. </div>
  403. </body>
  404. </html>