%clear all;
%close all;
clc
%% Setup
%%%%%%%%%%%%%%%%%%%%%%%% filter %%%%%%%%%%%%%%%%%%%%%%%%%%%
bitrate=19200; %k*Rs;
Fs = 96000; % Sampling frequency (Hz)
% Define parameters.
M = input('insert M: '); % Size of signal constellation
k = log2(M); % Number of bits per symbol
if input('Ready to record?[Y/N]\n','s')=='y'
%EbNo = 0;
z=audiorecorder(Fs , 8, 2);
disp('start speech');
record(z);
while input('wanna stop?[Y/N]\n','s')~='y'
end;
pause(z);
signal = getaudiodata(z)*(k-1);
filename = 'signal.wav';
audiowrite(filename,getaudiodata(z),Fs,'BitsPerSample',8,...
'Comment','This is PSK modulated signal.');
%%%%%%%%%%%%%%%%%%%%% Read from file %%%%%%%%%%%%%%%%
else
EbNo = 20;
snr = EbNo + 10*log10(2) ;
filename='signal_sent.wav';
signal = audioread(filename);
audioinfo(filename)
hAWGN = comm.AWGNChannel('NoiseMethod', ...
'Signal to noise ratio (SNR)','SNR',snr);
signal=[ zeros( randperm(50,1),2) ; signal ]; % simulating delay button
signal = step(hAWGN, signal); %Add AWGN to the transmitted signal.
signal(:,1)=signal(:,1)*rand(1,1);
signal(:,2)=signal(:,2)*rand(1,1);
end
%% flag
signal(:,1)=signal(:,1)/max(signal(:,1));
signal(:,2)=signal(:,2)/max(signal(:,2));
clearvars newsignal
b1=signal(:,1)<0.5;
[srt, X1]=sort(b1);
newsignal(:,1)=signal(X1(1):end,1);
newsignal(:,1)=newsignal(:,1)/trapz(newsignal(1:50,1))*50*(k-1);
%%%
b2=signal(:,2)<0.5;
[srt, X2]=sort(b2);
newsignal(:,2)=signal(X1(1):end,2);
newsignal(:,2)=newsignal(:,2)/trapz(newsignal(1:50,2))*50*(k-1);
newrxSig =newsignal(51:end,1)+1i*newsignal(51:end,2);
%%%%%%%%%%%%%%%% params %%%%%%%%%%%%%%%%%%%%%
Rs = bitrate/k; %nsym/max(t); % Symbol rate (Sps)
sps= round(Fs/Rs);
num1=(sps*k)-length(signal)+(sps*k)*floor(length(signal)/(sps*k));
signal1=signal;
signal=[ signal ; zeros( num1,2) ];
nbit = length(signal)*k/sps; % Number of Bits to process
nsym = nbit / k ; % Number of Symbols to process
t = 0:1/Fs:sps*nsym/Fs-1/Fs;
%% demodulation
rxSig = downsample(newrxSig,sps,round(sps/2));
hDemod = comm.RectangularQAMDemodulator(M, ...
'BitOutput', true, ...
'SymbolMapping', 'Gray');
dataOut = step(hDemod, rxSig);
dataOut = dataOut(1 : Nbit);
%% Convert the bits in x into k-bit symbols.
datasymOut = bi2de(reshape(dataOut,k,length(dataOut)/k).','left-msb')';
%% eye diagram
eyediagram(newrxSig , 2*sps , sps);
%% scatter plot
scatterplot(rxSig);
%% 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);
fprintf('Error rate = %f\nNumber of errors = %d\n', ...
errorStats(1), errorStats(2))
%% SER
[number_of_sym_errors,sym_error_rate] = symerr(datasym,datasymOut)
%% Create an EVM object, output maximum and 90-percentile EVM
% measurements, and symbol count
hEVM = comm.EVM('MaximumEVMOutputPort',true,...
'XPercentileEVMOutputPort', true, 'XPercentileValue', 90,...
'SymbolCountOutputPort', true);
% Calculate measurements
[RMSEVM,MaxEVM,PercentileEVM,NumSym] = step(hEVM,dataMod,rxSig(1:Nsym))