Getting wrong frequency from fft compared to curve fit
Show older comments
I'm trying to get frequency of a signal using fft and plotting its spectrum.
However I get slightly off frequency and cannot figure out why, I've even tried to do a simple fit to my signal and got the correct frequnecy from it.
I suspect that I'm creating a wrong frequency vector which causes this deviation, but can't tell for certain.
This is the relevent code -
%%%
lambda = 1000e-9;
k = 2*pi/lambda;
delta_k = 5000;
N = 100;
k_vec = (k-(N)/2*delta_k):delta_k:(k+(N-1)/2*delta_k);
A = 0.1; % reflection coef.
delta_x = 50e-6; %m
E_mirror = 1;
E_obj = A*exp(1i*k_vec*2*delta_x);
I = abs(E_mirror+E_obj).^2;
I_mirror = abs(E_mirror)^2;
I_obj = abs(E_obj).^2;
I_diff = I-I_mirror-I_obj;
F_I_diff = fft(I_diff);
z_scale = 1/delta_k;
z_vec = z_scale/N*(-N/2:N/2-1);
%%%
The signal which frequency Im trying to find is I_diff.
Help would be appreciated, Thanks!
Accepted Answer
More Answers (1)
Assuming you're only interested in the magnitude of the DFT, it looks like everything is correct except for not applying fftshift to the output from fft. Howver, if you're also interested in the phase, then an additional step may be required because the first point in I_diff does not correspond to k = 0.
lambda = 1000e-9;
k = 2*pi/lambda;
delta_k = 5000;
N = 100;
k_vec = (k-(N)/2*delta_k):delta_k:(k+(N-1)/2*delta_k);
A = 0.1; % reflection coef.
delta_x = 50e-6; %m
E_mirror = 1;
E_obj = A*exp(1i*k_vec*2*delta_x);
I = abs(E_mirror+E_obj).^2;
I_mirror = abs(E_mirror)^2;
I_obj = abs(E_obj).^2;
I_diff = I-I_mirror-I_obj;
figure
plot(k_vec,I_diff),ylabel('Idiff')
F_I_diff = fft(I_diff);
z_scale = 1/delta_k;
zvec is correc for an even-length, fftshifted DFT
numel(I_diff)
z_vec = z_scale/N*(-N/2:N/2-1);
F_I_diff = fftshift(F_I_diff);
plot(z_vec,abs(F_I_diff))
The peaks seem to correspond to the input signal I_diff. My by-eye estimate is that the peaks should be at roughly +- 1.5e-5.
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



