function ber = sim_ASK2(snrIndB,N,flag)
% 二进制ASK调制解调仿真程序
% 输入
% snr: 信噪比
% N: 仿真比特数
% flag:绘制调制解调波形标志位
% 输出
% y: 误码率
x = round(rand(1,N));% 产生信号比特
fc = 200; % 载波频率
fs = 2000;% 采样频率
fb = 50; % 比特频率
nSamples = fs/fb;% 每比特采样数
signal_in_one_bit = cos(2*pi*fc*(0:nSamples-1)/fs);% 传号
space_in_one_bit = zeros(1,nSamples); % 空号limitation = sum(signal_in_one_bit.^2)/2; % 判决门限
sig_ori = reshape(repmat(x,nSamples,1),1,N*nSamples); % 原始信号
%% ASK调制
y=zeros(1,nSamples*N);
for i=1:length(x)
if(x(i)==1)
y((i-1)*nSamples+1:i*nSamples) = signal_in_one_bit;
else
y((i-1)*nSamples+1:i*nSamples) = space_in_one_bit;
end
end
mod_sig = y;% 调制信号
%% 高斯信号传输
% rec_sig = awgn(mod_sig,snr,0); % 通过高斯信道后的接收信号
snr = 10^(0.1*snrIndB)/(fb/4);sigma = 1/sqrt(snr);
rec_sig = awgn_gauss(mod_sig,sigma);
%% ASK解调
bits_judged = zeros(1,N);
for i = 1:N % 逐比特处理
rec_seg = rec_sig((i-1)*nSamples+1:(i*nSamples));
sum_rec = rec_seg*signal_in_one_bit'; % 相关累积
if(sum_rec>=limitation) % 判决
bits_judged(i) = 1;
else