音频无损预测编码

任务:

1) 读入给定的文本格式音频序列;
2) 对1)输入的音频序列进行无损预测编码,将编码结果以文本文件输出;
3) 读入 2)得到的结果, 对其进行解码,将解码结果以文本文件输出。

 

程序:

 1 //main.cpp
 2 #include "lossless_predictive_decode.h"
 3 #include "lossless_predictive_encode.h"
 4 #include <iostream>
 5 
 6 using std::cout;  using std::endl;  using std::cin;
 7 
 8 int main()
 9 {
10     cout << "操作命令说明:" << endl;
11     cout << "无损预测编码: 1" << endl;
12     cout << "无损预测解码: 2" << endl;
13     cout << "       退出 : 3" << endl;
14 
15     char option(0);
16 
17     do{
18         cout << endl << ">"; //用户选择操作命令键入
19         cin >> option;
20 
21         switch (option)
22         {
23         case '1':  //无损预测编码,输入音频序列,编码并输出误差序列
24             lossless_predictive_encode();
25             break;
26         case '2':  //无损预测解码,输入误差序列,解码并输出重构序列
27             lossless_predictive_decode();
28             break;
29         default:
30             cout << "无效指令";
31         }
32     } 
33     while (option != '3');
34 
35     return 0;
36 }

 

 1 //lossless_predictive_encode.cpp
 2 #include "lossless_predictive_encode.h"
 3 #include <iostream>
 4 #include <vector>
 5 #include <fstream>
 6 #include <string>
 7 
 8 using std::cout;  using std::cin;  using std::ifstream;  using std::ofstream;
 9 using std::endl;  using std::string;  using std::vector;
10 
11 void lossless_predictive_encode()
12 {
13     cout << "请输入文件名:";
14     string file_name;
15     cin >> file_name;
16 
17     //读入文件中的信号值
18     ifstream in(file_name + ".txt");
19     vector<int> f;
20     if (in){
21         int buf;
22         while (in >> buf)
23             f.push_back(buf);
24         in.close();
25     }
26     else{
27         cout << "文件未能打开" << endl;
28         return;
29     }
30 
31     //计算预测值
32     vector<int> pre_f;
33     pre_f.push_back(f[0]);
34     pre_f.push_back(f[0]);
35 
36     int tmp;
37     auto size = f.size();
38     for (decltype(size) i = 2; i != size; ++i)
39     {
40         tmp = (int)(0.5*f[i - 1] + 0.5*f[i - 2]);    //预测器
41         pre_f.push_back(tmp);
42     }
43 
44     //计算误差值
45     vector<int> e;
46     for (decltype(size) i = 0; i != size; ++i)
47     {
48         tmp = f[i] - pre_f[i];
49         e.push_back(tmp);
50     }
51 
52     //写入文件
53     ofstream out(file_name + "_predictive_encode.txt");
54     if (out){
55         out << f[0] << endl;
56         for (decltype(size) i = 1; i != size; ++i)    //e的元素数等于f,故用同一size
57             out << e[i] << endl;
58         out.close();
59     }
60     else
61         cout << "写入失败" << endl;
62 }

第一个头文件为函数声明

 1 //lossless_predictive_decode.cpp
 2 #include "lossless_predictive_decode.h"
 3 #include <iostream>
 4 #include <vector>
 5 #include <fstream>
 6 #include <string>
 7 
 8 using std::cout;  using std::cin;  using std::ifstream;  using std::ofstream;
 9 using std::endl;  using std::string;  using std::vector;
10 
11 void lossless_predictive_decode()
12 {
13     cout << "请输入文件名:";
14     string file_name;
15     cin >> file_name;
16 
17     //读入文件中的误差值
18     ifstream in(file_name + "_predictive_encode.txt");
19     vector<int> e;
20     if (in){
21         int buf;
22         while (in >> buf)
23             e.push_back(buf);
24         in.close();
25     }
26     else{
27         cout << "文件未能打开";
28         return;
29     }
30 
31     //计算信号值
32     vector<int> f;
33     f.push_back(e[0]);
34     f.push_back(e[0] + e[1]);
35 
36     auto size = e.size();
37     for (decltype(size) i = 2; i != size; ++i)
38         f.push_back(e[i] + (int)(0.5*f[i - 1] + 0.5*f[i - 2])); //预测器
39     
40     //写入信号值
41     ofstream out(file_name + "_predictive_decode.txt");
42     if (out){
43         for (auto a : f)
44             out << a << endl;
45         out.close();
46     }
47     else{
48         cout << "写入失败";
49     }
50 }

 

示例:

 位置分别对应原信号值,编码后及解码后

posted @ 2014-12-03 14:14  胸口碎大锤  阅读(226)  评论(0)    收藏  举报