MATLAB Answers


How can i get fourier transform of this function

Asked by can evkuran on 27 Dec 2018
Latest activity Commented on by Image Analyst
on 29 Dec 2018
A = 2;
f = @(t,t1,t2) A.*((t1<t) & (t<t2));
t = linspace(0, 10);
plot(t, f(t, t1, t2))
This is the code. I need to get fourier transform of this.


What are you having trouble with?
From what I understand you already have your signal you want the fourier transform (fft) of, which is
y = f(t,t1,t2);
You only need to learn how to use the fft() function in Matlab. Check the website for the fft function where there is a simple example. Modify the code given for the example for your case.
i checked but ı cannot get the answer i try but ı got errors can you help ?

Sign in to comment.


3 Answers

Answer by Image Analyst
on 27 Dec 2018

There is a function called fft() that you should learn about.
A = 2;
f = @(t,t1,t2) A.*((t1<t) & (t<t2));
t = linspace(0, 10);
y = f(t, t1, t2);
plot(t, y)
grid on
fty = fft(y)
subplot(2, 1, 1);
plot(real(fty), 'b-');
grid on;
subplot(2, 1, 2);
plot(imag(fty), 'b-');
grid on;
0001 Screenshot.png


my question is how can ı take inverse fourier transform and how can ı plot ıt ı write wrong sorry ı need ınverse fourier of function
I have no idea what s is a function of: t or x.
And I don't know what "a" is, or what condition the "otherwise" is referring to.
Regardless, after you have the FT of some array, just use ifft() to get its inverse FT.
Inverse fourier with respect to which variable? When you use t as the function parameter, then in this context t would refer to time, and you would rarely do inverse fourier of something based upon time (you would do it with respect to frequency.)

Sign in to comment.

Answer by Aquatris
on 27 Dec 2018

Here is a one way of doing it;
A = 2;
f = @(t,t1,t2) A.*((t1<t) & (t<t2));
Fs = 1000;
t = 0:(1/Fs):(8-(1/Fs)); %minus to have an integer length
y = f(t, t1, t2); % signal
L = length(y);
Y = fft(y);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L; % single sided
fshift = (-L/2:L/2-1)*(Fs/L); % double sided
subplot(2,1,1),semilogy(f,P1),xlim([0 10]),ylim([1e-4 1e1])
xlabel('f [Hz]'),ylabel('Magnitude')
xlim([-10 10]),ylim([1e-4 1])
xlabel('f [Hz]'),ylabel('Magnitude')


i think you are right i understand the problem my function is sinc function and i need to do taking sinc functions inverse fourier transform
When you make up your time domain signal, make sure you have enough space on either side of the pulse that is zero. Because you really don't have a rect. What you REALLY have is a rect, that is your pulse, multiplied by the rect that is the total window from the left side of the array to the right side. So, in the Fourier domain, the Foureir transform of a rect multiplied by a rect is the convolution of the two sincs. So, if your total signal length can be longer, that its since will be narrower (closer to a delta function) and so the final Fourier signal will be closer to the sinc of your pulse. If the total window length is smaller, like only twice the length of your pulse, then its sinc will be wide and the convolution of the two sincs will look like a mess - not as much like the sinc of the pulse alone. Just further explanation that might help. But looks like you think you have it under control.
i understand what is your saying but i dont have idea what i will write on matlab i am new at this platform

Sign in to comment.

Answer by Image Analyst
on 29 Dec 2018

