How to design a Class to apply operations for a collection of data?

1 次查看(过去 30 天)
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

采纳的回答

Jeff Miller
Jeff Miller 2021-4-10
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.
  1 个评论
zhehao.nkd
zhehao.nkd 2021-4-12
Thank you very much! I tried coding a class that process a single audio/neuron. This method works well, and it is convenient to add new methods to the class.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Simulation, Tuning, and Visualization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by