%% Setup
clear
clc
BT = input('insert BT: ');% Size of signal constellation
for EbNo=10:30
%% %%%%%%%%%%%%%%%%%%%%%% Define modulation parameters %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
bitrate=9600; %k*Rs;
span = 10;
M = 2; %input('insert M: '); % Size of signal constellation
k = log2(M); % Number of bits per symbol
% Signal Source
%datashap = [0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 1 0 0 1 1 1];
datashap = randi([0 1],10000,1)';
datashap=[datashap , zeros(1,k-length(datashap)+floor(length(datashap)/k)*k)];
Nbit = length(datashap); % Number of Bits to process
Nsym = Nbit / k ; % Number of Symbols to process
%% Define parameters.
Fs = 96000; % Sampling frequency (Hz)
Rs = bitrate/k; %Nsym/max(t); % Symbol rate (Sps)
sps= round(Fs/Rs);
t = 0:1/Fs:sps*Nsym/Fs-1/Fs;
Rs = Fs/sps;
bitrate=k*Rs;
%% Create the filter coefficients using the rcosdesign function.
filtCoeff = gaussdesign(BT, span, sps);
%% modulation
newdataMod=rectpulse(datashap,sps);
%Filter the modulated data.
%txSig = upfirdn(dataMod,filtCoeff, sps);
txSig2 = upfirdn(newdataMod,filtCoeff);
newtxSig2=txSig2(sps*span/2+1:end-sps*span/2);
signal = newtxSig2;
signal = [ones(1,50) , signal]; %flag
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%% demodulate %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
snr = EbNo ;
hAWGN = comm.AWGNChannel('NoiseMethod', ...
'Signal to noise ratio (SNR)','SNR',snr);
signal = step(hAWGN, signal); %Add AWGN to the transmitted signal.
%% Setup
%% flag
signal=signal/max(signal);
signal=signal/(max(signal)-min(signal))-min(signal)/2;
b=signal<0.4;
[srt, X]=sort(b);
signal=signal(X(1)+51:end);
%% Define parameters.
M = 2; %input('insert M: '); % Size of signal constellation
k = log2(M); % Number of bits per symbol
Rs = bitrate; %nsym/max(t); % Symbol rate (Sps)
sps= round(Fs/Rs);
num1=(sps)-length(signal)+(sps)*floor(length(signal)/(sps));
signal=[ signal , zeros( 1,num1) ];
nbit = length(signal)/sps; % Number of Bits to process
nsym = nbit ; % Number of Symbols to process
t = 0:1/Fs:sps*nsym/Fs-1/Fs;
%% demod
%Demodulate the filtered signal.
dataOut = downsample(signal,sps,round(sps/2));
newdataOut = rectpulse(dataOut,sps);
dataOut(dataOut < 0.5) = 0;
dataOut(dataOut > 0.5) = 1;
dataOut = dataOut(1 : Nbit);
%% BER
%Compare x and z to obtain the number of errors and then calculate the bit error rate
[number_of_errors,bit_error_rate] = biterr(datashap,dataOut)
hError = comm.ErrorRate;
errorStats = step(hError, datashap', dataOut');
BER(EbNo)=errorStats(1);
SNR(EbNo)=snr;
if errorStats(1)<=0.00001
break
end
end
figure(60); hold all; %1.blue 2.green 3.red
semilogy(SNR,BER,'-o');
ylim([0 1])
ylabel('Bit Error rate');
xlabel('SNR');
YScale = 'log';
hold off;