Diffraction using fourier transform in Matlab - matlab

I am very new to Matlab and am trying to implement the following fresnel diffraction using the fourier transform:
This is taken from the following wikipedia page: http://en.wikipedia.org/wiki/Fresnel_diffraction
I am trying a square aperture of width 5 cm.
clc;clear;
lambda=1*10^-6;
w=.05;
z=2.0;
k=(2*pi)/lambda;
x1=linspace(-0.2,0.2,2048);
y1=linspace(-0.2,0.2,2048);
U1=((abs(x1)<=(w)/2))&((abs(y1)<=(w)/2));
u1=double(U1);
figure(1)
plot(x1,u1)
g=u1'*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
G=fftshift(fft2(g));
h=(exp(1i*k*z)/(1i*lambda*z))*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
H=fftshift(fft2(h));
u2=H*G;
figure(2)
plot(x1,abs(u2));
When I plot the field u2, for any of the distances, z, that I try, it just shows up like some random pattern and not the expected diffraction pattern for a square aperture.
Like I said, I am very new to MATLAB and find it difficult to understand. I think I am making this more complicated than it needs to be, and am implementing the integrals incorrectly.
Any pointers or advice? I am quite stuck...
Thanks!

Looks like you want to simulate Fresnel diffraction with this code.
With a few changes you can view the actual image. Shown here is the amplitude. The intensity would be u2.*conj(u2).
Note however that critical sampling will occur much further away than 2 meters, more like 100 meters, for the physical parameters you are using.
Also there needs to be some additional re-scaling on the output plane, not done here. Otherwise the diffraction pattern will get very small for low Fresnel numbers.
clc;clear;
N=1048;
L=0.4;
lambda=1*10^-6;
w=.05;
z=500.0;
k=(2*pi)/lambda;
src_delta = L/N;
critical_sampling_z = N*src_delta^2/lambda;
[x1 y1] = meshgrid((-N/2 : N/2-1) * src_delta);
X = double(abs(x1)<w/2); Y = double(abs(y1)<w/2);
u1 = X.*Y;
figure(1);
colormap(gray); imagesc(mat2gray(u1));
g=u1'*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
G=fftshift(fft2(g));
h=(exp(1i*k*z)/(1i*lambda*z))*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
H=fftshift(fft2(h));
u2=H*G;
figure(2);
colormap(gray); imagesc(mat2gray(abs(u2)));

Related

Fourier transform of normal density function

