Attachment 'adish03.c'

Download

   1 // uniformly illuminated parabolic dish, beam
   2 //
   3 // compile with:       cc -o adish03 adish03.c -lgd -lpng -lm -lgsl -lgslcblas
   4 #  define NAME      "adish03"
   5 //
   6 // Uses the libgd library.  For documentation see the file:
   7 //      /usr/share/doc/gd-*/index.html  
   8 //   or the website:  http://www.libgd.org/Reference
   9 //  
  10 // You will need truetype fonts.  If you don't have them, you can
  11 // copy ../fonts/truetype/.. from openoffice.org to /usr/share/
  12 //
  13 // Uses swftools to build a swf movie from individual png images
  14 //        for more information, see http://www.swftools.org/
  15 //
  16 // Uses the GNU Scientific Library to make the J1() bessel function
  17 // for more information, see:
  18 //  http://www.gnu.org/software/gsl/manual/html_node/Regular-Cylindrical-Bessel-Functions.html
  19 //
  20 // This constructs half the slides, then mirrors them
  21 
  22 #include "gd.h"
  23 #include "math.h"
  24 #include <stdio.h>
  25 #include <stdlib.h>
  26 #include <unistd.h>
  27 #include <gsl/gsl_sf_bessel.h>
  28 
  29 #define  YCENT       130
  30 #define  INTPTS      100          // integration points 
  31 #define  WAVELENGTH  86.0         // radio wavelength in pixel units
  32 #define  SCALE       200.0        // disk scaling in pixel units
  33 #define  NPICS       40           // number of frames
  34 
  35 #define  XSIZE       1000         // display window in pixels
  36 #define  YSIZE       750          // display window in pixels
  37 #define  FNT         "DejaVuMonoSans"
  38 #define  FSZ         10
  39 
  40 double   amult[XSIZE][YSIZE]     ; //
  41 double   aphase[XSIZE][YSIZE]    ; //
  42 double   behind[XSIZE][YSIZE]    ; //
  43 
  44 // ==========================================================================
  45 
  46 int main() {
  47 
  48    gdImagePtr im                    ; // pixel image map used by gd
  49    gdPoint  beam[4]                 ; // trapezoidal output beam
  50    double   lambda =     WAVELENGTH ; //
  51    double   scale  = (double) SCALE ; // dish scale
  52    double   yacent = (double) YCENT ; // center of upper drawing
  53    double   xsize  = (double) XSIZE ; // width of pixels
  54    double   ysize  = (double) YSIZE ; // height of pixels
  55    int      ybot   = 2*YCENT        ; // top of output pixel array
  56    double   dybot  = (double) ybot  ; // real ""
  57    double   pi2    = 8.0*atan(1.0)  ; // two times PI
  58    double   d2r    = pi2/360.0      ; // degrees to radians conversion factor
  59    int      oxc    = XSIZE/2        ; // center of output pixel array
  60    int      oyc    = YSIZE/2        ; // center of output pixel array
  61    double   xacent = (double) oxc   ; // real ""
  62    FILE     *pngout                 ; // file handle for PNG output frame
  63    char     dirname[200]            ; // frame output directory
  64    char     framename[200]          ; // directory/name of frame
  65    char     labstring[200]          ; // used for labellin
  66    char     rmmkdir[200]            ; // remove and make frame directory
  67    char     png2swf[200]            ; // make frames into swf
  68    char     pngfmt[200]             ; // format to make frame path
  69    char     link0[200]              ; // directory/name of source 
  70    char     link1[200]              ; // directory/name of target hardlink
  71    int      black, sun1, white, red ; // color map numbers
  72    int      green, blue, gray       ; // color map numbers
  73    int      dgray, trans            ; // color map numbers
  74    int      bcolor[256]             ; // color map numbers
  75    int      gcolor[256]             ; // color map numbers
  76    int      ox, oy                  ; // output pixel position
  77    int      oxm                     ; //
  78    int      i                       ; // general counter
  79    int      fs                      ; // big label font size
  80    int      cir                     ; //
  81    double   dcir                    ; //
  82    int      icolor                  ; //
  83    int      jcolor                  ; //
  84    int      mag                     ; // pixel magnitude, 0-255
  85    int      frame                   ; // animation frame count
  86    int      pct=0                   ; // percentage of complete frame
  87    int      pct0=0                  ; // previous percentage
  88    int      ya= YCENT               ; // antenna height
  89    double   bx                      ; // bessel argument
  90    double   j                       ; // 
  91    double   j0                      ; // 
  92    double   ffrac                   ; // frame scaling of variable, 0 to 1
  93    double   phase                   ; // phase
  94    double   k=pi2/lambda            ; // wavenumber
  95    double   oxd                     ; // 
  96    double   oxd2                    ; // 
  97    double   oyd                     ; //
  98    double   oyd2                    ; //
  99    double   oym                     ; //
 100    double   dis                     ; // distance from focus spot
 101    double   dis2                    ; // 
 102    int      rate = 20               ; // swf rate
 103    double   xm                      ; //
 104    double   max                     ; // normalize beam amplitude
 105    double   norm                    ; // normalize beam amplitude
 106    double   delta                   ; //
 107    double   sumsin                  ; //
 108    double   sumcos                  ; //
 109    double   sum                     ; //
 110    double   adis                    ; //
 111    double   amp[INTPTS]             ; //
 112    double   dn, da, di              ; //
 113 
 114 // set up names
 115 
 116    sprintf(rmmkdir, "rm -rf %sdir ; mkdir %sdir", NAME, NAME);
 117    sprintf(png2swf, "png2swf -o %s.swf -r%3d %sdir/*.png", NAME, rate, NAME );
 118    sprintf(pngfmt , "%sdir/a%%04d.png", NAME, frame );
 119 
 120 //  set up target directory
 121 
 122    system( rmmkdir );
 123 
 124 //  make array amplitude values ------------------------------------------
 125    
 126    for( i=0 ; i<INTPTS ; i++ ) {
 127       dn = ( (double)INTPTS ) ;
 128       di = (double) i      ;
 129       da = (2.0*di+1.0-dn)/dn;
 130       amp[i]=sqrt(1.0-da*da );
 131       // printf( "%3d%12.6f\n", i, amp[i] );
 132    }
 133 
 134 //  make phase and magnitude array ---------------------------------------
 135 
 136    max   = 0.0 ;
 137    cir   = (int) (0.39*scale)      ;
 138    dcir  = (double) cir            ;
 139    xm    = 2.0*dcir                ;
 140    delta = 2.0*xm/((double)INTPTS) ;
 141    xm   -= 0.5*delta-xacent        ;
 142    dcir *= 0.99*dcir               ;
 143 
 144    for( oy=0 ; oy<YSIZE ; oy++ ) {
 145       oyd   = (double)(oy-ya) ;
 146       oyd2  = oyd*oyd ;
 147       oym   = 0.5/(0.5+exp(-0.1*oyd ) ) ; // kludge!!
 148 
 149       printf( "setup %04d/%04d\r", oy, YSIZE ) ;
 150       fflush(stdout);
 151 
 152       // do half the array, then mirror
 153       for( ox=0 ; ox < oxc ; ox++ ) {
 154          oxd = xm - (double)ox ;
 155          sumsin = 0.0 ;
 156          sumcos = 0.0 ;
 157          for( i=0 ; i<INTPTS ; i++ ) {
 158             oxd2    = oxd*oxd ;
 159             dis2    = oxd2 + oyd2 ;
 160             dis     = sqrt( dis2 );
 161             sumsin += amp[i]*sin( k*dis ) / dis;
 162             sumcos += amp[i]*cos( k*dis ) / dis;
 163             oxd    -= delta ;
 164          }
 165 
 166          // is it behind the antenna?
 167          oxd  = (double) ( ox-oxc );
 168          oyd  = (double) ( oy-ya  );
 169          adis = 0.25*oxd*oxd + 4.0*oyd*oyd ;
 170          if( adis > dcir ) {
 171             sum            = sqrt(  sumsin*sumsin + sumcos*sumcos ) ;
 172 // normalization kludge !!!
 173             sum           *= oym*sqrt(oxd*oxd + oyd*oyd);
 174 
 175             amult[ox][oy]  = sum ;
 176             aphase[ox][oy] = atan2( sumsin, sumcos );
 177             if( max < sum ) { max = sum ; }
 178           }
 179           else {
 180              amult[ox][oy]  = 0.0 ;
 181              aphase[ox][oy] = 0.0 ;
 182           }
 183    }  }
 184 
 185    // normalize and mirror
 186 
 187    norm = 1.0 / max ;
 188 
 189    for( oy=0 ; oy<YSIZE ; oy++ ) {
 190       for( ox=0 ; ox<oxc ; ox++ ) {
 191          amult[ox][oy] *= norm ;
 192          oxm = XSIZE-(ox+1);
 193          amult[ oxm][oy] = amult[ ox][oy] ;
 194          aphase[oxm][oy] = aphase[ox][oy] ;
 195    }  }
 196          
 197    printf("                  \r");
 198 
 199 //  outer loop, sinusodial random factor change --------------------------
 200 //  from 0 to 1 to 0
 201   
 202    for( frame=0 ; frame<NPICS ; frame++ ) {
 203       phase = pi2*((double)frame)/((double)(NPICS-1));
 204 
 205 // set up array and define colors -----------------------------------------
 206 
 207       im = gdImageCreateTrueColor(XSIZE, YSIZE );
 208    
 209       // Allocate standard colors
 210       black = gdImageColorAllocate(im,   0,   0,   0);
 211       white = gdImageColorAllocate(im, 255, 255, 255);
 212       sun1  = gdImageColorAllocate(im,  51,  51, 102);
 213       red   = gdImageColorAllocate(im, 255,   0,   0);
 214       green = gdImageColorAllocate(im,   0, 255,   0);
 215       blue  = gdImageColorAllocate(im,   0,   0, 255);
 216       gray  = gdImageColorAllocate(im, 160, 160, 160);
 217       dgray = gdImageColorAllocate(im,  48,  48,  48);
 218       trans = gdImageColorAllocate(im, 1, 1, 1);
 219    
 220       // allocate gray scale array 
 221       for( icolor=0 ; icolor<256 ; icolor++ ) {
 222         gcolor[icolor] = gdImageColorAllocate(im, icolor,   icolor,   icolor);
 223         bcolor[icolor] = gdImageColorAllocate(im, icolor/3, icolor, icolor/3);
 224       }
 225 
 226       printf( "%04d/%04d\r", frame, NPICS ) ;
 227       fflush(stdout);
 228 
 229 // compute pixels ---------------------------------------------------------
 230    
 231       for( ox=0 ; ox<XSIZE ; ox++ ) {
 232          for( oy=0 ; oy<YSIZE ; oy++ ) {
 233             j  = amult[ox][oy]*sin( phase-aphase[ox][oy] ) ;
 234             if( j >  1.0 ) { j= 1.0; }
 235             if( j < -1.0 ) { j=-1.0; }
 236             mag = (int) ( 127.5*(1.0 + j ));
 237             gdImageSetPixel( im, ox,             oy, gcolor[mag] ) ;
 238       }  } 
 239   
 240  // labels -----------------------------------------------------------------
 241    
 242       fs= ybot/6 ;
 243    
 244       gdImageStringFT( im, NULL,               // imagespace, bounding box
 245                        white , FNT, fs, 0.0,   // color, font, fontsize, angle
 246                        15     , fs+15  ,       //  x, y
 247                        "Dish Antenna" );       //  text
 248    
 249 // draw dish --------------------------------------------------------------
 250    
 251       jcolor    = bcolor[255] ;
 252    
 253       // disk
 254       gdImageFilledEllipse( im, oxc, ya,     4*cir,  cir,   jcolor );
 255    
 256       //  square feed 
 257       beam[0].x = oxc+6     ;
 258       beam[0].y = ya+cir-1  ;
 259       beam[1].x = oxc-6     ;
 260       beam[1].y = ya+cir-1  ;
 261       beam[2].x = oxc-6     ;
 262       beam[2].y = ya+cir+15 ;
 263       beam[3].x = oxc+6     ;
 264       beam[3].y = ya+cir+15 ;
 265    
 266       gdImageFilledPolygon( im, beam, 4,             jcolor );
 267    
 268       // feed struts
 269       gdImageLine( im, oxc-2*cir, ya, oxc,  ya+cir,  jcolor );
 270       gdImageLine( im, oxc+2*cir, ya, oxc,  ya+cir,  jcolor );
 271       gdImageLine( im, oxc,       ya, oxc,  ya+cir,  jcolor );
 272 
 273    
 274       // labels on array -------------------------------------------------------
 275       fs= ybot/8 ;
 276    
 277       // gdImageStringFT( im, NULL,               // imagespace, bounding box
 278       //                  white , FNT, fs, 0.0,   // color, font, fontsize, angle
 279       //                  15, YSIZE-15,           //  x, y
 280       //                  "Ground Pattern" );     //  text
 281    
 282       // output the frame ------------------------------------------------------
 283    
 284       sprintf( framename, pngfmt , frame );
 285       pngout = fopen( framename, "wb");
 286       gdImagePngEx( im, pngout, 1 );
 287       gdImageDestroy(im);
 288       fclose(pngout);
 289    
 290    }  // end of single frame loop
 291    
 292    system( png2swf );
 293 }
 294    
 295    

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2012-04-29 17:18:11, 10.8 KB) [[attachment:adish03.c]]
  • [get | view] (2012-04-19 05:40:34, 5075.7 KB) [[attachment:dish_beam.swf]]
  • [get | view] (2012-04-19 05:41:03, 4505.1 KB) [[attachment:dish_wide.swf]]
  • [get | view] (2012-04-19 05:45:29, 10678.0 KB) [[attachment:many_few.swf]]
  • [get | view] (2012-04-29 18:26:44, 9.1 KB) [[attachment:pa03.c]]
  • [get | view] (2012-04-29 18:27:01, 9.1 KB) [[attachment:pa04.c]]
  • [get | view] (2012-04-19 05:41:48, 3771.3 KB) [[attachment:phased00.swf]]
  • [get | view] (2012-04-19 05:43:21, 5383.6 KB) [[attachment:phased30.swf]]
  • [get | view] (2012-04-19 05:48:19, 29653.2 KB) [[attachment:random.swf]]
  • [get | view] (2012-04-19 05:47:03, 28568.0 KB) [[attachment:rotate.swf]]
  • [get | view] (2012-04-19 05:44:20, 26020.8 KB) [[attachment:wide_narrow.swf]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.