Home > matlab code, Uncategorized > freq_plot.m


%a function which plots the magnitude spectrum of a signal from DC (0Hz) up to
%   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)
        error('Max freq must be less than Nyquist frequency');
    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)')
Categories: matlab code, Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: