matlab实现0,1简单序列2FSK调制
0,1简单序列2FSK调制
前言
本代码是最简单的0,1序列2FSK调制,未做同步与均衡,概念性问题网上能随便查到,不做过多叙述。
代码
参数配置
// 参数配置
fs=3840000; % 采样率配为3840kHz,为128000*30所得,且30720000/8
sample_num=30; %一个符号采样点数为30
Rb=fs/sample_num; %符号速率,128k bps
len_symbol=2500; %符号个数,可计算总样点数为N=len_sambol*sample_num,
bits_per_sybol=1; %2FSK一个符号对应1个比特
len=len_symbol*bits_per_sybol; %数据源比特个数
f1=Rb*4; %载波频率,单位Hz,载波频率配置需为Rb的整数倍,同时能被采样率整除,为128kHz
f2=Rb*2;
N=len_symbol*sample_num*100 %符号采样点数
dt=1/fs;
t0=0:dt:(N*2-1)*dt;
t=0:dt:(N/30-1)*dt;%符号时间轴
采样率依照3480kHz来设置,这是通信类学生符合实验中硬件类需求的标准。
生成数据源
%% 生成数据源
st1=randi([0,1],1,len);%随机生成0,1序列
st2=-st1+1;%生成基带信号反码,1变成0,0变成1
g11=(ones(1,100))'*st1; %产生方波信号
g1a=g11(:)';
g12=(ones(1,100))'*st2; %产生方波信号
g2a=g12(:)';
figure(1);
subplot(211);
plot(g1a);
title('基带信号st1');
axis([0,500,-1,2]);
subplot(212);
plot(g2a);
title('基带信号反码st2');
axis([0,500,-1,2]);
方波信号的设置是为了让结果显得更加清晰,如果觉得不需要也可去掉方波信号设置,直接使用产生的随机序列进行还原亦可,只是效果会不尽理想。
设置载波
%载波信号
s1=cos(2*pi*f1*t);
s2=cos(2*pi*f2*t);
figure(2)
subplot(211),plot(s1);
axis([0 500 -1.5 1.5]);
title('载波信号s1');
subplot(212),plot(s2);
axis([0 500 -1.5 1.5]);
title('载波信号s2');
调制
F1=g1a.*s1;%加入载波1
F2=g2a.*s2;%加入载波2
figure(3);
subplot(411);
plot(F1);
axis([0 500 -1.5 1.5]);
title('F1=s1*st1');
subplot(412);
plot(F2);
axis([0 500 -1.5 1.5]);
title('F2=s2*st2');
e_fsk=F1+F2;
subplot(413);
plot(e_fsk);
axis([0 500 -1.5 1.5]);
title('2FSK信号');%键控法产生的信号在相邻码元之间相位不一定连续
进行两个载波调制并加到一起。
加噪
%加噪
nosie=rand(1,len*100);%加入均值为0,方差为1的高斯白噪声
fsk=e_fsk+nosie;
subplot(414);
plot(fsk);
axis([0 500 -1.5 1.5]);
title('加噪声后信号')
fsk=conv(fsk,20);%过匹配滤波器
figure(4);
subplot(311);
plot(fsk);
axis([0 500 -30 30]);
title('过匹配滤波器后的加噪声后信号')
匹配滤波器fsk=conv(fsk,20),其实就是卷积,加噪后噪声是很小的,通俗点说,翻倍增长,原序列比例为10,噪声比例为0.1,二者比例为100,而假如翻二倍,原序列比例为100,噪声比例为0.01,二者比例为10000,因而大大降低了噪声对原序列还原时会造成的影响。
相干解调
%相干解调
st1=fsk.*s1; %与载波1相乘
st2=fsk.*s2;%与载波2相乘
subplot(312);
plot(t,st1);
axis([0 0.001 -30 30]);
title('加噪后的信号与s1相乘后波形');
subplot(313);
plot(t,st2);
axis([0 0.001 -30 30]);
title('加噪后的信号与s2相乘后波形');
过低通滤波器
%通过低通滤波器
st1=filter(erlow,st1);
st2=filter(erlow2,st2);
figure(5);
subplot(211);
plot(t,st1);
axis([0 0.001 -30 30]);
title('过低通滤波器后st1波形');
subplot(212);
plot(t,st2);
axis([0 0.001 -30 30]);
title('过低通滤波器后st2波形');
相干解调后两个波形要通过低通滤波器,这儿用到两个,但其实用的是一个参数,本人软件方面有点问题,故而设了两个低通滤波器。
判决
%判决
for m=0:200
if st1(1,m*30+15)>st2(1,m*30+15)
for j=m*30+1:(m+1)*30
at(1,j)=1;
end
else
for j=m*30+1:(m+1)*30
at(1,j)=0;
end
end
end
此处注意的是判决长度需与符号采样点数保持一致,代码中参数为一个符号采样点数为30。
%subplot(313);
figure(6)
subplot(311);
plot(g1a);
title('基带信号st1');
axis([0,5000,-1,2]);
subplot(312);
plot(g2a);
title('基带信号反码st2');
axis([0,5000,-1,2]);
subplot(313);
plot(at);
axis([0,5000,-1,2]);
title('抽样判决后波形')
将基带信号与最终还原出来的信号放在一起做对比,看还原的是否成功。
总代码
fskk.m(主代码)
fs=3840000; % 采样率配为3840kHz,为128000*30所得,且30720000/8
sample_num=30; %一个符号采样点数
Rb=fs/sample_num; %符号速率,128k bps
len_symbol=2500; %符号个数,可计算总样点数为N=len_sambol*sample_num,
bits_per_sybol=1; %2FSK一个符号对应1个比特
len=len_symbol*bits_per_sybol; %数据源比特个数
f1=Rb*4; %载波频率,单位Hz,载波频率配置需为Rb的整数倍,同时能被采样率整除,为128kHz
f2=Rb*2;
N=len_symbol*sample_num*100 %符号采样点数
dt=1/fs;
t0=0:dt:(N*2-1)*dt;
t=0:dt:(N/30-1)*dt;%符号时间轴
%% 生成数据源
st1=randi([0,1],1,len);
st2=-st1+1;%生成基带信号反码,1变成0,0变成1
g11=(ones(1,100))'*st1; %产生方波信号
g1a=g11(:)';
g12=(ones(1,100))'*st2; %产生方波信号
g2a=g12(:)';
figure(1);
subplot(211);
plot(g1a);
title('基带信号st1');
axis([0,500,-1,2]);
subplot(212);
plot(g2a);
title('基带信号反码st2');
axis([0,500,-1,2]);
%载波信号
s1=cos(2*pi*f1*t);
s2=cos(2*pi*f2*t);
figure(2)
subplot(211),plot(s1);
axis([0 500 -1.5 1.5]);
title('载波信号s1');
subplot(212),plot(s2);
axis([0 500 -1.5 1.5]);
title('载波信号s2');
%调制
F1=g1a.*s1;%加入载波1
F2=g2a.*s2;%加入载波2
figure(3);
subplot(411);
plot(F1);
axis([0 500 -1.5 1.5]);
title('F1=s1*st1');
subplot(412);
plot(F2);
axis([0 500 -1.5 1.5]);
title('F2=s2*st2');
e_fsk=F1+F2;
subplot(413);
plot(e_fsk);
axis([0 500 -1.5 1.5]);
title('2FSK信号');%键控法产生的信号在相邻码元之间相位不一定连续
%加噪
nosie=rand(1,len*100);%加入均值为0,方差为1的高斯白噪声
fsk=e_fsk+nosie;
subplot(414);
plot(fsk);
axis([0 500 -1.5 1.5]);
title('加噪声后信号')
fsk=conv(fsk,20);%过匹配滤波器
figure(4);
subplot(311);
plot(fsk);
axis([0 500 -30 30]);
title('过匹配滤波器后的加噪声后信号')
%相干解调
st1=fsk.*s1; %与载波1相乘
st2=fsk.*s2;%与载波2相乘
subplot(312);
plot(t,st1);
axis([0 0.001 -30 30]);
title('加噪后的信号与s1相乘后波形');
subplot(313);
plot(t,st2);
axis([0 0.001 -30 30]);
title('加噪后的信号与s2相乘后波形');
%通过低通滤波器
st1=filter(erlow,st1);
st2=filter(erlow2,st2);
figure(5);
subplot(211);
plot(t,st1);
axis([0 0.001 -30 30]);
title('过低通滤波器后st1波形');
subplot(212);
plot(t,st2);
axis([0 0.001 -30 30]);
title('过低通滤波器后st2波形');
%判决
for m=0:200
if st1(1,m*30+15)>st2(1,m*30+15)
for j=m*30+1:(m+1)*30
at(1,j)=1;
end
else
for j=m*30+1:(m+1)*30
at(1,j)=0;
end
end
end
%subplot(313);
figure(6)
subplot(311);
plot(g1a);
title('基带信号st1');
axis([0,5000,-1,2]);
subplot(312);
plot(g2a);
title('基带信号反码st2');
axis([0,5000,-1,2]);
subplot(313);
plot(at);
axis([0,5000,-1,2]);
title('抽样判决后波形')
erlow.m(低通滤波器1)
function Hd = erlow
%ERLOW Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.2 and the Signal Processing Toolbox 7.4.
% Generated on: 29-Nov-2020 22:17:42
% FIR Window Lowpass filter designed using the FIR1 function.
% All frequency values are in Hz.
Fs = 48000; % Sampling Frequency
N = 11; % Order
Fc = 12800; % Cutoff Frequency
flag = 'scale'; % Sampling Flag
Beta = 0.5; % Window Parameter
% Create the window vector for the design algorithm.
win = kaiser(N+1, Beta);
% Calculate the coefficients using the FIR1 function.
b = fir1(N, Fc/(Fs/2), 'low', win, flag);
Hd = dfilt.dffir(b);
% [EOF]
erlow.m(低通滤波器2)
function Hd = erlow2
%ERLOW Returns a discrete-time filter object.
% MATLAB Code
% Generated by MATLAB(R) 9.2 and the Signal Processing Toolbox 7.4.
% Generated on: 29-Nov-2020 22:17:42
% FIR Window Lowpass filter designed using the FIR1 function.
% All frequency values are in Hz.
Fs = 48000; % Sampling Frequency
N = 11; % Order
Fc = 12800; % Cutoff Frequency
flag = 'scale'; % Sampling Flag
Beta = 0.5; % Window Parameter
% Create the window vector for the design algorithm.
win = kaiser(N+1, Beta);
% Calculate the coefficients using the FIR1 function.
b = fir1(N, Fc/(Fs/2), 'low', win, flag);
Hd = dfilt.dffir(b);
% [EOF]
祝您生活顺心,事事顺利。