-
Notifications
You must be signed in to change notification settings - Fork 16
/
SignalObj.m
2382 lines (2234 loc) · 103 KB
/
SignalObj.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef SignalObj < handle
%SIGNALOBJ Class representing a signal abstraction
% SignalObj consist of data that is indexed by time (as a default). The
% indexing variable can be any other type of data and the x-axis labels
% modified to represent this change.
%
% A SignalObj can be multivariate in that the data can have more than one component. The
% sample rate of the SignalObj is determined by the time increment used
% in the time sequence used when the SingalObj is created
%
% Usage:
% >> s=SignalObj(time, data, name, xlabelval, xunits, yunits, dataLabels, plotProps)
%
% Only time and data need to be specified. Other arguments are optional.
%
%
% time: indexing variable for the data. n x 1 or 1 x n array. The sample
% rate is determined by the time increment between samples of this vector. Units of
% [sec] are assumed, but need not be used. If the time vector is
% in units of [sec], the sampleRate is in units of [Hz]. If the
% time vector is in units of [msec] then the sampleRate is in
% units of [1/msec] or 10^3 [Hz].
%
% data: n x m or m x n array reprenting the signal at each index of the time vector.
% The dimension that is compatible with the time vector will be automatically detected.
% Thus a SignalObj can be created by either passing the data matrix or its transpose. The remaining
% dimension will determine the dimensionality of the SignalObj.
%
% name: string that determines the name of the signal. This is used to
% label the y-axis of the SignalObj.
%
% xlabelval: A string specifying the name of the indexing variable. If
% this value is not specified, 'time' is used.
%
% xunits: A string specifying the name of the units of the indexing
% variable. In not specified, 'sec' is used.
%
% yunits: A string specifying the units of the SignalObj. Used when plotting the SignalObj.
%
% dataLabels: If data is multivariate, the names of the components of the SignalObj can be specified.
% These can be used to reference specific data within the
% signal (e.g. the x-component of a 3-d vector) and are
% also used for plotting. SignalObj's will be created for
% each component of the orignal SignalObj under the
% vars field. Can be specified all at once or by a cell of
% strings.
%
% plotProps: Can be specified for each component of the SignalObj
% individually or by a cell of string of same dimension as the
% number of components in the data.
%
%
% <a href="matlab: methods('SignalObj')">methods</a>
% <a href="matlab:web('SignalObjExamples.html', '-helpbrowser')">SignalObj Examples</a>
%
% Reference page in Help browser
% <a href="matlab:doc('SignalObj')">doc SignalObj</a>
%
% nSTAT v1 Copyright (C) 2012 Masschusetts Institute of Technology
% Cajigas, I, Malik, WQ, Brown, EN
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU General Public License as published
% by the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
% See the GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software Foundation,
% Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
properties (SetAccess = private)
name % name of the SignalObj
time % time vector. Time increment determines sampleRate
data % actual SignalObj data
dimension % number of different components of the SignalObj
minTime % minimum Time value of the SignalObj
maxTime % maximum Time value of the SignalObj
xlabelval % label to use for the x-axis
xunits % units for x-axis
yunits % units for y-axis data
dataLabels % labels for each dimension of the data;
dataMask % vector same length as SignalObj dimension. a 1 indicates this SignalObj should be output a 0 otherwie
sampleRate % Hz if time is in seconds
plotProps % Plotting properties
end
properties (Hidden)
origSampleRate
originalTime %original timeVector
originalData %original Data
end
% properties (Dependent = true)
% vars %Contains subfields of the same names as the dataLabels that contain Signals with only the data corresponding to that label.
% end
methods
%Constructor
function s=SignalObj(time, data, name, xlabelval, xunits, yunits, dataLabels, plotProps)
if(nargin<6)
yunits='';
end
if(nargin<5)
xunits='s';
end
if(nargin<4)
xlabelval='time';
end
if(nargin<3)
name='';
end
[l,w]=size(time);
if(l>=w)
if(w>1)
error('Time vector can only have one dimension');
else
s.time=time;
end
elseif(l<=w)
if(l>1)
error('Time vector can only have one dimension');
else
s.time=time';
end
end
s.originalTime=s.time;
[l,w]=size(data);
if(l==length(s.time));
s.data=data;
s.dimension =w;
elseif(w==length(s.time))
s.data=data';
s.dimension=l;
else
error('Data dimensions do not match the time vector specified');
end
s.originalData = s.data;
if(nargin <7)
if(s.dimension==0)
dataLabels ='';
else
for i=1:s.dimension
dataLabels{i} = '';
end
end
end
s.dataMask = ones(1,s.dimension);
if(nargin<8)
plotProps = cell(s.dimension,1);
end
deltaT=mean(diff(s.time));
if(isnan(deltaT))%diff not well defined
deltaT=0.001;
end
precision =2*ceil(log10(1/deltaT));
deltaT = roundn(deltaT,-precision);
% deltaT = roundn(mean(diff(s.time)),-3); %To avoid round-off error, when computing samplerate
s.sampleRate = 1/deltaT;
s.origSampleRate = s.sampleRate;
s.name=name;
s.xlabelval=xlabelval;
s.xunits=xunits;
s.yunits=yunits;
s.minTime=min(s.time);
s.maxTime=max(s.time);
s.setPlotProps(plotProps);
s.setDataLabels(dataLabels);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Set functions
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function setName(sObj,name)
% setName(sObj,name)
% set the name after construction
if(isa(name,'char'))
sObj.name = name;
else
error('Name must be a string!');
end
end
function setXlabel(sObj,name)
%setXlabel(sObj,name)
%set the x-axis label to string name
sObj.xlabelval = name;
end
function setYLabel(sObj,name)
%setYLabel(sObj,name)
%set the ylabel to string name;
%Same as calling setName(sObj,name);
sObj.setName(name);
end
function setUnits(sObj, xUnits, yUnits)
%setUnits(sObj, xUnits, yUnits)
%Set the axis units.
%Same as calling sObj.setXUnits(xUnits) and
%sObj.setYUnits(yUnits) separately.
%yUnits is optional argument. If it is not specified, the this
%function behaves like setXUnits.
if(nargin==3)
if(isa(yUnits,'char'))
sObj.setYunits(yUnits);
end
end
if(nargin>=2)
if(isa(xUnits,'char'))
sObj.setXunits(xUnits);
end
end
end
function setXUnits(sObj, units)
%setXUnits(sObj, units)
%Sets the units of the x-axis
if(isa(units, 'char'))
sObj.xunits = units;
end
end
function setYUnits(sObj, units)
%setYUnits(sObj, units)
%Sets the units of the y-axis
if(isa(units,'char'))
sObj.yunits = units;
end
end
function setSampleRate(sObj, sampleRate)
% setSampleRate(sObj, sampleRate)
% sets the current sampleRate of the object to rate specified
if(sObj.sampleRate~=sampleRate)
if(~(floor(sampleRate*1000)/1000==floor(sObj.sampleRate*1000)/1000)) %Compare to 3 decimal places (finite precision has caused errors 500.000001 ~= 500.00000x
if(sampleRate>sObj.sampleRate)
%fprintf(strcat('SignalObj,',sObj.name',', upsampled to:',num2str(sampleRate)));
else
%fprintf(strcat('SignalObj,',sObj.name',', downsampled to:',num2str(sampleRate)));
end
sObj.resampleMe(sampleRate);
end
end
end
function setDataLabels(sObj,dataLabels)
%setDataLabels(sObj,dataLabels)
%sets the labels for each of the components of the SignalObj.
%if sObj has only a single component, then dataLabels can be a
%string. Otherwise, dataLabels must be a cell with the same
%dimensions as sObj. dataLabels{i} specifies the string for the
%ith component of sObj.
if(~isempty(dataLabels))
if(isa(dataLabels,'char'))
if(sObj.dimension==1)
sObj.dataLabels{1}=dataLabels;
else
display('Adding single dataLabel to a SignalObj with more that 1 dimension. All dimensions have same label now!');
for i=1:sObj.dimension
sObj.dataLabels{i} = dataLabels;
end
end
elseif(isa(dataLabels,'cell'))
if(length(dataLabels)==sObj.dimension)
%ind=sObj.findIndFromDataMask;
%for i=ind
% sObj.dataLabels{i} = dataLabels{i};
%end
sObj.dataLabels = dataLabels;
else
error('Need the number of labels to match the number of dimensions of the SignalObj');
end
end
else
if(sObj.dimension==1)
sObj.dataLabels='';
else
for i=1:sObj.dimension
sObj.dataLabels{i}='';
end
end
end
end
function setMinTime(sObj,minTime,holdVals)
%setMinTime(sObj,minTime,holdVals)
%sets the minimun value of the time vector to minTime. If
%minTime>min(sObj.time) then the data before minTime will be
%ignored. If minTime < min(sObj.time) then the time vector is
%extended at the current sampleRate to minTime.
%holdVals: 1 or 0. If not specifed, defaults to 0. If
%holdVals=1, then the value at min(sObj.time) is extended to
%the new minTime. Otherwise, the added time is padded with
%zeros.
if(nargin<3)
holdVals=0;
end
if(nargin<2)
minTime=sObj.time(1);
end
timeVec=sObj.getTime;
if(minTime<min(timeVec))
maxTime=max(timeVec);
newTime=minTime:1/sObj.sampleRate:maxTime;
newTime=newTime';
numSamples = length(newTime)-length(timeVec);
if(holdVals==1)
newData=[ones(numSamples,1)*sObj.data(1,:);sObj.data];
else
newData=[zeros(numSamples,sObj.dimension);sObj.data];
end
sObj.data=newData;
sObj.time=newTime;
sObj.minTime=min(sObj.time);
elseif(min(timeVec)==minTime)
%do nothing
else
startIndex = sObj.findNearestTimeIndex(minTime);
sObj.time=sObj.time(startIndex:end);
sObj.data=sObj.data(startIndex:end,:);
end
sObj.minTime=min(sObj.time);
end
function setMaxTime(sObj,maxTime, holdVals)
%setMaxTime(sObj,maxTime,holdVals)
%sets the maximum value of the time vector to maxTime. If
%maxTime<max(sObj.time) then the data after maxTime will be
%ignored. If maxTime > max(sObj.time) then the time vector is
%extended at the current sampleRate to maxTime.
%holdVals: 1 or 0. If not specifed, defaults to 0. If
%holdVals=1, then the value at min(sObj.time) is extended to
%the new minTime. Otherwise, the added time is padded with
%zeros.
if(nargin<3)
holdVals=0;
end
if(nargin<2)
maxTime=sObj.time(end);
end
timeVec=sObj.getTime;
if(max(timeVec)<maxTime)
minTime=min(timeVec);
newTime=linspace(minTime,maxTime,(sObj.sampleRate)*(maxTime-minTime)+1);
newTime = newTime';
numSamples = length(newTime)-length(timeVec);
if(holdVals==1)
newData=[sObj.data;ones(numSamples,1)*sObj.data(end,:)];
else
newData=[sObj.data;zeros(numSamples,sObj.dimension)];
end
sObj.data=newData;
sObj.time=newTime;
sObj.maxTime=max(sObj.time);
elseif(max(timeVec)==maxTime)
%do nothing
else
endIndex = sObj.findNearestTimeIndex(maxTime);
sObj.time=sObj.time(1:endIndex);
sObj.data=sObj.data(1:endIndex,:);
end
sObj.maxTime=max(sObj.time);
end
function setPlotProps(sObj, plotProps,index)
%setPlotProps(sObj, plotProps,index)
%if index is not specified:
% - plotProps is a cell with sObj.dimension elements, then plotProps{i} specifies a string
% that will be used to plot the ith component of sObj.
% - plotProps is a string, then the string will be used to
% plot all of the components of sObj.
%
%if index is specified and index is within range of the number
%of components of the signal:
% - plotProps is a cell of length 1 then the property is
% applied to the component specified by the index
% - plotProps is a string, the property is applied to the
% component specified by the index.
if(nargin<=2)
if(isa(plotProps,'cell'))
if(length(plotProps) == sObj.dimension)
for i=1:sObj.dimension
sObj.plotProps{i} = cell2str(plotProps{i});
end
elseif(length(plotProps)==1)
for i=1:sObj.dimension
sObj.plotProps{i} = cell2str(plotProps);
% display('Index not specified. All dimensions set to have same plotting properties');
end
else
error('Index not specified and more than 1 plotProp specified. Need to number of plotProps same as sObj.dimension or length 1');
end
elseif(isa(plotProps,'char'))
for i=1:sObj.dimension
sObj.plotProps{i} = cell2str(plotProps);
end
display('All dimensions set to have same plotting properties')
end
else
if(isa(plotProps,'cell') && length(plotProps)==1)
if(index>0 && index<=sObj.dimension)
sObj.plotProps{index} = plotProps{:};
else
error('Index out of bounds during setPlotProps');
end
elseif(isa(plotProps,'char'))
if(index>0 && index<=sObj.dimension)
sObj.plotProps{index} = plotProps;
else
error('Index out of bounds during setPlotProps');
end
end
end
end
function setMask(sObj, mask)
%setMask(sObj, mask)
% if called with no arguments, all the components of the signal
% are masked. No data will be visible.
% mask: either a set of indices or a cell array of characters
% indicating which signal components are to remain visible.
if(nargin<2)
mask=zeros(1,sObj.dimension);
sObj.setDataMask(mask);
return;
end
%mask is either a set of indices or names;
if(isa(mask,'cell'))
if(isa(mask{1},'char'))
sObj.setMaskByLabels(mask);
else
error('Mask cells must contains strings!');
end
elseif(isa(mask,'double'))
sObj.setMaskByInd(mask);
else
error('Can only set datamask with strings or indices')
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function tVec = getTime(sObj)
% tVec = getTime(sObj)
% returns the time vector of the Signal Obj
tVec=sObj.time;
end
function data = getData(sObj)
% data = getData(sObj)
% Returns the signal data as a matrix. If masks are set, then
% only the components that are visible will be returned. Each
% column corresponds to each component of the SignalObj that is
% visible. The columns are in the same order as the dataLabels.
data=sObj.dataToMatrix;
end
function [t,d] = getOriginalData(sObj)
% [t,d] = getOriginalData(sObj)
% SignalObjs have memory. The original data and time vectors
% are stored even when the signal is resamples, windowed, etc.
% This commands returns the original data used to create the
% SignalObj
t=sObj.originalTime;
d=sObj.originalData;
end
function s = getOrigDataSig(sObj)
% s = getOrigDataSig(sObj)
% same as getOriginalData, except that a SignalObj containing the original data is returned.
[time,data]=sObj.getOriginalData;
name=sObj.name;
xlabelval=sObj.xlabelval;
xunits=sObj.xunits;
yunits=sObj.yunits;
dataLabels=sObj.dataLabels;
plotProps=sObj.plotProps;
evalstring = strcat('s=',class(sObj),'(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps)');
eval(evalstring);
%s = SignalObj(time, data,name, xlabelval, xunits, yunits,dataLabels,plotProps);
end
function val = getValueAt(sObj,x)
%val = getValueAt(sObj,x)
%returns a row vector of length sObj.dimension corresponding to
%the values of the signal evaluated at time=x
%ind=sObj.findNearestTimeIndices(x);
%val=sObj.data(ind,:);
[l,w]=size(x);
if(w>l)
x=x';
end
% val = interp1(sObj.time,sObj.data,x,'spline',0); %extrapolate to zero
val = interp1(sObj.time,sObj.data,x,'nearest',0); %extrapolate to zero
% if(any(isnan(sObj.data)))
% pause
% end
% if(sObj.dimension==1)
% val=val';
% end
end
function PropsStr = getPlotProps(sObj,index)
%PropsStr = getPlotProps(sObj,index)
%Returns the string correspond to the plotting properties of
%the SignalObj component corresponding to index
if(index>0 && index<=sObj.dimension)
PropsStr = cell2str(sObj.plotProps{index});
else
error('index is out of bounds!');
end
end
function indices = getIndicesFromLabels(sObj,label)
%indices = getIndicesFromLabels(sObj,label)
%Returns a cell array if the label appears for various point
%in the SignalObj. indices{i} contains all the the indices
%corresponding to label{i} if label is a cell-array or label if
%it is a string.
%Returns an array if the SignalObj label appears only once in the
%SignalObj
if(isa(label,'cell'))
indices =cell(1,length(label));
numInd =zeros(1,length(label));
for i=1:length(label)
tempInd = sObj.getIndexFromLabel(label{i});
if(~isempty(tempInd))
numInd(i) = length(tempInd);
indices{i}=tempInd;
else
error('Label does not exist!');
end
end
elseif(isa(label,'char'))
indices = sObj.getIndexFromLabel(label);
numInd(1) = length(indices);
end
if(max(numInd)==1) %For backwards compatibility if assuming only on index per label
if(isa(indices,'cell'))
for i=1:length(numInd)
tempInd(i) = indices{i};
end
indices = tempInd;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Operand Definitions and other mathematical operations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function s3 = plus(s1,s2)
% s3 = plus(s1,s2)
% Adds two signals
if(isa(s1,'SignalObj') && isa(s2,'SignalObj'))
% What if s2 is a constant or double?
if(s1.dimension == s2.dimension)
[s1c,s2c] = makeCompatible(s1,s2);
s3=s1c.copySignal;
s3.data = s1c.data+s2c.data;
for i=1:length(s3.dataLabels)
if(~s2c.areDataLabelsEmpty && ~isempty(s2c.dataLabels{i}))
if(strcmp(s2c.dataLabels{i}(1),'-'))
s3.dataLabels{i} = [s1c.dataLabels{i} '-' s2c.dataLabels{i}(2:end)];
else
s3.dataLabels{i} = [s1c.dataLabels{i} '+' s2c.dataLabels{i}];
end
end
end
else
error('Can only add signals if they have the same dimension');
end
elseif(isa(s1,'double') || isa(s2,'double'))
if(isa(s1,'double'))
s3=s2.copySignal;
[l,w] = size(s1);
if(w==s3.dimension && l==1)
s3.data = s3.data+ones(length(s3.data),1)*s1;
for i=1:length(s3.dataLabels)
if(sign(s1(i))==-1)
s3.dataLabels{i} = [s2.dataLabels{i} '-' num2str(abs(s1(i)))];
else
s3.dataLabels{i} = [s2.dataLabels{i} '+' num2str(abs(s1(i)))];
end
end
else
s3.data = s3.data+s1;
%dont modify dataLabels since s1 is a matrix;
%for i=1:length(s3.dataLabels)
% s3.dataLabels{i} = [s2.dataLabels{i} '+' num2str(s1(i))];
%end
end
else
s3=s1.copySignal;
[l,w] = size(s2);
%size(s3.data)
if(w==s3.dimension && l==1)
s3.data = s3.data+ones(length(s3.data),1)*s2;
for i=1:length(s3.dataLabels)
if(sign(s2(i))==-1)
s3.dataLabels{i} = [s1.dataLabels{i} '-' num2str(abs(s2(i)))];
else
s3.dataLabels{i} = [s1.dataLabels{i} '+' num2str(abs(s2(i)))];
end
end
else
s3.data = s3.data+s2;
%dont modify dataLabels since s2 is a matrix;
%for i=1:length(s3.dataLabels)
% s3.dataLabels{i} = [s1.dataLabels{i} '+' num2str(s2)];
%end
end
end
else
error('only Signals or doubles are currently supported');
end
end
function s3 = minus(s1,s2)
% s3 = minus(s1,s2)
% Subtracts two signals
s3=plus(s1,-s2);
end
function s3 = uplus(s1) %+s1
% s3 = uplus(s1)
% Multiplies signal by +1
s3=s1.copySignal;
end
function s3 = uminus(s1) %-s1
% s3 = uminus(s1) %-s1
% Multiplies the signal by -1.
% dataLabels are updated to reflect this.
s3=s1.copySignal;
s3.data=-s3.data;
for i=1:length(s3.dataLabels)
s3.dataLabels{i} = strcat('-',s1.dataLabels{i});
end
end
function s3 = power(s1,exponent)
if(isa(exponent,'double'))
s3=s1.copySignal;
s3.data=s3.data.^exponent;
else
error('Exponent should be a double');
end
end
function s3 = sqrt(s1)
s3=s1.copySignal;
s3.data=sqrt(s3.data);
end
function s3 = times(s1,s2) %s1.*s2
%s3 = times(s1,s2)
%Multiplies each sample in s1 with each sample of s2.
%s1 or s2 can be doubles
if(isa(s1,'SignalObj') && isa(s2,'SignalObj'))
if(s1.dimension == s2.dimension)
[s1c,s2c] = makeCompatible(s1,s2);
s3 = s1c;
s3.data = s1c.data.*s2c.data;
%can multiply units
else
error('can only multiply signals with same dimension');
end
elseif(isa(s1,'double') || isa(s2,'double'))
if(isa(s1,'double'))
s3=s2.copySignal;
[l,w] = size(s1);
if(w==s3.dimension && l==1)
s3.data = s3.data.*(ones(length(s3.data),1)*s1);
else
s3.data = s3.data.*s1;
end
else
s3=s1.copySignal;
[l,w] = size(s2);
if(w==s3.dimension && l==1)
s3.data = s3.data.*(ones(length(s3.data),1)*s2);
else
s3.data = s3.data.*s2;
end
end
end
end
function s3 = mtimes(s1,s2) %s1*s2
%Matrix multiplication of two signals
%Needs work
%If s1 and s2 are signals, same as times(s1,s2)
if(isa(s1,'SignalObj') && isa(s2,'SignalObj'))
%[s1c,s2c] = makeCompatible(s1,s2);
s3 = s1.copySignal;
s3.data = s1.data.*s2.data;
%can multiply units
elseif(isa(s1,'double') || isa(s2,'double'))
if(isa(s1,'double'))
s3=s2.copySignal;
s3.data = (s1*s3.data')';
else
s3=s1.copySignal;
s3.data = (s3.data'*s2)';
end
end
end
function s3 = rdivide(s1,s2)
if(isa(s1,'SignalObj') && isa(s2,'SignalObj'))
if(s1.dimension == s2.dimension)
[s1c,s2c] = makeCompatible(s1,s2);
s3 = s1c;
s3.data = s1c.data./s2c.data;
%can multiply units
else
error('can only multiply signals with same dimension');
end
elseif(isa(s1,'double') || isa(s2,'double'))
if(isa(s1,'double'))
s3=s2.copySignal;
s3.data = s1./s3.data;
else
s3=s1.copySignal;
s3.data = s3.data./s2;
end
end
end
function s3 = ldivide(s1,s2)
if(isa(s1,'SignalObj') && isa(s2,'SignalObj'))
if(s1.dimension == s2.dimension)
[s1c,s2c] = makeCompatible(s1,s2);
s3 = s1c;
s3.data = s1c.data.\s2c.data;
%can multiply units
else
error('can only multiply signals with same dimension');
end
elseif(isa(s1,'double') || isa(s2,'double'))
if(isa(s1,'double'))
s3=s2.copySignal;
s3.data = s1.\s3.data;
else
s3=s1.copySignal;
s3.data = s3.data.\s2;
end
end
end
function s3 = ctranspose(s1)
s3=s1.copySignal;
s3.data=s3.data.';
[l,w]=size(s3.data);
s3.dimension=w;
end
function s3 = transpose(s1)
s3=s1.copySignal;
s3.data=s3.data.';
[l,w]=size(s3.data);
s3.dimension=w;
end
function s3 = derivative(sObj)
%Computes derivative of each component of the SignalObj with
%respect to the x-axis variable.
% B=[1 -1]*sObj.sampleRate;
% A=1;
% s3=sObj.filter(B,A);
s3=sObj.copySignal;
tData=diff(s3.data)*s3.sampleRate;
tData=[zeros(1,s3.dimension); tData];
s3.data=tData;
s3.setYUnits(strcat('\frac{',s3.yunits,'}{',s3.xunits,'}'));
denomstr = strcat('d', s3.xlabelval(1));
s3.setName(strcat('\frac{d}{',denomstr,'}',s3.name));
for i=1:s3.dimension
if(s3.dimension ==1)
if(~strcmp(sObj.dataLabels,''))
s3.dataLabels{i}= strcat('\frac{d}{',denomstr,'}',s3.dataLabels);
end
else
if(~strcmp(sObj.dataLabels{i},''))
s3.dataLabels{i}= strcat('\frac{d}{',denomstr,'}',s3.dataLabels{i});
end
end
end
end
function val = derivativeAt(sObj,x0)
% val = derivativeAt(sObj,x0)
%computes the derivative of the Signal at x0. Returns a row
%vector of length equal to sObj.dimension.
sTemp = sObj.derivative;
val = sTemp.getValueAt(x0);
end
function s3 = integral(sObj,t0,tf)
%s3 = integral(sObj,t0,tf)
%computes the integral of the signal in the window from t0 to tf.
%if tf is not specified, sObj.maxTime is used.
%if t0 is not specified, sObj.minTime is used.
% Both t0 and tf are optional but t0 must be specified if tf is
% to be specified (e.g. cant specified tf only)
% Data labels are updated with latex notation for integral.
% the value of the returned signal at time t is the value of
% the integral from minTime to t.
if(nargin<3)
tf=sObj.maxTime;
end
if(nargin<2)
t0=sObj.minTime;
end
%y[n] = y[n-1] + x[n]*deltaT
B=1*1/sObj.sampleRate;
A=[1 -1];
s3=sObj.getSigInTimeWindow(t0,tf);
s3=s3.filter(B,A);
s3.setYUnits(strcat(s3.yunits,'*',s3.xunits));
dtstr = strcat(' d','\tau');
s3.setName(['\int_',num2str(s3.minTime),'^',s3.xlabelval(1),'\!\!{',[s3.name dtstr],'}']);
if(~sObj.areDataLabelsEmpty)
for i=1:s3.dimension
if(~strcmp(sObj.dataLabels{i},''))
s3.dataLabels{i}= ['\int_',num2str(s3.minTime),'^',s3.xlabelval(1),'\!\!{',[s3.dataLabels{i} dtstr],'}'];
else
s3.dataLabels{i} = '';
end
end
end
end
function s3 = filter(sObj, B,A)
%s3 = filter(sObj, B,A)
%applies the discrete filter specified by B and A to the sObj
%data. Same as running filter(B,A,sObj.dataToMatrix). dataMasks
%are ignores so that the signal dimensionality does not change.
s3=sObj.copySignal;
s3.data = filter(B,A,s3.data);
end
function s3 = filtfilt(sObj,B,A)
%s3 = filtfilt(sObj,B,A)
%applies the discrete filter specified by B and A to the sObj
%data using filtfilt. Same as running filtfilt(B,A,sObj.dataToMatrix). dataMasks
%are ignores so that the signal dimensionality does not change.
s3=sObj.copySignal;
s3.data = filtfilt(B,A,s3.data);
end
function [s1c,s2c] = makeCompatible(s1,s2,holdVals)
%[s1c,s2c] = makeCompatible(s1,s2,holdVals)
%returns two signals that copies of the original signals but
%that have been resampled or extended in time so that the time
%axis of both signals in the same. This is done before most
%mathmatical operations to make sure that the signals have the
%same support.
% holdVals = 1 makes the signals keep their endpoint values if
% they are extended in time. holdVals is an optional argument.
if(nargin<3)
holdVals=0;
end
if(s1.minTime~=s2.minTime || s1.maxTime~=s2.maxTime || s1.sampleRate ~=s2.sampleRate)
s1c = s1.copySignal; s2c = s2.copySignal;
minTime=min(s1c.minTime,s2c.minTime);
maxTime=max(s1c.maxTime,s2c.maxTime);
sampleRate=max(s1c.sampleRate,s2c.sampleRate);
s1c.setSampleRate(sampleRate); s2c.setSampleRate(sampleRate);
s1c.setMinTime(minTime,holdVals); s2c.setMinTime(minTime,holdVals);
s1c.setMaxTime(maxTime,holdVals); s2c.setMaxTime(maxTime,holdVals);
%pause
% for i=1:s2c.dimension
%if(max(s2c.time-s1c.time)>0)
data = interp1(s2c.time,s2c.data,s1c.time,'nearest',0);
%else
% data = s2c.data;
%end
% end
s2c.time = s1c.time;
[nrows,ncolumns] = size(data);
if(nrows>ncolumns)
s2c.data = data;
else
s2c.data = data';
end
else
s1c = s1;
s2c = s2;
end
end
function s = abs(sObj)
absData=abs(sObj.data);
[nrows,ncolumns]=size(absData);
name = ['|', sObj.name '|'];
plotProps = sObj.plotProps;
if(~sObj.areDataLabelsEmpty)
dataLabels = cell(size(sObj.dataLabels));
% plotProps = sObj.plotProps;
for i=1:sObj.dimension
dataLabels{i} = strcat('|',sObj.dataLabels{i},'|');
end
evalstring = strcat('s=',class(sObj),'(sObj.time, absData,name,sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels,plotProps);');
else
evalstring = strcat('s=',class(sObj),'(sObj.time, absData,name,sObj.xlabelval, sObj.xunits,sObj.yunits,[],plotProps);');
end
eval(evalstring);
end
function s = log(sObj)
logData=log(sObj.data);
[nrows,ncolumns]=size(logData);
name = ['ln(' sObj.name ')'];
yunits = ['ln(' sObj.yunits ')'];
plotProps = sObj.plotProps;
if(~sObj.areDataLabelsEmpty)
dataLabels = cell(size(sObj.dataLabels));
% plotProps = sObj.plotProps;
for i=1:sObj.dimension
dataLabels{i} = strcat('ln(',sObj.dataLabels{i},')');
end
evalstring = strcat('s=',class(sObj),'(sObj.time, logData,name,sObj.xlabelval, sObj.xunits,yunits,dataLabels,plotProps);');
else
evalstring = strcat('s=',class(sObj),'(sObj.time, logData,name,sObj.xlabelval, sObj.xunits,yunits,[],plotProps);');
end
eval(evalstring);
end
function m=median(sObj,varargin)
%m=median(sObj,varargin)
%Computes the column-wise median of SignalObj data. Returns a
%signal with the corresponding median values
%same as calling median(sObj.dataToMatrix,varargin)
%Additional parameters are passed to the matlab median function.
%Default computes median of each signal component across time.
%mean(sObj,2) computes median value of the SignalObj at each
%point in time.
mdata=median(sObj.data,varargin{:});
[nrows,ncolumns]=size(mdata);
if( (nrows==length(sObj.time)) && (ncolumns==1) ) %mean across dimensions
name = ['median(', sObj.name ')'];
evalstring = strcat('m=',class(sObj),'(sObj.time, mdata,name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
eval(evalstring);
elseif( (nrows==1) && (ncolumns == sObj.dimension) ) %mean of each dimension
if(~sObj.areDataLabelsEmpty)
dataLabels = cell(size(sObj.dataLabels));
for i=1:sObj.dimension
dataLabels{i} = strcat('median(',sObj.dataLabels{i},')');
end
name = ['median(', sObj.name ')'];
evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);');
eval(evalstring);
%m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata], ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);
else
name = ['median(', sObj.name ')'];
evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
eval(evalstring);
%m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata], ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits);
end
end
end
function m=mode(sObj,varargin)
%m=mode(sObj,varargin)
%Computes the column-wise mode of SignalObj data. Returns a
%signal with the corresponding mode values
%same as calling mode(sObj.dataToMatrix,varargin)
%Additional parameters are passed to the matlab mode function.
%Default computes mode of each signal component across time.
%mean(sObj,2) computes mode value of the SignalObj at each
%point in time.
mdata=mode(sObj.data,varargin{:});
[nrows,ncolumns]=size(mdata);
if( (nrows==length(sObj.time)) && (ncolumns==1) ) %mean across dimensions
name = ['mode(', sObj.name ')'];
evalstring = strcat('m=',class(sObj),'(sObj.time, mdata,name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
eval(evalstring);
elseif( (nrows==1) && (ncolumns == sObj.dimension) ) %mean of each dimension
if(~sObj.areDataLabelsEmpty)
dataLabels = cell(size(sObj.dataLabels));
for i=1:sObj.dimension
dataLabels{i} = strcat('mode(',sObj.dataLabels{i},')');
end
name = ['mode(', sObj.name ')'];
evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);');
eval(evalstring);
%m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata], ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits,dataLabels);
else
name = ['mode(', sObj.name ')'];
evalstring = strcat('m=',class(sObj),'([sObj.time(1); sObj.time(end)], [mdata;mdata],name,sObj.xlabelval, sObj.xunits,sObj.yunits);');
eval(evalstring);
%m=SignalObj([sObj.time(1); sObj.time(end)], [mdata;mdata], ['Mean of ' sObj.name],sObj.xlabelval, sObj.xunits,sObj.yunits);
end
end
end