フィルターのクリア

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

4 ビュー (過去 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 Center および File ExchangeParametric Spectral Estimation についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by