%------------------------------------------------------------------------
% 通信系统实验
% 名称: QAM16_receive
% 作者: Dorus Tan
% 日期: 2015-06-26
%-------------------------------------------------------------------------
clc;
clear all;
close all;
%% ---------------------------------------------------------------
Fs = 100000; %22050;
NFs = 40; %36
f_c = 10000;
snr = 15;
T_bit = 4e-4;
T_trans = 20;
t = 1/Fs:1/Fs:T_bit;
%y_get = wavrecord(T_trans*Fs,Fs,1);
load y_outq;y_get = y_outq;%+1/(10^(snr/20))*randn(1,length(y_outq));
%% --------------------------------------------------------------
h1 = fir1(64,[9700*2/Fs,10300*2/Fs]);
h2 = fir1(32,[1/Fs,4000/Fs]);
y_in = filter(h1,1,y_get);
wav_cos = cos(2*pi*f_c*t);
wav_sin = -sin(2*pi*f_c*t);
%% ------同步检测-------------------------------------------------------------
qam_barker = [3-3i,3-3i,3-3i,-3+3i,-3+3i,-3+3i,3-3i,-3+3i,-3+3i,3-3i,-3+3i];
N_B = length(qam_barker);
K=0;
for i = 1:length(y_get)-N_B*NFs+1
if y_in(i)>0.05
tempI = zeros(1,N_B);
tempQ = tempI;
yinI = y_in(i:i+11*NFs-1).*repmat(wav_cos,1,11);
yinQ = y_in(i:i+11*NFs-1).*repmat(wav_sin,1,11);
ysyI = filter(h2,1,yinI);
ysyQ = filter(h2,1,yinQ);
gate3 = 0.8*2*max(abs([ysyI,ysyQ]))/3;
tempI(ysyI(floor(NFs/2+(0:NFs:N_B*NFs-1)))>gate3) = 3;
tempI(ysyI(floor(NFs/2+(0:NFs:N_B*NFs-1)))<-gate3) = -3;
tempQ(ysyQ(floor(NFs/2+(0:NFs:N_B*NFs-1)))>gate3) = 3;
tempQ(ysyQ(floor(NFs/2+(0:NFs:N_B*NFs-1)))<-gate3) = -3;
if (qam_barker == tempI+1j*tempQ)
K = i;
break;
end
end
end
figure;
subplot(311);plot(y_in(1:NFs*13));
subplot(312);plot(yinI);
subplot(313);plot(ysyI);
figure;
subplot(311);plot(y_in(1:NFs*13));
subplot(312);plot(yinQ);
subplot(313);plot(ysyQ);
%% ---------解调--------------------------------------------
l_bit = fix((length(y_in)-K)/NFs);
y_sig = y_in(K:K+l_bit*NFs-1);
y_tI = y_sig.*repmat(wav_cos,1,l_bit);
y_tQ = y_sig.*repmat(wav_sin,1,l_bit);
y_sigI = filter(h2,1,y_tI);
y_sigQ = filter(h2,1,y_tQ);
figure;
subplot(311);plot(y_sig(1:NFs*30));
subplot(312);plot(y_tI(1:NFs*30));
subplot(313);plot(y_sigI(1:NFs*30));
figure;
subplot(311);plot(y_sig(1:NFs*30));
subplot(312);plot(y_tQ(1:NFs*30));
subplot(313);plot(y_sigQ(1:NFs*30));
yjI(y_sigI(NFs/2+(0:NFs:NFs*(l_bit-1)))>0) = 1;
yjI(y_sigI(NFs/2+(0:NFs:NFs*(l_bit-1)))<=0) = -1;
yjI(y_sigI(NFs/2+(0:NFs:NFs*(l_bit-1)))>gate3) = 3;
yjI(y_sigI(NFs/2+(0:NFs:NFs*(l_bit-1)))<-gate3) = -3;
yjQ(y_sigQ(NFs/2+(0:NFs:NFs*(l_bit-1)))>0) = 1;
yjQ(y_sigQ(NFs/2+(0:NFs:NFs*(l_bit-1)))<=0) = -1;
yjQ(y_sigQ(NFs/2+(0:NFs:NFs*(l_bit-1)))>gate3) = 3;
yjQ(y_sigQ(NFs/2+(0:NFs:NFs*(l_bit-1)))<-gate3) = -3;
yj = yjI+1j*yjQ;
y_dem = demodulate(modem.qamdemod(16),yj);
%% -----------纠错---------------------------------------------
y_bi = de2bi(y_dem(N_B+1:end)')';
y_bi1 = reshape(y_bi,1,4*length(y_bi));
y_dec = decode(y_bi1,7,4,'hamming/binary');
N_z = 8-mod(length(y_dec),8);
y_de = reshape([y_dec,zeros(1,N_z)],8,(length(y_dec)+N_z)/8)';
w = 2.^[7,6,5,4,3,2,1,0]';
y_ok = (y_de*w-2^7)/2^7;
sound(y_ok,11025);