classdef matlab constructor, how does it work?

조회 수: 11 (최근 30일)
Minh Nguyen
Minh Nguyen 2016년 5월 25일
댓글: Guillaume 2016년 5월 26일
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

답변 (1개)

Guillaume
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
Minh Nguyen
Minh Nguyen 2016년 5월 26일
https://github.com/isrish/VAD-LTSD
The link above is the repo for the code. If you understand the classdef constructor works after looking at the code. please let me know
Guillaume
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 CenterFile Exchange에서 Time-Frequency Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by