classdef matlab constructor, how does it work?
11 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Guillaume
on 25 May 2016
Edited: Guillaume
on 25 May 2016
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 Comments
Guillaume
on 26 May 2016
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.
See Also
Categories
Find more on Time-Frequency Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!