-
-
Save caiorss/4005233a8c515ec40f1f8c2905744c8a to your computer and use it in GitHub Desktop.
Matlab FFT plotting function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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