How Do I Perform Spectral Analysis on a Curve in MATLAB Using FFT?

Click For Summary
SUMMARY

This discussion focuses on performing spectral analysis on a curve in MATLAB using the Fast Fourier Transform (FFT). A specific function, easyspec, is provided, which computes the spectrum of an input vector x and optionally includes a sample frequency fs. Key features of the function include the use of a Hanning window, zero-padding, and the omission of the mirror part of the spectrum. The function is designed to produce smooth spectrum vectors and is released to the public domain by Eli Billauer.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of Fast Fourier Transform (FFT)
  • Knowledge of Hanning windowing techniques
  • Basic concepts of spectral analysis
NEXT STEPS
  • Explore MATLAB's built-in FFT functions and their parameters
  • Learn about different windowing techniques in spectral analysis
  • Investigate methods for improving the accuracy of FFT results
  • Study the implications of non-stationary signals in spectral analysis
USEFUL FOR

Engineers, data analysts, and researchers involved in signal processing and spectral analysis using MATLAB.

Cyrus
Messages
3,246
Reaction score
17
Does someone have a code that would let me do a spectral analysis in MATLAB on a curve?

I tried using fft but I can't get it to work.
 
Physics news on Phys.org
Ah I found some code online. Try it, it works!

Code:
function [s,f]=easyspec(x,fs)
%EASYSPEC Easy plot of spectrum estimate
%         S=EASYSPEC(X) will return a spectrum vector of
%         X. [S,F]=EASYSPEC(X,FS) will also return a freuqency
%         axis in F, while the sample frequency is given in FS.
%         EASYSPEC(X) and EASYSPEC(X,FS), will plot the spectrum
%         and return the S vector in MATLAB's "ans".
%         Notes:
%         1. Spectrum values are in dB.
%         2. The frequency domain goes from DC to the Nyquist 
%            frequency. The "mirror part" of the spectrum is
%            omitted.
%         3. The sample segments, from which the spectrum is
%            calculated are picked by random. The function might
%            return significantly different results each time it's
%            called to if X isn't stationary.
%         4. EASYSPEC uses a hanning window and zero-pads by a
%            factor of 4. The spectrum vector will look smooth.

% Eli Billauer, 17.1.01 (Explicitly not copyrighted).
% This function is released to the public domain; Any use is allowed.

if nargin==0
  error('No input vector given');
end

if (nargin==1)
  fs=2;
end

NFFT=16384; NWIN=NFFT/4;
LOOP=100;
win=hanning(NWIN)';

x=x(:)'*(17.127/NFFT/sqrt(LOOP));
n=length(x);
maxshift=n-NWIN;

if (n<2*NWIN)
  error(['Input vector should be at least of length '...
      num2str(2*NWIN)]);
end

s=zeros(1,NFFT);

for i=1:LOOP
  zuz=floor(rand*maxshift);
  s=s+abs(fft([win.*x(1+zuz:NWIN+zuz) zeros(1,NFFT-NWIN)])).^2;
end

s=10*log10(s(1:NFFT/2));
f=linspace(0,fs/2,NFFT/2);

if nargout==0
  hold off;
  plot(f,s);
  ylabel('Power Spectrum [dB]');
  xlabel('Frequency');
	grid on; zoom on;  
end
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
Replies
6
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K