Anonymous function handle incosistence problem with Fourier functions
Hello, during some optics simulation I dared to shorten my code (a .m type) by making fourier transform an anonymous function as such:
F = @(x) fftshift(fft(ifftshift(x)));
iF = @(x) ifftshift(ifft(fftshift(x)));
poow = @(x) abs(x).^ 2;
with these I would Transform, inverse transform and Find the intensity of the signal but for a reason I don’t know it only works when I post it in the command window… sometimes.
here is an example of it in action:
L = 15; % mm
F = 100; % mm
f0 = 1/100e-3; % lp/mm
f1 = 1/300e-3; % lp/mm
dx = 4e-3; % mm
t = 0:dx:L-dx; % mm
n = length(t);
f = -1/(2*dx):1/L:(1/(2*dx)-1/L);
object = chirp(t,f0,t(end),f1,"quadratic");
I = poow(object);
%% transform the object and display
object_ = F(object);
I_object_ = poow(object_);
Now, the problem is as you might see here is that the error this time is:
Array indices must be positive integers or logical values.
but on my machine its:
Error: File: LiveEditorEvaluationHelperE1735339498.m Line: 57 Column: 7
Invalid expression. Check for missing or extra characters.
Error in LiveEditorEvaluationHelperE1735339498>@(x)fftshift(fft(ifftshift(x))) (line 3)
F = @(x) fftshift(fft(ifftshift(x)));
^
this error happens to all previewed anonymous functions.
I have checked if a simpler handle would work and it does only crash when calling these 3 specific ones when running the script.Hello, during some optics simulation I dared to shorten my code (a .m type) by making fourier transform an anonymous function as such:
F = @(x) fftshift(fft(ifftshift(x)));
iF = @(x) ifftshift(ifft(fftshift(x)));
poow = @(x) abs(x).^ 2;
with these I would Transform, inverse transform and Find the intensity of the signal but for a reason I don’t know it only works when I post it in the command window… sometimes.
here is an example of it in action:
L = 15; % mm
F = 100; % mm
f0 = 1/100e-3; % lp/mm
f1 = 1/300e-3; % lp/mm
dx = 4e-3; % mm
t = 0:dx:L-dx; % mm
n = length(t);
f = -1/(2*dx):1/L:(1/(2*dx)-1/L);
object = chirp(t,f0,t(end),f1,"quadratic");
I = poow(object);
%% transform the object and display
object_ = F(object);
I_object_ = poow(object_);
Now, the problem is as you might see here is that the error this time is:
Array indices must be positive integers or logical values.
but on my machine its:
Error: File: LiveEditorEvaluationHelperE1735339498.m Line: 57 Column: 7
Invalid expression. Check for missing or extra characters.
Error in LiveEditorEvaluationHelperE1735339498>@(x)fftshift(fft(ifftshift(x))) (line 3)
F = @(x) fftshift(fft(ifftshift(x)));
^
this error happens to all previewed anonymous functions.
I have checked if a simpler handle would work and it does only crash when calling these 3 specific ones when running the script. Hello, during some optics simulation I dared to shorten my code (a .m type) by making fourier transform an anonymous function as such:
F = @(x) fftshift(fft(ifftshift(x)));
iF = @(x) ifftshift(ifft(fftshift(x)));
poow = @(x) abs(x).^ 2;
with these I would Transform, inverse transform and Find the intensity of the signal but for a reason I don’t know it only works when I post it in the command window… sometimes.
here is an example of it in action:
L = 15; % mm
F = 100; % mm
f0 = 1/100e-3; % lp/mm
f1 = 1/300e-3; % lp/mm
dx = 4e-3; % mm
t = 0:dx:L-dx; % mm
n = length(t);
f = -1/(2*dx):1/L:(1/(2*dx)-1/L);
object = chirp(t,f0,t(end),f1,"quadratic");
I = poow(object);
%% transform the object and display
object_ = F(object);
I_object_ = poow(object_);
Now, the problem is as you might see here is that the error this time is:
Array indices must be positive integers or logical values.
but on my machine its:
Error: File: LiveEditorEvaluationHelperE1735339498.m Line: 57 Column: 7
Invalid expression. Check for missing or extra characters.
Error in LiveEditorEvaluationHelperE1735339498>@(x)fftshift(fft(ifftshift(x))) (line 3)
F = @(x) fftshift(fft(ifftshift(x)));
^
this error happens to all previewed anonymous functions.
I have checked if a simpler handle would work and it does only crash when calling these 3 specific ones when running the script. fft, ifft, fftshift, ifftshift, anonymous function, fourier MATLAB Answers — New Questions