Clear Filters
Clear Filters

Fourier transform of a function

5 views (last 30 days)
JdC
JdC on 7 Mar 2022
Commented: Star Strider on 7 Mar 2022
Hello everyone, I come to you with a question on the fourier transform of a function.
I am trying to obtain the fourier transform of the signal of the position of an object depending on the time.
For now here is my signal (on the two image the top graph) and the fourier transform I obtain from the code I write below the images (taken from a previous question found in this site, quoted from Star Strider's answer).
function fourier_transform(z,h)
%z is 10 000
%h changes a lot depending on the result
figure;
subplot(211);
plot(z,h);
n = length(z);
Ts = 1; % Sampling Interval (one measure every one seconds for 10 000 seconds)
Fs = 1/Ts; % Sampling Frequency (samples)
Fn = Fs/2; % Nyquist Frequency
nfft = 2^nextpow2(n);
Hn = h-mean(h); % Remove Constant Offset > might not be necessary
ft = fft(Hn,nfft)/n;
Fv = linspace(0, 1, fix(n/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
subplot(212);
plot(Fv, abs(ft(Iv))*2)
The result obtained seem really wrong. Did I make a mistake in my code?
Thank you in advance for your help
JdC

Accepted Answer

Star Strider
Star Strider on 7 Mar 2022
The only problem I see is in the ‘Fv’ calculation.
It should be:
Fv = linspace(0, 1, (nfft/2)+1)*Fn; % Frequency Vector
Apparently, my original code (that I thank you for quoting) did not use ‘nfft’ as calculated here. (If it did, not calculating ‘Fv’ correctly was my error, and I apologise for it.)
.
  2 Comments
JdC
JdC on 7 Mar 2022
Thank you very much for your anwer, indeed I forgot to quote you on my post, and changed it now.
It does indeed work now.
Thanks again
JdC
Star Strider
Star Strider on 7 Mar 2022
As always, my pleasure!
No worries — I just recognised my code (there’s a lot of it out there). No specific citation necessary, however I like to be cerrtain that my posted code works correctly.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!