-
Notifications
You must be signed in to change notification settings - Fork 1
/
Communication_principle.m
112 lines (65 loc) · 2.19 KB
/
Communication_principle.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
clc; clear;
% 读取MP3文件
fs = 44100;
% samples = [1,500*fs];
% [inputAudio, fs] = audioread('周杰伦-反方向的钟.mp3',samples);
[inputAudio, fs] = audioread('周杰伦-反方向的钟.mp3');
% 将左右声道平均合并为单声道
monoAudio = mean(inputAudio, 2);
% 绘制原始信号
subplot(3, 1, 1);
plot(monoAudio);
title('原始信号');
% 8kHz抽样
targetFs = 8000;
eightksampledAudio = resample(monoAudio, targetFs, fs);
% 绘制8kHz抽样后的信号
subplot(3, 1, 2);
plot(eightksampledAudio);
title('8kHz抽样后的信号');
% 量化
% 设定均匀量化的位数
bits = 16;
quantizedSignal = uencode(eightksampledAudio, bits);
% 计算均匀量化步长
quantizationStep = (max(eightksampledAudio) - min(eightksampledAudio)) / (2^bits);
% % 进行均匀量化
% quantizedSignal = round(eightksampledAudio / quantizationStep) * quantizationStep;
% 绘制量化后的信号
subplot(3, 1, 3);
plot(quantizedSignal);
title('量化后的信号');
% 自然二进制编码
binaryEncodedChannel = de2bi(quantizedSignal, bits, 'left-msb');
% 将矩阵转换为一维数组
binaryEncodedChannel_1D = reshape(binaryEncodedChannel.', 1, []);
data = binaryEncodedChannel_1D;
%2psk调制
modulated = pskmod(data, 2);
% 添加高斯白噪声
SNR = 10; % 信噪比(dB)
noisySignal = awgn(modulated, SNR);
% 2PSK解调
demodulated = pskdemod(noisySignal, 2);
figure;
subplot(3, 1, 1);
stem(data(1:100), 'b', 'LineWidth', 2);
title('原始数据');
subplot(3, 1, 2);
stem(modulated(1:100), 'r', 'LineWidth', 2);
title('2PSK调制数据');
subplot(3, 1, 3);
stem(demodulated(1:100), 'g', 'LineWidth', 2);
title('2PSK解调数据');
scale = size(binaryEncodedChannel);
% 一维变回多维
demodulated_array = reshape(demodulated', scale(2),scale(1))';
%逆编码
decodedSignal = bi2de(reshape(binaryEncodedChannel_1D, bits, []).', 'left-msb');
% 逆量化
decodedata = udecode(decodedSignal, bits);
% 恢复原来的MP3
resampledAudio = resample(decodedata, fs, targetFs);
% 写入恢复的MP3文件
audiowrite('output.mp3', resampledAudio, fs);
sound(resampledAudio,fs)