how to obtain the frequencies from the fft function
Show older comments
after the fft of the input signal,how to get the frequencies?
2 Comments
irem asci
on 6 Apr 2020
I have 10 seconds of EEG recording that I have used fft on. How can I obtain the frequencies?
numberOfSamples = 20000
samplingRate = 2000
eegFile = readtable('Hit_0_EEG1.txt');
electrodeTimeSeries = eegFile.EEG1;
timeSeries = electrodeTimeSeries(1:numberOfSamples);
y = fft(timeSeries);
posFreq = y(1:(numberOfSamples/2));%
spec = abs(posFreq);
Matan Dor hai
on 29 May 2022
fs = 2000; % sample rate N = len(your_data); % 20000 in your case fshift = (-N/2:N/2-1)*(fs/N); Y = fftshift(fft(your_data); Y = abs(Y).^2 / N; plot(fshift,Y) % 2 sided fft
fshift = (0:N/2-1)*(fs/N); Yshift = Y(N/2+1:end); plot(fshift,Yshift) % right sided fft
Accepted Answer
More Answers (6)
dpb
on 30 Sep 2013
Frequency is totally dependent upon the sample rate of the time signal and duration.
Sampling relationships --
Fmax=1/2dt; T=Ndt; df=Fmax/(N/2); T=1/df
Daniel Frisch
on 12 Nov 2020
easyFFT is not part of Matlab itself, but you have to download it and put the path where it is located to Matlab's path, for example using the addpath() function.
I also helped you with PCA. You have to differentiate between the PCA vector (coeff) in the 3D multivariate space, and the time signals in x,y,z data(:,2:4) or the time signals in the PCA base system, score.
addpath('path/to/folder/of/easyFFT.m')
% Generate random data
L = 1000;
Fs = 5000;
t = (1:L)'/Fs;
f = 200;
data = [ t, sin(2*pi*f*t), cos(2*pi*f*t), t*0];
data(:,2:4) = data(:,2:4) + randn(size(data(:,2:4)))*.1; % add some noise
% Extract data
t = data(:,1);
L = size(data,1);
Accel = data(:,2:4);
% Perform PCA
[coeff,score,latent] = pca(Accel); % need to identify the dominant tremor axis of the 3D accelerometer
% Plot multivariate data in 3D
figure()
plot3([zeros(1,3);(coeff(:,1).*sqrt(latent))'], [zeros(1,3);(coeff(:,2).*sqrt(latent))'], [zeros(1,3);(coeff(:,3).*sqrt(latent))'], 'DisplayName','PCA Base')
hold on
scatter3(data(:,2), data(:,3), data(:,4), 'DisplayName','Data')
axis equal
xlabel('x'), ylabel('y'), zlabel('z')
legend;
% Plot time data, separate for each PCA axes
figure();
plot(t,score, 'DisplayName','Accel along PCA axes');
% 1D Time signal along most dominant tremor axis
score1 = score(:,1); % extract signal projected on most dominant tremor axis coef(:,1)
[Y,f] = easyFFT( score1, length(score1), 1, Fs );
figure();
plot(f, abs(Y), 'DisplayName','Dominant Tremor FFT');
7 Comments
KR
on 12 Nov 2020
You are a legend! Thank you for clarifying how to use your easyFFT. However, what am I doing wrong here? I have downloaded easyFFT and saved into the following path:
addpath('D:\Matlab\easyFFT.m');
But I get this error:
Warning: Name is nonexistent or not a directory: D:\Matlab\easyFFT.m
> In path (line 109)
In addpath (line 86)
I imagine this would be an easy fix and I should be able to figure it out after I play with it for a bit, but if you can spot my error, that'd be great!
Also, thank you so much for fixing the pca! I spend too long trying to resolve this, so I greatly appreciate you taking the time to help me :). I have tried this code and it works :)!
Thanks again for your help!
Daniel Frisch
on 12 Nov 2020
You must add the folder, not the file itself. Sorry my addpath-line was a bit misleading.
addpath('D:\Matlab')
KR
on 12 Nov 2020
That was silly of me - thank you for identify that error. I have run the code, and it works!
Thanks again - I greatly appreciate your help!

Just quickly, I know that I can right click and extract the x- and y-values shown in the figure above, but is there a function that I can use to extract these values, such as dsearchn?
Thanks again for your help!
Daniel Frisch
on 12 Nov 2020
You want to find the maximum? Just use the max function on the Y values that you used for the plot. It gives you the maximum value and the index.
[M,I] = max(Y)
KR
on 13 Nov 2020
Thanks for your help, Daniel! I will be able to try the max function to obtain the max peak, once I resolve an issue I am having.
I had no issues performing the easyFFT yesterday, but for some reason, I am getting the following error today:
[Y,f] = easyFFT(score1,length(score1),1,Fs);
Error: File: easyFFT.m Line: 20 Column: 20
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax
error. To construct matrices, use brackets instead of parentheses.
I have downloaded and saved easyFFT to path: 'E:\Matlab', and have used addpath('E:\Matlab'); any ideas of what I am doing incorrectly?
Again, thank you for your time and help!
Daniel Frisch
on 13 Nov 2020
I'm not sure, maybe download easyFFT again, make sure you use Matlab R2020b, and otherwise send me a working example to daniel.frisch\at\posteo.de.
KR
on 15 Nov 2020
Thanks, Daniel. Re-downloading easyFFT resolved the issue. Thanks again for all of your help!
Daniel Frisch
on 31 Aug 2020
0 votes
You can use my little tool easyFFT. It automatically calculates the frequency vector in addition to the FFT.
2 Comments
KR
on 12 Nov 2020
Hi Daniel,
Do you mind identifying my error in using your easyFFT function? Please note that I need to perform the easyFFT on the first component derived from pca (which I haven't been able to work yet).
data = table2array(acceler);
t = data(:,1);
L = size(data,1);
Accel = data(:,2);
Fs = 5000;
Fn = Fs/2;
coeff = pca(Accel); %need to identify the dominant tremor axis of the 3D accelerometer
figure()
plot(pca(Accel))
legend({'X';'Y';'Z'})
[Y,f] = easyFFT(Accel); %error states 'undefined function or variable 'easyFFT'
Daniel Frisch
on 13 Oct 2021
Hi, the "undefined function or variable" error means that the function is not on the path. Either put the file easyFFT.m in your current folder, or add the folder containing it via the addpath() function.
Jiahaw Fu
on 12 Oct 2021
0 votes
Mark Newman
on 16 May 2022
0 votes
The FFT gives you a list of results. Each item in the list represents a sinusoid with a different frequency. The position of each item in the list tells you its frequency. See the following video for more details: https://www.youtube.com/watch?v=3aOaUv3s8RY
here's a simple wrapper that might help (here dim refers to the matrix dimension to loop the 1-D FFT over):
function [X, f] = run_fft(x, dt, dim, shiftSwitch)
if nargin < 3 || isempty(dim), dim = 2 ; end
if nargin < 4 || isempty(shiftSwitch), shiftSwitch = false ; end
N = size(x, dim) ;
if dim == 1
n = size(x, 2) ;
else
n = size(x, 1) ;
end
NFFT = 2 ^ nextpow2(n) ; % for zero-padding
f = (1 / dt) * (-NFFT / 2 : NFFT / 2 - 1) / NFFT ; % f = 1 / (2 * dt) * linspace(0, 1, NFFT/2 + 1) ; % f = (0 : 1 : N - 1) * fs / N ; % fk = k fs / N where k=0,1,2,...N-1 --- % Map the frequency bin to the frequency (Hz)
if dim == 2, f = f( : ) ; end
if dim == 2, X = zeros(NFFT, N) ; else X = zeros(N, NFFT) ; end
for kx = 1 : N
if dim == 1
xk = x(kx, :) ;
else
xk = x(:, kx) ;
end
nank = isnan(xk) ;
xk(nank) = 0 ; % turn nan's into zeros for zero-padding
Xk = fft(xk, NFFT) ;
if shiftSwitch
Xk = fftshift(Xk) ;
else
f = fftshift( f ) ;
end
if dim == 1
X(kx, :) = Xk ;
else
X(:, kx) = Xk ;
end
end
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!