%求幅度响应dB的函数
function [db,mag,pha,w]=freqz_m(b,a);
[H,w]=freqz(b,a,1000,'whole');
H=(H(1:1:501))';
w=(w(1:1:501))';
mag=abs(H);
db=20*log10((mag+eps)/max(mag));
pha=angle(H);
% 求理想低通滤波器的函数
function hd=ideal_lp(wc,N);
alpha=(N-1)/2;
n=0:1:N-1;
m=n-alpha+eps;
hd=sin(wc*m)./(pi*m);
%求理想带通滤波器的函数
function hd=ideal_bp1(wc1,wc2,N);
alpha=(N-1)/2;
n=0:1:N-1;
m=n-alpha+eps;
hd=sin((wc2*m)-(wc1*m))./(pi*m);
%主函数
%产生基带信号
figure(1);
a=[-1,-1,-1,-1,1,-1,-1,1,-1,-1,-1,-1,1,-1,-1,-1];
t=1:3200;
k=1;
for j=1:16
for (i=(j-1)*200+1:j*200)
y(i)=a(k);
end
k=k+1;
end
subplot(2,2,1);
plot(t,y);
title('基带信号');
axis([0,3200,-1.1,1.1]);
subplot(2,2,2);
plot(t,abs(fft(y)));
title('基带信号频谱');
%低通滤波器设计
fs=2000;fp=100;fr=300;
tr_width=(fr-fp)/fs;
N=ceil(6.6*pi/(tr_width*2*pi))+1;
n=0:1:N-1;
wc=(fr+fp)*pi/fs;
hd=ideal_lp(wc,N);
w_ham=(hamming(N))';
h=hd.*w_ham;
[db,mag,pha,w]=freqz_m(h,[1]);
subplot(2,2,3);
stem(n,h,'.');
title('实际单位脉冲响应h(n)');
axis([0,(N-1),-0.1,0.3]);
subplot(2,2,4);
plot(w/pi,db);
title('幅度响应(dB)');
axis([0,1.2,-150,10]);
figure(2);
b=conv(y,h);
subplot(2,2,1);
plot(b);
title('低通滤波后信号');
axis([0,3400,-1.2,1.2]);
subplot(2,2,2);
plot(abs(fft(b)));
title('低通滤波后信号频谱');
%调制
t=0:480/(length(b)-1):480;
y1=cos(2*pi*1000*t);
subplot(2,2,3);
plot(t,y1);
title('载波');
axis([0,500,-1.1,1.1]);
subplot(2,2,4);
plot(t,abs(fft(y1)));
title('载波频谱');
figure(3);
c=y1.*b;
subplot(2,2,1);
plot(c);
title('调制后信号');
axis([0,3200,-1.1,1.2]);
subplot(2,2,2);
plot(abs(fft(c)));
title('调制后信号频谱');
%加噪声
noise=0.1*randn(1,length(c));
d=c+noise;
t3=1:length(d);
subplot(2,2,3);
plot(t3,d);
title('加噪声后信号');
axis([0,3200,-1.1,1.2]);
subplot(2,2,4);
plot(t3,abs(fft(d)));
title('加噪声后信号频谱');
%带通滤波器设计
figure(4);
fs1=600;
fr1=700;fp1=900;fr2=1300;fp2=1100;
tr_width=min((fp1-fr1),(fr2-fp2))/(2*fs1);
N1=ceil(6.6/(tr_width*2))+1;
n=0:1:N1-1;
wc1=(fr1+fp1)*pi/fs1;
wc2=(fr2+fp2)*pi/fs1;
hd1=ideal_bp1(wc1,wc2,N1);
w_ham=(hamming(N1))';
h1=hd1.*w_ham;
subplot(2,2,1);
stem(n,h1,'.');
axis([0,N1,-0.4,1.5]);
title('带通滤波器');
subplot(2,2,2);
plot(n,abs(fft(h1)));
title('带通滤波器频谱');
axis([0,N1,0.8,2.2]);
e=conv(d,h1);
T=0:length(e)-1;
subplot(2,2,3);
plot(T,e);
title('带通滤波后信号');
axis([0,3200,-3,3]);
subplot(2,2,4);
plot(T,abs(fft(e)));
title('带通滤波后信号频谱');
%解调
T1=0:480/(length(e)-1):480;
Z=cos(2*pi*1000*T1);
f=e.*Z;
figure(5);
subplot(2,2,1);
T2=0:length(f)-1;
plot(T2,f);
title('解调后信号');
axis([0,3200,-3,3]);
subplot(2,2,2);
plot(T2,abs(fft(f)));
title('解调后信号频谱');
g=conv(f,h);
T3=0:length(g)-1;
subplot(2,2,3);
plot(T3,g);
title('低通滤波后信号');
axis([0,3200,-1.2,1.2]);
subplot(2,2,4);
plot(T3,abs(fft(g)));
title('低通滤波信号频谱');
%抽样判决
for j=1:16
sum=0;
for (i=(j-1)*204+1:j*204)
sum=sum+g(i);
end
h(j)=sum./204;
if(h(j)>=0.018)
h(j)=1;
else
h(j)=-1;
end
end
k=1;
for j=1:16
for (i=(j-1)*200+1:j*200)
z(i)=h(k);
end
k=k+1;
end
figure(6);
subplot(2,2,1);
plot(z);
axis([0,3200,-1.1,1.1]);
subplot(2,2,2);
plot(abs(fft(z)));