Skip to content

Instantly share code, notes, and snippets.

@dhda
Created March 13, 2012 03:47
Show Gist options
  • Save dhda/2026614 to your computer and use it in GitHub Desktop.
Save dhda/2026614 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