Asked by can evkuran
on 27 Dec 2018

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.

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);

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;

can evkuran
on 28 Dec 2018

Image Analyst
on 28 Dec 2018

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.

Walter Roberson
on 28 Dec 2018

Sign in to comment.

Answer by Aquatris
on 27 Dec 2018

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')

can evkuran
on 29 Dec 2018

Image Analyst
on 29 Dec 2018

can evkuran
on 29 Dec 2018

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);

Image Analyst
on 29 Dec 2018

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?

can evkuran
on 29 Dec 2018

Image Analyst
on 29 Dec 2018

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Aquatris (view profile)

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/437414-how-can-i-get-fourier-transform-of-this-function#comment_654812

## can evkuran (view profile)

## Direct link to this comment

https://uk.mathworks.com/matlabcentral/answers/437414-how-can-i-get-fourier-transform-of-this-function#comment_654814

Sign in to comment.