MATLAB Answers

How can i get fourier transform of this function

121 ビュー (過去 30 日間)
can evkuran
can evkuran 2018 年 12 月 27 日
コメント済み: Image Analyst 2018 年 12 月 29 日
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 件のコメント
can evkuran
can evkuran 2018 年 12 月 27 日
i checked but ı cannot get the answer i try but ı got errors can you help ?

サインインしてコメントする。

回答 (3 件)

Image Analyst
Image Analyst 2018 年 12 月 29 日
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 件のコメント
Image Analyst
Image Analyst 2018 年 12 月 29 日
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.

サインインしてコメントする。


Image Analyst
Image Analyst 2018 年 12 月 27 日
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 件のコメント
Walter Roberson
Walter Roberson 2018 年 12 月 28 日
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.)

サインインしてコメントする。


Aquatris
Aquatris 2018 年 12 月 27 日
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 件のコメント
can evkuran
can evkuran 2018 年 12 月 29 日
i understand what is your saying but i dont have idea what i will write on matlab i am new at this platform

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by