1 #ifdef HAVE_STDLIB_H
2 #include <stdlib.h>
3 #endif
4 #include <stdio.h>
5 #include <math.h>
6 #include <errno.h>
7 #ifdef sun
8 
9 #endif
10 #include "yagi.h"
11 
12 extern double boom_factor;
13 #include <errno.h>
14 #define MIN_RDS   0.05 /* Minimum reflector-driven spacing in lambda */
15 
randomise(int randomisation_method,double frequency,double max_percent,double ** driven_data,double ** parasitic_data,int driven,int parasites)16 void randomise(int randomisation_method, double frequency, double max_percent, double **driven_data, double**parasitic_data, int driven, int parasites)
17 {
18 	int i,elements;
19 	static int run_first_time=0;
20 	double y, old_boom_length, new_boom_length, a, lambda,**z,l,x,taper;
21 	static double max_boom_length;
22 	lambda=300/frequency;
23 	if(run_first_time==0)
24 	{
25 		max_boom_length=(1+boom_factor/100)*parasitic_data[parasites][X];
26 	}
27 	run_first_time++;
28 	if ((randomisation_method & DRIVEN_LENGTH) == DRIVEN_LENGTH) /* 1 */
29 	{
30 		for(i=1;i<=driven; ++i) /* randomise the driven */
31 		{
32 			y=1+0.02*max_percent*(randreal()-0.5);
33 			driven_data[i][LENGTH]*=y;
34 		}
35 	}
36 	if ((randomisation_method & DRIVEN_POSITION) == DRIVEN_POSITION) /* 2 */
37 	{
38 		for(i=1;i<=driven; ++i) /* randomise the driven position */
39 		{
40 			y=1+0.02*max_percent*(randreal()-0.5);
41 			driven_data[i][X]*=y;
42 		}
43 	}
44 	if ((randomisation_method & REFLECTOR_LENGTH) == REFLECTOR_LENGTH) /* 4 */
45 	{
46 
47 	   /* reflector position stays at x=0 */
48 		y=1+0.02*max_percent*(randreal()-0.5);
49 		parasitic_data[1][LENGTH]*=y;
50 	}
51 
52 	if ((randomisation_method & DIRECTOR_LENGTH) == DIRECTOR_LENGTH) /* 8 */
53 	{
54 		for(i=2; i<=parasites; ++i)
55 		{
56 			y=1+0.02*max_percent*(randreal()-0.5);
57 			parasitic_data[i][LENGTH]*=y;
58 			/* make sure directors dont get longer down the boom */
59 			if((parasitic_data[i][LENGTH]>parasitic_data[i-1][LENGTH]) && (i>2))
60 				parasitic_data[i][LENGTH]=parasitic_data[i-1][LENGTH];
61 		}
62 	}
63 
64 	if ((randomisation_method & DIRECTOR_POSITION) == DIRECTOR_POSITION) /* 16 */
65 	{
66 		old_boom_length=parasitic_data[parasites][X];
67 		y=1+0.02*max_percent*(randreal()-0.5);
68 		new_boom_length=old_boom_length*y;
69 		if(new_boom_length > max_boom_length)
70 			new_boom_length=max_boom_length;
71 		/* rearrange directors */
72 		for(i=2; i<=parasites; ++i)
73 		{
74 			y=1+0.02*max_percent*(randreal()-0.5);
75 			parasitic_data[i][X]*=y*(new_boom_length/old_boom_length);
76 		}
77 	}
78 	if ((randomisation_method & ALL_ELEMENT_LENGTHS_IDENTICAL)==ALL_ELEMENT_LENGTHS_IDENTICAL) /* 32 */
79 	{
80 			y=1+0.02*max_percent*(randreal()-0.5);
81 			for(i=1; i<=driven; ++i)
82 				driven_data[i][LENGTH]*=y;
83 			for(i=1; i<=parasites; ++i)
84 				parasitic_data[i][LENGTH]=driven_data[1][LENGTH];
85 	}
86 	if ((randomisation_method & LINEAR_TAPER)==LINEAR_TAPER) /* 64 */
87 	{
88 		a=1+(0.1*randreal()); /* 1.00 to 1.1 */
89 		taper=(a-1)/driven_data[1][X];
90 		parasitic_data[1][LENGTH]=a*driven_data[1][3]; /* reflector = a*driven */
91 		for(i=2; i<=parasites; ++i)
92 				parasitic_data[i][LENGTH]=parasitic_data[1][3]-taper*(parasitic_data[i][X]-driven_data[1][X]);
93 	}
94 
95 	if ((randomisation_method & RESONATE_DRIVEN)==RESONATE_DRIVEN) /* 128 */
96 	{
97 		if(run_first_time==1)
98 		{
99 			elements=driven+parasites;
100 			z=dmatrix(1L,(long)elements,2L,2L*(long)elements);
101 			l=0.44*lambda;
102 			do
103 			{
104 				driven_data[1][LENGTH]=l;
105 				self_impedance(1, frequency*1e6,driven,parasites,driven_data,z);
106 				l+=0.00005*lambda;
107 				driven_data[1][LENGTH]=l;
108 				x=z[1][2];
109 			}while(z[1][2]<0.0);
110 			printf("Driven element set to :l=%f m = %f wavlengths\n",l,l/lambda);
111 			free_dmatrix(z,1L,elements,1L,2L*elements);
112 		}
113 	}
114 #ifdef DEBUG
115 	if(errno)
116 	{
117 	fprintf(stderr,"Errno =%d in randomis.c\n", errno);
118 	exit(1);
119 	}
120 #endif
121 }
122 
123