Extrapolate Values MATLAB 2D [duplicate] - matlab

I am doing a 2-D interpolation using interp2. For some data values, the
interp2 command returns NaN because one of the dimensions are outside
of the range defined by the vector of known values.
Its possible to extrapolate with the interp1 command. However, Is
there a way to do this for interp2?
Thanks
Here is the code in which I am using the interp2 command:
function [Cla] = AirfoilLiftCurveSlope(obj,AFdata,Rc,M)
% Input:
% AFdata: Airfoil coordinates.
% Rc: Local Reynolds number.
% M: Mach number for Prandtle Glauert compressibility correction.
% Output:
% Cla: 2 dimensional lift curve slopea applicable to linear region of lift polar.
load('ESDU84026a.mat');
xi = size(AFdata);
if mod(xi(1,1),2) == 0
%number is even
AFupper = flipud(AFdata(1:(xi(1,1)/2),:));
AFlower = AFdata(((xi(1,1)/2)+1):end,:);
else
%number is odd
AFupper = flipud(AFdata(1:floor((xi(1,1)/2)),:));
AFlower = AFdata((floor(xi(1,1)/2)+1):end,:);
end
t_c = Airfoil.calculateThickness(AFdata(:,2));
Y90 = ((interp1(AFupper(:,1),AFupper(:,2),0.9,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.9,'linear')))*100;
Y99 = ((interp1(AFupper(:,1),AFupper(:,2),0.99,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.99,'linear')))*100;
Phi_TE = (2 * atan( ( (Y90/2) - (Y99/2) )/9))*180/pi; % Degrees
Tan_Phi_Te = ( (Y90/2) - (Y99/2) )/9;
Cla_corr = interp2(Tan_Phi,Rc_cla,cla_ratio,Tan_Phi_Te,Rc,'linear');
beta =sqrt((1-M^2)); % Prandtle Glauert correction
Cla_theory = 2*pi + 4.7*t_c*(1+0.00375 * Phi_TE); % per rad
Cla = (1.05/beta) * Cla_corr * Cla_theory; % per rad
if isnan(Cla) == 1 %|| Cla > 2*pi
Cla = 2*pi;
end
end

Yes, there are two ways to get interp2 to return a meaningful value out of bounds according to the docs.
Use the 'spline' interpolation method. Unlike option #2, this will actually extrapolate the data based on the boundary conditions of the spline.
Specify a final extrapval parameter. This constant will be returned instead of NaN for all other interpolation methods.
Unfortunately, there does not appear to be a way to specify something like "nearest neighbor on the grid" or something like that. If the out-of bounds elements are close to the edges, perhaps you could just expand the input array. For example like this:
x = [x(1, 1), x(1, :), x(1, end); ...
x(:, 1), x, x(:, end); ...
x(end, 1), x(end, :), x(end, end)]

