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

In summary, the function easyspec allows for easy spectral analysis in MATLAB and can also plot the spectrum and return the results. It uses a hanning window and zero-padding for smooth results. The frequency domain goes from DC to the Nyquist frequency and the function uses random sample segments for calculation.
  • #1
Cyrus
3,238
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
  • #2
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
 
  • #3


Sure, I'd be happy to help with your spectral analysis code in MATLAB. Can you provide more information about the curve you are trying to analyze and what specific issue you are having with the fft function? That will help me provide you with a more tailored solution. In the meantime, here is a general outline for performing spectral analysis in MATLAB using the fft function:

1. First, make sure your data is in a vector format. If it is in a matrix format, you can use the "reshape" function to convert it into a vector.
2. Next, use the fft function to compute the fast Fourier transform of your data. This will give you a complex-valued vector.
3. To plot the spectrum, you can use the "abs" function to get the magnitude of the complex values and then plot them against the corresponding frequency values using the "plot" function.
4. If you want to see the spectrum in decibels, you can use the "20*log10" function to convert the magnitude values.
5. You can also use the "fftshift" function to shift the zero-frequency component to the center of the spectrum for better visualization.
6. Finally, you can use the "xlabel" and "ylabel" functions to label your plot with the appropriate frequency and magnitude units.

I hope this helps get you started with your spectral analysis in MATLAB. If you have any specific questions or issues, please feel free to provide more details and I will do my best to assist you further.
 

Similar threads

Back
Top