fs=2000;%抽样频率dt=1/fs;f1=20;%定义两列载波的频率f2=100;a=round(rand(1,10));%产生二进制随机序列g1=a;g2=~a;g11=(ones(1,2000))'*g1;%产生方波信号g1a=g11(:)';g21=(ones(1,2000))'*g2;g2a=g21(:)';t=0:dt:10-dt;t1=length(t);tuf1=cos(2*pi*f1.*t)tuf2=cos(2*pi*f2.*t)subplot(311)n=0:9;x=square(1,50);stem([0:9],a*x);grid;xlabel('二进制随机序列')ylabel('幅度')subplot(312);plot(t,tuf1);title('频率为f1的余弦波')ylabel('幅度')subplot(313);plot(t,tuf2);title('频率为f2的余弦波')ylabel('幅度')figure(2)fsk1=g1a.*tuf1;fsk2=g2a.*tuf2;fsk=fsk1+fsk2;no=0.01*randn(1,t1);%噪声sn=fsk+no;subplot(211);plot(t,fsk);title('2fsk波形')ylabel('幅度')subplot(212);plot(t,sn);title('加入高斯噪声后的2fsk波形')ylabel('幅度的大小')xlabel('t')figure(3)%FSK解调b1=fir1(101,[10/80020/800]);b2=fir1(101,[90/800110/800]);%设置带宽参数H1=filter(b1,1,sn);%b1为分子,1为分母,sn为滤波器输入序列H2=filter(b2,1,sn);%噪声信号同时通过两个滤波器subplot(211);plot(t,H1);title('经过带通滤波器H1后的波形')%画出经过H1滤波器后的波形ylabel('幅度');subplot(212);plot(t,H2);%画出经过滤波器二后的波形title('经过带通滤波器H2后的波形')ylabel('幅度')xlabel('t')sw1=H1.*H1;%相干解调乘以同频同相的载波sw2=H2.*H2;%经过相乘器figure(4)subplot(211);plot(t,sw1);title('经过相乘器h1后的波形')%画出乘以同频同相载波后的波形ylabel('幅度')subplot(212);plot(t,sw2);title('经过相乘器h2后的波形')ylabel('.幅度')xlabel('t')bn=fir1(101,[2/80010/800]);%经过低通滤波器figure(5)st1=filter(bn,1,sw1);st2=filter(bn,1,sw2);subplot(211);plot(t,st1);title('经过低通滤波器sw1后的波形')%经过低通滤波器,滤出频率ylabel('幅度')%为f1,f2的基带调制信号波形subplot(212);plot(t,st2);title('经过低通滤波器sw2后的波形')ylabel('幅度')xlabel('t')%判决fori=1:length(t)if(st1(i)=st2(i))st(i)=0;elsest(i)=st2(i);endendfigure(6)st=st1+st2;subplot(211);plot(t,st);title('经过抽样判决器后的波形')%画出经过抽样判决的波形ylabel('幅度')subplot(212);plot(t,sn);title('原始的波形')ylabel('幅度')xlabel('t')figure(7)y1=fftshift(a);%对调制信号进行傅里叶变换plot(abs(fft(y1)));grid;title('调制信号的频谱图');axis([0,125,1,5]);figure(8)y2=fftshift(tuf1);%对载波信号进行傅里叶变换plot(abs(fft(y2)));grid;title('载波1的频谱图');axis([0,1000,0,0.0000000001]);figure(9)y3=fftshift(tuf2);plot(abs(fft(y3)));grid;title('载波2的频谱图');axis([500,1500,0,0.000000001]);figure(10)y4=fftshift(st);%对已调信号进行傅里叶变换plot(abs(fft(y4)));grid;title('已调信号的频谱图');axis([0,1000,0,1000]);