Hey please find my code for interp2 it just take max bound values;
function vq = Linear2dInterpWithClipExtrap(x,y,v,xq,yq);
vq = interp2(x,y,v,xq,yq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
if ~isempty(yq(idxMax));
vq(idxMax) = LinearInterpWithClipExtrap(y,v(:,idxVMax),yq(idxMax));
end
if ~ isempty(yq(idxMin))
vq(idxMin) = LinearInterpWithClipExtrap(y,v(:,idxVMin),yq(idxMin));
end
[YMax, idyVMax] = max(y);
[YMin, idyVMin] = min(y);
idyMax = yq > YMax;
idyMin = yq < YMin;
if ~isempty(xq(idyMax));
vq(idyMax) = LinearInterpWithClipExtrap(x,v(idyVMax,:),xq(idyMax));
end
if ~ isempty(xq(idyMin));
vq(idyMin) = LinearInterpWithClipExtrap(x,v(idyVMin,:),xq(idyMin));
end
function vq = LinearInterpWithClipExtrap(x,v,xq);
vq = interp1(x,v,xq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
vq(idxMax) = v(idxVMax);
vq(idxMin) = v(idxVMin
);

Related

How to find Orientation of axis of contour in matlab?

I want to find Orientation, MajorAxisLengthand MinorAxisLength of contour which is plotted with below code.
clear
[x1 , x2] = meshgrid(linspace(-10,10,100),linspace(-10,10,100));
mu = [1,3];
sigm = [2,0;0,2];
xx_size = length(mu);
tem_matrix = ones(size(x1));
x_mesh= cell(1,xx_size);
for i = 1 : xx_size
x_mesh{i} = tem_matrix * mu(i);
end
x_mesh= {x1,x2};
temp_mesh = [];
for i = 1 : xx_size
temp_mesh = [temp_mesh x_mesh{i}(:)];
end
Z = mvnpdf(temp_mesh,mu,sigm);
z_plat = reshape(Z,size(x1));
figure;contour(x1, x2, z_plat,3, 'LineWidth', 2,'color','m');
% regionprops(z_plat,'Centroid','Orientation','MajorAxisLength','MinorAxisLength');
In my opinion, I may have to use regionprops command but I don't know how to do this. I want to find direction of axis of contour and plot something like this
How can I do this task? Thanks very much for your help
Rather than trying to process the graphical output of contour, I would instead recommend using contourc to compute the ContourMatrix and then use the x/y points to estimate the major and minor axes lengths as well as the orientation (for this I used this file exchange submission)
That would look something like the following. Note that I have modified the inputs to contourc as the first two inputs should be the vector form and not the output of meshgrid.
% Compute the three contours for your data
contourmatrix = contourc(linspace(-10,10,100), linspace(-10,10,100), z_plat, 3);
% Create a "pointer" to keep track of where we are in the output
start = 1;
count = 1;
% Now loop through each contour
while start < size(contourmatrix, 2)
value = contourmatrix(1, start);
nPoints = contourmatrix(2, start);
contour_points = contourmatrix(:, start + (1:nPoints));
% Now fit an ellipse using the file exchange
ellipsedata(count) = fit_ellipse(contour_points(1,:), contour_points(2,:));
% Increment the start pointer
start = start + nPoints + 1;
count = count + 1;
end
orientations = [ellipsedata.phi];
% 0 0 0
major_length = [ellipsedata.long_axis];
% 4.7175 3.3380 2.1539
minor_length = [ellipsedata.short_axis];
% 4.7172 3.3378 2.1532
As you can see, the contours are actually basically circles and therefore the orientation is zero and the major and minor axis lengths are almost equal. The reason that they look like ellipses in your post is because your x and y axes are scaled differently. To fix this, you can call axis equal
figure;contour(x1, x2, z_plat,3, 'LineWidth', 2,'color','m');
axis equal
Thank you #Suever. It help me to do my idea.
I add some line to code:
clear
[X1 , X2] = meshgrid(linspace(-10,10,100),linspace(-10,10,100));
mu = [-1,0];
a = [3,2;1,4];
a = a * a';
sigm = a;
xx_size = length(mu);
tem_matrix = ones(size(X1));
x_mesh= cell(1,xx_size);
for i = 1 : xx_size
x_mesh{i} = tem_matrix * mu(i);
end
x_mesh= {X1,X2};
temp_mesh = [];
for i = 1 : xx_size
temp_mesh = [temp_mesh x_mesh{i}(:)];
end
Z = mvnpdf(temp_mesh,mu,sigm);
z_plat = reshape(Z,size(X1));
figure;contour(X1, X2, z_plat,3, 'LineWidth', 2,'color','m');
hold on;
% Compute the three contours for your data
contourmatrix = contourc(linspace(-10,10,100), linspace(-10,10,100), z_plat, 3);
% Create a "pointer" to keep track of where we are in the output
start = 1;
count = 1;
% Now loop through each contour
while start < size(contourmatrix, 2)
value = contourmatrix(1, start);
nPoints = contourmatrix(2, start);
contour_points = contourmatrix(:, start + (1:nPoints));
% Now fit an ellipse using the file exchange
ellipsedata(count) = fit_ellipse(contour_points(1,:), contour_points(2,:));
% Increment the start pointer
start = start + nPoints + 1;
count = count + 1;
end
orientations = [ellipsedata.phi];
major_length = [ellipsedata.long_axis];
minor_length = [ellipsedata.short_axis];
tet = orientations(1);
x1 = mu(1);
y1 = mu(2);
a = sin(tet) * sqrt(major_length(1));
b = cos(tet) * sqrt(major_length(1));
x2 = x1 + a;
y2 = y1 + b;
line([x1, x2], [y1, y2],'linewidth',2);
tet = ( pi/2 + orientations(1) );
a = sin(tet) * sqrt(minor_length(1));
b = cos(tet) * sqrt(minor_length(1));
x2 = x1 + a;
y2 = y1 + b;
line([x1, x2], [y1, y2],'linewidth',2);

Matlab interp2 extrapolation

I am doing a 2-D interpolation using interp2. For some data values, the
interp2 command returns NaN because one of the dimensions are outside
of the range defined by the vector of known values.
Its possible to extrapolate with the interp1 command. However, Is
there a way to do this for interp2?
Thanks
Here is the code in which I am using the interp2 command:
function [Cla] = AirfoilLiftCurveSlope(obj,AFdata,Rc,M)
% Input:
% AFdata: Airfoil coordinates.
% Rc: Local Reynolds number.
% M: Mach number for Prandtle Glauert compressibility correction.
% Output:
% Cla: 2 dimensional lift curve slopea applicable to linear region of lift polar.
load('ESDU84026a.mat');
xi = size(AFdata);
if mod(xi(1,1),2) == 0
%number is even
AFupper = flipud(AFdata(1:(xi(1,1)/2),:));
AFlower = AFdata(((xi(1,1)/2)+1):end,:);
else
%number is odd
AFupper = flipud(AFdata(1:floor((xi(1,1)/2)),:));
AFlower = AFdata((floor(xi(1,1)/2)+1):end,:);
end
t_c = Airfoil.calculateThickness(AFdata(:,2));
Y90 = ((interp1(AFupper(:,1),AFupper(:,2),0.9,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.9,'linear')))*100;
Y99 = ((interp1(AFupper(:,1),AFupper(:,2),0.99,'linear')) - (interp1(AFlower(:,1),AFlower(:,2),0.99,'linear')))*100;
Phi_TE = (2 * atan( ( (Y90/2) - (Y99/2) )/9))*180/pi; % Degrees
Tan_Phi_Te = ( (Y90/2) - (Y99/2) )/9;
Cla_corr = interp2(Tan_Phi,Rc_cla,cla_ratio,Tan_Phi_Te,Rc,'linear');
beta =sqrt((1-M^2)); % Prandtle Glauert correction
Cla_theory = 2*pi + 4.7*t_c*(1+0.00375 * Phi_TE); % per rad
Cla = (1.05/beta) * Cla_corr * Cla_theory; % per rad
if isnan(Cla) == 1 %|| Cla > 2*pi
Cla = 2*pi;
end
end
Yes, there are two ways to get interp2 to return a meaningful value out of bounds according to the docs.
Use the 'spline' interpolation method. Unlike option #2, this will actually extrapolate the data based on the boundary conditions of the spline.
Specify a final extrapval parameter. This constant will be returned instead of NaN for all other interpolation methods.
Unfortunately, there does not appear to be a way to specify something like "nearest neighbor on the grid" or something like that. If the out-of bounds elements are close to the edges, perhaps you could just expand the input array. For example like this:
x = [x(1, 1), x(1, :), x(1, end); ...
x(:, 1), x, x(:, end); ...
x(end, 1), x(end, :), x(end, end)]
Hey please find my code for interp2 it just take max bound values;
function vq = Linear2dInterpWithClipExtrap(x,y,v,xq,yq);
vq = interp2(x,y,v,xq,yq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
if ~isempty(yq(idxMax));
vq(idxMax) = LinearInterpWithClipExtrap(y,v(:,idxVMax),yq(idxMax));
end
if ~ isempty(yq(idxMin))
vq(idxMin) = LinearInterpWithClipExtrap(y,v(:,idxVMin),yq(idxMin));
end
[YMax, idyVMax] = max(y);
[YMin, idyVMin] = min(y);
idyMax = yq > YMax;
idyMin = yq < YMin;
if ~isempty(xq(idyMax));
vq(idyMax) = LinearInterpWithClipExtrap(x,v(idyVMax,:),xq(idyMax));
end
if ~ isempty(xq(idyMin));
vq(idyMin) = LinearInterpWithClipExtrap(x,v(idyVMin,:),xq(idyMin));
end
function vq = LinearInterpWithClipExtrap(x,v,xq);
vq = interp1(x,v,xq);
[XMax, idxVMax] = max(x);
[XMin, idxVMin] = min(x);
idxMax = xq > XMax;
idxMin = xq < XMin;
vq(idxMax) = v(idxVMax);
vq(idxMin) = v(idxVMin
);

Octave: Kmeans clustering not working on an image matrix

I have tried the following code.
img=imread("test1.jpg");
gimg=rgb2gray(img);
imshow(gimg);
bw = gimg < 255;
L = bwlabel(bw);
imshow(label2rgb(L, #jet, [.7 .7 .7]))
s = regionprops(L, 'PixelIdxList', 'PixelList');
s(1).PixelList(1:4, :)
idx = s(1).PixelIdxList;
sum_region1 = sum(gimg(idx));
x = s(1).PixelList(:, 1);
y = s(1).PixelList(:, 2);
xbar = sum(x .* double(gimg(idx))) / sum_region1
ybar = sum(y .* double(gimg(idx))) / sum_region1
hold on
for k = 1:numel(s)
idx = s(k).PixelIdxList;
pixel_values = double(gimg(idx));
sum_pixel_values = sum(pixel_values);
x = s(k).PixelList(:, 1);
y = s(k).PixelList(:, 2);
xbar = sum(x .* pixel_values) / sum_pixel_values;
ybar = sum(y .* pixel_values) / sum_pixel_values;
plot(xbar, ybar, '*')
end
hold off
a=round(xbar)-90;
b=round(xbar)+90;
c=round(ybar)-90;
d=round(ybar)+90;
roi=gimg(a:b,c:d);
imshow(roi);
roi(:,:,2)=0;
roi(:,:,3)=0;
se = strel('cube',20);
closeBW = imclose(roi,se);
figure
imshow(closeBW);
de=rgb2gray(closeBW);
ed=edge(de,"canny");
imshow(ed);
j=kmeans(ed,3);
What i did was take an image and extracted its grayscale.I concentrated on the part of image which has very high intensity.I then took the red component of the image and then applied closing operation on the resultant image.After that I applied edge detection using canny method.Then I tried to use kmeans on result of edge detection.
I get an error saying kmeans requires real matrix.
help would appreciated.
edge in MATLAB / Octave returns a binary / logical matrix. kmeans requires that the input be a double or single matrix.
Therefore, simply cast ed to double and continue:
ed=edge(de,"canny");
imshow(ed);
ed = double(ed); %// Change
j=kmeans(ed,3);

ode45 solving of diff.equation with further fitting to exp.results

I am building a code to solve a diff. equation:
function dy = KIN1PARM(t,y,k)
%
% version : first order reaction
% A --> B
% dA/dt = -k*A
% integrated form A = A0*exp(-k*t)
%
dy = -k.*y;
end
I want this equation to be solved numerically and the results (y as a function of t, and k) to be used for minimization with respect to the experimental values to get the optimal value of parameter k.
function SSE = SSE_minimization_1parm(tspan_inp,val_exp,k_inp,y0_inp)
f = #(Tt,Ty) KIN1PARM(Tt,Ty,k_inp); %function to call ode45
size_limit = length(y0_inp);
options = odeset('NonNegative',1:size_limit,'RelTol',1e-4,'AbsTol', 1e-4);
[ts,val_theo] = ode45(f, tspan_inp, y0_inp,options); %Cexp is the state variable predicted by the model
err = val_exp - val_theo;
SSE = sum(err.^2); %sum squared-error
The main code to plot the experimental and calculated data is:
% Analyzing first order kinetics
clear all; clc;
figure_title = 'Experimental Data';
label_abscissa = 'Time [s]';
label_ordinatus = 'Concentration [mol/L]';
%
abscissa = [ 0;
240;
480;
720;
960;
1140;
1380;
1620;
1800;
2040;
2220;
2460;
2700;
2940];
ordinatus = [ 0;
19.6;
36.7;
49.0;
57.1;
64.5;
71.4;
75.2;
78.7;
81.3;
83.3;
85.5;
87.0;
87.7];
%
title_string = [' Time [s]', ' | ', ' Complex [mol/L] ', ' '];
disp(title_string);
for i=1:length(abscissa)
report_raw_data{i} = sprintf('%1.3E\t',abscissa(i),ordinatus(i));
disp([report_raw_data{i}]);
end;
%---------------------/plotting dot data/-------------------------------------
%
f = figure('Position', [100 100 700 500]);
title(figure_title,'FontName','arial','FontWeight','bold', 'FontSize', 12);
xlabel(label_abscissa, 'FontSize', 12);
ylabel(label_ordinatus, 'FontSize', 12);
%
grid on; hold on;
%
marker_style = { 's'};
%
plot(abscissa,ordinatus, marker_style{1},...
'MarkerFaceColor', 'black',...
'MarkerEdgeColor', 'black',...
'MarkerSize',4);
%---------------------/Analyzing/----------------------------------------
%
options = optimset('Display','iter','TolFun',1e-4,'TolX',1e-4);
%
CPUtime0 = cputime;
Time_M = abscissa;
Concentration_M = ordinatus;
tspan = Time_M;
y0 = 0;
k0 = rand(1);
[k, fval, exitflag, output] = fminsearch(#(k) SSE_minimization_1parm(tspan,Concentration_M,k,y0),k0,options);
CPUtimex = cputime;
CPUtime_delay = CPUtimex - CPUtime0;
%
%---------------------/plotting calculated data/-------------------------------------
%
xupperlimit = Time_M(length(Time_M));
xval = ([0:1:xupperlimit])';
%
yvector = data4plot_1parm(xval,k,y0);
plot(xval,yvector, 'r');
hold on;
%---------------------/printing calculated data/-------------------------------------
%
disp('RESULTS:');
disp(['CPU time: ',sprintf('%0.5f\t',CPUtime_delay),' sec']);
disp(['k: ',sprintf('%1.3E\t',k')]);
disp(['fval: ',sprintf('%1.3E\t',fval)]);
disp(['exitflag: ',sprintf('%1.3E\t',exitflag)]);
disp(output);
disp(['Output: ',output.message]);
The corresponding function, which uses the optimized parameter k to yield the calculated y = f(t) data :
function val = data4plot_1parm(tspan_inp,k_inp,y0_inp)
f = #(Tt,Ty) KIN1PARM(Tt,Ty,k_inp);
size_limit = length(y0_inp);
options = odeset('NonNegative',1:size_limit,'RelTol',1e-4,'AbsTol',1e-4);
[ts,val_theo] = ode45(f, tspan_inp, y0_inp, options);
The code runs optimization cycles always giving different values of parameter k, which are different from the value calculated using ln(y) vs t (should be around 7.0e-4 for that series of exp. data).
Looking at the outcome of the ode solver (SSE_minimization_1parm => val_theo) I found that the ode function gives me a vector of zeroes.
Could someone help me , please, to figure out what's going with the ode solver ?
Thanks much in advance !
So here comes the best which I can get right now. For my way I tread ordinatus values as time and the abscissa values as measured quantity which you try to model. Also, you seem to have set alot of options for the solver, which I all omitted. First comes your proposed solution using ode45(), but with a non-zero y0 = 100, which I just "guessed" from looking at the data (in a semilogarithmic plot).
function main
abscissa = [0;
240;
480;
720;
960;
1140;
1380;
1620;
1800;
2040;
2220;
2460;
2700;
2940];
ordinatus = [ 0;
19.6;
36.7;
49.0;
57.1;
64.5;
71.4;
75.2;
78.7;
81.3;
83.3;
85.5;
87.0;
87.7];
tspan = [min(ordinatus), max(ordinatus)]; % // assuming ordinatus is time
y0 = 100; % // <---- Probably the most important parameter to guess
k0 = -0.1; % // <--- second most important parameter to guess (negative for growth)
k_opt = fminsearch(#minimize, k0) % // optimization only over k
% nested minimization function
function e = minimize(k)
sol = ode45(#KIN1PARM, tspan, y0, [], k);
y_hat = deval(sol, ordinatus); % // evaluate solution at given times
e = sum((y_hat' - abscissa).^2); % // compute squarederror
end
% // plot with optimal parameter
[T,Y] = ode45(#KIN1PARM, tspan, y0, [], k_opt);
figure
plot(ordinatus, abscissa,'ko', 'markersize',10,'markerfacecolor','black')
hold on
plot(T,Y, 'r--', 'linewidth', 2)
% // Another attempt with fminsearch and the integral form
t = ordinatus;
t_fit = linspace(min(ordinatus), max(ordinatus))
y = abscissa;
% create model function with parameters A0 = p(1) and k = p(2)
model = #(p, t) p(1)*exp(-p(2)*t);
e = #(p) sum((y - model(p, t)).^2); % minimize squared errors
p0 = [100, -0.1]; % an initial guess (positive A0 and probably negative k for exp. growth)
p_fit = fminsearch(e, p0); % Optimize
% Add to plot
plot(t_fit, model(p_fit, t_fit), 'b-', 'linewidth', 2)
legend('location', 'best', 'data', 'ode45 with fixed y0', ...
sprintf ('integral form: %5.1f*exp(-%.4f)', p_fit))
end
function dy = KIN1PARM(t,y,k)
%
% version : first order reaction
% A --> B
% dA/dt = -k*A
% integrated form A = A0*exp(-k*t)
%
dy = -k.*y;
end
The result can be seen below. Quit surprisingly to me, the initial guess of y0 = 100 fits quite well with the optimal A0 found. The result can be seen below:

spiral meshgrid in matlab

I'm trying to produce some computer generated holograms by using MATLAB. I used equally spaced mesh grid to initialize the spatial grid, and I got the following image
This pattern is sort of what I need except the center region. The fringe should be sharp but blurred. I think it might be the problem of the mesh grid. I tried generate a grid in polar coordinates and the map it into Cartesian coordinates by using MATLAB's pol2cart function. Unfortunately, it doesn't work as well. One may suggest that using fine grids. It doesn't work too. I think if I can generate a spiral mesh grid, perhaps the problem is solvable. In addition, the number of the spiral arms could, in general, be arbitrary, could anyone give me a hint on this?
I've attached the code (My final projects are not exactly the same, but it has a similar problem).
clc; clear all; close all;
%% initialization
tic
lambda = 1.55e-6;
k0 = 2*pi/lambda;
c0 = 3e8;
eta0 = 377;
scale = 0.25e-6;
NELEMENTS = 1600;
GoldenRatio = (1+sqrt(5))/2;
g = 2*pi*(1-1/GoldenRatio);
pntsrc = zeros(NELEMENTS, 3);
phisrc = zeros(NELEMENTS, 1);
for idxe = 1:NELEMENTS
pntsrc(idxe, :) = scale*sqrt(idxe)*[cos(idxe*g), sin(idxe*g), 0];
phisrc(idxe) = angle(-sin(idxe*g)+1i*cos(idxe*g));
end
phisrc = 3*phisrc/2; % 3 arms (topological charge ell=3)
%% post processing
sigma = 1;
polfilter = [0, 0, 1i*sigma; 0, 0, -1; -1i*sigma, 1, 0]; % cp filter
xboundl = -100e-6; xboundu = 100e-6;
yboundl = -100e-6; yboundu = 100e-6;
xf = linspace(xboundl, xboundu, 100);
yf = linspace(yboundl, yboundu, 100);
zf = -400e-6;
[pntobsx, pntobsy] = meshgrid(xf, yf);
% how to generate a right mesh grid such that we can generate a decent result?
pntobs = [pntobsx(:), pntobsy(:), zf*ones(size(pntobsx(:)))];
% arbitrary mesh may result in "wrong" results
NPNTOBS = size(pntobs, 1);
nxp = length(xf);
nyp = length(yf);
%% observation
Eobs = zeros(NPNTOBS, 3);
matlabpool open local 12
parfor nobs = 1:NPNTOBS
rp = pntobs(nobs, :);
Erad = [0; 0; 0];
for idx = 1:NELEMENTS
rs = pntsrc(idx, :);
p = exp(sigma*1i*2*phisrc(idx))*[1 -sigma*1i 0]/2; % simplified here
u = rp - rs;
r = sqrt(u(1)^2+u(2)^2+u(3)^2); %norm(u);
u = u/r; % unit vector
ut = [u(2)*p(3)-u(3)*p(2),...
u(3)*p(1)-u(1)*p(3), ...
u(1)*p(2)-u(2)*p(1)]; % cross product: u cross p
Erad = Erad + ... % u cross p cross u, do not use the built-in func
c0*k0^2/4/pi*exp(1i*k0*r)/r*eta0*...
[ut(2)*u(3)-ut(3)*u(2);...
ut(3)*u(1)-ut(1)*u(3); ...
ut(1)*u(2)-ut(2)*u(1)];
end
Eobs(nobs, :) = Erad; % filter neglected here
end
matlabpool close
Eobs = Eobs/max(max(sum(abs(Eobs), 2))); % normailized
%% source, gaussian beam
E0 = 1;
w0 = 80e-6;
theta = 0; % may be titled
RotateX = [1, 0, 0; ...
0, cosd(theta), -sind(theta); ...
0, sind(theta), cosd(theta)];
Esrc = zeros(NPNTOBS, 3);
for nobs = 1:NPNTOBS
rp = RotateX*[pntobs(nobs, 1:2).'; 0];
z = rp(3);
r = sqrt(sum(abs(rp(1:2)).^2));
zR = pi*w0^2/lambda;
wz = w0*sqrt(1+z^2/zR^2);
Rz = z^2+zR^2;
zetaz = atan(z/zR);
gaussian = E0*w0/wz*exp(-r^2/wz^2-1i*k0*z-1i*k0*0*r^2/Rz/2+1i*zetaz);% ...
Esrc(nobs, :) = (polfilter*gaussian*[1; -1i; 0]).'/sqrt(2)/2;
end
Esrc = [Esrc(:, 2), Esrc(:, 3), Esrc(:, 1)];
Esrc = Esrc/max(max(sum(abs(Esrc), 2))); % normailized
toc
%% visualization
fringe = Eobs + Esrc; % I'll have a different formula in my code
normEsrc = reshape(sum(abs(Esrc).^2, 2), [nyp nxp]);
normEobs = reshape(sum(abs(Eobs).^2, 2), [nyp nxp]);
normFringe = reshape(sum(abs(fringe).^2, 2), [nyp nxp]);
close all;
xf0 = linspace(xboundl, xboundu, 500);
yf0 = linspace(yboundl, yboundu, 500);
[xfi, yfi] = meshgrid(xf0, yf0);
data = interp2(xf, yf, normFringe, xfi, yfi);
figure; surf(xfi, yfi, data,'edgecolor','none');
% tri = delaunay(xfi, yfi); trisurf(tri, xfi, yfi, data, 'edgecolor','none');
xlim([xboundl, xboundu])
ylim([yboundl, yboundu])
% colorbar
view(0,90)
colormap(hot)
axis equal
axis off
title('fringe thereo. ', ...
'fontsize', 18)
I didn't read your code because it is too long to do such a simple thing. I wrote mine and here is the result:
the code is
%spiral.m
function val = spiral(x,y)
r = sqrt( x*x + y*y);
a = atan2(y,x)*2+r;
x = r*cos(a);
y = r*sin(a);
val = exp(-x*x*y*y);
val = 1/(1+exp(-1000*(val)));
endfunction
%show.m
n=300;
l = 7;
A = zeros(n);
for i=1:n
for j=1:n
A(i,j) = spiral( 2*(i/n-0.5)*l,2*(j/n-0.5)*l);
end
end
imshow(A) %don't know if imshow is in matlab. I used octave.
the key for the sharpnes is line
val = 1/(1+exp(-1000*(val)));
It is logistic function. The number 1000 defines how sharp your image will be. So lower it for more blurry image or higher it for sharper.
I hope this answers your question ;)
Edit: It is real fun to play with. Here is another spiral:
function val = spiral(x,y)
s= 0.5;
r = sqrt( x*x + y*y);
a = atan2(y,x)*2+r*r*r;
x = r*cos(a);
y = r*sin(a);
val = 0;
if (abs(x)<s )
val = s-abs(x);
endif
if(abs(y)<s)
val =max(s-abs(y),val);
endif
%val = 1/(1+exp(-1*(val)));
endfunction
Edit2: Fun, fun, fun! Here the arms do not get thinner.
function val = spiral(x,y)
s= 0.1;
r = sqrt( x*x + y*y);
a = atan2(y,x)*2+r*r; % h
x = r*cos(a);
y = r*sin(a);
val = 0;
s = s*exp(r);
if (abs(x)<s )
val = s-abs(x);
endif
if(abs(y)<s)
val =max(s-abs(y),val);
endif
val = val/s;
val = 1/(1+exp(-10*(val)));
endfunction
Damn your question I really need to study for my exam, arghhh!
Edit3:
I vectorised the code and it runs much faster.
%spiral.m
function val = spiral(x,y)
s= 2;
r = sqrt( x.*x + y.*y);
a = atan2(y,x)*8+exp(r);
x = r.*cos(a);
y = r.*sin(a);
val = 0;
s = s.*exp(-0.1*r);
val = r;
val = (abs(x)<s ).*(s-abs(x));
val = val./s;
% val = 1./(1.+exp(-1*(val)));
endfunction
%show.m
n=1000;
l = 3;
A = zeros(n);
[X,Y] = meshgrid(-l:2*l/n:l);
A = spiral(X,Y);
imshow(A)
Sorry, can't post figures. But this might help. I wrote it for experiments with amplitude spatial modulators...
R=70; % radius of curvature of fresnel lens (in pixel units)
A=0; % oblique incidence by linear grating (1=oblique 0=collinear)
B=1; % expanding by fresnel lens (1=yes 0=no)
L=7; % topological charge
Lambda=30; % linear grating fringe spacing (in pixels)
aspect=1/2; % fraction of fringe period that is white/clear
xsize=1024; % resolution (xres x yres number data pts calculated)
ysize=768; %
% define the X and Y ranges (defined to skip zero)
xvec = linspace(-xsize/2, xsize/2, xsize); % list of x values
yvec = linspace(-ysize/2, ysize/2, ysize); % list of y values
% define the meshes - matrices linear in one dimension
[xmesh, ymesh] = meshgrid(xvec, yvec);
% calculate the individual phase components
vortexPh = atan2(ymesh,xmesh); % the vortex phase
linPh = -2*pi*ymesh; % a phase of linear grating
radialPh = (xmesh.^2+ymesh.^2); % a phase of defocus
% combine the phases with appropriate scales (phases are additive)
% the 'pi' at the end causes inversion of the pattern
Ph = L*vortexPh + A*linPh/Lambda + B*radialPh/R^2;
% transmittance function (the real part of exp(I*Ph))
T = cos(Ph);
% the binary version
binT = T > cos(pi*aspect);
% plot the pattern
% imagesc(binT)
imagesc(T)
colormap(gray)