File:Mehrfachspalt-Numerisch.png

From testwiki
Jump to navigation Jump to search
    Original file (3,600 × 2,700 pixels, file size: 278 KB, MIME type: image/png)

    This file is from Wikimedia Commons and may be used by other projects. The description on its file description page there is shown below.

    Summary

    Description
    Deutsch: Numerisch berechnete Intensitätsverteilung hinter einem Mehrfachspalt dargestellt als Hitzefeld. Nicht berücksichtigt wurde die Freiraumdämpfung. Deutlich zu erkennen die Hauptmaximas und dazu schwach die Nebenmaximas. Hier wurde zur weiteren Veranschaulichung kein endliches Gitter sondern eine Wellenfront mit begrenzter Weite eingezeichnet. In der Nähe des Gitter treten Alias-Effekte auf, die wegen der begrenzten Auflösung entstehen.
    Date
    Source Own work
    Author Miessen
    PNG development
    InfoField
     This diagram was created with MATLAB.
     config.ini 
    ;
    ; Some nice values
    ;
    
    dim_x = 4000
    dim_y = 2700
    free_space_loss = 0
    ; gap width as factor to grating_distance
    gap_width = 0.2
    grating_count = 10
    ; grating_distance approx 2000 nm
    ; scaling_meter_per_px * 3/gap_width
    grating_distance = 2250.e-9
    offset_x = 600
    ; igonre...; 30 px per wave = 700.e-9 / 30px
    ; calculate near field / far field
    scaling_meter_per_px = 150e-9
    slit_width = 0
    slit_iterations = 0
    ; 700 nm
    wave_length = 700.e-9
    
    Source code
    InfoField

    diffraction_grating.m code

    #!/usr/bin/octave -qf
    % \usr\bin\octave
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    % Miessen 2014
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    %  getParameters
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    
    function setParameters(config)
      global parameters;
      
    %  parameters.grating_distance = getConfigValue(config, "grating_width");
      parameters.dim_x = int32(getConfigValue(config, "dim_x"));
      parameters.dim_y = int32(getConfigValue(config, "dim_y"));
      parameters.free_space_loss = getConfigValue(config, "free_space_loss");
      parameters.gap_width = getConfigValue(config, "gap_width");
      parameters.grating_count = int32(getConfigValue(config, "grating_count"));
      parameters.grating_distance = getConfigValue(config, "grating_distance");
      parameters.offset_x = int32(getConfigValue(config, "offset_x"));
      parameters.scaling_meter_per_px = getConfigValue(config, "scaling_meter_per_px");
      parameters.wave_length = getConfigValue(config, "wave_length");
    endfunction
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    %  findGapPositions
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    
    function gaps = findGapPositions(grating_distance, grating_count)
      if (grating_count < 2)
        error("grating_count too small");
      endif
      
      width = grating_distance * (double(grating_count)-1);
      gaps = [];
      for i = 0:(grating_count-1)
        gaps = [gaps (i*grating_distance-width/2)];
      endfor
    endfunction
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    %  gratingSignalStrength
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    
    function amplitude = gratingSignalStrength(x, y, gaps, wave_length, free_space_loss)
    % grating {gaps, sub_steps, gap_width }
    % free_space_loss not implemented
    
      % calculate distances
      distances = [];
      for gap_pos = gaps
        distances = [ distances sqrt((y-gap_pos)^2 + x^2) ];
      endfor
      
      % calculate amplitudes
      c_amps = [];
      base_distance = sqrt(x^2 + y^2);
      
      for d = distances
    %    delay = d - base_distance;
    %    phase = 2. * pi * delay/wave_length;
    
        % amplitude phasor = c_amp
        residual = rem(d, wave_length);
        phase = 2. * pi * residual/wave_length;
        c_amp = complex(cos(phase), sin(phase));
        c_amp = c_amp / d; % free_space_loss ?? check - Integrate a slit!
    
        c_amps = [c_amps c_amp];
      endfor
      
      % calculate amplitude
      amplitude = abs(sum(c_amps))/length(gaps);
    
      % eliminate free_space_loss
      max_sum = sum(abs(c_amps))/length(gaps);
      amplitude = amplitude/max_sum;
      
    endfunction
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    %  slitSignalStrength
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    
    function amplitude = slitSignalStrength(x, y, sub_steps, gap_width, wave_length, free_space_loss)
    
    endfunction
    
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    %
    %  calculateDiffraction
    %
    %%%%%%%%%%%%%%%%%%%%%%%%%%
    
    function amplitudes = calculateDiffraction(gaps )
      printf("Calculate Diffraction\n");
    
    
      global parameters;
      
      % Read globals
      dim_y = parameters.dim_y;
      dim_x = parameters.dim_x;
      scaling_meter_per_px = parameters.scaling_meter_per_px;
      offset_x = parameters.offset_x;
      wave_length = parameters.wave_length;
      free_space_loss = parameters.free_space_loss;
    
      % Calculate dimensions
      x_max = double(dim_x - offset_x - 1) * scaling_meter_per_px;
      y_width = double(dim_y - 1) * scaling_meter_per_px;
      y_max = y_width / 2.;
      y_min = -1. * y_max;
      
      % Initialize
      amplitudes = zeros(dim_y, dim_x - offset_x);
      
      
      for amp_pos_x = int32(1:(dim_x - offset_x))
        printf("%3d%%", 100 * amp_pos_x/(dim_x - offset_x));
        for amp_pos_y = int32(1:1:dim_y)
          x = double(amp_pos_x) * scaling_meter_per_px;
          y = y_min + double(amp_pos_y) * scaling_meter_per_px;
          
          amp = gratingSignalStrength(x, y, gaps, wave_length, free_space_loss );
          amplitudes(amp_pos_y, amp_pos_x) = amp; % row = y / column = x
        endfor
        printf("\b\b\b\b");
      endfor
      printf("Finished calculateDiffraction\n");
      
      
    
    endfunction
    
    %%%%%%%%%%%%%%%%%%%%
    %
    %  generateGrating
    %
    %%%%%%%%%%%%%%%%%%%%
    
    function rgb = generateGrating(rgb)
      printf("Generate Grating\n");
      global parameters;
      
      dim_y = parameters.dim_y;
      grating_count = parameters.grating_count;
      grating_distance = parameters.grating_distance;
      gap_width = parameters.gap_width;
      offset_x = parameters.offset_x;
      scaling_meter_per_px = parameters.scaling_meter_per_px;
    
    %//  gray = zeros(dim_y, offset_x);
    
      y_width = double(dim_y - 1) * scaling_meter_per_px;
      y_max = y_width / 2.;
      
      % even or odd 
      if ( round(rem(grating_count, 2)) != 1. )
        even_factor = 0.5; % grating_distance/2.;
      else
        even_factor = 0.;
      endif
      
      
      for y_px = int32(1:dim_y)
        y = double(y_px) * scaling_meter_per_px;
        stepper = y / grating_distance; % normalize
        sub_stepper = stepper + even_factor - round(stepper);
        % sub_stepper = rem(stepper,1);
        % stepper
        % odd: -0.5..+0.5
        % even: 0.0..1.0
        % Nachkommazahlen
      
      % sub_stepper +/- 0.5 * grating_distance around gap
        for x_px = offset_x-int32(double(offset_x)/40.):offset_x
          if(abs(sub_stepper) <= gap_width)
            rgb(y_px, x_px, :) = [0., 0., 0.];
          else
            rgb(y_px, x_px, :) = [0.8, 0.8, 0.8];
          endif
        endfor
      endfor
       
    endfunction
    
    
    %%%%%%%%%%%%%%%%%%%
    %
    %  generateLaser
    %
    %%%%%%%%%%%%%%%%%%%
    
    function amplitudes = generateLaser()
      printf("Generate Laser\n");
      global parameters;
    
      dim_y = parameters.dim_y;
      grating_count = parameters.grating_count;
      grating_distance = parameters.grating_distance;
      offset_x = parameters.offset_x;
      scaling_meter_per_px = parameters.scaling_meter_per_px;
      
      y_width = double(dim_y - 1) * scaling_meter_per_px;
      y_min = -1. * y_width / 2.; 
      
      laser_width = grating_distance * double(grating_count)
      
      amplitudes = [];
     
      for y_px = int32(1:dim_y)
        y = y_min + double(y_px) * scaling_meter_per_px;
        
        if (abs(y) <= laser_width / 2.) 
          amplitudes = [amplitudes; ones(1,offset_x)];
        else
          amplitudes = [amplitudes; zeros(1,offset_x)];
        endif
      endfor
      
    endfunction
    
    %%%%%%%%%%%%%
    %           %
    %  M A I N  %
    %           %
    %%%%%%%%%%%%%
    
    global parameters
    
    begin_cputime = cputime();
    
    
    printf("Including CSV library\n");
    echo_csv_lib = 0;
    source ("./lib/csv_lib.m");
    printf("Including config library\n");
    global echo_config_lib = 0;
    source ("./lib/load_config_lib.m");
    
    printf("Starting\n");
    
    
    arg_list = argv ();
    
    if (rows(arg_list) != 2);
      printf("wrong parameters, config file and output file needed.\n");
      exit();
    endif
    
    config_filename = arg_list{1};
    output_filename = arg_list{2};
    
    config = loadConfig(config_filename);
    
    % config
    
    setParameters(config);
    
    gaps = findGapPositions(parameters.grating_distance, parameters.grating_count);
    
    gaps
    
    % dim
    % step
    % wavelength
    %
    
    printf("Dimensions:\n");
    
    % Debug
    %x_width = parameters.scaling_meter_per_px * double(parameters.dim_x)
    %y_width = parameters.scaling_meter_per_px * double(parameters.dim_y)
    %amp = gratingSignalStrength(x_width, 0, gaps, parameters.wave_length, parameters.free_space_loss );
    %amp
    
    diffractionMap = calculateDiffraction(gaps);
    max(max(diffractionMap))
    diffractionPowerMap = diffractionMap.^2;
    
    laserMap = generateLaser();
    laserPowerMap = laserMap.^2;
    
    
    map = jet();
    
    diffractionInd = gray2ind(diffractionPowerMap);
    diffractionRgb = ind2rgb (diffractionInd, map);
    
    laserInd = gray2ind(laserPowerMap);
    laserRgb = ind2rgb (laserInd, map);
    
    
    
    offsetRgb = generateGrating(laserRgb);
    
    finalRgb = [offsetRgb diffractionRgb];
    %finalRgb = [offsetRgb]; % debug
    
    
    
    imwrite(finalRgb, output_filename, "png");
    
    
    printf("Total cpu time: %f seconds\n", cputime()-begin_cputime);
    printf("Finished\n");
    

    Lib/csv_lib.m code

    %
    % GNU Octave library to write results to a csv file
    %
    
    1; % not a function file
    
    
    %
    % Add header to CSV file
    %
    
    function csvFile = addCsvFileHeader(csvFile, header)
      if (isfield(csvFile, "headerList") == true)
        csvFile.headerList = [csvFile.headerList; header];
      else
        csvFile.headerList = [header];
      endif
    endfunction
    
    %
    % Add record to a structure CsvLine based on existing header
    %
    
    function csvLine = addCsvLineRecord(csvLine, header, value)
    
      csvLine.(header) = value; 
    
    endfunction
    
    %
    % Close CSV file
    %
    
    function csvFile = closeCsvFile(csvFile)
    
      fclose(csvFile.fid);
    
      csvFile.isOpened = false;
    endfunction
    
    %
    % Create a CSV line for this file
    %
    
    function csvLine = createCsvFileLine(csvFile)
    
      csvLine.headerList = csvFile.headerList;
      for header = csvLine.headerList'
        csvLine.(strtrim(header')) = "";
      endfor
    
    endfunction
    
    %
    % Output values of a csv line
    %
    
    function csvLine = printCsvLine(csvLine)
    
      csvLine
    
    endfunction
    
    %
    % Open CSV file
    %
    
    function csvFile = openCsvFile(filename)
      csvFile.filename = filename;
      
      % test if already exists
      
      csvFile.fid = fopen(csvFile.filename, "w");
    
      csvFile.isOpened = true;
    
    endfunction
    
    %
    %
    %
    
    function csvLine = writeCsvFileHeader(csvFile)
      line = "";
      for header = csvFile.headerList'
        line = [line, strtrim(header')];
        line = [line, ","];
      endfor
    
      line = substr(line, 1, length(line)-1);
      fprintf(csvFile.fid, "%s\r\n", line);
    
    endfunction
    
    %
    %
    %
    
    function csvLine = writeCsvLine(csvFile, csvLine)
      line = "";
      for header = csvLine.headerList'
        line = [line, csvLine.(strtrim(header'))];
        line = [line, ","];  
      endfor
      line = substr(line, 1, length(line)-1); % remove last comma
      fprintf(csvFile.fid, "%s\r\n", line);
    endfunction
    

    Lib/load_config_lib.m code

    %
    % GNU Octave library to load values from a config file
    %
    
    if(exist("echo_config_lib", "var") == 0)
      global echo_config_lib = 1;
    endif
    
    
    
    %
    %
    %
    
    function value = getConfigValue(config, var_name)
      if (isfield(config, var_name) == false)
        printf("Error: \"%s\" missing in config file\n", var_name); 
        exit();
      endif
      [value, success] = str2num(config.(var_name));
      if(success != true)
        printf("Error: Converting \"%s\" failed!\n", var_name); 
        exit();
      endif
    endfunction
    
    
    %
    %
    %
    
    function value = getConfigInt32(config, var_name)
      if (isfield(config, var_name) == false)
        printf("Error: \"%s\" missing in config file\n", var_name); 
        exit();
      endif
      [value, success] = str2double(config.(var_name));
      if(success != true)
        printf("Error: Converting \"%s\" failed!\n", var_name); 
        exit();
      endif
    endfunction
    
    
    %
    %
    %
    
    function value = getConfigString(config, var_name)
      if (isfield(config, var_name) == false)
        printf("Error: \"%s\" missing in config file\n", var_name); 
        exit();
      endif
      value = config.(var_name);
    endfunction
    
    
    %
    %
    %
    
    function arrayKeyValuePair = loadConfig(filename)
      % open file
      % while has next line
      % skip if blank
      % skip if comment
      % add key value pair to array
      
      global echo_config_lib;
      
      file = fopen (filename, "r");
      line = fgetl(file);
      lineNum = 1;
      while (ischar(line));
        if (echo_config_lib != 0)
          printf("Line %i: %s\n",lineNum, line);
        endif
        if (strcmp(line, "") == 0);
          if (strcmp(substr(line,1,1), ";") == 0);
            if(length(findstr(line, "="))== 0)
              printf("Error: Invalid line!\n");
              exit();
            endif
            key = substr(line, 1, findstr(line, "=")(1)-1);
            key = strtrim(key);
            value = substr(line
                , findstr(line, "=")(1)+1
                , length(line)-findstr(line, "=")(1) );
            value = strtrim(value);
            printf("key = %s\n", key);
            printf("value = %s\n", value);
            if(strcmp(key, "") != 0)
              printf("Error: Invalid key!\n");
              exit();
            endif
            if(isfield("arrayKeyValuePair", "key") != 0)
              printf("Error: Duplicate key!\n");
              exit();
            endif
          arrayKeyValuePair.(key) = value;
          else
            if (echo_config_lib != 0);
              printf("Comment\n");
            endif
          endif      
        else
          if (echo_config_lib != 0);
            printf("Blank line\n");
          endif
        endif
        line = fgetl(file);
        lineNum += 1;
      endwhile
    
      fclose(file);
    endfunction
    
    
    
    % INI format
    % no white space at the beginning of a line
    % empty lines allowed
    % comment lines start with a semicolon
    % key an value are seperated by a "="
    % ascending white spaces are trimed from key
    % prepending and ascending white spaces are trimed from values
    % in case of duplicate values the first is valid
    % crlf prefered, see RFC
    %
    

    Licensing

    I, the copyright holder of this work, hereby publish it under the following license:
    Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
    The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

    Captions

    Add a one-line explanation of what this file represents

    Items portrayed in this file

    depicts

    15 March 2014

    284,696 byte

    2,700 pixel

    3,600 pixel

    image/png

    c7bdd3942ffbe401ceddc1d1c4f33b34ca27b28e

    File history

    Click on a date/time to view the file as it appeared at that time.

    Date/TimeThumbnailDimensionsUserComment
    current20:45, 26 September 2016Thumbnail for version as of 20:45, 26 September 20163,600 × 2,700 (278 KB)wikimediacommons>CmdrjamesonCompressed with pngout. Reduced by 100kB (26% decrease).

    There are no pages that use this file.