Designing a container to store a structure
10 views (last 30 days)
Show older comments
I am working with signals, i have a class where a signal is chopped into multiple parts.
Test script:
filname = load('file');
signal = filname.xvalue
time = filname.tvalue;
f = ltiFilter.datahandle();
segment = f.addsig(signal,time);
Datahandle is a class where addsig is my function which does the chopping.
Datahandle: Not so important, just understand, this addsig function returns a structure with chopped signals
classdef datahandle
properties
test = struct();
end
methods
function y = addsig(this,s,ti)
%chops the signal into N number of small segments, based on the
%peak theshold of the difference of the time vector
interval = diff(ti);
[value,ind] = findpeaks(interval,'THRESHOLD',0.7);
if length(ind) == 0
this.test(1).y = s;
this.test(1).t =ti;
elseif length(ind) == 1
this.test(1).y = s(1:ind(1)-1);
this.test(1).t = ti(1:ind(1)-1);
this.test(end+1).y = s(ind(end)+1:end);
this.test(end).t = ti(ind(end)+1:end);
elseif length(ind) >= 2
this.test(1).y = s(1:ind(1)-1);
this.test(1).t = ti(1:ind(1)-1);
for i = 1:length(ind)-1
this.test(i+1).y = s(ind(i)+1:ind(i+1)-1);
this.test(i+1).t = ti(ind(i)+1:ind(i+1)-1);
end
this.test(end+1).y = s(ind(end)+1:end);
this.test(end).t = ti(ind(end)+1:end);
end
y = this.test;
end
Let's say x,t will be chopped into 3 parts, so now segment will have 3 vales.
segment(1).y,segment(1).t
segment(2).y,segment(2).t
segment(3).y,segment(3).t
I want to implement a function in a data handle class, where it remembers the old data and adds the new one. for example:
segment = f.addsig(signal,time)
segment = f.addsig(signal2,time2)
so we know that x1,t1 will be chopped into 3 parts. and say x2,t2 should be chopped into 2 parts., so now segment should have 5 parts.
segment(1).y,segment(1).t
segment(2).y,segment(2).t
segment(3).y,segment(3).t
segment(4).y,segment(4).t
segment(5).y,segment(5).t
I need to do this in datahandle class, not in my testscript. any suggestions will be helpful
I can call the addsign function N number of times, and each signal can be chopped into N number of segments, i want a logic which handles arbitrary number of signals
2 Comments
Guillaume
on 11 Jul 2016
Your post and your code would be much easier to understand if you'd used variable names that have meaning. It also helps self document the code since there's no comments.
As it is, I keep having to scroll back to find out what each variable is. What's x? Ah yes, it's the signal. So why not call it signal? What's a again? The diff of the time signal (maybe?), so let's call that timeinterval, etc.
In the end, I've scrolled back and forth trying to understand what is what but still have no idea of your precise question. You've posted code for a chopp method but are asking about an addsig method. How are the two related?
Accepted Answer
Guillaume
on 11 Jul 2016
Please heed my comments about variable names. x, y, f etc. are completely useless variable names. They don't describe anything about their content.
Your class code is also confusing as it shows some behaviour of a handle class (it modifies properties of the this object) but the class is a value class. Please see comparison of handle and value class.
To get what you want you can either continue with a value class and in your chopping function return a new object of the class with the chopped bit (as you're doing now). The only changes required is to add the chopped bits to the badly named test structure instead of filling it from the start each time, and to modify the way you use the class:
classdef signalstorage
properties
segments = struct('signal', {}, 'time', {}); %empty structure with correct fields
end
methods
function newsignalstorage = addsignal(this, signal, time)
interval = diff(time);
[~, locations] = findpeaks(interval,'THRESHOLD',0.7);
%better algorithm to split the signal. Does that same as your if...else in less lines
edges = [0, locations, numel(signal)+1]; %note that 0 and one past the end is on purpose
newsegments = struct('signal', cell(numel(edges)-1, 1), 'time', cell(numel(edges)-1, 1));
%this loop works for no peaks, 1 peak and more than one peak (because of the 0 and numel+1)
for edgeidx = 1 : numel(edges) - 1
newsegments(edgeidx).signal = signal(edges(edgeidx)+1 : edges(edgesidx+1)-1)
end
newsignalstorage = this; %copy this into return value
newsignalstorage.segments = [newsignalstorage.segments, newsegments]; %and append structure
%note that there is no point in modifying this.segments if the class is a value class
end
end
end
with usage:
storage = signalstorage; %initialise empty storage
storage = storage.addsignal(signal, time); %add signal
storage = storage.addsignal(signal2, time2); add other signal.
%Note that you need the added signals are in the return value, not the original object.
Or, you change the class to a handle class:
classdef signalstorage < handle
properties
segments = struct('signal', {}, 'time', {}); %empty structure with correct fields
end
methods
function this = addsignal(this, signal, time)
interval = diff(time);
[~, locations] = findpeaks(interval,'THRESHOLD',0.7);
%better algorithm to split the signal. Does that same as your if...else in less lines
edges = [0, locations, numel(signal)+1]; %note that 0 and one past the end is on purpose
newsegments = struct('signal', cell(numel(edges)-1, 1), 'time', cell(numel(edges)-1, 1));
%this loop works for no peaks, 1 peak and more than one peak (because of the 0 and numel+1)
for edgeidx = 1 : numel(edges) - 1
newsegments(edgeidx).signal = signal(edges(edgeidx)+1 : edges(edgesidx+1)-1)
end
this.segments = [this.segments, newsegments]; %and append structure
%this time we're modifying the this object as it's a handle class
end
end
end
Usage:
storage = signalstorage; %initialise empty storage
storage.addsignal(signal, time); %add signal
storage.addsignal(signal2, time2); add other signal.
4 Comments
Guillaume
on 12 Jul 2016
If the inherited class (superclass) is a value class, then the derived class (subclass) must also be a value class. It is a requirement of matlab.
A value class cannot modify its own state (properties). That is also a requirement of matlab. So if container is a value class, you will have to use this syntax:
f = container(); %we're back to meaningless variable names!
f = f.addsignal(signal1,time1);
f = f.addsignal(signal2,time2);
However, if the class you're inheriting from forces you to use a syntax you don't want, should you inherit from it? After all, since the class is a value class, you already have to use value syntax for any method you inherit from it.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!