How can i get fourier transform of this function

A = 2;
f = @(t,t1,t2) A.*((t1<t) & (t<t2));
t = linspace(0, 10);
t1=2;
t2=6;
figure
plot(t, f(t, t1, t2))
grid
This is the code. I need to get fourier transform of this.

2 Comments

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.

Answers (3)

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

4 Comments

Thank you for your help but this code is very complicated for my level in matlab i only need given functions inverse fourier transform and plot i only need thanks again and can you help me for inverse fourier of it ?
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.

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);
t1=2;
t2=6;
figure
y = f(t, t1, t2);
plot(t, y)
grid on
fty = fft(y)
figure;
subplot(2, 1, 1);
plot(real(fty), 'b-');
grid on;
subplot(2, 1, 2);
plot(imag(fty), 'b-');
grid on;
0001 Screenshot.png

5 Comments

thanks for help ı have one other question how can get thıs fourier
S(t)=1 , x=0
s(t)= a*sin(x) ,otherwise
a=2
Are you sure that you want to be examining x when determining s(t) ? Rather than examining t ?
The values 0 and a*sin(x) are constant in t, so the fourier transform of them would be dirac delta .
... Like, Yes, you can in theory rewrite the piecewise definition of s(t) in terms of dirac(x), and take the fourier transform with respect to t dealing with dirac delta again, getting out -2*Dirac(w)*Pi*((a*sin(x) - 1)*Dirac(x) - a*sin(x)) ...but is that really what is wanted?
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.

Here is a one way of doing it;
clear;clc
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
t1=2;t2=6;
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
figure(1)
subplot(2,1,1),semilogy(f,P1),xlim([0 10]),ylim([1e-4 1e1])
xlabel('f [Hz]'),ylabel('Magnitude')
subplot(2,1,2),semilogy(fshift,fftshift(P2))
xlim([-10 10]),ylim([1e-4 1])
xlabel('f [Hz]'),ylabel('Magnitude')

9 Comments

S(t)=1 , x=0
s(t)= a*sin(x) ,otherwise
a=2
how can ı get thıs fourier thanks for your answer
I recommend you start looking into given code and try to understand them. The answer to your question is, if you were to change the line "y = f(t, t1, t2); % signal" to "y = 2*sin(t)", the code will give you the fft. Keep in mind the frequency of this sine wave is 1 rad/s, so instead of using sin(t) I reccomend you use sin(1*2*pi*t) to generate a 1 Hz sine wave, which would be easier to interpret the fft.
Pasting the same code for the sine wave below for your convenience;
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
t1=2;t2=6;
y = 2*sin(2*pi*t); % 1 Hz sine wave 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
figure(1)
subplot(2,1,1),semilogy(f,P1),xlim([0 10]),ylim([1e-4 1e1])
xlabel('f [Hz]'),ylabel('Magnitude')
subplot(2,1,2),semilogy(fshift,fftshift(P2))
xlim([-10 10]),ylim([1e-4 1])
xlabel('f [Hz]'),ylabel('Magnitude')
the question is
s(t)=1 , x=0
s(t)=2sin(x)/x , other
i think s(t)=1 condition is amplitude of signal on x=0 but i need to inverse fourier of it and after plot it iam new in matlab and i dont know how can i do it
can, again, what is your horizontal axis: t or x ???????????????????????????????
What is your vertical axis: t or s ??????????????????????????
You're confusing us when you say "s as a function of t is 2 * sin(x)/x" when the function is NOT a function of t at all - it's a function of x. What does t have to do with anything? Please get your terminology straight.
let me explain basically 2<t<6 variable and we can take the t as 4 inverse transform with respect to t . function is 2.sinx/x and take the graphs vertical axis is s(t) in time domain horizontal axis is the t. s(t)=1 is only x=0 point
if i cant express my self i need only s(t)=2sinx/x inverse fourier transfrom forget the others only thıs functions inverse fourier with respect to t
thanks
OK, if I can try to rephrase this.
You have a signal in the time domain that is (what we call) a "rect" signal and is uniform between t=2 and t=6 in the time domain.
Then you want to Fourier Transform this with fft() to get a 2*sin(x)/x in the frequency domain, where x is a frequency. We call that function "sinc". It's well known that a rect signal gives a sinc signal as its transform. It is a very useful function. In nature, it's the distribution of light diffracting from a long, narrow slit, for one famous example. (Light diffracting from a circular pinhole is a sombrero function, which is a damped Bessel function in case anyone is interested).
Conveniently, there is a sinc() function built into MATLAB. But when you call fft(), if you have enough sample points in your signal, you should actually see the familiar sinc shape in the magnitude of the signal. Be aware that since your time pulse is not centered on the origin, you'll have both real and imaginary parts of your spectrum. You should plot the real part, the imaginary part, the magnitude, and the phase to learn the most from this exercise.
So, do you have enough to go on? If not, is the above correct? If it is, then the rest of us will no longer be confused and will be able to help you.
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.

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Tags

Asked:

on 27 Dec 2018

Commented:

on 29 Dec 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!