Archive

Archive for February, 2013

freq_plot

February 14, 2013 Leave a comment
%a function which plots the magnitude spectrum of a signal from DC (0Hz) up to a specified max_freq.
%
%   example: 
%           fs = 10000;
%           t = 0:1/fs: 4; 
%           sig = 3*cos(2*pi*100*t) + 3*cos(2*pi*20*t); 
%           freq_plot(sig, 120, fs)
%
% by david.dorran@gmail.com, Feb 2012
function freq_plot(sig, max_freq, fs)
if(max_freq>fs/2)
    error('Max freq must be less than Nyquist frequency');
end
    X = abs(fft(sig));
    N = length(sig);
    
    f = 0:fs/length(sig):fs;
    num_bins = find(f> max_freq);
    X = X(1:num_bins(1));
    f = f(1:num_bins(1));
    plot(f, X/(N)*2+0.0001)
    xlabel('frequency (Hz)')
    ylabel('Magnitude');
Advertisement
Categories: Uncategorized