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

조회 수: 2 (최근 30일)
Bob Li
Bob Li 2011년 12월 29일
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
Bob Li
Bob Li 2012년 1월 2일
Daniel,
I tested it. The 1st argument was indeed the case.
Thanks,
Bob

댓글을 달려면 로그인하십시오.

추가 답변 (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)
Bob Li
Bob Li 2012년 1월 2일
Wayne,
I got it, thanks for the answer.
Bob

댓글을 달려면 로그인하십시오.

카테고리

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