matlab 信道编解码

clc; clear;
fs=2000;
%采样频率
dt=1/fs;
f1=20;
f2=120;
%两个信号的频率
a=round(rand(1, 10)); %随机信号
%%%%信源编码
len=length(a);
code_date=zeros(1,len);
ini_date=1;
for n=1:len
 if a(n)==1
     code_date(n)=ini_date;
     ini_date=-1*ini_date;
 end
end 

g1=code_date;
g2=~ code_date;
%信号反转,和g1反向
g11= (ones(1, 2000))'*g1; %抽样
g1a=g11(:)';
g21= (ones(1,2000))'*g2;
g2a=g21(:)';
t=0:dt:10-dt;
t1=length(t);
fsk1=g1a.*cos(2*pi*f1.*t);
fsk2=g2a.*cos (2*pi*f2.*t) ;
fsk=fsk1+fsk2;
%产生的信号

figure(1);
no=0.05*randn(1,t1); %噪声
sn=fsk+no;
subplot(311);plot(t, no) ;
title('噪声波形');ylabel('噪声幅值'); %噪声波形
subplot (312);plot(t, fsk) ;
title('信号');ylabel('信号幅度');
subplot (313) ;plot(t, sn);
title('叠加有噪声的信号');ylabel('幅度A');xlabel('时间t' );
figure(2); 
b1=fir1(101, [10/800 20/800]);
b2=fir1 (101, [90/800 110/800]); %设置带通参数
H1=filter(b1, 1, sn) ;H2=filter(b2, 1, sn); %经过 带通滤波器后的信号
subplot (211) ;plot (t, H1) ;
title('经过带通滤波器后f1的波形');ylabel('幅度')
subplot (212) ;plot (t, H2) ;
title('经过带通滤波器后f2的波形’);ylabel(幅度');xlabel('t')
sw1=H1.*H1;sw2=H2.*H2; %经过相乘器
figure(3)
subplot (211) ;plot(t, sw1);
title('经过相乘器h1后的波形');ylabel('幅度')
subplot (212) ;plot (t, sw2);
title('经过相乘器h2后的波形');ylabel('?幅度');xlabel('t')
bn=fir1(101, [2/800 10/800]) ; %经过低通滤波器
st1=filter (bn, 1, sw1) ;st2=filter (bn, 1, sw2) ;
figure(4)
subplot (211);plot(t, st1);
title('经过低通滤波器swl 后的波形');ylabel('幅度')
subplot (212) ;plot (t, st2) ;
title('经过低通滤波器sw2 后的波形');ylabel( '幅度');xlabel('t')
%判决
for i=1:length(t)
if(st1(i)>=st2(i))
st(i)=1;
else
    st(i)=0;
end
end
%%%%信源解码
decode_date=zeros(1,len);
for n=1:t1
 if st(n)==1||st(n)==-1
     decode_date(n)=1;
 else
     decode_date(n)=0
 end
end    
figure(5)
subplot (211) ;plot (t, decode_date);title('经过抽样判决器后的波形');ylabel('幅度')
subplot (212) ;plot (t, sn);title('原始的波形');ylabel('幅度');xlabel('t')

如何添加信道的编、解码

代码实现和解释如下,望采纳

可以使用 fft() 函数来计算信号的快速傅里叶变换(FFT)。下列代码计算了信号的 FFT 并绘制了频谱图:

% 读入信号
signal = read_signal();

% 计算 FFT
fft_signal = fft(signal);

% 计算频率轴
n = length(signal);
f = (0:n-1)/n*fs;

% 绘制频谱图
plot(f, abs(fft_signal));
xlabel('频率 (Hz)');
ylabel('幅值');

read_signal() 是读入信号的函数,它返回一个信号的向量。fs 是信号的采样频率,fft() 函数计算出的 FFT 结果是一个复数向量,我们使用了 abs() 函数来计算其绝对值,然后使用了 plot() 函数来绘制频谱图。