| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 493 人关注过本帖
标题:信源、信道编解码,其中信道编码要能产生1-2位随机错误,能纠1位错,直接读 ...
只看楼主 加入收藏
你是我的菜
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-6-6
收藏
 问题点数:0 回复次数:0 
信源、信道编解码,其中信道编码要能产生1-2位随机错误,能纠1位错,直接读取图片来完成(海明码和行程编码的程序如下,就是不知如何链接起来)
行程编码:
package com.abc;
import
import
import

public class RLE {
    public void compress(String source, String dis) {//压缩 source源文件,dis目标文件
        try {
            FileInputStream in = new FileInputStream(source);
            FileOutputStream out = new FileOutputStream(dis);
            int next = 0;
            int count = in.read();//取第一个字节
            while ((next = in.read()) >= 0) {//当文件没有结束时执行
                int counter = 1;//计重复的次数
                if (count == next) {//如果有相同的
                    counter++;
                    while (next == (count = in.read())) {//计算重复的次数
                        counter++;
                    }
                    while (counter >= 63) {//重复次数大于63的情况
                        out.write(255);//63个(192+63)
                        out.write(next);
                        System.out.println("大于63的情况" + (0xc0 + 63) + " " + count);
                        counter -= 63;//减去处理的63个字节
                    }
                    if (counter > 1) {//处理剩下的字节
                        out.write(0xc0 + counter);
                        out.write(next);
                        System.out.println("重复剩余的" + (0xc0 + counter) + " " + counter);
                    }
                } else {
                    if (count <= 0xc0) {//不重复小于192的情况
                        out.write(count);
                        count = next;
                    } else {//不重复大于192的情况
                        out.write(0xc1);
                        out.write(count);
                        count = next;
                        System.out.println("0xc1的" + (0xc1) + count);
                    }
                }
            }
            //处理最后一个字节
            if (count <= 0xc0) {
                System.out.println(count);
                out.write(count);
            } else {
                out.write(0xc1);
                out.write(count);
                System.out.println("0xc1的" + (0xc1) + count);
            }
            in.close();//关闭输入流
            out.close();//关闭输出流
        } catch (IOException e) {
        }
    }

    public void decompress(String source, String dis) {//解压缩  source源文件,dis目标文件
        try {
            FileInputStream in = new FileInputStream(source);
            FileOutputStream out = new FileOutputStream(dis);
            int count = 0;
            while ((count = in.read()) >= 0) {
                if (count == 0xc1) {
                    out.write(in.read());
                } else if (count <= 0xc0) {
                    out.write(count);
                } else if (count > 0xc1) {
                    int next = in.read();
                    for (int i = 0; i < (count - 0xc0); i++) {
                        out.write(next);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //new RLE().compress("D:\\123.bmp", "D:\\456.cjj");//压缩到456.cjj ,文件后缀名随便取
        new RLE().decompress("D:\\456.cjj", "D:\\789.bmp");//从;'[456.cjj 解压到789.BMP
    }
}


海明码:
package
import java.util.Scanner;
public class Ham {
 Scanner enter=new Scanner(System.in);
 String s_data;           //输入的字符串类型的数据
 int dlength;             //输入数据的长度
 int[] data;              //将输入的数据转换成整型数据
 int hlength;            //海明码长度
 int[] ham;              //海明码
 int plength;            //校验位长度
 int[] p;                //校验位
 int[] c;                //验证出错位数的数组

  void input(){
  System.out.println("输入数据:");
  String s_data=enter.nextLine();
  dlength=s_data.length();
  data=new int[dlength];
  for(int i=0;i<dlength;i++){
   data[i]=Integer.parseInt(s_data.substring(i, i+1));
//System.out.print(data[i]);
  }
  generateHam();
 }

 void generateHam(){
  int i=1;
  int label=1; //指向海明码的校验位
  int k=data.length-1;    //指向数据位
  while(Math.pow(2, i)<dlength+i+1){
   i++;
  }
  plength=i;  //校验位位数
p=new int[plength];
c=new int[plength];
//System.out.println("plength "+plength);
  hlength=dlength+plength;
  ham=new int[hlength];

  /*
   * 向海明码中填写数据位
   */
  for(int j=0;j<hlength;j++){
   if(j+1==label){
    ham[j]=0;
    label*=2;
   }else{
    ham[j]=data[k--];
   }
  }
/*for(int m=hlength-1;m>=0;m--){
 System.out.print(ham[m]);
}*/

  /*
   * 求海明码中的校验位
   */
  int r; //指向校验位位数
  int sum=0;
  for(i=hlength;i>0;i--){
   for(r=plength-1;r>=0;r--){
    if(i!=Math.pow(2, r)&&sum+Math.pow(2, r)<=i){
     ham[(int)Math.pow(2, r)-1]=(ham[(int)Math.pow(2, r)-1]+ham[i-1])%2;
//p[r]=(p[r]+ham[i-1])%2;
     sum=sum+(int)Math.pow(2, r);
    }else{
     if(i==Math.pow(2, r)){
      break;
     }
    }
   }
   sum=0;
  }
  /*for(int m=hlength-1;m>=0;m--){
   System.out.print(ham[m]);
  }*/
  System.out.print("生成海明码为:");
  for(int m=0;m<hlength;m++){
   System.out.print(ham[m]);
  }
  System.out.println();
  /*for(int m=plength-1;m>=0;m--){
   System.out.print(p[m]);
  }*/
 }

 void group(){
  int i;
  int r; //指向校验位位数
  int sum=0;
  for(i=hlength;i>0;i--){
   for(r=plength-1;r>=0;r--){
    if(i!=Math.pow(2, r)&&sum+Math.pow(2, r)<=i){
     c[r]=(c[r]+ham[i-1])%2;
//p[r]=(p[r]+ham[i-1])%2;
     sum=sum+(int)Math.pow(2, r);
    }else{
     if(i==Math.pow(2, r)){
      c[r]=(c[r]+ham[i-1])%2;
      break;
     }
    }
   }
   sum=0;
 }
  /*for(i=c.length-1;i>=0;i--){
   System.out.print(c[i]);
  }*/
 }

    void check(){
      int sum=0;
      System.out.print("提示处错位:");
      for(int i=c.length-1;i>=0;i--){  //输出组信息
       System.out.print(c[i]);
      }
      System.out.println();
      for(int i=0;i<c.length;i++){
       sum=sum+(int)(c[i]*Math.pow(2, i)); //求出第几位出错
      }
  //System.out.println(sum);
      if(sum==0){
       System.out.println("输入正确!");
      }else{
       System.out.println("输入错误,第"+sum+"位出错");
      }
     }

  /*
  * 输入海明码,若输入正确提示正确信息,输入错误,提示帝即位出错。
  */
  void inputError(){
      System.out.print("请输入海明码:");
      s_data=enter.nextLine();

      for(int i=0;i<ham.length;i++){
       ham[i]=Integer.parseInt(s_data.substring(i,i+1));
      }

      System.out.print("输入的海明码为:");
      for(int i=0;i<ham.length;i++){
       System.out.print(ham[i]);
      }
      System.out.println();

      group();
      check();
     }

 public static void main(String[] args){
  Ham ham=new Ham();
  ham.input();
  ham.inputError();
 }
}






[ 本帖最后由 你是我的菜 于 2010-6-6 16:38 编辑 ]
搜索更多相关主题的帖子: 源文件 
2010-06-06 16:36
快速回复:信源、信道编解码,其中信道编码要能产生1-2位随机错误,能纠1位错,直 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015210 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved