function [Pe]=ask2(snr_in_dB)
%==================程序初始化及产生基带信号=================%
N = 1000; fdc = 100; %码子个数N;抽样频率fdc=100
t = 0.01:0.01:N;
source = randsrc(1,N,[0,1;0.5,0.5]); %产生0、1随机序列
A1 = ones(fdc,1);
seq = A1 * source;
A = seq(:)';
sy = sin(2*pi*3*t); %生成信号源
s =A .* sy; %被调信号s(t)
%subplot(2,3,1);
%plot(t,s);
%hold on
%=========================ASK信号的调制=====================%
c = cos(300*t); %产生载波信号载波频率fc=300Hz
x = s.*c; %ASK基带信号
%subplot(2,3,2);
%plot(t,x);
%hold on
%==================ASK基带信号加入高斯白噪声=================%
sigma = sqrt(10^(-snr_in_dB/10)/2);
noise=sigma*randn(1,N*fdc);
%subplot(2,3,3);
%plot(t,noise);
%hold on
xnoise=x+noise; %有高斯白噪声的ASK信号
y=1/2*xnoise.*c; %相干解调
%subplot(2,3,4);
%plot(t,y);
%hold on
%==============设计巴特沃思数字低通滤波器====================%
wp=0.1*pi;ws=0.12*pi;Rp=1;Rs=15;
[n,wn] = buttord(wp/pi,ws/pi,Rp,Rs);
[b,a] = butter(n,wn);
s1 = filter(b,a,y); %滤波
[H,w]=freqz(b,a,N,'whole');
f=(-N/2:1:N/2-1);
%subplot(2,3,5);
%plot(f,abs(fftshift(H)));
%hold on
S = 2*s1;
%subplot(2,3,6);
%plot(t,S);
%=========================计算误码率==========================%
%====================输入接收信号,并判决=====================%
cntError = 0; %误码个数
Pe = 0; %误码率
temp = 1;errortemp = 0;
e = 0;
B = ones(1,N*fdc);
T = B.*S;
for i =1:N;
T1 = T(temp:(i*fdc));
p = abs(T1);
for I = temp:(i*fdc);
if I <= 100;
if p(I) > 0.1;
e = e + 1;
else
e = e;
end
else
if p(I-(i-1)*fdc) > 0.1;
e = e + 1;
else
e = e;
end
end
end
if e < fdc/2 ;
receive(i) = 0;
else
receive(i) = 1;
end
temp=i*fdc+1;
e=0;
end
for j=1:N;
if source(j) == receive(j);
errortemp = 0;
else
errortemp = 1;
end
cntError = cntError + errortemp;
end
Pe=cntError/N;