0
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
1 function varargout = computepsd(Sxx,Svxx,w,range,nfft,Fs,esttype)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
2 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
3 % Slight modification of original MATLAB's computepsd to include correct
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
4 % scaling for the variance, i.e var(a*x) = a^2*var(x)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
5 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
6 % VERSION: $Id: computepsd.m,v 1.1 2009/07/22 15:29:59 miquel Exp $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
7 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
8 % HISTORY: 21-07-09 M Nofrarias
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
9 % Creation
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
10 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
11 %%%%%%%%%%%%%%%%%
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
12 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
13 % COMPUTEPSD Compute the one-sided or two-sided PSD or Mean-Square.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
14 % [Pxx,Pvxx,W,UNITS] = COMPUTEPSD(Sxx,Svxx,W,RANGE,NFFT,Fs,ESTTYPE) where the
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
15 % inputs and outputs are:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
16 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
17 % Inputs:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
18 % Sxx - Whole power spectrum [Power]; it can be a vector or a matrix.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
19 % For matrices the operation is applied to each column.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
20 % W - Frequency vector in rad/sample or in Hz.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
21 % RANGE - Determines if a 'onesided' or a 'twosided' Pxx and Sxx are
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
22 % returned.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
23 % NFFT - Number of frequency points.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
24 % Fs - Sampling Frequency.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
25 % ESTTYPE - A string indicating the estimate type: 'psd', or 'ms' value.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
26 %
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
27 % Outputs:
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
28 % Pxx - One-sided or two-sided PSD or MEAN-SQUARE (not scaled by Fs)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
29 % depending on the input arguments RANGE and TYPE.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
30 % W - Frequency vector 0 to 2*Nyquist or 0 to Nyquist depending on
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
31 % range, units will be either rad/sample (if Fs is empty) or Hz
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
32 % (otherwise).
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
33 % UNITS - Either 'rad/sample' or 'Hz'
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
34 % Author(s): R. Losada
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
35 % Copyright 1988-2005 The MathWorks, Inc.
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
36 % $Revision: 1.1 $ $Date: 2009/07/22 15:29:59 $
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
37
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
38 if nargin < 7
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
39 iswinfs = false;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
40 if nargin < 6,
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
41 esttype = 'psd';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
42 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
43 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
44
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
45 w = w(:); % Make sure we always returns a column vector for frequency
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
46
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
47 % Generate the one-sided spectrum [Power] if so wanted
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
48 if strcmp(range,'onesided'),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
49 if rem(nfft,2),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
50 select = 1:(nfft+1)/2; % ODD
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
51 Sxx_unscaled = Sxx(select,:); % Take only [0,pi] or [0,pi)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
52 Svxx_unscaled = Svxx(select,:); % Take only [0,pi] or [0,pi)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
53 Sxx = [Sxx_unscaled(1,:); 2*Sxx_unscaled(2:end,:)]; % Only DC is a unique point and doesn't get doubled
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
54 Svxx = [Svxx_unscaled(1,:); 4*Svxx_unscaled(2:end,:)];
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
55 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
56 select = 1:nfft/2+1; % EVEN
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
57 Sxx_unscaled = Sxx(select,:); % Take only [0,pi] or [0,pi)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
58 Svxx_unscaled = Svxx(select,:); % Take only [0,pi] or [0,pi)
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
59 Sxx = [Sxx_unscaled(1,:); 2*Sxx_unscaled(2:end-1,:); Sxx_unscaled(end,:)]; % Don't double unique Nyquist point
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
60 Svxx = [Svxx_unscaled(1,:); 4*Svxx_unscaled(2:end-1,:); Svxx_unscaled(end,:)]; % Don't double unique Nyquist point
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
61 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
62 w = w(select);
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
63 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
64
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
65 % Compute the PSD [Power/freq]
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
66 if ~isempty(Fs),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
67 Pxx = Sxx./Fs; % Scale by the sampling frequency to obtain the psd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
68 Pvxx = Svxx./Fs^2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
69 units = 'Hz';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
70 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
71 Pxx = Sxx./(2.*pi); % Scale the power spectrum by 2*pi to obtain the psd
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
72 Pvxx = Svxx./(2.*pi)^2;
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
73 units = 'rad/sample';
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
74 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
75
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
76 if strcmpi(esttype,'ms'),
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
77 varargout = {Sxx,Svxx,w,units}; % Mean-square
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
78 else
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
79 varargout = {Pxx,Pvxx,w,units}; % PSD
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
80 end
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
81
|
Daniele Nicolodi <nicolodi@science.unitn.it>
parents:
diff
changeset
|
82 % [EOF] computepsd.m
|