I am using the following MATLAB code to perform a Fourier transformation of a normal density function:
N=100;
j=0:(N-1);
a=-5;
b=5;
dx = (b-a)/N;
x = a+j*dx;
dt = 2*pi/(N*dx);
f1 = -N/2*dt;
f2 = N/2*dt;
t= f1+ j*dt;
GX = normpdf(x,0,1);
fft_GX = real(fft(GX))';
However, I do not get the expected bell shaped curve when I try to plot fft_GX.
The Fourier transformation of a normal density has the form of e^(-t^2/2). Can someone please help as to what I am doing incorrect?
Trying using abs instead of real.
Another helpful function to recenter the frequency domain is fftshift. Otherwise you will see the plot from 0 to 2*pi I believe, instead of the more recognizable view from -pi to pi.
fft_GX = abs(fftshift((fft(GX))');
plot(fft_GX);
You may need to do some further normalization based on the number of samples you have, but it looks more like the expected bell curve than what you were seeing originally.

Shouldn't fitdist(data,'Lognormal') give the same result and plot as fitdist(log(data),'Normal') in Matlab?

I am trying to fit a distribution curve to the histogram of some data. (I have used some model data here instead because it is difficult to upload the actual data. I have included the complete code after my question.)
Because the histogram looks normally distributed when I plotted the x-axis in logscale, I transform the data first before fitting a normal distribution to it and I got the following results:
>>pdn=fitdist(log(data),'Normal')
pdn =
Normal distribution
mu = -0.334458 [-0.34704, -0.321876]
sigma = 0.351478 [0.342804, 0.360605]
When I plotted out the pdf with the histogram, I got this:
The result seems reasonable to me. Then I discovered that in the Matlab fitdist(), it already has a 'Lognormal' option and I don't really need the transform my data first and this is what I got:
>>pdln = fitdist(data,'Lognormal')
pdln =
Lognormal distribution
mu = -0.334458 [-0.34704, -0.321876]
sigma = 0.351478 [0.342804, 0.360605]
Exactly the same mean and standard deviation as I have got before. However, when I plotted it out with the histogram, I got a different curve:
This curve fits better to the data but the positions of the mean and the mean+/-std points are not as I have expected (i.e. mean at the peak and the mean+/-std at the same levels).
Which come to my question, why would fitdist(data,'Lognormal') give the same result as fitdist(log(data),'Normal') but a different plot? I have looked through the Matlab help pages and I still could not understand why, or where are my mistakes, please help.
My aim for all this is to get some numerical parameters about the distributions of my data under different conditions and compare them to see if there is any difference. At the moment, I am not certain which way would give me reliable estimates of the means and standard deviations.
The code for the graphs is below:
%random data in lognormal distribution
mu=-0.335742;
sigma=0.35228;
data=lognrnd(mu,sigma,[3000 1]);
%make histogram
interval=0.1;
svalue=sort(data);
bx(1)=interval/2;
i=2;
while bx(i-1)<=max(svalue)
bx(i)=bx(i-1)+interval;
i=i+1;
end
by=hist(svalue,bx);
subplot(211)
h = bar(bx,by,'hist');
set(h,'FaceColor',[.9 .9 .9]);
set(gca,'xlim',[0.05 10]);
xticks=[0.05 0.1 0.2 0.5 1 2 5 10];
set(gca,'xscale','log','xminortick','on')
set(gca,'xtick',xticks)
ylabel('counts')
subplot(212)
h = bar(bx,by,'hist');
set(h,'FaceColor',[.9 .9 .9]);
set(gca,'xlim',[0.05 10]);
xticks=[0.05 0.1 0.2 0.5 1 2 5 10];
set(gca,'xscale','log','xminortick','on')
set(gca,'xtick',xticks)
ylabel('counts')
% fit distribution curves
pdf_x = 0:0.01:max(data);
max_by=max(by); % for scaling the pdf to the histogram
% case 1 - PDF fitted using fitdist(log(data),'Normal')
subplot(211)
hold on
pdn = fitdist(log(data),'Normal')
pdf_y = pdf(pdn,log(pdf_x));
h1=plot(pdf_x,pdf_y./max(pdf_y).*max_by,'-k');
range=[exp(pdn.mu-pdn.sigma) exp(pdn.mu+pdn.sigma)];
h2=plot(exp(pdn.mu),pdf(pdn,(pdn.mu))./max(pdf_y).*max_by,'sk') ;
h3=plot(range,pdf(pdn,log(range))./max(pdf_y).*max_by,'ok') ;
title('PDF fitted using fitdist(log(data),''Normal'')');
legend([h1 h2 h3],'pdf','mean','meam+/-std');
% case 2 - PDF fitted using fitdist(data,'Lognormal')
subplot(212)
hold on
pdln = fitdist(data,'Lognormal')
pdf_y = pdf(pdln,pdf_x);
h1=plot(pdf_x,pdf_y./max(pdf_y).*max_by,'-b');
range=[exp(pdln.mu-pdln.sigma) exp(pdln.mu+pdln.sigma)];
h2=plot(exp(pdln.mu),pdf(pdln,exp(pdln.mu))./max(pdf_y).*max_by,'sb');
h3=plot(range,pdf(pdln,range)./max(pdf_y).*max_by,'ob') ;
title('PDF fitted using fitdist(data,''Lognormal'')');
legend([h1 h2 h3],'pdf','mean','meam+/-std');

Changing the length of x axis in MATLAB

I have the following code.
% Generate message signal
t1 = -1:0.1:1;
message_sig = rectpuls(t1);
L = length(message_sig);
figure(2)
stairs(0:L-1:L, 'linewidth',1.5);
axis([0 L -1 2]);
title('Message Signal');
When i run this, the length of my x axis is from 0 to 20.
How can i reduce it to say 0 to 8, while plotting the same bit pattern. Because when i try modulating and add noise, the whole plot (noise modulated signal) is blue and needs to be zoomed alot to see accurately.
So can someone help me with a code that could solve this.
simply use xlim([0,8]), that will restrict the x-axis from going beyond 8, or edit your axis call to be axis([0,8,-1,2])
UPDATE
Assuming you have the image processing toolbox, it is really simple
in = [0,1,0];
imresize(in,[1,8],'nearest');
This will take that pattern and expand it to whatever dimension you want.

Rotating Axes Around Line of Fit MATLAB

I'm currently frustrated by the following problem:
I've got trajectory data (i.e.: Longitude and Latitude data) which I interpolate to find a linear fitting (using polyfit and polyval in matlab).
What I'd like to do is to rotate the axes in a way that the x-axis (the Longitude one) ends up lying on the best-fit line, and therefore my data should now lie on this (rotated) axis.
What I've tried is to evaluate the rotation matrix from the slope of the line-of-fit (m in the formula for a first grade polynomial y=mx+q) as
[cos(m) -sin(m);sin(m) cos(m)]
and then multiply my original data by this matrix...to no avail!
I keep obtaining a plot where my data lay in the middle and not on the x-axis where I expect them to be.
What am I missing?
Thank you for any help!
Best Regards,
Wintermute
A couple of things:
If you have a linear function y=mx+b, the angle of that line is atan(m), not m. These are approximately the same for small m', but very different for largem`.
The linear component of a 2+ order polyfit is different than the linear component of a 1st order polyfit. You'll need to fit the data twice, once at your working level, and once with a first order fit.
Given a slope m, there are better ways of computing the rotation matrix than using trig functions (e.g. cos(atan(m))). I always try to avoid trig functions when performing geometry and replace them with linear algebra operations. This is usually faster, and leads to fewer problems with singularities. See code below.
This method is going to lead to problems for some trajectories. For example, consider a north/south trajectory. But that is a longer discussion.
Using the method described, plus the notes above, here is some sample code which implements this:
%Setup some sample data
long = linspace(1.12020, 1.2023, 1000);
lat = sin ( (long-min(long)) / (max(long)-min(long))*2*pi )*0.0001 + linspace(.2, .31, 1000);
%Perform polynomial fit
p = polyfit(long, lat, 4);
%Perform linear fit to identify rotation
pLinear = polyfit(long, lat, 1);
m = pLinear(1); %Assign a common variable for slope
angle = atan(m);
%Setup and apply rotation
% Compute rotation metrix using trig functions
rotationMatrix = [cos(angle) sin(angle); -sin(angle) cos(angle)];
% Compute same rotation metrix without trig
a = sqrt(m^2/(1+m^2)); %a, b are the solution to the system:
b = sqrt(1/(1+m^2)); % {a^2+b^2 = 1}, {m=a/b}
% %That is, the point (b,a) is on the unit
% circle, on a line with slope m
rotationMatrix = [b a; -a b]; %This matrix rotates the point (b,a) to (1,0)
% Generally you rotate data after removing the mean value
longLatRotated = rotationMatrix * [long(:)-mean(long) lat(:)-mean(lat)]';
%Plot to confirm
figure(2937623)
clf
subplot(211)
hold on
plot(long, lat, '.')
plot(long, polyval(p, long), 'k-')
axis tight
title('Initial data')
xlabel('Longitude')
ylabel('Latitude')
subplot(212)
hold on;
plot(longLatRotated(1,:), longLatRotated(2,:),'.b-');
axis tight
title('Rotated data')
xlabel('Rotated x axis')
ylabel('Rotated y axis')
The angle you are looking for in the rotation matrix is the angle of the line makes to the horizontal. This can be found as the arc-tangent of the slope since:
tan(\theta) = Opposite/Adjacent = Rise/Run = slope
so t = atan(m) and noting that you want to rotate the line back to horizontal, define the rotation matrix as:
R = [cos(-t) sin(-t)
sin(-t) cos(-t)]
Now you can rotate your points with R

How to plot a 2D FFT in Matlab?

I am using fft2 to compute the Fourier Transform of a grayscale image in MATLAB.
What is the common way to plot the magnitude of the result?
Assuming that I is your input image and F is its Fourier Transform (i.e. F = fft2(I))
You can use this code:
F = fftshift(F); % Center FFT
F = abs(F); % Get the magnitude
F = log(F+1); % Use log, for perceptual scaling, and +1 since log(0) is undefined
F = mat2gray(F); % Use mat2gray to scale the image between 0 and 1
imshow(F,[]); % Display the result
Here is an example from my HOW TO Matlab page:
close all; clear all;
img = imread('lena.tif','tif');
imagesc(img)
img = fftshift(img(:,:,2));
F = fft2(img);
figure;
imagesc(100*log(1+abs(fftshift(F)))); colormap(gray);
title('magnitude spectrum');
figure;
imagesc(angle(F)); colormap(gray);
title('phase spectrum');
This gives the magnitude spectrum and phase spectrum of the image. I used a color image, but you can easily adjust it to use gray image as well.
ps. I just noticed that on Matlab 2012a the above image is no longer included. So, just replace the first line above with say
img = imread('ngc6543a.jpg');
and it will work. I used an older version of Matlab to make the above example and just copied it here.
On the scaling factor
When we plot the 2D Fourier transform magnitude, we need to scale the pixel values using log transform to expand the range of the dark pixels into the bright region so we can better see the transform. We use a c value in the equation
s = c log(1+r)
There is no known way to pre detrmine this scale that I know. Just need to
try different values to get on you like. I used 100 in the above example.