phydm_math_lib.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * The full GNU General Public License is included in this distribution in the
  15. * file called LICENSE.
  16. *
  17. * Contact Information:
  18. * wlanfae <wlanfae@realtek.com>
  19. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  20. * Hsinchu 300, Taiwan.
  21. *
  22. * Larry Finger <Larry.Finger@lwfinger.net>
  23. *
  24. *****************************************************************************/
  25. /*@************************************************************
  26. * include files
  27. ************************************************************/
  28. #include "mp_precomp.h"
  29. #include "phydm_precomp.h"
  30. const u16 db_invert_table[12][8] = {
  31. {1, 1, 1, 2, 2, 2, 2, 3},
  32. {3, 3, 4, 4, 4, 5, 6, 6},
  33. {7, 8, 9, 10, 11, 13, 14, 16},
  34. {18, 20, 22, 25, 28, 32, 35, 40},
  35. {45, 50, 56, 63, 71, 79, 89, 100},
  36. {112, 126, 141, 158, 178, 200, 224, 251},
  37. {282, 316, 355, 398, 447, 501, 562, 631},
  38. {708, 794, 891, 1000, 1122, 1259, 1413, 1585},
  39. {1778, 1995, 2239, 2512, 2818, 3162, 3548, 3981},
  40. {4467, 5012, 5623, 6310, 7079, 7943, 8913, 10000},
  41. {11220, 12589, 14125, 15849, 17783, 19953, 22387, 25119},
  42. {28184, 31623, 35481, 39811, 44668, 50119, 56234, 65535} };
  43. /*Y = 10*log(X)*/
  44. s32 odm_pwdb_conversion(s32 X, u32 total_bit, u32 decimal_bit)
  45. {
  46. s32 Y, integer = 0, decimal = 0;
  47. u32 i;
  48. if (X == 0)
  49. X = 1; /* @log2(x), x can't be 0 */
  50. for (i = (total_bit - 1); i > 0; i--) {
  51. if (X & BIT(i)) {
  52. integer = i;
  53. if (i > 0) {
  54. /*decimal is 0.5dB*3=1.5dB~=2dB */
  55. decimal = (X & BIT(i - 1)) ? 2 : 0;
  56. }
  57. break;
  58. }
  59. }
  60. Y = 3 * (integer - decimal_bit) + decimal; /* @10*log(x)=3*log2(x), */
  61. return Y;
  62. }
  63. s32 odm_sign_conversion(s32 value, u32 total_bit)
  64. {
  65. if (value & BIT(total_bit - 1))
  66. value -= BIT(total_bit);
  67. return value;
  68. }
  69. /*threshold must form low to high*/
  70. u16 phydm_find_intrvl(void *dm_void, u16 val, u16 *threshold, u16 th_len)
  71. {
  72. struct dm_struct *dm = (struct dm_struct *)dm_void;
  73. u16 i = 0;
  74. u16 ret_val = 0;
  75. u16 max_th = threshold[th_len - 1];
  76. for (i = 0; i < th_len; i++) {
  77. if (val < threshold[i]) {
  78. ret_val = i;
  79. break;
  80. } else if (val >= max_th) {
  81. ret_val = th_len;
  82. break;
  83. }
  84. }
  85. return ret_val;
  86. }
  87. void phydm_seq_sorting(void *dm_void, u32 *value, u32 *rank_idx, u32 *idx_out,
  88. u8 seq_length)
  89. {
  90. u8 i = 0, j = 0;
  91. u32 tmp_a, tmp_b;
  92. u32 tmp_idx_a, tmp_idx_b;
  93. for (i = 0; i < seq_length; i++)
  94. rank_idx[i] = i;
  95. for (i = 0; i < (seq_length - 1); i++) {
  96. for (j = 0; j < (seq_length - 1 - i); j++) {
  97. tmp_a = value[j];
  98. tmp_b = value[j + 1];
  99. tmp_idx_a = rank_idx[j];
  100. tmp_idx_b = rank_idx[j + 1];
  101. if (tmp_a < tmp_b) {
  102. value[j] = tmp_b;
  103. value[j + 1] = tmp_a;
  104. rank_idx[j] = tmp_idx_b;
  105. rank_idx[j + 1] = tmp_idx_a;
  106. }
  107. }
  108. }
  109. for (i = 0; i < seq_length; i++)
  110. idx_out[rank_idx[i]] = i + 1;
  111. }
  112. u32 odm_convert_to_db(u32 value)
  113. {
  114. u8 i;
  115. u8 j;
  116. u32 dB;
  117. value = value & 0xFFFF;
  118. for (i = 0; i < 12; i++) {
  119. if (value <= db_invert_table[i][7])
  120. break;
  121. }
  122. if (i >= 12)
  123. return 96; /* @maximum 96 dB */
  124. for (j = 0; j < 8; j++) {
  125. if (value <= db_invert_table[i][j])
  126. break;
  127. }
  128. dB = (i << 3) + j + 1;
  129. return dB;
  130. }
  131. u32 phydm_db_2_linear(u32 value)
  132. {
  133. u8 i;
  134. u8 j;
  135. u32 linear;
  136. /* @1dB~96dB */
  137. value = value & 0xFF;
  138. i = (u8)((value - 1) >> 3);
  139. j = (u8)(value - 1) - (i << 3);
  140. linear = db_invert_table[i][j];
  141. return linear;
  142. }
  143. u16 phydm_show_fraction_num(u32 frac_val, u8 bit_num)
  144. {
  145. u8 i = 0;
  146. u16 val = 0;
  147. u16 base = 5000;
  148. for (i = bit_num; i > 0; i--) {
  149. if (frac_val & BIT(i - 1))
  150. val += (base >> (bit_num - i));
  151. }
  152. return val;
  153. }
  154. u32 phydm_gen_bitmask(u8 mask_num)
  155. {
  156. u8 i = 0;
  157. u32 bitmask = 0;
  158. if (mask_num > 32)
  159. return 1;
  160. for (i = 0; i < mask_num; i++)
  161. bitmask = (bitmask << 1) | BIT(0);
  162. return bitmask;
  163. }
  164. s32 phydm_cnvrt_2_sign(u32 val, u8 bit_num)
  165. {
  166. if (val & BIT(bit_num - 1)) /*Sign BIT*/
  167. val -= (1 << bit_num); /*@2's*/
  168. return val;
  169. }