clear; %Clear all variables in the workspace 
close all; %Closes all figure windows
clc %Clear command window

%Load the data for Assignment 1, which 
%contains the variables ComplexAmplitude and TxRxDist:
load ('Assignment1'); 

RxPow=(abs(ComplexAmplitude).^2); %Received power

%(note: the Tx power has been removed, so the data is in dB, not in dBm!)

Dist_dB=10*log10(TxRxDist); %Tx-Rx separation in dB.
 
D_win=[]; P_SSA=[]; ssfamplitude=[]; %preallocation of variables in the for-loop
for i=6:5:length(ComplexAmplitude)-5
     D_win=[D_win; mean(TxRxDist(i-5:i+5))]; %Average TxRxDistance for the small-scale averaged power
     P_SSA=[P_SSA; mean(RxPow(i-5:i+5))]; %Small scale averaged power.
     
     ratio=(abs(ComplexAmplitude(i-5:i+5)))./(mean(abs(ComplexAmplitude(i-5:i+5))));
     ssfamplitude=[ssfamplitude ratio]; %Small-scale fading amplitude based on the small-scale averaged power. 
end
figure(1) %new figure window
semilogx((TxRxDist),10*log10(RxPow),'k.') %plot distance against received power, with logarithmic x-axis.
xlabel('Tx-Rx separation distance [m]') %label for the x-axis
ylabel('Channel gain (dB)') %label for the y-axis.
grid on %add grid
hold on %hold on, to be able to plot another figure, while keeping the old plot.
semilogx((D_win),10*log10(P_SSA),'bo') %Plot small-scale averaged channel gain

%A plot of the free-space pathloss at 2.4 GHz, using the same figure as above:
semilogx(([10 200]),-20*log10(4*pi*[10 200]/(3e8/2.4e9)),'r-')
%A legend of for window 1:
legend('Channel gain','Small-scale averaged channel gain','Free space channel gain')
%--------------------------------------------------

%A2: Find the ordinary least-squares estimate of PL(d0)
%and pathloss exponent n:

%Plot the average received power using the pathloss estimates found above: 

%--------------------------------------------------

%A3: In a new window, plot the empirical cdf of the large-scale fading from
%the measurement:
figure(2)


%Plot the CDF for the normal distribution based on the maximum-likelihood estimate
%for a normal distribution: 

%--------------------------------------------------

%A4: In a new window, plot the empirical cdf of the small scale fading amplitude
figure(3)

%--------------------------------------------------

%Use the maximum-likelihood expression for the
%Rayleigh distribution to find an estimate of the scale
%parameter sigma:

%--------------------------------------------------
%Plot the cdf of the Rayleigh distribution, using
% the estimate obtained in A5:

%-------------------------------------------------- 

%--------------------------------------------------
%A5: Plot the cdf for the Rice distribution with the
%parameter estimates given in the assignment. Hint:
%Type "help marcumq".
sigRice=0.489;
v=0.84185;

%-------------------------








