global edges2
global avg2

angles=[];
edges=[];
edges2=[];
avg=[];
avg2=[];
PowerDiff=[];
for x=1:203
    for y=1:203
        if Cell_Info_A.ReceivedPowerMap(y,x)~=0 & Cell_Info_D.ReceivedPowerMap(y,x)~=0
            Aangle=90-Cell_Info_A.azimuth;
            Dangle=90-Cell_Info_D.azimuth+360;
            angles(end+1)=atan2(y-101,x-101)*180/pi-(Aangle+Dangle)/2;
            PowerDiff(end+1)=Cell_Info_D.ReceivedPowerMap(y,x)-Cell_Info_A.ReceivedPowerMap(y,x);
        end
    end
end

for n=1:length(angles)
    if angles(n)<-180
        angles(n)=angles(n)+360;
    end
end

edges=linspace(-180,180,20)
for n=1:length(edges)-1
    avg(n)=mean(PowerDiff(find(edges(n)<=angles & angles<=edges(n+1))));
    edges2(n)=(edges(n)+edges(n+1))/2'
end

figure
plot(angles,PowerDiff,'.')
hold on
plot(edges2,avg,'r-')
title('Difference in Received Power caused by moving azimuth 122 degrees clockwise')
xlabel('Location in degrees measured clockwise from the angle bisector of the azimuth angles')
ylabel('Change in received power (dBm)')
axis([-180,180,-30,40]);

figure
nn=histc(angles,edges);
bar(edges,nn,'histc')
title('Number of measurements per angle')
xlabel('Location in degrees measured clockwise from the angle bisector of the azimuth angles')
ylabel('Number of measurements')

for n=1:length(edges)
    if n<length(edges)/2
        n2=length(edges)-n;
        avg2(n)=(nn(n)*avg(n)-nn(n2)*avg(n2))/(nn(n)+nn(n2));
        avg2(n2)=-avg2(n); 
    end
end
figure
plot(angles,PowerDiff,'.')
hold on
plot(edges2,avg2,'r-')
title('Difference in Received Power caused by moving azimuth 122 degrees clockwise')
xlabel('Location in degrees measured clockwise from the average of the azimuth angles')
ylabel('Change in received power (dBm)')
axis([-180,180,-30,40]);       
