classdef matlab constructor, how does it work?
조회 수: 11 (최근 30일)
이전 댓글 표시
I have this piece of code and i have no idea to interpret it.
classdef LTSD
properties
winsize
window
order
amplitude
avgnoise
windownum
alpha
speechThr
end
[sound,rfs] = audioread(fn);
sound = sound(rfs*cs+1:end-rfs*ce,1);
THRESHOLD = -6; % Threshold to update the noise spectrum
ALPHA = 0.54; % update rate (forgotten factor)
NORDER = 6; % order
WINSIZE = 2048; % window size
WINDOW = hamming(WINSIZE,'symmetric'); % hamming window type
ltsd = LTSD(WINSIZE,WINDOW,NORDER,ALPHA,THRESHOLD);
res = ltsd.compute(sound);
The problem is that the LTSD constructor doesnt have enough arguments. I wonder how default constructor handle this case
댓글 수: 0
답변 (1개)
Guillaume
2016년 5월 25일
편집: Guillaume
2016년 5월 25일
It's not clear from your post where the class definition ends and where the calling code (obviously in a different file) starts, I assume it's at [sound, rfs] = …
If a class does not have an explicit constructor, a default constructor is indeed generated for it. That constructor always takes no arguments and leaves all properties at their default (so in your case, leaves everything empty).
So, yes, without an explicit constructor that takes at least 5 arguments, the line
ltsd = LTSD(WINSIZE,WINDOW,NORDER,ALPHA,THRESHOLD);
stands no chance of ever succeeding.
댓글 수: 2
Guillaume
2016년 5월 26일
The class does have a constructor, it's in the middle of the methods block (I prefer to have the constructor at the start but there's no requirement that it is other than clarity).
function obj = LTSD(varargin) %winsize,window,order,adap_rate,threshold
if nargin>3
obj.speechThr = varargin{5};
obj.alpha = varargin{4};
end
obj.winsize = varargin{1};
obj.window = varargin{2};
obj.order = varargin{3};
obj.amplitude = {};
end
The constructor accepts a variable number of arguments. Practically, the code fails if you don't pass either 3 or 5 arguments. If you only pass three arguments, then alpha and speechThr property are left empty which means that some sort of filtering won't happen in the method ltsd (I think it's a bad idea to have a method name that only differs from the constructor name by case, but again that's allowed).
You're passing 5 arguments to the constructor so there's no issue.
참고 항목
카테고리
Help Center 및 File Exchange에서 Time-Frequency Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!