Quelltext 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
%
|