Skip to content

Instantly share code, notes, and snippets.

@caiorss
Forked from dhda/plotFFT.m
Created December 25, 2016 21:18
Show Gist options
  • Save caiorss/4005233a8c515ec40f1f8c2905744c8a to your computer and use it in GitHub Desktop.
Save caiorss/4005233a8c515ec40f1f8c2905744c8a to your computer and use it in GitHub Desktop.
Matlab FFT plotting function
function [f, X, peaks, locs] = plotFFT( t, x, p )
Fs = 1 / mean(abs(t(2:end) - t(1:end-1)));
%n = 2^nextpow2(length(x));
n = length(x);
X = fft(x-mean(x), n) / length(x);
f = 0.5*Fs * linspace(0, 1, ceil(0.5*n+1));
X = X(1:ceil(0.5*n+1));
mX = abs(X);
plot(f, mX);
xlabel('Frequency (Hz)');
ylabel('Amplitude');
if (nargin < 3 || p == true)
[peaks, locs] = findpeaks(abs(X), 'NPEAKS',12, 'MINPEAKDISTANCE',3, 'SORTSTR','descend');
hold on;
plot(f(locs), peaks, 'ro');
hold off;
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment