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

1 view (last 30 days)
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 Comment
Walter Roberson
Walter Roberson on 29 Dec 2011
Bob, when you are adding tags, please separate the tags with comma instead of semi-colon. I have fixed this for you here.

Sign in to comment.

Accepted Answer

Daniel Shub
Daniel Shub on 29 Dec 2011
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 Comment
Bob Li
Bob Li on 2 Jan 2012
Daniel,
I tested it. The 1st argument was indeed the case.
Thanks,
Bob

Sign in to comment.

More Answers (1)

Wayne King
Wayne King on 29 Dec 2011
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 Comments
Wayne King
Wayne King on 29 Dec 2011
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 on 2 Jan 2012
Wayne,
I got it, thanks for the answer.
Bob

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!