1 #ifdef HAVE_STDLIB_H
2 #include <stdlib.h>
3 #endif
4 #include <stdio.h>
5 #include <math.h>
6 #include "nrutil.h"
7 #include "yagi.h"
8 
9 /* This routines finds the maximum sidelobe level in the antenna pattern,
10 outside the main beam. The fasteest one, find_max_sidelobe_fast is used by
11 optimise when seeing how clean the pattern is. Its not perfect, but its
12 fast.
13 
14 The other one, find_max_sidelobe_slow is much more accurate, doing a good
15 job of finding the sidelobes, but takes longer to do. Hence its not
16 really suitable for optimisation, but its okay for 'output' where we dont
17 care too much about spped. */
18 
19 extern double angular_stepsize_2,min_offset_from_peak;
20 
21 #define BETTER 1
22 #define WORSE 2
23 
24 #define STEP  1   /* anglur step in degrees */
25 
dB_down_from_peak(double x,double pin,struct element_data * coordinates,struct FCOMPLEX * current,int elements,double f,double design_f)26 double dB_down_from_peak(double x, double pin, struct  element_data *coordinates, struct FCOMPLEX *current,int elements, double f, double design_f)
27 {
28 		double ans,gain_H_plane,peak_gain, gain_at_x;
29 		gain(90.0, 0.0, pin, f/design_f, coordinates, current, elements, &peak_gain, &gain_H_plane, f, design_f);
30 		gain(x, 0.000, pin, f/design_f, coordinates, current, elements, &gain_at_x, &gain_H_plane, f, design_f);
31 		ans=peak_gain-gain_at_x;
32 		return(ans);
33 }
34 
35 
find_max_sidelobe_slow(double gain,double pin,struct element_data * coordinates,struct FCOMPLEX * current,int elements,double frequency,double design_f)36 double find_max_sidelobe_slow(double gain, double pin,struct element_data *coordinates, struct FCOMPLEX *current, int elements, double frequency,double design_f)
37 {
38 	double level,best=-1000.0,a=90.0,theta,max=1e8;
39 	int k;
40 	do{
41 		a+=STEP; /* Use 1 degree or similar step*/
42 		level=dB_down_from_peak(a,pin,coordinates,current,elements,frequency,design_f);
43 		if(level>best)
44 		{
45 			k=BETTER;
46 			best=level;
47 		}
48 		else
49 			k=WORSE;
50 		}while(k==BETTER && a<=270.0);
51 	a-=STEP;
52 	for(theta=a; theta<270.01; theta+=STEP)
53 	{
54 		level=dB_down_from_peak(theta,pin,coordinates,current,elements,frequency,design_f);
55 		if(level<max)
56 		{
57 			max=level;
58 			a=theta;
59 		}
60 	}
61 	return(max);
62 }
63 
64 
find_max_sidelobe_fast(double gain,double pin,struct element_data * coordinates,struct FCOMPLEX * current,int elements,double frequency,double design_f)65 double find_max_sidelobe_fast(double gain, double pin,struct element_data *coordinates, struct FCOMPLEX *current, int elements, double frequency,double design_f)
66 {
67 	double angle=90.0,min_angle,min,max=270,three_dB_point,min_level=1000000.0;
68 	double level, min_times;
69 	int N;
70 
71 	if(min_offset_from_peak==0.0)
72 	{
73 		three_dB_point=sqrt(41000.0/(pow(10.0,gain/10.0)));
74 		if(three_dB_point > 90.0)
75 			min=180.0;
76 		else
77 			min=90.0+(three_dB_point);
78 	}
79 	else
80 		min=90.0+min_offset_from_peak;
81 	if(angular_stepsize_2==0.0)
82 		angular_stepsize_2=three_dB_point/30.0; /* step sixe to use - rough */
83 		/* Since this is only an approximate quick method, you cant expect
84 		supeurb accuracy - use _fast if you want that. I'll evaluate at
85 		1/10th of the beamwidth, so if the 3dB beamwidth is estimated to be
86 		40 deg, we will evualate every 4 deg. However, we need to evalate at
87 		precise 270 degrees, otherwise its possible the max sidelobe will be
88 		less than the FB, which is silly. To avoid this, I'll ensure its
89 		evaluated at 270 deg, so the step size will be altered to do this. */
90    min_times=(max-min)/angular_stepsize_2;
91 	N=(int) (min_times+1.0);
92 
93 	for(angle=min; angle <=max;angle+=(max-min)/N)
94 	{
95 		level=dB_down_from_peak(angle,pin,coordinates,current,elements,frequency,design_f);
96 		if(level<min_level)
97 		{
98 			min_level=level;
99 			min_angle=angle;
100 		}
101 	}
102 	/* printf("min=%.2f min_angle=%.2f angle=%.2f f=%f\n ",min_level,min_angle,angle,frequency);  */
103 	return(min_level);
104 }
105 
106 
107 
108 
109