How to resolve between @spectrum/psd.m and psd.m?

2 次查看(过去 30 天)
Hi,
In Spectral Analysis http://www.mathworks.com/help/toolbox/signal/ug/f12-6587.html, under Nonparametric Methods>> Periodogram, the following code snippet is shown
fs = 1000; % Sampling frequency
t = (0:fs)/fs; % One second worth of samples
A = [1 2]; % Sinusoid amplitudes (row vector)
f = [150;140]; % Sinusoid frequencies (column vector)
xn = A*sin(2*pi*f*t) + 0.1*randn(size(t));
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
I am curious on which function psd is, so I used which to locate it:
>> which psd
C:\Program Files\MATLAB\R2008a\toolbox\signal\signal\psd.m
>> which spectrum.psd
C:\Program Files\MATLAB\R2008a\toolbox\signal\signal\@spectrum\psd.m % static method or package function
It seems that “which psd” returns a standalone function, whereas “which spectrum.psd” returns a static function of the spectrum class.
In standard Object-oriented programming language like C++, compiler examines argument list to resolve overloaded functions. But here,
Standalone psd.m;
function [Pxx, Pxxc, f] = psd(varargin)
@spectrum\psd.m:
function varargout = psd(this,x,varargin)
both take variable input argument lists, and the calling statement
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
has no return argument which, if length ≥4, could possibly be used to resolve ambiguity since standalone psd.m has maximum of 3 outputs.
So how could the compiler determine which psd.m is executed, the standalone one or the spectrum class method under @spectrum?
Bob
  1 个评论
Walter Roberson
Walter Roberson 2011-12-29
Bob, when you are adding tags, please separate the tags with comma instead of semi-colon. I have fixed this for you here.

请先登录,再进行评论。

采纳的回答

Daniel Shub
Daniel Shub 2011-12-29
For
...
Hs = spectrum.periodogram;
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
MATLAB knows to call the spectrum.psd method sicne the first argument is of class spectrum.
I don't have access to MATLAB right now, but if instead you do
...
eval('Hs = spectrum.periodogram;');
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
I am pretty sure you confuse the JIT and you get the psd function.

更多回答(1 个)

Wayne King
Wayne King 2011-12-29
Hi Bob, in
fs = 1000; % Sampling frequency
t = (0:fs)/fs; % One second worth of samples
A = [1 2]; % Sinusoid amplitudes (row vector)
f = [150;140]; % Sinusoid frequencies (column vector)
xn = A*sin(2*pi*f*t) + 0.1*randn(size(t));
Hs = spectrum.periodogram;
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
You are using the psd() method for spectrum objects. C:\Program Files\MATLAB\R2008a\toolbox\signal\signal\@spectrum\psd.m
Note that I have added the line: Hs = spectrum.periodogram above. This was not present in your original post, but it must have been somewhere in the documentation previous to the call
psd(Hs,xn,'Fs',fs,'NFFT',1024,'SpectrumType','onesided')
Hs is the spectrum object. Note the following use of the psd method for spectrum objects.
t = 0:0.001:1-0.001;
x = cos(2*pi*100*t)+randn(size(t));
psdest = psd(spectrum.periodogram,x,'NFFT',length(x),'Fs',1/0.001);
plot(psdest);
Note that psd() the function is being deprecated.
  3 个评论
Wayne King
Wayne King 2011-12-29
psd() is a method of the spectrum object. Hs is a spectrum object. MATLAB knows because you have a spectrum object as the first input to the method. this (is the spectrum object)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Parametric Spectral Estimation 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by