Frequency Plots with Matlab/Octave

When doing frequency analysis of signals its always important to remember to window the input data prior to performing a FFT. If the data is not windowed and the signal of interest does not fall into a single FFT bin (meaning it is continuous about the length of the FFT) then there will be noise across the entire bandwidth. The code will normalize the largest spectral component to 0dB making it easy to compare relative strengths of signals for measurement of Spurious Free Dynamic Range (SPDR), adjacent channel interference etc… The other thing that is interesting is that I trim the lowest dB to -140dB. The frequency scale has a range from -fs/2 to fs/2. There is compensation for even and odd length transforms.

Here is the complete code for windowing your FFT (Here is a link to the source.)

FILE: plotfft.m

plotfft.m
function e = plotfft(in,sampleRate);
%
% This function computes the FFT of an input and plots it on a easy to read plot.  The function also
% provides hamming squared windowing on the input data to reduce spectral splatter at the edges of the 
% block of input data.
%
% Kreeger Research LLC
% Copyright (c) 2009
%
MIN_dB = -120;  % minimum of 120 db below 0dB;
win = hanning(length(in))';
%win = hamming(length(in))'; 
winsq = win.*win;
bb = in.*winsq;
c = fft(bb);
 
if nargin < 2
  sampleRate = 1.0;
end
 
l = floor(length(in)/2)+1;
idx = [[l+1:length(in)] [1:l]];
c = c(idx);
%normalize
d = 10*log10(c.*conj(c));
e = d - max(d);
 
% trim to MIN
x = find(e < MIN_dB);  %trim to MIN_db 
e(x) = MIN_dB;
 
if(mod(length(in),2)==0)
  t = (1:length(in))/length(in) - 0.5; % works for even input lengths
else
  t = linspace(-0.5,0.5,length(in));; % works for odd input lengths
end
plot(t*sampleRate,e);
grid on;
xlabel('fs');
ylabel('dB');
 
title(['Normalized Frequency Plot (peak= ' int2str(max(d)) 'dB)'])