can, does this help?
Note how the magnitide of the spectrum looks like a sinc function. Also note how the signal is Hermitian (symmetric real part, anti-symmmetric imaginary part), which it must be because the time domain signal is real (not complex).
I'm a little bit rusty on how to get the absolute frequencies along the frequency axis so I'll depend on dpb, Star, Walter, Stephen, Guillaume, Jan, etc. to correct it if I'm wrong.
% Program to take the FFT of a pulse.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 15;
amplitude = 2;
numSamples = 1024;
% Set up the time axis to go from 0 to 20 with 1000 sample points.
t = linspace(0, 32, numSamples);
% Create a signal in the time domain.
timeBasedSignal = zeros(1, numSamples); % First make everything zero.
% Now figure out what indexes go from t=2 to t=6
pulseIndexes = (t >= 2) & (t <= 6); % Logical indexes.
% Now make the pulse.
timeBasedSignal(pulseIndexes) = amplitude;
% Plot time based signal
subplot(5, 1, 1);
plot(t, timeBasedSignal, 'b-', 'LineWidth', 2);
grid on;
xlabel('Time', 'FontSize', fontSize);
ylabel('Signal Amplitude', 'FontSize', fontSize);
title('Signal in the Time Domain', 'FontSize', fontSize);
ylim([0, 3]); % Set range for y axis to be 0 to 3.
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Not take the Fourier Transform of it.
ft = fft(timeBasedSignal);
% Shift it so that the zero frequency signal is in the middle of the array.
ftShifted = fftshift(ft);
% Get the magnitude, phase, real part, and imaginary part.
ftMag = fftshift(abs(ft));
ftReal = fftshift(real(ft));
ftImag = fftshift(imag(ft));
ftPhase = ftImag ./ ftReal;
% Compute the frequency axis (I'm a little rusty on this part so it might not be right).
% freqs = linspace(-1/(2*min(t)), 1/(2*max(t)), numSamples);
deltat = max(t) - min(t);
freqs = linspace(-1/(2*deltat), 1/(2*deltat), numSamples);
% Plot the magnitude of the spectrum in Fourier space
subplot(5, 1, 2);
plot(freqs, ftMag, 'b-', 'LineWidth', 2);
grid on;
xlabel('Frequency', 'FontSize', fontSize);
ylabel('Magnitude', 'FontSize', fontSize);
title('Magnitude of the Signal in the Frequency (Fourier) Domain', 'FontSize', fontSize);
% Plot the imaginary part of the spectrum in Fourier space
subplot(5, 1, 3);
plot(freqs, ftPhase, 'b-', 'LineWidth', 2);
grid on;
xlabel('Frequency', 'FontSize', fontSize);
ylabel('Magnitude', 'FontSize', fontSize);
title('Phase of the Signal in the Frequency (Fourier) Domain. Note that it is symmetric because you started with a real signal.', 'FontSize', fontSize);
% Plot the real part of the spectrum in Fourier space
subplot(5, 1, 4);
plot(freqs, ftReal, 'b-', 'LineWidth', 2);
grid on;
xlabel('Frequency', 'FontSize', fontSize);
ylabel('Magnitude', 'FontSize', fontSize);
title('Real Part of the Signal in the Frequency (Fourier) Domain', 'FontSize', fontSize);
% Plot the imaginary part of the spectrum in Fourier space
subplot(5, 1, 5);
plot(freqs, ftImag, 'b-', 'LineWidth', 2);
grid on;
xlabel('Frequency', 'FontSize', fontSize);
ylabel('Magnitude', 'FontSize', fontSize);
title('Imaginary Part of the Signal in the Frequency (Fourier) Domain', 'FontSize', fontSize);
0000 Screenshot.png


Show 1 older comment
Most of the extraordinarily well commented code is just to fancy up the graph, and provide comments for you to understand it. The fft part is just one line:
ft = fft(timeBasedSignal);
To inverse fft, you'd simply do
timeBasedSignalRecovered = ifft(ft);
However to make sure you're doing it correctly I think you need to do the plotting, don't you. Would you want to trust it blind?
my last request is im going 3th grade on college its my midterm project actually one question in project i need to explain how can i write it that code its so professional i need more basic one could you help for that i need just inverse fourier of it by the way thank you for helping
To make it simpler and make it look like a 3rd grader did it, maybe you can just take out all plotting code and all comments, and rename the variables to single letters like beginners do instead of professional programmers. And take out all the initilalization steps at the beginning. Can't really take out much more than that without then making the code not work.

Sign in to comment.