Home > functions > device > filter_func > vb_calc_spectrum.m

vb_calc_spectrum

PURPOSE ^

Calculate spectrum

SYNOPSIS ^

function [spx ,freq ] = vb_calc_spectrum(xdata,fs,window_type)

DESCRIPTION ^

 Calculate spectrum
  [spx ,freq ] = vb_calc_spectrum(xdata,fs,window_type)
 --- Input
  xdata  : signal to analyze (Xdim x TimeSample)
  fs     : Sampling frequency (Hz)
  window_type : name of window function
         = 'hanning'    (Default)
         = 'hamming'
         = 'blackman'
 --- Output
 spx  : Fourier spectrum of 'xdata'
 freq : frequency value for spx

 Copyright (C) 2011, ATR All Rights Reserved.
 License : New BSD License(see VBMEG_LICENSE.txt)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function    [spx ,freq ] = vb_calc_spectrum(xdata,fs,window_type)
0002 % Calculate spectrum
0003 %  [spx ,freq ] = vb_calc_spectrum(xdata,fs,window_type)
0004 % --- Input
0005 %  xdata  : signal to analyze (Xdim x TimeSample)
0006 %  fs     : Sampling frequency (Hz)
0007 %  window_type : name of window function
0008 %         = 'hanning'    (Default)
0009 %         = 'hamming'
0010 %         = 'blackman'
0011 % --- Output
0012 % spx  : Fourier spectrum of 'xdata'
0013 % freq : frequency value for spx
0014 %
0015 % Copyright (C) 2011, ATR All Rights Reserved.
0016 % License : New BSD License(see VBMEG_LICENSE.txt)
0017 
0018 if ~exist('window_type','var') | isempty(window_type), 
0019     window_type = 'hanning'; 
0020 end;
0021 
0022 if ~exist('fs','var') | isempty(fs), fs = 1000; end;
0023 
0024 [xdim,T] = size(xdata);
0025 
0026 com = ['win=' window_type '(T);'];
0027 eval(com);
0028 
0029 xx  = vb_repmultiply(xdata' ,win);
0030 
0031 nfft = nextpow2(T);
0032 nfft = 2^nfft;
0033 
0034 spx = abs(fft(xx,nfft))';
0035 spx = spx(:,1:nfft/2+1);
0036 
0037 d=fs/nfft;
0038 freq=0:d:d*(nfft/2);
0039 
0040 return
0041 
0042 %spx = zeros(xdim,T);
0043 %
0044 %for n = 1:xdim
0045 %    spx(n,:) = abs(fft(xdata(n,:).*win));
0046 %end

Generated on Tue 27-Aug-2013 11:46:04 by m2html © 2005