Advertisement
Guest User

comparacion imagenes con hamming

a guest
Jun 3rd, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. /*
  2. input : data bits
  3. outpot : hamming code
  4. Description : b= number of bits in our case it is 7
  5. r= number of parity bits i.e. p1 p2 p3 p4
  6. hb= number of bits in hamming code in our case (b+r) = 11
  7. d[]= Array containing data bits i.e. input
  8. h[]= Array containing hamming code i.e. output
  9. COUNT= is static counter for counting number of 1's
  10.  
  11. */
  12. import java.io.*;
  13.  
  14. class Hamming_Gen
  15. {
  16. static int COUNT,K;
  17. public static void main(String[] args) throws IOException
  18. {
  19.  
  20. int r,b,hb;
  21. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  22.  
  23. System.out.println("Enter number of bits : ");
  24. b=Integer.parseInt(br.readLine());
  25. int d[]= new int[b+1];
  26. for(r=1; ;r++)
  27. {
  28. if((int) Math.pow(2,r) >= (b+r+1))
  29. break;
  30. }
  31.  
  32. hb=(b+r+1);
  33. System.out.println("Number of parity bits : "+r);
  34. System.out.println("Number of hamming bits : "+hb);
  35. int h[]= new int[hb];
  36. int p[]=new int[r];
  37. int location[][] = new int [r][hb];
  38.  
  39. System.out.println("Enter bit number : ");
  40. for(int i=1;i<=b;i++)
  41. d[i]=Integer.parseInt(br.readLine());
  42.  
  43.  
  44. System.out.println("Data bits are : ");
  45. for(int i=1;i<=b;i++)
  46. System.out.print(d[i]);
  47. System.out.println();
  48.  
  49. hamming_array_generation(d,h,r,hb);
  50.  
  51. show_hamming_code(h,hb);
  52.  
  53. System.out.println("Locations");
  54. for(int i=0;i<r;i++)
  55. get_Locations(i,h,location,p);
  56.  
  57. for (int i=0; i<location.length; i++) {
  58. for (int j=0; j<location[i].length; j++) {
  59. System.out.print(" "+location[i][j]);
  60. }
  61. System.out.println();
  62. }
  63.  
  64.  
  65. Generate_parity(location, p, h);
  66. for(int i=0;i<r;i++)
  67. System.out.print(" "+p[i]);
  68. show_hamming_code(h,hb);
  69.  
  70. System.out.println("Enter hamming code of "+(hb-1)+" bits : ");
  71. for(int i=1;i<hb;i++)
  72. h[i]=Integer.parseInt(br.readLine());
  73.  
  74. error_calculate(location,h,p,r);
  75. show_hamming_code(h,hb);
  76.  
  77. System.out.println("THANK YOU!!......ufff");
  78. }
  79.  
  80.  
  81. static void hamming_array_generation(int d[],int h[],int r,int hb)
  82. {
  83. int i=1,j=0,k=1;
  84. while(i<hb || j<r)
  85. {
  86. if((int)Math.pow(2,j) == i)
  87. {
  88. h[i]=0;
  89. j++;
  90. }
  91. else
  92. {
  93. h[i]=d[k];
  94. k++;
  95. }
  96. i++;
  97. }
  98. }
  99.  
  100.  
  101.  
  102. static void get_Locations(int i,int h[], int location[][], int p[])
  103. {
  104. K=0;
  105. int a,add,num,b,count=0;
  106. num=(int)Math.pow(2,i);
  107. add=num+num;
  108. a=num;
  109. while(a<=11)
  110. {
  111. calculate(a,h,i,location);
  112. if(num>1)
  113. {
  114. b=a;
  115. for(int k=1;k<num;k++)
  116. {
  117. b=b+1;
  118. if(b<=11)
  119. {
  120. calculate(b,h,i,location);
  121. }
  122. else
  123. break;
  124. }
  125. }
  126. a=a+add;
  127. }
  128.  
  129. //COUNT=0;
  130.  
  131. }
  132.  
  133.  
  134. static void calculate(int a, int h[], int i, int location[][])
  135. {
  136. if((int)Math.pow(2,i) == a)
  137. return;
  138. location[i][K] = h[a];
  139. K++;
  140.  
  141. }
  142.  
  143. static void Generate_parity(int location[][], int p[], int h[])
  144. {
  145. int count=0;
  146.  
  147. for (int i=0; i<location.length; i++) {
  148. for (int j=0; j<location[i].length; j++) {
  149. if(location[i][j] == 1)
  150. count++;
  151. }
  152. int k=(int)Math.pow(2,i);
  153. if(count%2 == 0)
  154. {
  155. p[i]=0;
  156. h[k]=0;
  157. }
  158. else
  159. {
  160. p[i]=1;
  161. h[k]=1;
  162. }
  163. count=0;
  164. }
  165. }
  166.  
  167. static void error_calculate(int location[][], int h[], int p[], int r)
  168. {
  169. int k=0,d=0;
  170. for(int i=0;i<r;i++)
  171. {
  172. k=(int)Math.pow(2,i);
  173. p[i]=h[k];
  174. }
  175. int count=0;
  176. for (int i=0; i<location.length; i++) {
  177. for (int j=0; j<location[i].length; j++) {
  178. if(location[i][j] == 1)
  179. count++;
  180. }
  181. k=(int)Math.pow(2,i);
  182. if((count%2 == 0 && h[k]==0)||(count%2 != 0 && h[k]==1))
  183. {
  184. p[i]=0;
  185. }
  186. else
  187. {
  188. p[i]=1;
  189. }
  190. count=0;
  191. }
  192. for(int i=3;i>=0;i--)
  193. d=d+(p[i]*((int)Math.pow(2,i)));
  194.  
  195. if(d==0)
  196. System.out.println("No Error");
  197. else
  198. {
  199. System.out.println("The "+d+" bit is wrongly received");
  200. if(h[d]==0)
  201. h[d]=1;
  202. else
  203. h[d]=0;
  204. System.out.println("");
  205. System.out.println("The correct hamming code is ");
  206.  
  207. }
  208. }
  209.  
  210. static void show_hamming_code(int h[],int hb)
  211. {
  212. System.out.println("HAMMING CODE ARRAY : ");
  213. for(int i=1; i<hb; i++)
  214. {
  215. System.out.print(h[i]);
  216. }
  217. System.out.println();
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement