How to design a Class to apply operations for a collection of data?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
zhehao.nkd
am 9 Apr. 2021
Kommentiert: zhehao.nkd
am 12 Apr. 2021
For example, I have two collections of data, audio stimuli and neurons' response to these audios. What I need is to (1) calculate some values for each audio ( e.g. fs,y, spectrogram) or each neuron's response and (2) calculate other values based on the whole collections of audios or all neurons' response ( e.g. find the audio file which has the maximum average volume) and finally (3) aggregate all these values from audios and neural responses together for more complex calculations.(e.g. fitting the stimulus-response function).
Basically I plan to design three Classes, Class Audio for analyzing audios, Class Neuron for analyzing neural response, and Class Aggregation which aggregate the first two classes and do complex calculations. My question is, should I create a Class that processes only a single audio/neuron or a class that processes all audios/neurons at once? Or is there any better way to design such a structure?
I found both ways have some weaknesses. If the Class is for a single audio/neuron, I have to write functions outside this Class to calculate values based on the whole audios/neurons collection.
classdef Audio
properties
y
fs
end
methods
function obj = Audio(path)
[y,fs] = audioread(path);
end
%...
end
end
function maxvolume = maxVolume(path_collection)
for path = 1: length(path_collection)
% iterate for each path
end
% calculations based on the whole collection of data
end
While If the Class is for collections of audio files, then I may have to write iterations in each methods, especially for the class Aggregation:
classdef Aggregation
%...
function f1(audios, neurons)
for m = 1: length(neurons)
for n = 1: length(audios)
... detailed calculations
end
end
end
function f2(audios, neurons)
for m = 1: length(neurons)
for n = 1: length(audios)
... detailed calculations
end
end
end
%... same for other functions
end
0 Kommentare
Akzeptierte Antwort
Jeff Miller
am 10 Apr. 2021
This is perhaps a bit subjective, but personally I would strongly prefer the solution with classes that process only a single audio/neuron. Small, highly focussed classes are easier to use for unanticipated future purposes,
"If the Class is for a single audio/neuron, I have to write functions outside this Class to calculate values based on the whole audios/neurons collection." This does not sound like a disadvantage to me. A function to process a collection should be outside the class defining items of that collection, since that function is not applicable with a single instance. You could make a new class for collection processing if that seems useful, but from your description it just sounds to me like this collection processor should be a stand-alone function.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Audio Plugin Creation and Hosting finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!